From 36995e089b07268bd0149e582b9733671bf32e07 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Thu, 1 Nov 2018 13:25:33 +0100 Subject: [PATCH] Locate profiles via relative URLs Avoid absolute URLs, profiles directory is always relative to documents. Assume profiles to be based in the documents root unless directory is != nil. Given that, starting from iOS 8, the documents location can change from time to time, could this fix #19? --- .../Model/ConnectionService+Configurations.swift | 2 +- Passepartout/Sources/Model/ConnectionService.swift | 10 +++++++--- Passepartout/Sources/Model/TransientStore.swift | 11 ++--------- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Passepartout/Sources/Model/ConnectionService+Configurations.swift b/Passepartout/Sources/Model/ConnectionService+Configurations.swift index c45a04bb..5d559f7c 100644 --- a/Passepartout/Sources/Model/ConnectionService+Configurations.swift +++ b/Passepartout/Sources/Model/ConnectionService+Configurations.swift @@ -59,7 +59,7 @@ extension ConnectionService { func pendingConfigurationURLs() -> [URL] { do { - let list = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil, options: []) + let list = try FileManager.default.contentsOfDirectory(at: rootURL, includingPropertiesForKeys: nil, options: []) return list.filter { $0.pathExtension == "ovpn" } } catch let e { log.error("Could not list imported configurations: \(e)") diff --git a/Passepartout/Sources/Model/ConnectionService.swift b/Passepartout/Sources/Model/ConnectionService.swift index 5bf30af4..345e0378 100644 --- a/Passepartout/Sources/Model/ConnectionService.swift +++ b/Passepartout/Sources/Model/ConnectionService.swift @@ -83,14 +83,18 @@ class ConnectionService: Codable { } } - lazy var directory = FileManager.default.userURL(for: .documentDirectory, appending: nil) + var directory: String? = nil + + var rootURL: URL { + return FileManager.default.userURL(for: .documentDirectory, appending: directory) + } private var providersURL: URL { - return directory.appendingPathComponent(AppConstants.Store.providersDirectory) + return rootURL.appendingPathComponent(AppConstants.Store.providersDirectory) } private var hostsURL: URL { - return directory.appendingPathComponent(AppConstants.Store.hostsDirectory) + return rootURL.appendingPathComponent(AppConstants.Store.hostsDirectory) } private var build: Int diff --git a/Passepartout/Sources/Model/TransientStore.swift b/Passepartout/Sources/Model/TransientStore.swift index 52c7be8a..9525b029 100644 --- a/Passepartout/Sources/Model/TransientStore.swift +++ b/Passepartout/Sources/Model/TransientStore.swift @@ -35,12 +35,10 @@ class TransientStore { static let shared = TransientStore() - private let rootURL: URL - - private let serviceURL: URL - let service: ConnectionService + private let serviceURL = FileManager.default.userURL(for: .documentDirectory, appending: AppConstants.Store.serviceFilename) + var didHandleSubreddit: Bool { get { return UserDefaults.standard.bool(forKey: Keys.didHandleSubreddit) @@ -51,9 +49,6 @@ class TransientStore { } private init() { - rootURL = FileManager.default.userURL(for: .documentDirectory, appending: nil) - serviceURL = rootURL.appendingPathComponent(AppConstants.Store.serviceFilename) - let cfg = AppConstants.VPN.baseConfiguration() do { ConnectionService.migrateJSON(at: serviceURL, to: serviceURL) @@ -64,7 +59,6 @@ class TransientStore { log.verbose(content) } service = try JSONDecoder().decode(ConnectionService.self, from: data) - service.directory = rootURL service.baseConfiguration = cfg service.loadProfiles() } catch let e { @@ -73,7 +67,6 @@ class TransientStore { withAppGroup: GroupConstants.App.appGroup, baseConfiguration: cfg ) - service.directory = rootURL // // hardcoded loading // _ = service.addProfile(ProviderConnectionProfile(name: .pia), credentials: nil)