fix(file-provider): reject single-file fetches after a move
Check the starting path and ETag before publishing a file download. A move can preserve the ETag while changing the parent of the returned item. Test that the original fetch fails and a retry returns the new parent when the move is recorded before publication. Assisted-by: Codex:GPT-6 Signed-off-by: Claudio Cambra <[email protected]>
This commit is contained in:
3 files changed
+67
-6
No files matched your search
+6
-4
@@ -235,8 +235,7 @@ extension FilesDatabaseManager {
|
||||
status: Status,
|
||||
downloaded: Bool,
|
||||
error: String? = nil,
|
||||
contentType: String? = nil,
|
||||
expectedETag: String? = nil
|
||||
contentType: String? = nil
|
||||
) throws -> SendableItemMetadata {
|
||||
let ocId = operation.ocId
|
||||
let statusValue = status.rawValue
|
||||
@@ -272,8 +271,11 @@ extension FilesDatabaseManager {
|
||||
guard !downloaded else { throw NSFileProviderError(.cannotSynchronize) }
|
||||
return item.metadata
|
||||
}
|
||||
// A folder listing can update the ETag while the download is in flight.
|
||||
if let expectedETag, item.etag != expectedETag {
|
||||
// A listing can report a newer version while we download, leaving the bytes stale.
|
||||
// A move can leave the fetch with the old parent identifier even if the ETag matches.
|
||||
if downloaded,
|
||||
item.etag != started.etag || !item.hasSameRemotePath(as: started.remotePath)
|
||||
{
|
||||
throw NSFileProviderError(.cannotSynchronize)
|
||||
}
|
||||
item.status = statusValue
|
||||
|
||||
+1
-2
@@ -381,8 +381,7 @@ public extension Item {
|
||||
operation,
|
||||
status: .normal,
|
||||
downloaded: true,
|
||||
contentType: detectedContentType,
|
||||
expectedETag: updatedMetadata.etag
|
||||
contentType: detectedContentType
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
|
||||
+60
@@ -2007,6 +2007,66 @@ final class ItemFetchTests: NextcloudFileProviderKitTestCase {
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.downloaded, true)
|
||||
}
|
||||
|
||||
func testFetchRetriesAfterFileMovesWithoutChangingETag() async throws {
|
||||
let (item, remoteInterface, remoteItem) = makeFetchItem()
|
||||
let destination = makeDirectoryChild(of: rootItem)
|
||||
let dbManager = Self.dbManager
|
||||
let ocId = remoteItem.identifier
|
||||
let etag = remoteItem.versionIdentifier
|
||||
let oldPath = remoteItem.remotePath
|
||||
let newName = "moved.txt"
|
||||
let parentPath = destination.remotePath
|
||||
let newPath = parentPath + "/" + newName
|
||||
let contents = try XCTUnwrap(remoteItem.data)
|
||||
|
||||
remoteInterface.downloadCompletionHandler = {
|
||||
// Another client moves the file after the GET writes its bytes.
|
||||
let move = await remoteInterface.move(
|
||||
remotePathSource: oldPath,
|
||||
remotePathDestination: newPath,
|
||||
account: Self.account
|
||||
)
|
||||
XCTAssertEqual(move.error, .success)
|
||||
let listing = await Enumerator.readServerUrl(
|
||||
parentPath,
|
||||
account: Self.account,
|
||||
remoteInterface: remoteInterface,
|
||||
dbManager: dbManager,
|
||||
log: FileProviderLogMock()
|
||||
)
|
||||
XCTAssertNil(listing.error)
|
||||
}
|
||||
defer {
|
||||
remoteInterface.downloadCompletionHandler = nil
|
||||
removeDownloadedContents(remoteInterface: remoteInterface)
|
||||
}
|
||||
|
||||
let (url, fetchedItem, error) = await item.fetchContents(dbManager: dbManager)
|
||||
|
||||
XCTAssertNil(url)
|
||||
XCTAssertNil(fetchedItem)
|
||||
XCTAssertEqual((error as? NSFileProviderError)?.code, .cannotSynchronize)
|
||||
let oldDestination = try XCTUnwrap(remoteInterface.downloadDestinationURL)
|
||||
defer { try? FileManager.default.removeItem(at: oldDestination) }
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: oldDestination.path))
|
||||
let moved = try XCTUnwrap(dbManager.itemMetadata(ocId: ocId))
|
||||
XCTAssertEqual(moved.remotePath(), newPath)
|
||||
XCTAssertEqual(moved.etag, etag)
|
||||
XCTAssertFalse(moved.downloaded)
|
||||
XCTAssertFalse(moved.isDownload)
|
||||
|
||||
remoteInterface.downloadCompletionHandler = nil
|
||||
let (retryURL, retryItem, retryError) = await item.fetchContents(dbManager: dbManager)
|
||||
|
||||
XCTAssertNil(retryError)
|
||||
let completed = try XCTUnwrap(retryItem)
|
||||
XCTAssertEqual(completed.itemIdentifier, item.itemIdentifier)
|
||||
XCTAssertEqual(completed.parentItemIdentifier.rawValue, destination.identifier)
|
||||
XCTAssertEqual(completed.filename, newName)
|
||||
XCTAssertEqual(try Data(contentsOf: XCTUnwrap(retryURL)), contents)
|
||||
XCTAssertTrue(completed.isDownloaded)
|
||||
}
|
||||
|
||||
func testFetchReturnsRetryableErrorWhenDownloadCompletionWriteFails() async throws {
|
||||
let (item, remoteInterface, remoteItem) = makeFetchItem()
|
||||
let dbManager = Self.dbManager
|
||||
|
||||
Reference in new issue
Block a user