From 079babd13aa151b8af11e3457479c0648b244017 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Sun, 5 Oct 2025 11:59:40 +0200 Subject: [PATCH 1/6] fix counter Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NKCommon.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index 36ccbd6b..17349fea 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -97,6 +97,7 @@ public struct NKCommon: Sendable { completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)], _ error: Error?) -> Void = { _, _ in }) { // Return existing chunks immediately if !filesChunk.isEmpty { + numChunks(filesChunk.count) return completion(filesChunk, nil) } From 0746b8f357dd3c6c98cb2cdeb93ef4de9988d6a9 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Sun, 5 Oct 2025 12:07:23 +0200 Subject: [PATCH 2/6] Update NKCommon.swift --- Sources/NextcloudKit/NKCommon.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index 17349fea..946769b2 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -97,7 +97,7 @@ public struct NKCommon: Sendable { completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)], _ error: Error?) -> Void = { _, _ in }) { // Return existing chunks immediately if !filesChunk.isEmpty { - numChunks(filesChunk.count) + numChunks(filesChunk.count + 1) return completion(filesChunk, nil) } From f74c0cdf51dc6e34fa7dbe22864f9181d06df585 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Sun, 5 Oct 2025 12:15:52 +0200 Subject: [PATCH 3/6] fix Signed-off-by: Marino Faggiana --- Sources/NextcloudKit/NKCommon.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index 946769b2..37328dc2 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -97,7 +97,7 @@ public struct NKCommon: Sendable { completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)], _ error: Error?) -> Void = { _, _ in }) { // Return existing chunks immediately if !filesChunk.isEmpty { - numChunks(filesChunk.count + 1) + numChunks(filesChunk.count - 1) return completion(filesChunk, nil) } From 2f9c2c7907bbd8d5ab663b4d1094446d31c1ecc5 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Sun, 5 Oct 2025 12:16:40 +0200 Subject: [PATCH 4/6] Update NKCommon.swift --- Sources/NextcloudKit/NKCommon.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index 37328dc2..37cb4876 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -97,7 +97,7 @@ public struct NKCommon: Sendable { completion: @escaping (_ filesChunk: [(fileName: String, size: Int64)], _ error: Error?) -> Void = { _, _ in }) { // Return existing chunks immediately if !filesChunk.isEmpty { - numChunks(filesChunk.count - 1) + numChunks(max(0, filesChunk.count - 1)) return completion(filesChunk, nil) } From 746792c142734d49753dd5fc6ecf112cbd36f779 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Sun, 5 Oct 2025 15:28:06 +0200 Subject: [PATCH 5/6] Update NKCommon.swift --- Sources/NextcloudKit/NKCommon.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index 37cb4876..b6c5e8ab 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -101,10 +101,6 @@ public struct NKCommon: Sendable { return completion(filesChunk, nil) } - defer { - NotificationCenter.default.removeObserver(self, name: notificationCenterChunkedFileStop, object: nil) - } - let fileManager = FileManager.default var isDirectory: ObjCBool = false var reader: FileHandle? @@ -117,10 +113,6 @@ public struct NKCommon: Sendable { let bufferSize = 1_000_000 var stop = false - NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in - stop = true - } - // If max chunk count is > 10000 (max count), add + 100 MB to the chunk size to reduce the count. This is an edge case. let inputFilePath = inputDirectory + "/" + fileName let totalSize = getFileSize(filePath: inputFilePath) @@ -148,6 +140,14 @@ public struct NKCommon: Sendable { return completion([], NSError(domain: "chunkedFile", code: -1, userInfo: [NSLocalizedDescriptionKey: "Failed to open the input file for reading."])) } + let tokenObserver = NotificationCenter.default.addObserver(forName: notificationCenterChunkedFileStop, object: nil, queue: nil) { _ in + stop = true + } + + defer { + NotificationCenter.default.removeObserver(tokenObserver) + } + outerLoop: repeat { if stop { return completion([], NSError(domain: "chunkedFile", code: -5, userInfo: [NSLocalizedDescriptionKey: "Chunking was stopped by user request or system notification."])) From 3e5c59c771b835d08c00058765f4771f0fa5d904 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Mon, 6 Oct 2025 08:14:25 +0200 Subject: [PATCH 6/6] Update NKCommon.swift --- Sources/NextcloudKit/NKCommon.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/NextcloudKit/NKCommon.swift b/Sources/NextcloudKit/NKCommon.swift index b6c5e8ab..f12bb48f 100644 --- a/Sources/NextcloudKit/NKCommon.swift +++ b/Sources/NextcloudKit/NKCommon.swift @@ -146,6 +146,9 @@ public struct NKCommon: Sendable { defer { NotificationCenter.default.removeObserver(tokenObserver) + + try? writer?.close() + try? reader?.close() } outerLoop: repeat { @@ -229,9 +232,6 @@ public struct NKCommon: Sendable { } } while true - writer?.closeFile() - reader?.closeFile() - // Update incremental chunk sizes for i in 0..