From 7cb5d272503b67792ad261efd4c489007c9bc79d Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Fri, 3 Jan 2020 15:24:07 -0800 Subject: [PATCH 1/5] Move the storage location on iOS to Application Support --- ios/RNCAsyncStorage.m | 103 +++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 18556296..ac39c337 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -79,13 +79,24 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray * return nil; } +// DO NOT USE +// This is used internally to migrate data from the old file location to the new one. +// Please use `RCTCreateStorageDirectoryPath` instead +static NSString *RCTCreateStorageDirectoryPath_deprecated(NSString *storageDir) { + NSString *storageDirectoryPath; + #if TARGET_OS_TV + storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject; + #else + storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; + #endif + storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:storageDir]; + return storageDirectoryPath; +} + static NSString *RCTCreateStorageDirectoryPath(NSString *storageDir) { - NSString *storageDirectoryPath; -#if TARGET_OS_TV - storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject; -#else - storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; -#endif + // We should use the "Application Support/[bundleID]" folder for persistent data storage that's hidden from users + NSString *storageDirectoryPath = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject; + storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:[[NSBundle mainBundle] bundleIdentifier]]; // Per Apple's docs, all app content in Application Support must be within a subdirectory of the app's bundle identifier storageDirectoryPath = [storageDirectoryPath stringByAppendingPathComponent:storageDir]; return storageDirectoryPath; } @@ -100,6 +111,14 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray * return storageDirectory; } +// DO NOT USE +// This is used internally to migrate data from the old file location to the new one. +// Please use `RCTCreateManifestFilePath` instead +static NSString *RCTCreateManifestFilePath_deprecated(NSString *storageDirectory) +{ + return [RCTCreateStorageDirectoryPath_deprecated(storageDirectory) stringByAppendingPathComponent:RCTManifestFileName]; +} + static NSString *RCTCreateManifestFilePath(NSString *storageDirectory) { return [RCTCreateStorageDirectoryPath(storageDirectory) stringByAppendingPathComponent:RCTManifestFileName]; @@ -162,7 +181,7 @@ static dispatch_queue_t RCTGetMethodQueue() dispatch_once(&onceToken, ^{ cache = [NSCache new]; cache.totalCostLimit = 2 * 1024 * 1024; // 2MB - + // Clear cache in the event of a memory warning [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(__unused NSNotification *note) { [cache removeAllObjects]; @@ -194,58 +213,58 @@ static void RCTStorageDirectoryMigrationLogError(NSString *reason, NSError *erro RCTLogWarn(@"%@: %@", reason, error ? error.description : @""); } -static void RCTStorageDirectoryCleanupOld() +static void RCTStorageDirectoryCleanupOld(NSString *oldDirectoryPath) { NSError *error; - if (![[NSFileManager defaultManager] removeItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) error:&error]) { + if (![[NSFileManager defaultManager] removeItemAtPath:RCTCreateStorageDirectoryPath_deprecated(oldDirectoryPath) error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to remove old storage directory during migration", error); } } -static void RCTStorageDirectoryMigrate() +static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath) { NSError *error; // Migrate data by copying old storage directory to new storage directory location - if (![[NSFileManager defaultManager] copyItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) toPath:RCTGetStorageDirectory() error:&error]) { + if (![[NSFileManager defaultManager] copyItemAtPath:RCTCreateStorageDirectoryPath_deprecated(oldDirectoryPath) toPath:RCTGetStorageDirectory() error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error); } else { // If copying succeeds, remove old storage directory - RCTStorageDirectoryCleanupOld(); + RCTStorageDirectoryCleanupOld(oldDirectoryPath); } } /** * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data. - * Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory. + * Check that: + * 1) Data is migrated from the Documents "RNCAsyncLocalStorage_V1" directory to the "Application Support" directory. + * 2) Data is migrated from the Documents "RCTAsyncLocalStorage_V1" directory to the "Application Support" directory. */ -static void RCTStorageDirectoryMigrationCheck() +static void RCTStorageDirectoryMigrationCheck(NSString *oldStorageDirectory) { - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - NSError *error; - BOOL isDir; - // If the old directory exists, it means we may need to migrate old data to the new directory - if ([[NSFileManager defaultManager] fileExistsAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) isDirectory:&isDir] && isDir) { - // Check if the new storage directory location already exists - if ([[NSFileManager defaultManager] fileExistsAtPath:RCTGetStorageDirectory()]) { - // If new storage location exists, check if the new storage has been modified sooner - if ([RCTManifestModificationDate(RCTGetManifestFilePath()) compare:RCTManifestModificationDate(RCTCreateManifestFilePath(RCTOldStorageDirectory))] == 1) { - // If new location has been modified more recently, simply clean out old data - RCTStorageDirectoryCleanupOld(); + NSError *error; + BOOL isDir; + NSFileManager *fileManager = [NSFileManager defaultManager]; + // If the old directory exists, it means we may need to migrate old data to the new directory + if ([fileManager fileExistsAtPath:RCTCreateStorageDirectoryPath_deprecated(oldStorageDirectory) isDirectory:&isDir] && isDir) { + // Check if the new storage directory location already exists + if ([fileManager fileExistsAtPath:RCTGetStorageDirectory()]) { + // If new storage location exists, check if the new storage has been modified sooner + if ([RCTManifestModificationDate(RCTGetManifestFilePath()) compare:RCTManifestModificationDate(RCTCreateManifestFilePath_deprecated(oldStorageDirectory))] == 1) { + // If new location has been modified more recently, simply clean out old data + RCTStorageDirectoryCleanupOld(oldStorageDirectory); + } else { + // If old location has been modified more recently, remove new storage and migrate + if (![fileManager removeItemAtPath:RCTGetStorageDirectory() error:&error]) { + RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error); } else { - // If old location has been modified more recently, remove new storage and migrate - if (![[NSFileManager defaultManager] removeItemAtPath:RCTGetStorageDirectory() error:&error]) { - RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error); - } else { - RCTStorageDirectoryMigrate(); - } + RCTStorageDirectoryMigrate(oldStorageDirectory); } - } else { - // If new storage location doesn't exist, migrate data - RCTStorageDirectoryMigrate(); } + } else { + // If new storage location doesn't exist, migrate data + RCTStorageDirectoryMigrate(oldStorageDirectory); } - }); + } } #pragma mark - RNCAsyncStorage @@ -269,7 +288,17 @@ - (instancetype)init if (!(self = [super init])) { return nil; } - RCTStorageDirectoryMigrationCheck(); + + // Both directories from our deprecated path (Documents) must be migrated to our new path (Application Support/[bundleID]) + static dispatch_once_t onceTokenOldStorage; + dispatch_once(&onceTokenOldStorage, ^{ + RCTStorageDirectoryMigrationCheck(RCTOldStorageDirectory); + }); + static dispatch_once_t onceTokenStorage; + dispatch_once(&onceTokenStorage, ^{ + RCTStorageDirectoryMigrationCheck(RCTStorageDirectory); + }); + return self; } From ad83d34301bd7d9db409c659ed7af73f10f0b701 Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Tue, 7 Jan 2020 11:04:06 -0800 Subject: [PATCH 2/5] remove unnecessary dispatch_once calls --- ios/RNCAsyncStorage.m | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index ac39c337..2d76ece8 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -181,7 +181,7 @@ static dispatch_queue_t RCTGetMethodQueue() dispatch_once(&onceToken, ^{ cache = [NSCache new]; cache.totalCostLimit = 2 * 1024 * 1024; // 2MB - + // Clear cache in the event of a memory warning [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(__unused NSNotification *note) { [cache removeAllObjects]; @@ -290,15 +290,9 @@ - (instancetype)init } // Both directories from our deprecated path (Documents) must be migrated to our new path (Application Support/[bundleID]) - static dispatch_once_t onceTokenOldStorage; - dispatch_once(&onceTokenOldStorage, ^{ - RCTStorageDirectoryMigrationCheck(RCTOldStorageDirectory); - }); - static dispatch_once_t onceTokenStorage; - dispatch_once(&onceTokenStorage, ^{ - RCTStorageDirectoryMigrationCheck(RCTStorageDirectory); - }); - + RCTStorageDirectoryMigrationCheck(RCTOldStorageDirectory); + RCTStorageDirectoryMigrationCheck(RCTStorageDirectory); + return self; } From 0c4153d6b833bd79b44e45e773ec4088e540e3e8 Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Fri, 31 Jan 2020 16:07:54 -0800 Subject: [PATCH 3/5] Add sequential migration and optional clearData flags so we can opt into or out of deleting deprecated storage locations --- ios/RNCAsyncStorage.m | 50 ++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 2d76ece8..cf8875f8 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -111,17 +111,9 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray * return storageDirectory; } -// DO NOT USE -// This is used internally to migrate data from the old file location to the new one. -// Please use `RCTCreateManifestFilePath` instead -static NSString *RCTCreateManifestFilePath_deprecated(NSString *storageDirectory) -{ - return [RCTCreateStorageDirectoryPath_deprecated(storageDirectory) stringByAppendingPathComponent:RCTManifestFileName]; -} - static NSString *RCTCreateManifestFilePath(NSString *storageDirectory) { - return [RCTCreateStorageDirectoryPath(storageDirectory) stringByAppendingPathComponent:RCTManifestFileName]; + return [storageDirectory stringByAppendingPathComponent:RCTManifestFileName]; } static NSString *RCTGetManifestFilePath() @@ -216,18 +208,18 @@ static void RCTStorageDirectoryMigrationLogError(NSString *reason, NSError *erro static void RCTStorageDirectoryCleanupOld(NSString *oldDirectoryPath) { NSError *error; - if (![[NSFileManager defaultManager] removeItemAtPath:RCTCreateStorageDirectoryPath_deprecated(oldDirectoryPath) error:&error]) { + if (![[NSFileManager defaultManager] removeItemAtPath:oldDirectoryPath error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to remove old storage directory during migration", error); } } -static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath) +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:RCTCreateStorageDirectoryPath_deprecated(oldDirectoryPath) toPath:RCTGetStorageDirectory() error:&error]) { + if (![[NSFileManager defaultManager] copyItemAtPath:oldDirectoryPath toPath:newDirectoryPath error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error); - } else { + } else if (shouldCleanupOldDirectory) { // If copying succeeds, remove old storage directory RCTStorageDirectoryCleanupOld(oldDirectoryPath); } @@ -239,30 +231,32 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath) * 1) Data is migrated from the Documents "RNCAsyncLocalStorage_V1" directory to the "Application Support" directory. * 2) Data is migrated from the Documents "RCTAsyncLocalStorage_V1" directory to the "Application Support" directory. */ -static void RCTStorageDirectoryMigrationCheck(NSString *oldStorageDirectory) +static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NSString *toStorageDirectory, BOOL shouldCleanupOldDirectory) { NSError *error; BOOL isDir; NSFileManager *fileManager = [NSFileManager defaultManager]; // If the old directory exists, it means we may need to migrate old data to the new directory - if ([fileManager fileExistsAtPath:RCTCreateStorageDirectoryPath_deprecated(oldStorageDirectory) isDirectory:&isDir] && isDir) { + if ([fileManager fileExistsAtPath:fromStorageDirectory isDirectory:&isDir] && isDir) { // Check if the new storage directory location already exists - if ([fileManager fileExistsAtPath:RCTGetStorageDirectory()]) { - // If new storage location exists, check if the new storage has been modified sooner - if ([RCTManifestModificationDate(RCTGetManifestFilePath()) compare:RCTManifestModificationDate(RCTCreateManifestFilePath_deprecated(oldStorageDirectory))] == 1) { + if ([fileManager fileExistsAtPath:toStorageDirectory]) { + // If new storage location exists, check if the new storage has been modified sooner in which case we may want to cleanup the old location + if ([RCTManifestModificationDate(RCTCreateManifestFilePath(toStorageDirectory)) compare:RCTManifestModificationDate(RCTCreateManifestFilePath(fromStorageDirectory))] == 1) { // If new location has been modified more recently, simply clean out old data - RCTStorageDirectoryCleanupOld(oldStorageDirectory); + if (shouldCleanupOldDirectory) { + RCTStorageDirectoryCleanupOld(fromStorageDirectory); + } } else { // If old location has been modified more recently, remove new storage and migrate - if (![fileManager removeItemAtPath:RCTGetStorageDirectory() error:&error]) { + if (![fileManager removeItemAtPath:toStorageDirectory error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error); } else { - RCTStorageDirectoryMigrate(oldStorageDirectory); + RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectory); } } } else { // If new storage location doesn't exist, migrate data - RCTStorageDirectoryMigrate(oldStorageDirectory); + RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectory); } } } @@ -289,9 +283,11 @@ - (instancetype)init return nil; } - // Both directories from our deprecated path (Documents) must be migrated to our new path (Application Support/[bundleID]) - RCTStorageDirectoryMigrationCheck(RCTOldStorageDirectory); - RCTStorageDirectoryMigrationCheck(RCTStorageDirectory); + // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" to "Documents/.../RCTAsyncLocalStorage_V1" + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES); + + // Then migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), RCTCreateStorageDirectoryPath(RCTStorageDirectory), NO); return self; } @@ -368,7 +364,7 @@ - (NSDictionary *)_ensureSetup } if (!_haveSetup) { NSDictionary *errorOut; - NSString *serialized = RCTReadFile(RCTGetManifestFilePath(), RCTManifestFileName, &errorOut); + NSString *serialized = RCTReadFile(RCTCreateStorageDirectoryPath(RCTGetManifestFilePath()), RCTManifestFileName, &errorOut); _manifest = serialized ? RCTJSONParseMutable(serialized, &error) : [NSMutableDictionary new]; if (error) { RCTLogWarn(@"Failed to parse manifest - creating new one.\n\n%@", error); @@ -383,7 +379,7 @@ - (NSDictionary *)_writeManifest:(NSMutableArray **)errors { NSError *error; NSString *serialized = RCTJSONStringify(_manifest, &error); - [serialized writeToFile:RCTGetManifestFilePath() atomically:YES encoding:NSUTF8StringEncoding error:&error]; + [serialized writeToFile:RCTCreateStorageDirectoryPath(RCTGetManifestFilePath()) atomically:YES encoding:NSUTF8StringEncoding error:&error]; NSDictionary *errorOut; if (error) { errorOut = RCTMakeError(@"Failed to write manifest file.", error, nil); From 1f01ddc6726f996f5d1a92b79ab7b8fca3f32ab9 Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Thu, 6 Feb 2020 11:10:51 -0800 Subject: [PATCH 4/5] Option to not overwrite ApplicationSupport data --- ios/RNCAsyncStorage.m | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index cf8875f8..d917ffb5 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -227,11 +227,13 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *new /** * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data. - * Check that: - * 1) Data is migrated from the Documents "RNCAsyncLocalStorage_V1" directory to the "Application Support" directory. - * 2) Data is migrated from the Documents "RCTAsyncLocalStorage_V1" directory to the "Application Support" directory. + * Check that data is migrated from the old location to the new location + * fromStorageDirectory: the directory where the older data lives + * toStorageDirectory: the directory where the new data should live + * shouldCleanupOldDirectory: YES if we should delete the old directory's contents after migrating to the new directory + * shouldOverwriteNewDirectory: YES if we should overwrite the new directory with the contents of the old directory IF the old directory has more recent content. If NO, perform no such overwrite on the new directory regardless of when data was last modified. */ -static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NSString *toStorageDirectory, BOOL shouldCleanupOldDirectory) +static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NSString *toStorageDirectory, BOOL shouldCleanupOldDirectory, BOOL shouldOverwriteNewDirectory) { NSError *error; BOOL isDir; @@ -246,7 +248,7 @@ static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NS if (shouldCleanupOldDirectory) { RCTStorageDirectoryCleanupOld(fromStorageDirectory); } - } else { + } else if (shouldOverwriteNewDirectory) { // If old location has been modified more recently, remove new storage and migrate if (![fileManager removeItemAtPath:toStorageDirectory error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error); @@ -284,10 +286,10 @@ - (instancetype)init } // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" to "Documents/.../RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES); + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES, YES); // Then migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), RCTCreateStorageDirectoryPath(RCTStorageDirectory), NO); + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), RCTCreateStorageDirectoryPath(RCTStorageDirectory), NO, NO); return self; } From 17ac36a13ba169da9569e7e7b228ae4b17805d10 Mon Sep 17 00:00:00 2001 From: Christopher Hogan Date: Fri, 7 Feb 2020 16:13:40 -0800 Subject: [PATCH 5/5] merge two flags into one --- ios/RNCAsyncStorage.m | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 55e93048..dc765907 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -230,10 +230,9 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, NSString *new * Check that data is migrated from the old location to the new location * fromStorageDirectory: the directory where the older data lives * toStorageDirectory: the directory where the new data should live - * shouldCleanupOldDirectory: YES if we should delete the old directory's contents after migrating to the new directory - * shouldOverwriteNewDirectory: YES if we should overwrite the new directory with the contents of the old directory IF the old directory has more recent content. If NO, perform no such overwrite on the new directory regardless of when data was last modified. + * shouldCleanupOldDirectoryAndOverwriteNewDirectory: YES if we should delete the old directory's contents and overwrite the new directory's contents during the migration to the new directory */ -static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NSString *toStorageDirectory, BOOL shouldCleanupOldDirectory, BOOL shouldOverwriteNewDirectory) +static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NSString *toStorageDirectory, BOOL shouldCleanupOldDirectoryAndOverwriteNewDirectory) { NSError *error; BOOL isDir; @@ -245,20 +244,20 @@ static void RCTStorageDirectoryMigrationCheck(NSString *fromStorageDirectory, NS // If new storage location exists, check if the new storage has been modified sooner in which case we may want to cleanup the old location if ([RCTManifestModificationDate(RCTCreateManifestFilePath(toStorageDirectory)) compare:RCTManifestModificationDate(RCTCreateManifestFilePath(fromStorageDirectory))] == 1) { // If new location has been modified more recently, simply clean out old data - if (shouldCleanupOldDirectory) { + if (shouldCleanupOldDirectoryAndOverwriteNewDirectory) { RCTStorageDirectoryCleanupOld(fromStorageDirectory); } - } else if (shouldOverwriteNewDirectory) { + } else if (shouldCleanupOldDirectoryAndOverwriteNewDirectory) { // If old location has been modified more recently, remove new storage and migrate if (![fileManager removeItemAtPath:toStorageDirectory error:&error]) { RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error); } else { - RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectory); + RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectoryAndOverwriteNewDirectory); } } } else { // If new storage location doesn't exist, migrate data - RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectory); + RCTStorageDirectoryMigrate(fromStorageDirectory, toStorageDirectory, shouldCleanupOldDirectoryAndOverwriteNewDirectory); } } } @@ -286,10 +285,10 @@ - (instancetype)init } // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" to "Documents/.../RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES, YES); + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES); // Then migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), RCTCreateStorageDirectoryPath(RCTStorageDirectory), NO, NO); + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), RCTCreateStorageDirectoryPath(RCTStorageDirectory), NO); return self; } @@ -367,7 +366,7 @@ - (NSDictionary *)_ensureSetup if (!_haveSetup) { NSDictionary *errorOut = nil; - NSString *serialized = RCTReadFile(RCTCreateStorageDirectoryPath(RCTGetManifestFilePath(), RCTManifestFileName, &errorOut); + NSString *serialized = RCTReadFile(RCTCreateStorageDirectoryPath(RCTGetManifestFilePath()), RCTManifestFileName, &errorOut); if (!serialized) { if (errorOut) { // We cannot simply create a new manifest in case the file does exist but we have no access to it.