From 8ede1ed3d4e7bf0992f806376207af66688823da Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 10 Mar 2019 14:44:36 +0100 Subject: [PATCH 1/4] Use App Group container for documents and caches --- Passepartout/Sources/AppConstants.swift | 2 +- Passepartout/Sources/GroupConstants.swift | 19 +++++++++++++++++++ .../Sources/Model/ConnectionService.swift | 6 +++++- .../Sources/Model/TransientStore.swift | 2 +- .../Services/InfrastructureFactory.swift | 2 +- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/Passepartout/Sources/AppConstants.swift b/Passepartout/Sources/AppConstants.swift index d7170a7b..28d392f7 100644 --- a/Passepartout/Sources/AppConstants.swift +++ b/Passepartout/Sources/AppConstants.swift @@ -97,7 +97,7 @@ class AppConstants { private static let fileName = "Debug.log" static var fileURL: URL { - return FileManager.default.userURL(for: .cachesDirectory, appending: fileName) + return GroupConstants.App.cachesURL.appendingPathComponent(fileName) } private static let console: ConsoleDestination = { diff --git a/Passepartout/Sources/GroupConstants.swift b/Passepartout/Sources/GroupConstants.swift index c2adb9e7..b52bd0a3 100644 --- a/Passepartout/Sources/GroupConstants.swift +++ b/Passepartout/Sources/GroupConstants.swift @@ -53,6 +53,25 @@ class GroupConstants { static let tunnelIdentifier = "com.algoritmico.macos.Passepartout.Tunnel" #endif + + private static var containerURL: URL { + guard let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else { + fatalError("Unable to access App Group container") + } + return url + } + + static let documentsURL: URL = { + let url = containerURL.appendingPathComponent("Documents", isDirectory: true) + try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) + return url + }() + + static let cachesURL: URL = { + let url = containerURL.appendingPathComponent("Library/Caches", isDirectory: true) + try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) + return url + }() } class VPN { diff --git a/Passepartout/Sources/Model/ConnectionService.swift b/Passepartout/Sources/Model/ConnectionService.swift index 7d0c3bc1..524260e4 100644 --- a/Passepartout/Sources/Model/ConnectionService.swift +++ b/Passepartout/Sources/Model/ConnectionService.swift @@ -58,7 +58,11 @@ class ConnectionService: Codable { var directory: String? = nil var rootURL: URL { - return FileManager.default.userURL(for: .documentDirectory, appending: directory) + var url = GroupConstants.App.documentsURL + if let directory = directory { + url.appendPathComponent(directory) + } + return url } private var providersURL: URL { diff --git a/Passepartout/Sources/Model/TransientStore.swift b/Passepartout/Sources/Model/TransientStore.swift index 0574e550..8a512ec8 100644 --- a/Passepartout/Sources/Model/TransientStore.swift +++ b/Passepartout/Sources/Model/TransientStore.swift @@ -36,7 +36,7 @@ class TransientStore { static let shared = TransientStore() private static var serviceURL: URL { - return FileManager.default.userURL(for: .documentDirectory, appending: AppConstants.Store.serviceFilename) + return GroupConstants.App.documentsURL.appendingPathComponent(AppConstants.Store.serviceFilename) } let service: ConnectionService diff --git a/Passepartout/Sources/Services/InfrastructureFactory.swift b/Passepartout/Sources/Services/InfrastructureFactory.swift index 87fda866..eb84655a 100644 --- a/Passepartout/Sources/Services/InfrastructureFactory.swift +++ b/Passepartout/Sources/Services/InfrastructureFactory.swift @@ -75,7 +75,7 @@ class InfrastructureFactory { } self.bundle = bundle - cachePath = FileManager.default.userURL(for: .cachesDirectory, appending: nil) + cachePath = GroupConstants.App.cachesURL cache = [:] lastUpdate = [:] } From 87249cb8adfa7c2e8396e637c9de53226046e95c Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 10 Mar 2019 14:58:01 +0100 Subject: [PATCH 2/4] Migrate documents to App Group --- .../Sources/Model/TransientStore.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Passepartout/Sources/Model/TransientStore.swift b/Passepartout/Sources/Model/TransientStore.swift index 8a512ec8..2b64a25c 100644 --- a/Passepartout/Sources/Model/TransientStore.swift +++ b/Passepartout/Sources/Model/TransientStore.swift @@ -51,6 +51,7 @@ class TransientStore { } private init() { + TransientStore.migrateDocumentsToAppGroup() // this must be graceful ConnectionService.migrateJSON(from: TransientStore.serviceURL, to: TransientStore.serviceURL) @@ -85,4 +86,26 @@ class TransientStore { service.saveProfiles() } } + + // + + private static func migrateDocumentsToAppGroup() { + let oldDocumentsURL = FileManager.default.userURL(for: .documentDirectory, appending: nil) + let newDocumentsURL = GroupConstants.App.documentsURL + log.debug("App documentsURL: \(oldDocumentsURL)") + log.debug("Group documentsURL: \(newDocumentsURL)") + let fm = FileManager.default + do { + for c in try fm.contentsOfDirectory(atPath: oldDocumentsURL.path) { + let old = oldDocumentsURL.appendingPathComponent(c) + let new = newDocumentsURL.appendingPathComponent(c) + log.verbose("Move:") + log.verbose("\tFROM: \(old)") + log.verbose("\tTO: \(new)") + try fm.moveItem(at: old, to: new) + } + } catch let e { + log.error("Could not migrate documents to App Group: \(e)") + } + } } From 9005bf6c199db6731baa77f331afa0f0d528e4c5 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 11 Mar 2019 10:12:16 +0100 Subject: [PATCH 3/4] Skip "Inbox" during migration No permission. --- Passepartout/Sources/Model/TransientStore.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Passepartout/Sources/Model/TransientStore.swift b/Passepartout/Sources/Model/TransientStore.swift index 2b64a25c..78e0aa18 100644 --- a/Passepartout/Sources/Model/TransientStore.swift +++ b/Passepartout/Sources/Model/TransientStore.swift @@ -97,6 +97,9 @@ class TransientStore { let fm = FileManager.default do { for c in try fm.contentsOfDirectory(atPath: oldDocumentsURL.path) { + guard c != "Inbox" else { + continue + } let old = oldDocumentsURL.appendingPathComponent(c) let new = newDocumentsURL.appendingPathComponent(c) log.verbose("Move:") From 39fb191309f7f97e50d7f2916e05ab8ebb5fb14c Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 11 Mar 2019 10:09:35 +0100 Subject: [PATCH 4/4] Log documents migration event If anything was moved. --- Passepartout/Sources/Model/TransientStore.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Passepartout/Sources/Model/TransientStore.swift b/Passepartout/Sources/Model/TransientStore.swift index 78e0aa18..389958af 100644 --- a/Passepartout/Sources/Model/TransientStore.swift +++ b/Passepartout/Sources/Model/TransientStore.swift @@ -90,6 +90,7 @@ class TransientStore { // private static func migrateDocumentsToAppGroup() { + var hasMigrated = false let oldDocumentsURL = FileManager.default.userURL(for: .documentDirectory, appending: nil) let newDocumentsURL = GroupConstants.App.documentsURL log.debug("App documentsURL: \(oldDocumentsURL)") @@ -106,9 +107,14 @@ class TransientStore { log.verbose("\tFROM: \(old)") log.verbose("\tTO: \(new)") try fm.moveItem(at: old, to: new) + hasMigrated = true } } catch let e { + hasMigrated = false log.error("Could not migrate documents to App Group: \(e)") } + if hasMigrated { + log.debug("Documents migrated to App Group") + } } }