Improve the robustness of the migration logic.

pull/1/head
Matthew Chen 7 years ago
parent cf507487c1
commit 706006539f

@ -1118,8 +1118,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
return [OWSFileSystem moveAppFilePath:self.legacyProfileAvatarsDirPath return [OWSFileSystem moveAppFilePath:self.legacyProfileAvatarsDirPath
sharedDataFilePath:self.sharedDataProfileAvatarsDirPath sharedDataFilePath:self.sharedDataProfileAvatarsDirPath];
exceptionName:@"ProfileManagerCouldNotMigrateProfileDirectory"];
} }
- (NSString *)profileAvatarsDirPath - (NSString *)profileAvatarsDirPath

@ -199,8 +199,7 @@ NS_ASSUME_NONNULL_BEGIN
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
return [OWSFileSystem moveAppFilePath:self.legacyAttachmentsDirPath return [OWSFileSystem moveAppFilePath:self.legacyAttachmentsDirPath
sharedDataFilePath:self.sharedDataAttachmentsDirPath sharedDataFilePath:self.sharedDataAttachmentsDirPath];
exceptionName:@"CouldNotMigrateAttachmentsDirectory"];
} }
+ (NSString *)attachmentsFolder + (NSString *)attachmentsFolder

@ -18,8 +18,6 @@
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
NSString *const TSStorageManagerExceptionName_CouldNotMoveDatabaseFile
= @"TSStorageManagerExceptionName_CouldNotMoveDatabaseFile";
NSString *const TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory NSString *const TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory
= @"TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory"; = @"TSStorageManagerExceptionName_CouldNotCreateDatabaseDirectory";
@ -255,21 +253,35 @@ void runAsyncRegistrationsForStorage(OWSStorage *storage)
[OWSFileSystem protectFileOrFolderAtPath:self.legacyDatabaseFilePath_WAL]; [OWSFileSystem protectFileOrFolderAtPath:self.legacyDatabaseFilePath_WAL];
NSError *_Nullable error = nil; NSError *_Nullable error = nil;
error = [OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath if ([fileManager fileExistsAtPath:self.legacyDatabaseFilePath] &&
sharedDataFilePath:self.sharedDataDatabaseFilePath [fileManager fileExistsAtPath:self.sharedDataDatabaseFilePath]) {
exceptionName:TSStorageManagerExceptionName_CouldNotMoveDatabaseFile]; // In the case that we have a "database conflict" (i.e. database files
// in the src and dst locations), ensure database integrity by renaming
// all of the dst database files.
for (NSString *filePath in @[
self.sharedDataDatabaseFilePath,
self.sharedDataDatabaseFilePath_SHM,
self.sharedDataDatabaseFilePath_WAL,
]) {
error = [OWSFileSystem renameFilePathUsingRandomExtension:filePath];
if (error) {
return error;
}
}
}
error =
[OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath sharedDataFilePath:self.sharedDataDatabaseFilePath];
if (error) { if (error) {
return error; return error;
} }
error = [OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath_SHM error = [OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath_SHM
sharedDataFilePath:self.sharedDataDatabaseFilePath_SHM sharedDataFilePath:self.sharedDataDatabaseFilePath_SHM];
exceptionName:TSStorageManagerExceptionName_CouldNotMoveDatabaseFile];
if (error) { if (error) {
return error; return error;
} }
error = [OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath_WAL error = [OWSFileSystem moveAppFilePath:self.legacyDatabaseFilePath_WAL
sharedDataFilePath:self.sharedDataDatabaseFilePath_WAL sharedDataFilePath:self.sharedDataDatabaseFilePath_WAL];
exceptionName:TSStorageManagerExceptionName_CouldNotMoveDatabaseFile];
if (error) { if (error) {
return error; return error;
} }

@ -17,9 +17,9 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString *)cachesDirectoryPath; + (NSString *)cachesDirectoryPath;
+ (nullable NSError *)moveAppFilePath:(NSString *)oldFilePath + (nullable NSError *)renameFilePathUsingRandomExtension:(NSString *)oldFilePath;
sharedDataFilePath:(NSString *)newFilePath
exceptionName:(NSString *)exceptionName; + (nullable NSError *)moveAppFilePath:(NSString *)oldFilePath sharedDataFilePath:(NSString *)newFilePath;
// Returns NO IFF the directory does not exist and could not be created. // Returns NO IFF the directory does not exist and could not be created.
+ (BOOL)ensureDirectoryExists:(NSString *)dirPath; + (BOOL)ensureDirectoryExists:(NSString *)dirPath;

@ -108,38 +108,47 @@ NS_ASSUME_NONNULL_BEGIN
return paths[0]; return paths[0];
} }
+ (nullable NSError *)moveAppFilePath:(NSString *)oldFilePath + (nullable NSError *)renameFilePathUsingRandomExtension:(NSString *)oldFilePath
sharedDataFilePath:(NSString *)newFilePath
exceptionName:(NSString *)exceptionName
{ {
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:oldFilePath]) { if (![fileManager fileExistsAtPath:oldFilePath]) {
return nil; return nil;
} }
DDLogInfo(@"%@ Moving file or directory from: %@ to: %@", self.logTag, oldFilePath, newFilePath); NSString *newFilePath =
[[oldFilePath stringByAppendingString:@"."] stringByAppendingString:[NSUUID UUID].UUIDString];
if ([fileManager fileExistsAtPath:newFilePath]) { DDLogInfo(@"%@ Moving file or directory from: %@ to: %@", self.logTag, oldFilePath, newFilePath);
// If a file/directory already exists at the destination,
// try to move it "aside" by renaming it with an extension.
NSString *legacyFilePath =
[[newFilePath stringByAppendingString:@"."] stringByAppendingString:[NSUUID UUID].UUIDString];
DDLogInfo(@"%@ Trying to rename pre-existing destination file or directory from: %@ to: %@",
self.logTag,
newFilePath,
legacyFilePath);
NSError *_Nullable error; NSError *_Nullable error;
BOOL success = [fileManager moveItemAtPath:newFilePath toPath:legacyFilePath error:&error]; BOOL success = [fileManager moveItemAtPath:oldFilePath toPath:newFilePath error:&error];
if (!success || error) { if (!success || error) {
OWSProdLogAndFail( OWSProdLogAndFail(@"%@ Could not move file or directory from: %@ to: %@, error: %@",
@"%@ Could not move pre-existing destination file or directory from: %@ to: %@, error: %@",
self.logTag, self.logTag,
oldFilePath,
newFilePath, newFilePath,
legacyFilePath,
error); error);
return error; return error;
} }
return nil;
}
+ (nullable NSError *)moveAppFilePath:(NSString *)oldFilePath sharedDataFilePath:(NSString *)newFilePath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:oldFilePath]) {
return nil;
}
DDLogInfo(@"%@ Moving file or directory from: %@ to: %@", self.logTag, oldFilePath, newFilePath);
if ([fileManager fileExistsAtPath:newFilePath]) {
// If a file/directory already exists at the destination,
// try to move it "aside" by renaming it with an extension.
NSError *_Nullable error = [self renameFilePathUsingRandomExtension:newFilePath];
if (error) {
return error;
}
} }
if ([fileManager fileExistsAtPath:newFilePath]) { if ([fileManager fileExistsAtPath:newFilePath]) {

Loading…
Cancel
Save