From 2915c533b2c18ddcd9b932f9299600367f3f0eee Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 13 Mar 2018 12:42:50 -0300 Subject: [PATCH] Streamline database configuration and cleanup. --- .../OWSBackupSettingsViewController.m | 7 -- Signal/src/util/OWSBackupExportJob.m | 95 ++++++++++++++----- Signal/src/util/OWSBackupImportJob.m | 24 ++--- Signal/src/util/OWSBackupJob.m | 2 + 4 files changed, 86 insertions(+), 42 deletions(-) diff --git a/Signal/src/ViewControllers/AppSettings/OWSBackupSettingsViewController.m b/Signal/src/ViewControllers/AppSettings/OWSBackupSettingsViewController.m index 9651a72e8..f8624f8da 100644 --- a/Signal/src/ViewControllers/AppSettings/OWSBackupSettingsViewController.m +++ b/Signal/src/ViewControllers/AppSettings/OWSBackupSettingsViewController.m @@ -88,13 +88,6 @@ NS_ASSUME_NONNULL_BEGIN if (OWSBackup.sharedManager.backupExportProgress) { NSUInteger progressPercent = (NSUInteger)round(OWSBackup.sharedManager.backupExportProgress.floatValue * 100); - DDLogVerbose(@"%@ '%@', '%@'", - self.logTag, - @(progressPercent).stringValue, - [NSString - stringWithFormat:NSLocalizedString(@"PERCENTAGE_FORMAT", - @"Format for percentages, e.g. 65%. Embeds {{percentage}}, e.g. 65."), - @(progressPercent).stringValue]); [progressSection addItem:[OWSTableItem labelItemWithText:NSLocalizedString(@"SETTINGS_BACKUP_PROGRESS", diff --git a/Signal/src/util/OWSBackupExportJob.m b/Signal/src/util/OWSBackupExportJob.m index 4dd855c27..59fd7438f 100644 --- a/Signal/src/util/OWSBackupExportJob.m +++ b/Signal/src/util/OWSBackupExportJob.m @@ -86,10 +86,10 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe @interface OWSBackupExportJob () -@property (nonatomic, nullable) OWSBackupStorage *backupStorage; - @property (nonatomic, nullable) OWSBackgroundTask *backgroundTask; +@property (nonatomic, nullable) OWSBackupStorage *backupStorage; + @property (nonatomic) NSMutableArray *databaseFilePaths; // A map of "record name"-to-"file name". @property (nonatomic) NSMutableDictionary *databaseRecordMap; @@ -135,8 +135,8 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe progress:nil]; __weak OWSBackupExportJob *weakSelf = self; - [self configureExport:^(BOOL success) { - if (!success) { + [self configureExport:^(BOOL configureExportSuccess) { + if (!configureExportSuccess) { [self failWithErrorDescription: NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT", @"Error indicating the a backup export could not export the user's data.")]; @@ -149,26 +149,29 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe [self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_EXPORT", @"Indicates that the backup export data is being exported.") progress:nil]; - if (![self exportDatabase]) { - [self failWithErrorDescription: - NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT", - @"Error indicating the a backup export could not export the user's data.")]; - return; - } - if (self.isComplete) { - return; - } - [self saveToCloud:^(NSError *_Nullable saveError) { - if (saveError) { - [weakSelf failWithError:saveError]; + [self exportDatabase:^(BOOL exportDatabaseSuccess) { + if (!exportDatabaseSuccess) { + [self failWithErrorDescription: + NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT", + @"Error indicating the a backup export could not export the user's data.")]; return; } - [self cleanUpCloud:^(NSError *_Nullable cleanUpError) { - if (cleanUpError) { - [weakSelf failWithError:cleanUpError]; + + if (self.isComplete) { + return; + } + [self saveToCloud:^(NSError *_Nullable saveError) { + if (saveError) { + [weakSelf failWithError:saveError]; return; } - [weakSelf succeed]; + [self cleanUpCloud:^(NSError *_Nullable cleanUpError) { + if (cleanUpError) { + [weakSelf failWithError:cleanUpError]; + return; + } + [weakSelf succeed]; + }]; }]; }]; }]; @@ -184,6 +187,48 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe OWSProdLogAndFail(@"%@ Could not create jobTempDirPath.", self.logTag); return completion(NO); } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + completion(YES); + }); + + // TSRequest *currentSignedPreKey = [OWSRequestFactory currentSignedPreKeyRequest]; + // [[TSNetworkManager sharedManager] makeRequest:currentSignedPreKey + // success:^(NSURLSessionDataTask *task, NSDictionary *responseObject) { + // NSString *keyIdDictKey = @"keyId"; + // NSNumber *keyId = [responseObject objectForKey:keyIdDictKey]; + // OWSAssert(keyId); + // + // OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage + // sharedManager]; NSNumber *currentSignedPrekeyId = [primaryStorage + // currentSignedPrekeyId]; + // + // if (!keyId || !currentSignedPrekeyId || ![currentSignedPrekeyId + // isEqualToNumber:keyId]) { + // DDLogError( + // @"%@ Local and service 'current signed prekey ids' + // did not match. %@ == %@ == %d.", self.logTag, keyId, + // currentSignedPrekeyId, + // [currentSignedPrekeyId isEqualToNumber:keyId]); + // } + // } + // failure:^(NSURLSessionDataTask *task, NSError *error) { + // if (!IsNSErrorNetworkFailure(error)) { + // OWSProdError([OWSAnalyticsEvents + // errorPrekeysCurrentSignedPrekeyRequestFailed]); + // } + // DDLogWarn(@"%@ Could not retrieve current signed key from the + // service.", self.logTag); + // + // // Mark the prekeys as _NOT_ checked on failure. + // [self markPreKeysAsNotChecked]; + // }]; +} + +- (void)exportDatabase:(OWSBackupJobBoolCompletion)completion +{ + OWSAssert(completion); + + DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); if (![OWSBackupJob generateRandomDatabaseKeySpecWithKeychainKey:kOWSBackup_ExportDatabaseKeySpec]) { OWSProdLogAndFail(@"%@ Could not generate database key spec for export.", self.logTag); @@ -205,6 +250,7 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe } return databaseKeySpec; }; + self.backupStorage = [[OWSBackupStorage alloc] initBackupStorageWithDatabaseDirPath:jobDatabaseDirPath keySpecBlock:keySpecBlock]; if (!self.backupStorage) { @@ -213,17 +259,18 @@ NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKe } // TODO: Do we really need to run these registrations on the main thread? + __weak OWSBackupExportJob *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [self.backupStorage runSyncRegistrations]; - [self.backupStorage runAsyncRegistrationsWithCompletion:^{ + [weakSelf.backupStorage runSyncRegistrations]; + [weakSelf.backupStorage runAsyncRegistrationsWithCompletion:^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - completion(YES); + completion([weakSelf exportDatabaseContents]); }); }]; }); } -- (BOOL)exportDatabase +- (BOOL)exportDatabaseContents { DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); diff --git a/Signal/src/util/OWSBackupImportJob.m b/Signal/src/util/OWSBackupImportJob.m index 8d8c8c63a..1ca547a60 100644 --- a/Signal/src/util/OWSBackupImportJob.m +++ b/Signal/src/util/OWSBackupImportJob.m @@ -24,6 +24,8 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe @property (nonatomic, nullable) OWSBackgroundTask *backgroundTask; +@property (nonatomic, nullable) OWSBackupStorage *backupStorage; + // A map of "record name"-to-"file name". @property (nonatomic) NSMutableDictionary *databaseRecordMap; @@ -347,9 +349,9 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe } return databaseKeySpec; }; - OWSBackupStorage *_Nullable backupStorage = + self.backupStorage = [[OWSBackupStorage alloc] initBackupStorageWithDatabaseDirPath:jobDatabaseDirPath keySpecBlock:keySpecBlock]; - if (!backupStorage) { + if (!self.backupStorage) { OWSProdLogAndFail(@"%@ Could not create backupStorage.", self.logTag); return completion(NO); } @@ -357,18 +359,18 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe // TODO: Do we really need to run these registrations on the main thread? __weak OWSBackupImportJob *weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ - [backupStorage runSyncRegistrations]; - [backupStorage runAsyncRegistrationsWithCompletion:^{ + [weakSelf.backupStorage runSyncRegistrations]; + [weakSelf.backupStorage runAsyncRegistrationsWithCompletion:^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [weakSelf restoreDatabaseContents:backupStorage completion:completion]; + [weakSelf restoreDatabaseContents:completion]; }); }]; }); } -- (void)restoreDatabaseContents:(OWSBackupStorage *)backupStorage completion:(OWSBackupJobBoolCompletion)completion +- (void)restoreDatabaseContents:(OWSBackupJobBoolCompletion)completion { - OWSAssert(backupStorage); + OWSAssert(self.backupStorage); OWSAssert(completion); DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__); @@ -377,7 +379,7 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe return completion(NO); } - YapDatabaseConnection *_Nullable tempDBConnection = backupStorage.newDatabaseConnection; + YapDatabaseConnection *_Nullable tempDBConnection = self.backupStorage.newDatabaseConnection; if (!tempDBConnection) { OWSProdLogAndFail(@"%@ Could not create tempDBConnection.", self.logTag); return completion(NO); @@ -480,13 +482,13 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe } DDLogInfo(@"%@ copiedEntities: %llu", self.logTag, copiedEntities); - [backupStorage logFileSizes]; + [self.backupStorage logFileSizes]; // Close the database. tempDBConnection = nil; - backupStorage = nil; + self.backupStorage = nil; - return completion(YES); + completion(YES); } - (void)ensureMigrations:(OWSBackupJobBoolCompletion)completion diff --git a/Signal/src/util/OWSBackupJob.m b/Signal/src/util/OWSBackupJob.m index 895845e50..eda6a4a4a 100644 --- a/Signal/src/util/OWSBackupJob.m +++ b/Signal/src/util/OWSBackupJob.m @@ -97,6 +97,7 @@ NSString *const kOWSBackup_Snapshot_ValidKey = @"kOWSBackup_Snapshot_ValidKey"; dispatch_async(dispatch_get_main_queue(), ^{ if (self.isComplete) { + OWSAssert(!self.hasSucceeded); return; } self.isComplete = YES; @@ -120,6 +121,7 @@ NSString *const kOWSBackup_Snapshot_ValidKey = @"kOWSBackup_Snapshot_ValidKey"; OWSProdLogAndFail(@"%@ %s %@", self.logTag, __PRETTY_FUNCTION__, error); dispatch_async(dispatch_get_main_queue(), ^{ + OWSAssert(!self.hasSucceeded); if (self.isComplete) { return; }