From 3d1a5e34bdbf7ccd616d43814fefe1baefd8c014 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 15 Oct 2024 14:17:56 +0800 Subject: [PATCH 1/2] Fix swift 6 concurrency error regarding use of authAttemptState Cannot pass a local variable into a concurrently executing Task. It's not a real issue as we are using a semaphore to wait before using this variable for anything outside of the Task block, but still. Signed-off-by: Claudio Cambra --- ...ileProviderExtension+ClientInterface.swift | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift index 3cae7f945acb..98a9cb250f2e 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift @@ -107,11 +107,10 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte @objc func setupDomainAccount( user: String, userId: String, serverUrl: String, password: String ) { - let semaphore = DispatchSemaphore(value: 0) - var authAttemptState = AuthenticationAttemptResultState.connectionError // default Task { let authTestNcKit = NextcloudKit() authTestNcKit.setup(user: user, userId: userId, password: password, urlBase: serverUrl) + var authAttemptState = AuthenticationAttemptResultState.connectionError // default // Retry a few times if we have a connection issue for authTimeout in AuthenticationTimeouts { @@ -123,49 +122,49 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte ) try? await Task.sleep(nanoseconds: authTimeout) } - semaphore.signal() - } - semaphore.wait() - switch (authAttemptState) { - case .authenticationError: - Logger.fileProviderExtension.info( - "\(user, privacy: .public) authentication failed due to bad creds, stopping" - ) - return - case .connectionError: - // Despite multiple connection attempts we are still getting connection issues, so quit. - Logger.fileProviderExtension.info( - "\(user, privacy: .public) authentication try failed, no connection." - ) - return - case .success: - Logger.fileProviderExtension.info( + switch (authAttemptState) { + case .authenticationError: + Logger.fileProviderExtension.info( + "\(user, privacy: .public) authentication failed due to bad creds, stopping" + ) + return + case .connectionError: + // Despite multiple connection attempts we are still getting connection issues. + // Connection error should be provided + Logger.fileProviderExtension.info( + "\(user, privacy: .public) authentication try failed, no connection." + ) + return + case .success: + Logger.fileProviderExtension.info( """ Authenticated! Nextcloud account set up in File Provider extension. User: \(user, privacy: .public) at server: \(serverUrl, privacy: .public) """ + ) + } + + let newNcAccount = + Account(user: user, id: userId, serverUrl: serverUrl, password: password) + guard newNcAccount != ncAccount else { return } + ncAccount = newNcAccount + ncKit.setup( + account: newNcAccount.ncKitAccount, + user: newNcAccount.username, + userId: newNcAccount.id, + password: newNcAccount.password, + urlBase: newNcAccount.serverUrl, + userAgent: "Nextcloud-macOS/FileProviderExt", + nextcloudVersion: 25, + delegate: nil) // TODO: add delegate methods for self + + changeObserver = RemoteChangeObserver( + remoteInterface: ncKit, changeNotificationInterface: self, domain: domain ) + ncKit.setup(delegate: changeObserver) + signalEnumeratorAfterAccountSetup() } - - let newNcAccount = Account(user: user, id: userId, serverUrl: serverUrl, password: password) - guard newNcAccount != ncAccount else { return } - ncAccount = newNcAccount - ncKit.setup( - account: newNcAccount.ncKitAccount, - user: newNcAccount.username, - userId: newNcAccount.id, - password: newNcAccount.password, - urlBase: newNcAccount.serverUrl, - userAgent: "Nextcloud-macOS/FileProviderExt", - nextcloudVersion: 25, - delegate: nil) // TODO: add delegate methods for self - - changeObserver = RemoteChangeObserver( - remoteInterface: ncKit, changeNotificationInterface: self, domain: domain - ) - ncKit.setup(delegate: changeObserver) - signalEnumeratorAfterAccountSetup() } @objc func removeAccountConfig() { From 46ac0f94dad9a5b1d94cc3ae6955f0f187538446 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 15 Oct 2024 14:21:03 +0800 Subject: [PATCH 2/2] Run post-auth state check with main actor task Signed-off-by: Claudio Cambra --- ...ileProviderExtension+ClientInterface.swift | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift index 98a9cb250f2e..2a7e5d8e5148 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/FileProviderExtension+ClientInterface.swift @@ -145,25 +145,27 @@ extension FileProviderExtension: NSFileProviderServicing, ChangeNotificationInte ) } - let newNcAccount = - Account(user: user, id: userId, serverUrl: serverUrl, password: password) - guard newNcAccount != ncAccount else { return } - ncAccount = newNcAccount - ncKit.setup( - account: newNcAccount.ncKitAccount, - user: newNcAccount.username, - userId: newNcAccount.id, - password: newNcAccount.password, - urlBase: newNcAccount.serverUrl, - userAgent: "Nextcloud-macOS/FileProviderExt", - nextcloudVersion: 25, - delegate: nil) // TODO: add delegate methods for self - - changeObserver = RemoteChangeObserver( - remoteInterface: ncKit, changeNotificationInterface: self, domain: domain - ) - ncKit.setup(delegate: changeObserver) - signalEnumeratorAfterAccountSetup() + Task { @MainActor in + let newNcAccount = + Account(user: user, id: userId, serverUrl: serverUrl, password: password) + guard newNcAccount != ncAccount else { return } + ncAccount = newNcAccount + ncKit.setup( + account: newNcAccount.ncKitAccount, + user: newNcAccount.username, + userId: newNcAccount.id, + password: newNcAccount.password, + urlBase: newNcAccount.serverUrl, + userAgent: "Nextcloud-macOS/FileProviderExt", + nextcloudVersion: 25, + delegate: nil) // TODO: add delegate methods for self + + changeObserver = RemoteChangeObserver( + remoteInterface: ncKit, changeNotificationInterface: self, domain: domain + ) + ncKit.setup(delegate: changeObserver) + signalEnumeratorAfterAccountSetup() + } } }