fix(file-provider): reject downloads with a changed ETag
Compare the current ETag with the one captured at the start of the fetch in the transaction that marks the file downloaded. Keep ownership on failure so cleanup can discard the temporary contents and preserve the newer ETag for retry. Cover a concurrent paginated folder listing that updates the ETag after the older bytes reach disk, then verify that retry returns the new contents and version. Assisted-by: Codex:GPT-6 Signed-off-by: Claudio Cambra <[email protected]>
This commit is contained in:
3 files changed
+96
-6
No files matched your search
+16
-5
@@ -164,7 +164,8 @@ extension FilesDatabaseManager {
|
||||
status: Status,
|
||||
downloaded: Bool,
|
||||
error: String? = nil,
|
||||
contentType: String? = nil
|
||||
contentType: String? = nil,
|
||||
expectedETag: String? = nil
|
||||
) throws -> SendableItemMetadata {
|
||||
let ocId = operation.ocId
|
||||
let statusValue = status.rawValue
|
||||
@@ -172,17 +173,25 @@ extension FilesDatabaseManager {
|
||||
guard operations[storeIdentity]?[ocId]?.identifier == operation.identifier else {
|
||||
throw NSFileProviderError(.cannotSynchronize)
|
||||
}
|
||||
// Keep ownership if publishing the download fails so the caller can clean up.
|
||||
var releaseOwnership = !downloaded
|
||||
defer {
|
||||
operations[storeIdentity]?.removeValue(forKey: ocId)
|
||||
if operations[storeIdentity]?.isEmpty == true {
|
||||
operations.removeValue(forKey: storeIdentity)
|
||||
if releaseOwnership {
|
||||
operations[storeIdentity]?.removeValue(forKey: ocId)
|
||||
if operations[storeIdentity]?.isEmpty == true {
|
||||
operations.removeValue(forKey: storeIdentity)
|
||||
}
|
||||
}
|
||||
}
|
||||
do {
|
||||
return try write(durability: .full) { db in
|
||||
let metadata = try write(durability: .full) { db in
|
||||
guard var item = try itemMetadata(ocId: ocId, in: db), !item.deleted else {
|
||||
throw NSError.fileProviderErrorForNonExistentItem(withIdentifier: NSFileProviderItemIdentifier(ocId))
|
||||
}
|
||||
// A folder listing can update the ETag while the download is in flight.
|
||||
if let expectedETag, item.etag != expectedETag {
|
||||
throw NSFileProviderError(.cannotSynchronize)
|
||||
}
|
||||
item.status = statusValue
|
||||
item.downloaded = downloaded
|
||||
item.sessionError = error ?? ""
|
||||
@@ -198,6 +207,8 @@ extension FilesDatabaseManager {
|
||||
}
|
||||
return item.metadata
|
||||
}
|
||||
releaseOwnership = true
|
||||
return metadata
|
||||
} catch {
|
||||
logger.error("Could not finish download in database.", [.item: ocId, .error: error])
|
||||
throw error
|
||||
|
||||
+7
-1
@@ -355,7 +355,13 @@ public extension Item {
|
||||
updatedMetadata = try dbManager.finishDirectoryDownload(operation, children: childOperations, metadatas: downloadedMetadatas)
|
||||
progress.completedUnitCount += 1
|
||||
} else {
|
||||
updatedMetadata = try dbManager.finishDownload(operation, status: .normal, downloaded: true, contentType: detectedContentType)
|
||||
updatedMetadata = try dbManager.finishDownload(
|
||||
operation,
|
||||
status: .normal,
|
||||
downloaded: true,
|
||||
contentType: detectedContentType,
|
||||
expectedETag: updatedMetadata.etag
|
||||
)
|
||||
}
|
||||
} catch {
|
||||
fetchErrorDescription = error.localizedDescription
|
||||
|
||||
+73
@@ -1708,6 +1708,79 @@ final class ItemFetchTests: NextcloudFileProviderKitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchRejectsOlderBytesAfterPaginatedEnumerationUpdatesVersion() async throws {
|
||||
let (item, remoteInterface, remoteItem) = makeFetchItem()
|
||||
let dbManager = Self.dbManager
|
||||
let oldContents = try XCTUnwrap(remoteItem.data)
|
||||
let oldVersion = remoteItem.versionIdentifier
|
||||
let newContents = Data("Updated server contents".utf8)
|
||||
let newVersion = "new-server-version"
|
||||
let (started, startedContinuation) = AsyncStream<Void>.makeStream()
|
||||
let (release, releaseContinuation) = AsyncStream<Void>.makeStream()
|
||||
remoteInterface.downloadCompletionHandler = {
|
||||
startedContinuation.yield(())
|
||||
do {
|
||||
_ = try await nextTestValue(from: release)
|
||||
} catch is CancellationError {
|
||||
// The test can stop the suspended fetch while unwinding.
|
||||
} catch {
|
||||
XCTFail("Suspended download was not released: \(error)")
|
||||
}
|
||||
}
|
||||
let task = Task { await item.fetchContents(dbManager: dbManager) }
|
||||
defer {
|
||||
startedContinuation.finish()
|
||||
releaseContinuation.finish()
|
||||
task.cancel()
|
||||
remoteInterface.downloadCompletionHandler = nil
|
||||
removeDownloadedContents(remoteInterface: remoteInterface)
|
||||
}
|
||||
guard try await nextTestValue(from: started) != nil else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
let oldDestination = try XCTUnwrap(remoteInterface.downloadDestinationURL)
|
||||
defer { try? FileManager.default.removeItem(at: oldDestination) }
|
||||
XCTAssertEqual(try Data(contentsOf: oldDestination), oldContents)
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.etag, oldVersion)
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.status, Status.downloading.rawValue)
|
||||
|
||||
remoteItem.data = newContents
|
||||
remoteItem.versionIdentifier = newVersion
|
||||
remoteInterface.pagination = true
|
||||
let listing = await Enumerator.readServerUrl(
|
||||
Self.account.davFilesUrl,
|
||||
pageSettings: (page: nil, index: 0, size: 5),
|
||||
account: Self.account,
|
||||
remoteInterface: remoteInterface,
|
||||
dbManager: dbManager,
|
||||
log: FileProviderLogMock()
|
||||
)
|
||||
XCTAssertNil(listing.error)
|
||||
XCTAssertEqual(listing.metadatas?.first { $0.ocId == remoteItem.identifier }?.etag, newVersion)
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.etag, newVersion)
|
||||
|
||||
releaseContinuation.finish()
|
||||
let (url, fetchedItem, error) = try await testTaskValue(of: task)
|
||||
|
||||
XCTAssertNil(url)
|
||||
XCTAssertNil(fetchedItem)
|
||||
XCTAssertEqual((error as? NSFileProviderError)?.code, .cannotSynchronize)
|
||||
XCTAssertFalse(FileManager.default.fileExists(atPath: oldDestination.path))
|
||||
let rejectedMetadata = try XCTUnwrap(dbManager.itemMetadata(ocId: remoteItem.identifier))
|
||||
XCTAssertEqual(rejectedMetadata.etag, newVersion)
|
||||
XCTAssertFalse(rejectedMetadata.downloaded)
|
||||
XCTAssertEqual(rejectedMetadata.status, Status.downloadError.rawValue)
|
||||
|
||||
remoteInterface.downloadCompletionHandler = nil
|
||||
let (retryURL, retryItem, retryError) = await item.fetchContents(dbManager: dbManager)
|
||||
|
||||
XCTAssertNil(retryError)
|
||||
XCTAssertEqual(try Data(contentsOf: XCTUnwrap(retryURL)), newContents)
|
||||
XCTAssertEqual(try XCTUnwrap(retryItem).itemVersion.contentVersion, Data(newVersion.utf8))
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.etag, newVersion)
|
||||
XCTAssertEqual(dbManager.itemMetadata(ocId: remoteItem.identifier)?.downloaded, true)
|
||||
}
|
||||
|
||||
func testSuccessfulFetchPreservesConcurrentMetadataChanges() async throws {
|
||||
for directory in [false, true] {
|
||||
let (item, remoteInterface, remoteItem) = makeFetchItem(directory: directory)
|
||||
|
||||
Reference in new issue
Block a user