Fix migration problems

This commit is contained in:
Yishi Lin 2017-06-14 21:43:33 +08:00
parent 33fff2afaf
commit 09865a02af
3 changed files with 18 additions and 16 deletions

View file

@ -112,7 +112,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")! let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL) let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!) let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!)
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: Globals.sharedContainerURL.appendingPathComponent("Documents/pass.sqlite"))] container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: URL(fileURLWithPath: Globals.dbPath))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? { if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately. // Replace this implementation with code to handle the error appropriately.

View file

@ -32,6 +32,7 @@ public class Globals {
public static let gitSSHPrivateKeyPath = documentPath + "/ssh_key" public static let gitSSHPrivateKeyPath = documentPath + "/ssh_key"
public static let gitSSHPrivateKeyURL = URL(fileURLWithPath: gitSSHPrivateKeyPath) public static let gitSSHPrivateKeyURL = URL(fileURLWithPath: gitSSHPrivateKeyPath)
public static let repositoryPath = libraryPath + "/password-store" public static let repositoryPath = libraryPath + "/password-store"
public static let dbPath = documentPath + "/pass.sqlite"
public static let iTunesFileSharingPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] public static let iTunesFileSharingPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
public static let iTunesFileSharingPGPPublic = iTunesFileSharingPath + "/gpg_key.pub" public static let iTunesFileSharingPGPPublic = iTunesFileSharingPath + "/gpg_key.pub"

View file

@ -74,7 +74,7 @@ public class PasswordStore {
let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")! let modelURL = Bundle(identifier: Globals.passKitBundleIdentifier)!.url(forResource: "pass", withExtension: "momd")!
let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL) let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL)
let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!) let container = NSPersistentContainer(name: "pass", managedObjectModel: managedObjectModel!)
container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: Globals.sharedContainerURL.appendingPathComponent("Documents/pass.sqlite"))] container.persistentStoreDescriptions = [NSPersistentStoreDescription(url: URL(fileURLWithPath: Globals.dbPath))]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? { if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately. // Replace this implementation with code to handle the error appropriately.
@ -129,30 +129,31 @@ public class PasswordStore {
} }
private func migrateIfNeeded() { private func migrateIfNeeded() {
let needMigration = fm.fileExists(atPath: Globals.documentPathLegacy) && !fm.fileExists(atPath: Globals.documentPath) && fm.fileExists(atPath: Globals.libraryPathLegacy) && !fm.fileExists(atPath: Globals.libraryPath) // migrate happens only if the repository was cloned and pgp keys were set up using earlier versions
let needMigration = !pgpKeyExists() && !gitSSHKeyExists() && !fm.fileExists(atPath: Globals.repositoryPath) && fm.fileExists(atPath: Globals.repositoryPathLegacy)
guard needMigration == true else { guard needMigration == true else {
return return
} }
do { do {
// migrate files
try fm.moveItem(atPath: Globals.documentPathLegacy, toPath: Globals.documentPath)
try fm.moveItem(atPath: Globals.libraryPathLegacy, toPath: Globals.libraryPath)
// migrate Defaults // migrate Defaults
SharedDefaults = Defaults SharedDefaults = Defaults
// migrate Keychain
let keychainLegacy = Keychain(service: Globals.bundleIdentifier) // migrate files
if let pgpPassphrase = try keychainLegacy.getString("pgpKeyPassphrase") { try fm.createDirectory(atPath: Globals.documentPath, withIntermediateDirectories: true, attributes: nil)
Utils.addPasswordToKeychain(name: "pgpKeyPassphrase", password: pgpPassphrase) try fm.createDirectory(atPath: Globals.libraryPath, withIntermediateDirectories: true, attributes: nil)
if fm.fileExists(atPath: Globals.pgpPublicKeyPathLegacy) {
try fm.moveItem(atPath: Globals.pgpPublicKeyPathLegacy, toPath: Globals.pgpPublicKeyPath)
} }
if let gitSSHPrivateKeyPassphrase = try keychainLegacy.getString("gitSSHPrivateKeyPassphrase") { if fm.fileExists(atPath: Globals.pgpPrivateKeyPathLegacy) {
Utils.addPasswordToKeychain(name: "gitSSHPrivateKeyPassphrase", password: gitSSHPrivateKeyPassphrase) try fm.moveItem(atPath: Globals.pgpPrivateKeyPathLegacy, toPath: Globals.pgpPrivateKeyPath)
} }
if let gitPassword = try keychainLegacy.getString("gitPassword") { if fm.fileExists(atPath: Globals.gitSSHPrivateKeyPathLegacy) {
Utils.addPasswordToKeychain(name: "gitPassword", password: gitPassword) try fm.moveItem(atPath: Globals.gitSSHPrivateKeyPathLegacy, toPath: Globals.gitSSHPrivateKeyPath)
} }
try keychainLegacy.removeAll() try fm.moveItem(atPath: Globals.repositoryPathLegacy, toPath: Globals.repositoryPath)
} catch { } catch {
print("Cannot migrate: \(error)") print("Migration error: \(error)")
} }
updatePasswordEntityCoreData() updatePasswordEntityCoreData()
} }