From af56935dcc9ce68aebef7fb2c13511395413a878 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Mon, 15 Mar 2021 18:16:20 -0700 Subject: [PATCH 1/4] [ios] Migrate Expo AsyncStorage location if it exists --- ios/RNCAsyncStorage.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 499c9597..36be9af5 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -16,6 +16,7 @@ static NSString *const RCTStorageDirectory = @"RCTAsyncLocalStorage_V1"; static NSString *const RCTOldStorageDirectory = @"RNCAsyncLocalStorage_V1"; +static NSString *const RCTExpoStorageDirectory = @"RCTAsyncLocalStorage"; static NSString *const RCTManifestFileName = @"manifest.json"; static const NSUInteger RCTInlineValueThreshold = 1024; @@ -392,6 +393,13 @@ - (instancetype)init RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), YES); + // Alternatively, migrate Expo's managed app path "Documents/.../RNCAsyncLocalStorage" to + // "Documents/.../RCTAsyncLocalStorage_V1" + RCTStorageDirectoryMigrationCheck( + RCTCreateStorageDirectoryPath_deprecated(RCTExpoStorageDirectory), + RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), + YES); + // Then migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to "Application // Support/[bundleID]/RCTAsyncLocalStorage_V1" RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), From f6bad535217c5ee10e8b087751295c11aa14fa41 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Wed, 17 Mar 2021 12:25:45 -0700 Subject: [PATCH 2/4] [ios] Handle the edge case where both the old storage directory and Expo storage directories exist --- ios/RNCAsyncStorage.m | 76 ++++++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 36be9af5..eca9780f 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -316,6 +316,46 @@ static void RCTStorageDirectoryMigrate(NSString *oldDirectoryPath, } } + +/** + * Determine which of RCTOldStorageDirectory or RCTExpoStorageDirectory needs to migrated. + * If both exist, we remove the least recently modified and return the most recently modified. + * Otherwise, this will return the path to whichever directory exists. + * If no directory exists, then return nil. + */ +static NSString *RCTGetStoragePathForMigration() +{ + BOOL isDir; + NSString *oldStoragePath = RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory); + NSString *expoStoragePath = RCTCreateStorageDirectoryPath_deprecated(RCTExpoStorageDirectory); + NSFileManager *fileManager = [NSFileManager defaultManager]; + BOOL oldStorageDirectoryExists = [fileManager fileExistsAtPath:oldStoragePath isDirectory:&isDir] && isDir; + BOOL expoStorageDirectoryExists = [fileManager fileExistsAtPath:expoStoragePath isDirectory:&isDir] && isDir; + + + // Check if both the old storage directory and Expo storage directory exist + if (oldStorageDirectoryExists && expoStorageDirectoryExists) { + // If the old storage has been modified more recently than Expo storage, then clear Expo storage. + // Otherwise, clear the old storage. + if ([RCTManifestModificationDate(RCTCreateManifestFilePath(oldStoragePath)) + compare:RCTManifestModificationDate( + RCTCreateManifestFilePath(expoStoragePath))] == NSOrderedDescending) { + RCTStorageDirectoryCleanupOld(expoStoragePath); + return oldStoragePath; + } else { + RCTStorageDirectoryCleanupOld(oldStoragePath); + return expoStoragePath; + } + } else if (oldStorageDirectoryExists) { + return oldStoragePath; + } else if (expoStorageDirectoryExists) { + return expoStoragePath; + } else { + return nil; + } +} + + /** * This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data. * Check that data is migrated from the old location to the new location @@ -386,25 +426,23 @@ - (instancetype)init return nil; } - // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" to - // "Documents/.../RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck( - RCTCreateStorageDirectoryPath_deprecated(RCTOldStorageDirectory), - RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), - YES); - - // Alternatively, migrate Expo's managed app path "Documents/.../RNCAsyncLocalStorage" to - // "Documents/.../RCTAsyncLocalStorage_V1" - RCTStorageDirectoryMigrationCheck( - RCTCreateStorageDirectoryPath_deprecated(RCTExpoStorageDirectory), - 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); + // Get the path to any old storage directory that needs to be migrated. If multiple exist, + // the oldest are removed and the most recently modified is returned. + NSString *oldStoragePath = RCTGetStoragePathForMigration(); + if (oldStoragePath != nil) { + // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" or + // "Documents/.../RCTsyncLocalStorage" to "Documents/.../RCTAsyncLocalStorage_V1" + RCTStorageDirectoryMigrationCheck( + oldStoragePath, + 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; } From 1cfb7fbfd9f555650c16fa1e8f598777de4e4c14 Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Mon, 29 Mar 2021 19:35:02 -0700 Subject: [PATCH 3/4] [ios] Address feedback from @Krizzu --- ios/RNCAsyncStorage.m | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index eca9780f..88e5717b 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -430,20 +430,20 @@ - (instancetype)init // the oldest are removed and the most recently modified is returned. NSString *oldStoragePath = RCTGetStoragePathForMigration(); if (oldStoragePath != nil) { - // First migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" or + // Migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" or // "Documents/.../RCTsyncLocalStorage" to "Documents/.../RCTAsyncLocalStorage_V1" RCTStorageDirectoryMigrationCheck( oldStoragePath, 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); } + // Migrate what's in "Documents/.../RCTAsyncLocalStorage_V1" to + // "Application Support/[bundleID]/RCTAsyncLocalStorage_V1" + RCTStorageDirectoryMigrationCheck(RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory), + RCTCreateStorageDirectoryPath(RCTStorageDirectory), + NO); + return self; } From e242fd14694e3761f01e7d342cf270d14b933fac Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Thu, 1 Apr 2021 10:19:53 -0700 Subject: [PATCH 4/4] [ios] Fix comment typo --- ios/RNCAsyncStorage.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ios/RNCAsyncStorage.m b/ios/RNCAsyncStorage.m index 88e5717b..b25cf861 100644 --- a/ios/RNCAsyncStorage.m +++ b/ios/RNCAsyncStorage.m @@ -431,7 +431,7 @@ - (instancetype)init NSString *oldStoragePath = RCTGetStoragePathForMigration(); if (oldStoragePath != nil) { // Migrate our deprecated path "Documents/.../RNCAsyncLocalStorage_V1" or - // "Documents/.../RCTsyncLocalStorage" to "Documents/.../RCTAsyncLocalStorage_V1" + // "Documents/.../RCTAsyncLocalStorage" to "Documents/.../RCTAsyncLocalStorage_V1" RCTStorageDirectoryMigrationCheck( oldStoragePath, RCTCreateStorageDirectoryPath_deprecated(RCTStorageDirectory),