Skip to content

Commit 964b16a

Browse files
committed
Check manifest's last modified date during migration
1 parent 2aa0c17 commit 964b16a

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

ios/RNCAsyncStorage.m

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,17 @@ static void RCTAppendError(NSDictionary *error, NSMutableArray<NSDictionary *> *
9999
return storageDirectory;
100100
}
101101

102+
static NSString *RCTCreateManifestFilePath(NSString *storageDirectory)
103+
{
104+
return [RCTCreateStorageDirectoryPath(storageDirectory) stringByAppendingString:RCTManifestFileName];
105+
}
106+
102107
static NSString *RCTGetManifestFilePath()
103108
{
104109
static NSString *manifestFilePath = nil;
105110
static dispatch_once_t onceToken;
106111
dispatch_once(&onceToken, ^{
107-
manifestFilePath = [RCTGetStorageDirectory() stringByAppendingPathComponent:RCTManifestFileName];
112+
manifestFilePath = RCTCreateManifestFilePath(RCTStorageDirectory);
108113
});
109114
return manifestFilePath;
110115
}
@@ -174,44 +179,70 @@ static dispatch_queue_t RCTGetMethodQueue()
174179
return error ? RCTMakeError(@"Failed to delete storage directory.", error, nil) : nil;
175180
}
176181

182+
static NSDate *RCTManifestModificationDate(NSString *manifestFilePath)
183+
{
184+
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:manifestFilePath error:nil];
185+
return [attributes fileModificationDate];
186+
}
177187

178188
NSString *const RCTOldStorageDirectory = @"RNCAsyncLocalStorage_V1";
179189
/**
180190
* Creates an NSException used during Storage Directory Migration.
181191
*/
182-
static void RCTStorageDirectoryMigrationLogError(NSString *reason, NSError *error) {
192+
static void RCTStorageDirectoryMigrationLogError(NSString *reason, NSError *error)
193+
{
183194
NSLog(@"%@: %@", reason, error ? error.description : @"");
184195
}
185196

197+
static void RCTStorageDirectoryCleanupOld()
198+
{
199+
NSError *error;
200+
if (![[NSFileManager defaultManager] removeItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) error:&error]) {
201+
RCTStorageDirectoryMigrationLogError(@"Failed to remove old storage directory during migration", error);
202+
}
203+
}
204+
205+
static void RCTStorageDirectoryMigrate()
206+
{
207+
NSError *error;
208+
// Migrate data by copying old storage directory to new storage directory location
209+
if (![[NSFileManager defaultManager] copyItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) toPath:RCTGetStorageDirectory() error:&error]) {
210+
RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory location during migration", error);
211+
} else {
212+
// If copying succeeds, remove old storage directory
213+
RCTStorageDirectoryCleanupOld();
214+
}
215+
}
216+
186217
/**
187218
* This check is added to make sure that anyone coming from pre-1.2.2 does not lose cached data.
188219
* Data is migrated from the "RNCAsyncLocalStorage_V1" directory to the "RCTAsyncLocalStorage_V1" directory.
189220
*/
190-
static void RCTStorageDirectoryMigrationCheck() {
221+
static void RCTStorageDirectoryMigrationCheck()
222+
{
191223
static dispatch_once_t onceToken;
192224
dispatch_once(&onceToken, ^{
193225
NSError *error;
194226
BOOL isDir;
195-
// If the old directory exists, it means we need to migrate data to the new directory
227+
// If the old directory exists, it means we may need to migrate old data to the new directory
196228
if ([[NSFileManager defaultManager] fileExistsAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) isDirectory:&isDir] && isDir) {
197229
// Check if the new storage directory location already exists
198-
BOOL newStorageDirectoryExists = [[NSFileManager defaultManager] fileExistsAtPath:RCTGetStorageDirectory() isDirectory:&isDir];
199-
if (newStorageDirectoryExists) {
200-
// If the new storage directory location already exists, remove existing directory
201-
newStorageDirectoryExists = !([[NSFileManager defaultManager] removeItemAtPath:RCTGetStorageDirectory() error:&error]);
202-
if (newStorageDirectoryExists) {
203-
RCTStorageDirectoryMigrationLogError(@"Failed to clear pre-existing storage directory", error);
204-
}
205-
}
206-
if (!newStorageDirectoryExists) {
207-
// If new storage direction doesn't exist, copy old storage directory to new location
208-
if (![[NSFileManager defaultManager] copyItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) toPath:RCTGetStorageDirectory() error:&error]) {
209-
RCTStorageDirectoryMigrationLogError(@"Failed to copy old storage directory to new storage directory", error);
230+
if ([[NSFileManager defaultManager] fileExistsAtPath:RCTGetStorageDirectory()]) {
231+
// If new storage location exists, check if the new storage has been modified sooner
232+
if ([RCTManifestModificationDate(RCTGetManifestFilePath()) compare:RCTManifestModificationDate(RCTCreateManifestFilePath(RCTOldStorageDirectory))] == 1) {
233+
// If new location has been modified more recently, simply clean out old data
234+
RCTStorageDirectoryCleanupOld();
210235
} else {
211-
// If copying succeeds, remove old storage directory
212-
[[NSFileManager defaultManager] removeItemAtPath:RCTCreateStorageDirectoryPath(RCTOldStorageDirectory) error:&error];
213-
if (error) RCTStorageDirectoryMigrationLogError(@"Failed to remove old storage directory after migration", error);
236+
// If old location has been modified more recently, remove new storage and migrate
237+
if (![[NSFileManager defaultManager] removeItemAtPath:RCTGetStorageDirectory() error:&error]) {
238+
RCTStorageDirectoryMigrationLogError(@"Failed to remove new storage directory during migration", error);
239+
} else {
240+
RCTStorageDirectoryMigrate();
241+
}
214242
}
243+
} else {
244+
// If new storage location doesn't exist, migrate data
245+
RCTStorageDirectoryMigrate();
215246
}
216247
}
217248
});
@@ -228,7 +259,8 @@ @implementation RNCAsyncStorage
228259
NSMutableDictionary<NSString *, NSString *> *_manifest;
229260
}
230261

231-
+ (BOOL)requiresMainQueueSetup {
262+
+ (BOOL)requiresMainQueueSetup
263+
{
232264
return NO;
233265
}
234266

0 commit comments

Comments
 (0)