From 35570a3160bacb11b52e952cd00b90cdaea51a0e Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Sat, 18 Mar 2023 18:04:19 +0100 Subject: [PATCH] Move remote sync functionality out of enumerator and into separate NextcloudSyncEngine class Signed-off-by: Claudio Cambra --- .../FileProviderExt/FileProviderEnumerator.swift | 12 ++++++++---- ...r+RemoteSync.swift => NextcloudSyncEngine.swift} | 13 ++++++++++--- .../NextcloudIntegration.xcodeproj/project.pbxproj | 8 ++++---- 3 files changed, 22 insertions(+), 11 deletions(-) rename shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/{FileProviderEnumerator+RemoteSync.swift => NextcloudSyncEngine.swift} (97%) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift index b94eb43ad..103179530 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator.swift @@ -30,6 +30,8 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { var serverUrl: String = "" var isInvalidated = false + let syncEngine = NextcloudSyncEngine() + private static func isSystemIdentifier(_ identifier: NSFileProviderItemIdentifier) -> Bool { return identifier == .rootContainer || identifier == .trashContainer || @@ -61,6 +63,8 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { } func invalidate() { + Logger.enumeration.debug("Enumerator is being invalidated for item with identifier: \(self.enumeratedItemIdentifier.rawValue, privacy: .public)") + syncEngine.invalidate() self.isInvalidated = true } @@ -92,7 +96,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { // We enumerate items as we get the server data for two reasons: // A) we avoid having a gigantic chunk of files to enumerate to the observer at the end // B) we don't need to worry about resolving which files are truly deleted vs moved at the end - fullRecursiveScan(ncAccount: self.ncAccount, ncKit: self.ncKit, scanChangesOnly: false, singleFolderScanCompleteCompletionHandler: { metadatas, error in + syncEngine.fullRecursiveScan(ncAccount: self.ncAccount, ncKit: self.ncKit, scanChangesOnly: false, singleFolderScanCompleteCompletionHandler: { metadatas, error in guard error == nil else { Logger.enumeration.error("There was an error during recursive item enumeration of working set for user: \(self.ncAccount.ncKitAccount, privacy: OSLogPrivacy.auto(mask: .hash)) with error: \(error!.errorDescription, privacy: .public)") @@ -156,7 +160,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { Logger.enumeration.debug("Enumerating initial page for user: \(self.ncAccount.ncKitAccount, privacy: OSLogPrivacy.auto(mask: .hash)) with serverUrl: \(self.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash))") - FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount, ncKit: ncKit) { _, _, _, _, readError in + NextcloudSyncEngine.readServerUrl(serverUrl, ncAccount: ncAccount, ncKit: ncKit) { _, _, _, _, readError in guard readError == nil else { Logger.enumeration.error("Finishing enumeration for user: \(self.ncAccount.ncKitAccount, privacy: OSLogPrivacy.auto(mask: .hash)) with serverUrl: \(self.serverUrl, privacy: OSLogPrivacy.auto(mask: .hash)) with error \(readError!.localizedDescription, privacy: .public)") @@ -219,7 +223,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { // Unlike when enumerating items we can't progressively enumerate items as we need to wait to resolve which items are truly deleted and which // have just been moved elsewhere. - fullRecursiveScan(ncAccount: self.ncAccount, + syncEngine.fullRecursiveScan(ncAccount: self.ncAccount, ncKit: self.ncKit, scanChangesOnly: true, singleFolderScanCompleteCompletionHandler: { _, _ in }) { _, newMetadatas, updatedMetadatas, deletedMetadatas, error in @@ -258,7 +262,7 @@ class FileProviderEnumerator: NSObject, NSFileProviderEnumerator { // No matter what happens here we finish enumeration in some way, either from the error // handling below or from the completeChangesObserver - FileProviderEnumerator.readServerUrl(serverUrl, ncAccount: ncAccount, ncKit: ncKit, stopAtMatchingEtags: true) { _, newMetadatas, updatedMetadatas, deletedMetadatas, readError in + NextcloudSyncEngine.readServerUrl(serverUrl, ncAccount: ncAccount, ncKit: ncKit, stopAtMatchingEtags: true) { _, newMetadatas, updatedMetadatas, deletedMetadatas, readError in // If we get a 404 we might add more deleted metadatas var currentDeletedMetadatas: [NextcloudItemMetadataTable] = [] diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator+RemoteSync.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/NextcloudSyncEngine.swift similarity index 97% rename from shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator+RemoteSync.swift rename to shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/NextcloudSyncEngine.swift index 8d2fba570..87f64ebf8 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderEnumerator+RemoteSync.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/NextcloudSyncEngine.swift @@ -16,7 +16,14 @@ import FileProvider import NextcloudKit import OSLog -extension FileProviderEnumerator { +class NextcloudSyncEngine : NSObject { + + var isInvalidated = false + + func invalidate() { + self.isInvalidated = true + } + func fullRecursiveScan(ncAccount: NextcloudAccount, ncKit: NextcloudKit, scanChangesOnly: Bool, @@ -33,7 +40,7 @@ extension FileProviderEnumerator { rootContainerDirectoryMetadata.ocId = NSFileProviderItemIdentifier.rootContainer.rawValue // Create a serial dispatch queue - let dispatchQueue = DispatchQueue(label: "recursiveChangeEnumerationQueue", qos: .background) + let dispatchQueue = DispatchQueue(label: "recursiveChangeEnumerationQueue", qos: .userInitiated) dispatchQueue.async { let results = self.scanRecursively(rootContainerDirectoryMetadata, @@ -101,7 +108,7 @@ extension FileProviderEnumerator { Logger.enumeration.debug("About to read: \(itemServerUrl, privacy: OSLogPrivacy.auto(mask: .hash))") - FileProviderEnumerator.readServerUrl(itemServerUrl, ncAccount: ncAccount, ncKit: ncKit, stopAtMatchingEtags: scanChangesOnly) { metadatas, newMetadatas, updatedMetadatas, deletedMetadatas, readError in + NextcloudSyncEngine.readServerUrl(itemServerUrl, ncAccount: ncAccount, ncKit: ncKit, stopAtMatchingEtags: scanChangesOnly) { metadatas, newMetadatas, updatedMetadatas, deletedMetadatas, readError in if readError != nil { let nkReadError = NKError(error: readError!) diff --git a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj index 17c5d351a..0ba89693b 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj +++ b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj @@ -36,7 +36,7 @@ 53903D37295618A400D0B308 /* LineProcessor.h in Headers */ = {isa = PBXBuildFile; fileRef = 53903D36295618A400D0B308 /* LineProcessor.h */; settings = {ATTRIBUTES = (Public, ); }; }; 539158AC27BE71A900816F56 /* FinderSyncSocketLineProcessor.m in Sources */ = {isa = PBXBuildFile; fileRef = 539158AB27BE71A900816F56 /* FinderSyncSocketLineProcessor.m */; }; 53D056312970594F00988392 /* LocalFilesUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53D056302970594F00988392 /* LocalFilesUtils.swift */; }; - 53ED472029C5E64200795DB1 /* FileProviderEnumerator+RemoteSync.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+RemoteSync.swift */; }; + 53ED472029C5E64200795DB1 /* NextcloudSyncEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53ED471F29C5E64200795DB1 /* NextcloudSyncEngine.swift */; }; C2B573BA1B1CD91E00303B36 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B573B91B1CD91E00303B36 /* main.m */; }; C2B573D21B1CD94B00303B36 /* main.m in Resources */ = {isa = PBXBuildFile; fileRef = C2B573B91B1CD91E00303B36 /* main.m */; }; C2B573DE1B1CD9CE00303B36 /* FinderSync.m in Sources */ = {isa = PBXBuildFile; fileRef = C2B573DD1B1CD9CE00303B36 /* FinderSync.m */; }; @@ -160,7 +160,7 @@ 539158B127BE891500816F56 /* LocalSocketClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LocalSocketClient.h; sourceTree = ""; }; 539158B227BEC98A00816F56 /* LocalSocketClient.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LocalSocketClient.m; sourceTree = ""; }; 53D056302970594F00988392 /* LocalFilesUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalFilesUtils.swift; sourceTree = ""; }; - 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+RemoteSync.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "FileProviderEnumerator+RemoteSync.swift"; sourceTree = ""; }; + 53ED471F29C5E64200795DB1 /* NextcloudSyncEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NextcloudSyncEngine.swift; sourceTree = ""; }; C2B573B11B1CD91E00303B36 /* desktopclient.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = desktopclient.app; sourceTree = BUILT_PRODUCTS_DIR; }; C2B573B51B1CD91E00303B36 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; C2B573B91B1CD91E00303B36 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; @@ -249,13 +249,13 @@ 5318AD8F29BF406500CBB71C /* Database */, 5352E85929B7BFB4002CE85C /* Extensions */, 538E397027F4765000FA63D5 /* FileProviderEnumerator.swift */, - 53ED471F29C5E64200795DB1 /* FileProviderEnumerator+RemoteSync.swift */, 538E396C27F4765000FA63D5 /* FileProviderExtension.swift */, 538E396E27F4765000FA63D5 /* FileProviderItem.swift */, 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */, 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */, 53D056302970594F00988392 /* LocalFilesUtils.swift */, 536EFC35295E3C1100F4CB13 /* NextcloudAccount.swift */, + 53ED471F29C5E64200795DB1 /* NextcloudSyncEngine.swift */, 538E397327F4765000FA63D5 /* FileProviderExt.entitlements */, 538E397227F4765000FA63D5 /* Info.plist */, ); @@ -564,7 +564,7 @@ 536EFC36295E3C1100F4CB13 /* NextcloudAccount.swift in Sources */, 538E396D27F4765000FA63D5 /* FileProviderExtension.swift in Sources */, 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */, - 53ED472029C5E64200795DB1 /* FileProviderEnumerator+RemoteSync.swift in Sources */, + 53ED472029C5E64200795DB1 /* NextcloudSyncEngine.swift in Sources */, 5318AD9929BF58D000CBB71C /* NKError+Extensions.swift in Sources */, 5318AD9529BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift in Sources */, 535AE30E29C0A2CC0042A9BA /* Logger+Extensions.swift in Sources */, -- 2.30.2