From bd83239418638395cb49a574f25bba1f4ff50546 Mon Sep 17 00:00:00 2001 From: "Son, Joon" Date: Wed, 19 Feb 2020 15:53:55 -0800 Subject: [PATCH 1/4] create [bundle ID] folder during migration if none exists --- ios/RNCAsyncStorage.m | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index dc765907..21ba9888 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -213,11 +213,29 @@ static void RCTStorageDirectoryCleanupOld(NSString *oldDirectoryPath) } } +static void _createStorageDirectory(NSString *storageDirectory, NSError **error) +{ + [[NSFileManager defaultManager] createDirectoryAtPath:storageDirectory + withIntermediateDirectories:YES + attributes:nil + error:error]; +} + static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *newDirectoryPath, BOOL shouldCleanupOldDirectory) { NSError *error; // Migrate data by copying old storage directory to new storage directory location if (![[NSFileManager defaultManager] copyItemAtPath:oldDirectoryPath toPath:newDirectoryPath error:&error]) { + // the new storage directory "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" seems unable to migrate + // because folder "Application Support/[bundleID]" doesn't exist.. create this folder so and attempt to migrate again + if (error != nil && error.code == 4 && [newDirectoryPath hasSuffix:RCTStorageDirectory]) { + NSError *error = nil; + _createStorageDirectory(RCTCreateStorageDirectoryPath(@""), &error); + if (error == nil) { + RCTStorageDirectoryMigrate(oldDirectoryPath, newDirectoryPath, shouldCleanupOldDirectory); + return; + } + } RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error); } else if (shouldCleanupOldDirectory) { // If copying succeeds, remove old storage directory @@ -354,10 +372,7 @@ - (NSDictionary *)_ensureSetup NSError *error = nil; if (!RCTHasCreatedStorageDirectory) { - [[NSFileManager defaultManager] createDirectoryAtPath:RCTGetStorageDirectory() - withIntermediateDirectories:YES - attributes:nil - error:&error]; + _createStorageDirectory(RCTGetStorageDirectory(), &error); if (error) { return RCTMakeError(@"Failed to create storage directory.", error, nil); } From 4a0d75658e4e9417e81b627c105d6c8fed6c754d Mon Sep 17 00:00:00 2001 From: "Son, Joon" Date: Wed, 19 Feb 2020 16:12:27 -0800 Subject: [PATCH 2/4] typo --- ios/RNCAsyncStorage.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 21ba9888..2c23b111 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -227,7 +227,7 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *new // Migrate data by copying old storage directory to new storage directory location if (![[NSFileManager defaultManager] copyItemAtPath:oldDirectoryPath toPath:newDirectoryPath error:&error]) { // the new storage directory "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" seems unable to migrate - // because folder "Application Support/[bundleID]" doesn't exist.. create this folder so and attempt to migrate again + // because folder "Application Support/[bundleID]" doesn't exist.. create this folder and attempt folder copying again if (error != nil && error.code == 4 && [newDirectoryPath hasSuffix:RCTStorageDirectory]) { NSError *error = nil; _createStorageDirectory(RCTCreateStorageDirectoryPath(@""), &error); From fdfcf40aec975a41378f8f21ed30889ab22c964e Mon Sep 17 00:00:00 2001 From: frankenthumbs Date: Tue, 25 Feb 2020 10:12:00 -0800 Subject: [PATCH 3/4] PR review changes Co-Authored-By: Krzysztof --- ios/RNCAsyncStorage.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 2c23b111..8ca035e9 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -228,7 +228,7 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *new if (![[NSFileManager defaultManager] copyItemAtPath:oldDirectoryPath toPath:newDirectoryPath error:&error]) { // the new storage directory "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" seems unable to migrate // because folder "Application Support/[bundleID]" doesn't exist.. create this folder and attempt folder copying again - if (error != nil && error.code == 4 && [newDirectoryPath hasSuffix:RCTStorageDirectory]) { + if (error != nil && error.code == 4 && [newDirectoryPath isEqualToString:RCTGetStorageDirectory()]) { NSError *error = nil; _createStorageDirectory(RCTCreateStorageDirectoryPath(@""), &error); if (error == nil) { From dc9de89f0c3543061479b80a7b14147505d7d4d6 Mon Sep 17 00:00:00 2001 From: "Son, Joon" Date: Tue, 25 Feb 2020 10:15:04 -0800 Subject: [PATCH 4/4] PR review changes + fix indent --- ios/RNCAsyncStorage.m | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 8ca035e9..45b4d1d7 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -229,14 +229,16 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *new // the new storage directory "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" seems unable to migrate // because folder "Application Support/[bundleID]" doesn't exist.. create this folder and attempt folder copying again if (error != nil && error.code == 4 && [newDirectoryPath isEqualToString:RCTGetStorageDirectory()]) { - NSError *error = nil; - _createStorageDirectory(RCTCreateStorageDirectoryPath(@""), &error); - if (error == nil) { - RCTStorageDirectoryMigrate(oldDirectoryPath, newDirectoryPath, shouldCleanupOldDirectory); - return; - } + NSError *error = nil; + _createStorageDirectory(RCTCreateStorageDirectoryPath(@""), &error); + if (error == nil) { + RCTStorageDirectoryMigrate(oldDirectoryPath, newDirectoryPath, shouldCleanupOldDirectory); + } else { + RCTStorageDirectoryMigrationLogError(@"Failed to create storage directory during migration.", error); } - RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error); + } else { + RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error); + } } else if (shouldCleanupOldDirectory) { // If copying succeeds, remove old storage directory RCTStorageDirectoryCleanupOld(oldDirectoryPath);