diff --git a/Signal/src/util/OWSBackup.m b/Signal/src/util/OWSBackup.m index ca966ba16..94a76ff5a 100644 --- a/Signal/src/util/OWSBackup.m +++ b/Signal/src/util/OWSBackup.m @@ -616,9 +616,11 @@ NS_ASSUME_NONNULL_BEGIN return completion(NO); } - if (![backupIO decryptFileAsFile:encryptedFilePath dstFilePath:decryptedFilePath encryptionKey:encryptionKey]) { - DDLogError(@"%@ Could not load decrypt file.", self.logTag); - return completion(NO); + @autoreleasepool { + if (![backupIO decryptFileAsFile:encryptedFilePath dstFilePath:decryptedFilePath encryptionKey:encryptionKey]) { + DDLogError(@"%@ Could not load decrypt file.", self.logTag); + return completion(NO); + } } NSString *_Nullable attachmentFilePath = [attachment filePath]; diff --git a/Signal/src/util/OWSBackupImportJob.m b/Signal/src/util/OWSBackupImportJob.m index b105ed000..37ccab8f1 100644 --- a/Signal/src/util/OWSBackupImportJob.m +++ b/Signal/src/util/OWSBackupImportJob.m @@ -255,53 +255,43 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe { DDLogVerbose(@"%@ %s: %zd", self.logTag, __PRETTY_FUNCTION__, self.attachmentsItems.count); - NSString *attachmentsDirPath = [TSAttachmentStream attachmentsFolder]; - - NSUInteger count = 0; - for (OWSBackupFragment *item in self.attachmentsItems) { - if (self.isComplete) { - return; - } - if (item.recordName.length < 1) { - DDLogError(@"%@ attachment was not downloaded.", self.logTag); - // Attachment-related errors are recoverable and can be ignored. - continue; - } - if (item.relativeFilePath.length < 1) { - DDLogError(@"%@ attachment missing relative file path.", self.logTag); - // Attachment-related errors are recoverable and can be ignored. - continue; - } - - count++; - [self updateProgressWithDescription:NSLocalizedString(@"BACKUP_IMPORT_PHASE_RESTORING_FILES", - @"Indicates that the backup import data is being restored.") - progress:@(count / (CGFloat)self.attachmentsItems.count)]; - - NSString *dstFilePath = [attachmentsDirPath stringByAppendingPathComponent:item.relativeFilePath]; - if ([NSFileManager.defaultManager fileExistsAtPath:dstFilePath]) { - DDLogError(@"%@ skipping redundant file restore: %@.", self.logTag, dstFilePath); - continue; - } - NSString *dstDirPath = [dstFilePath stringByDeletingLastPathComponent]; - if (![NSFileManager.defaultManager fileExistsAtPath:dstDirPath]) { - if (![OWSFileSystem ensureDirectoryExists:dstDirPath]) { - DDLogError(@"%@ couldn't create directory for file restore: %@.", self.logTag, dstFilePath); + __block NSUInteger count = 0; + [self.primaryStorage.newDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + for (OWSBackupFragment *item in self.attachmentsItems) { + if (self.isComplete) { + return; + } + if (item.recordName.length < 1) { + DDLogError(@"%@ attachment was not downloaded.", self.logTag); + // Attachment-related errors are recoverable and can be ignored. continue; } - } - @autoreleasepool { - if (![self.backupIO decryptFileAsFile:item.downloadFilePath - dstFilePath:dstFilePath - encryptionKey:item.encryptionKey]) { - DDLogError(@"%@ attachment could not be restored.", self.logTag); + if (item.attachmentId.length < 1) { + DDLogError(@"%@ attachment missing attachment id.", self.logTag); + // Attachment-related errors are recoverable and can be ignored. + continue; + } + if (item.relativeFilePath.length < 1) { + DDLogError(@"%@ attachment missing relative file path.", self.logTag); // Attachment-related errors are recoverable and can be ignored. continue; } + TSAttachmentStream *_Nullable attachment = + [TSAttachmentStream fetchObjectWithUniqueID:item.attachmentId transaction:transaction]; + if (!attachment) { + DDLogError(@"%@ attachment to restore could not be found.", self.logTag); + // Attachment-related errors are recoverable and can be ignored. + continue; + } + [attachment updateWithLazyRestoreFragment:item transaction:transaction]; + count++; + [self updateProgressWithDescription:NSLocalizedString(@"BACKUP_IMPORT_PHASE_RESTORING_FILES", + @"Indicates that the backup import data is being restored.") + progress:@(count / (CGFloat)self.attachmentsItems.count)]; } + }]; - DDLogError(@"%@ restored file: %@.", self.logTag, item.relativeFilePath); - } + DDLogError(@"%@ enqueued lazy restore of %zd files.", self.logTag, count); } - (void)restoreDatabaseWithCompletion:(OWSBackupJobBoolCompletion)completion diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h index ad3d18dd5..aabd1226d 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.h @@ -69,7 +69,8 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Update With... Methods // Marks attachment as needing "lazy backup restore." -- (void)updateWithLazyRestoreFragment:(OWSBackupFragment *)lazyRestoreFragment; +- (void)updateWithLazyRestoreFragment:(OWSBackupFragment *)lazyRestoreFragment + transaction:(YapDatabaseReadWriteTransaction *)transaction; // Marks attachment as having completed "lazy backup restore." - (void)updateWithLazyRestoreComplete; diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m index 343730a4b..d1f958315 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachmentStream.m @@ -624,21 +624,21 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Update With... Methods - (void)updateWithLazyRestoreFragment:(OWSBackupFragment *)lazyRestoreFragment + transaction:(YapDatabaseReadWriteTransaction *)transaction { OWSAssert(lazyRestoreFragment); + OWSAssert(transaction); - [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - if (!lazyRestoreFragment.uniqueId) { - // If metadata hasn't been saved yet, save now. - [lazyRestoreFragment saveWithTransaction:transaction]; + if (!lazyRestoreFragment.uniqueId) { + // If metadata hasn't been saved yet, save now. + [lazyRestoreFragment saveWithTransaction:transaction]; - OWSAssert(lazyRestoreFragment.uniqueId); - } - [self applyChangeToSelfAndLatestCopy:transaction - changeBlock:^(TSAttachmentStream *attachment) { - [attachment setLazyRestoreFragmentId:lazyRestoreFragment.uniqueId]; - }]; - }]; + OWSAssert(lazyRestoreFragment.uniqueId); + } + [self applyChangeToSelfAndLatestCopy:transaction + changeBlock:^(TSAttachmentStream *attachment) { + [attachment setLazyRestoreFragmentId:lazyRestoreFragment.uniqueId]; + }]; } - (void)updateWithLazyRestoreComplete