mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
611 lines
26 KiB
Matlab
611 lines
26 KiB
Matlab
7 years ago
|
//
|
||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
7 years ago
|
#import "OWSBackupExportJob.h"
|
||
7 years ago
|
#import "Signal-Swift.h"
|
||
7 years ago
|
#import "zlib.h"
|
||
7 years ago
|
#import <SSZipArchive/SSZipArchive.h>
|
||
7 years ago
|
#import <SignalServiceKit/NSData+Base64.h>
|
||
7 years ago
|
#import <SignalServiceKit/NSDate+OWS.h>
|
||
|
#import <SignalServiceKit/OWSBackgroundTask.h>
|
||
7 years ago
|
#import <SignalServiceKit/OWSBackupStorage.h>
|
||
7 years ago
|
#import <SignalServiceKit/OWSError.h>
|
||
7 years ago
|
#import <SignalServiceKit/OWSFileSystem.h>
|
||
|
#import <SignalServiceKit/TSAttachmentStream.h>
|
||
7 years ago
|
#import <SignalServiceKit/TSMessage.h>
|
||
|
#import <SignalServiceKit/TSThread.h>
|
||
|
#import <SignalServiceKit/Threading.h>
|
||
7 years ago
|
#import <SignalServiceKit/YapDatabaseConnection+OWS.h>
|
||
7 years ago
|
#import <YapDatabase/YapDatabase.h>
|
||
7 years ago
|
|
||
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
7 years ago
|
NSString *const kOWSBackup_ExportDatabaseKeySpec = @"kOWSBackup_ExportDatabaseKeySpec";
|
||
7 years ago
|
|
||
|
@interface OWSAttachmentExport : NSObject
|
||
|
|
||
7 years ago
|
@property (nonatomic, weak) id<OWSBackupJobDelegate> delegate;
|
||
|
@property (nonatomic) NSString *jobTempDirPath;
|
||
7 years ago
|
@property (nonatomic) NSString *attachmentId;
|
||
|
@property (nonatomic) NSString *attachmentFilePath;
|
||
|
@property (nonatomic, nullable) NSString *tempFilePath;
|
||
|
@property (nonatomic, nullable) NSString *relativeFilePath;
|
||
|
|
||
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
|
@implementation OWSAttachmentExport
|
||
|
|
||
|
- (void)dealloc
|
||
|
{
|
||
|
// Surface memory leaks by logging the deallocation.
|
||
|
DDLogVerbose(@"Dealloc: %@", self.class);
|
||
|
|
||
|
// Delete temporary file ASAP.
|
||
|
if (self.tempFilePath) {
|
||
|
[OWSFileSystem deleteFileIfExists:self.tempFilePath];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// On success, tempFilePath will be non-nil.
|
||
|
- (void)prepareForUpload
|
||
|
{
|
||
7 years ago
|
OWSAssert(self.jobTempDirPath.length > 0);
|
||
7 years ago
|
OWSAssert(self.attachmentId.length > 0);
|
||
|
OWSAssert(self.attachmentFilePath.length > 0);
|
||
|
|
||
|
NSString *attachmentsDirPath = [TSAttachmentStream attachmentsFolder];
|
||
|
if (![self.attachmentFilePath hasPrefix:attachmentsDirPath]) {
|
||
|
DDLogError(@"%@ attachment has unexpected path.", self.logTag);
|
||
|
OWSFail(@"%@ attachment has unexpected path: %@", self.logTag, self.attachmentFilePath);
|
||
|
return;
|
||
|
}
|
||
|
NSString *relativeFilePath = [self.attachmentFilePath substringFromIndex:attachmentsDirPath.length];
|
||
|
NSString *pathSeparator = @"/";
|
||
|
if ([relativeFilePath hasPrefix:pathSeparator]) {
|
||
|
relativeFilePath = [relativeFilePath substringFromIndex:pathSeparator.length];
|
||
|
}
|
||
|
self.relativeFilePath = relativeFilePath;
|
||
|
|
||
7 years ago
|
NSString *_Nullable tempFilePath = [OWSBackupExportJob encryptFileAsTempFile:self.attachmentFilePath
|
||
|
jobTempDirPath:self.jobTempDirPath
|
||
|
delegate:self.delegate];
|
||
7 years ago
|
if (!tempFilePath) {
|
||
|
DDLogError(@"%@ attachment could not be encrypted.", self.logTag);
|
||
|
OWSFail(@"%@ attachment could not be encrypted: %@", self.logTag, self.attachmentFilePath);
|
||
|
return;
|
||
|
}
|
||
7 years ago
|
self.tempFilePath = tempFilePath;
|
||
7 years ago
|
}
|
||
|
|
||
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
7 years ago
|
@interface OWSBackupExportJob () <SSZipArchiveDelegate>
|
||
7 years ago
|
|
||
7 years ago
|
@property (nonatomic, nullable) OWSBackupStorage *backupStorage;
|
||
7 years ago
|
|
||
7 years ago
|
@property (nonatomic, nullable) OWSBackgroundTask *backgroundTask;
|
||
7 years ago
|
|
||
7 years ago
|
@property (nonatomic) NSMutableArray<NSString *> *databaseFilePaths;
|
||
|
// A map of "record name"-to-"file name".
|
||
|
@property (nonatomic) NSMutableDictionary<NSString *, NSString *> *databaseRecordMap;
|
||
|
|
||
7 years ago
|
// A map of "attachment id"-to-"local file path".
|
||
|
@property (nonatomic) NSMutableDictionary<NSString *, NSString *> *attachmentFilePathMap;
|
||
7 years ago
|
// A map of "record name"-to-"file relative path".
|
||
|
@property (nonatomic) NSMutableDictionary<NSString *, NSString *> *attachmentRecordMap;
|
||
|
|
||
7 years ago
|
@property (nonatomic, nullable) NSString *manifestFilePath;
|
||
|
@property (nonatomic, nullable) NSString *manifestRecordName;
|
||
|
|
||
7 years ago
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
7 years ago
|
@implementation OWSBackupExportJob
|
||
7 years ago
|
|
||
7 years ago
|
- (void)startAsync
|
||
|
{
|
||
7 years ago
|
OWSAssertIsOnMainThread();
|
||
|
|
||
7 years ago
|
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
7 years ago
|
self.backgroundTask = [OWSBackgroundTask backgroundTaskWithLabelStr:__PRETTY_FUNCTION__];
|
||
|
|
||
7 years ago
|
[self updateProgressWithDescription:nil progress:nil];
|
||
|
|
||
7 years ago
|
__weak OWSBackupExportJob *weakSelf = self;
|
||
7 years ago
|
[OWSBackupAPI checkCloudKitAccessWithCompletion:^(BOOL hasAccess) {
|
||
|
if (hasAccess) {
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
[weakSelf start];
|
||
|
});
|
||
|
}
|
||
|
}];
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
- (void)start
|
||
|
{
|
||
7 years ago
|
[self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_CONFIGURATION",
|
||
|
@"Indicates that the backup export is being configured.")
|
||
|
progress:nil];
|
||
|
|
||
7 years ago
|
__weak OWSBackupExportJob *weakSelf = self;
|
||
7 years ago
|
[self configureExport:^(BOOL success) {
|
||
|
if (!success) {
|
||
|
[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;
|
||
|
}
|
||
7 years ago
|
[self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_EXPORT",
|
||
|
@"Indicates that the backup export data is being exported.")
|
||
|
progress:nil];
|
||
7 years ago
|
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;
|
||
|
}
|
||
7 years ago
|
[self saveToCloud:^(NSError *_Nullable saveError) {
|
||
|
if (saveError) {
|
||
|
[weakSelf failWithError:saveError];
|
||
|
return;
|
||
7 years ago
|
}
|
||
7 years ago
|
[self cleanUpCloud:^(NSError *_Nullable cleanUpError) {
|
||
|
if (cleanUpError) {
|
||
|
[weakSelf failWithError:cleanUpError];
|
||
|
return;
|
||
|
}
|
||
|
[weakSelf succeed];
|
||
|
}];
|
||
7 years ago
|
}];
|
||
7 years ago
|
}];
|
||
|
}
|
||
|
|
||
7 years ago
|
- (void)configureExport:(OWSBackupJobBoolCompletion)completion
|
||
7 years ago
|
{
|
||
7 years ago
|
OWSAssert(completion);
|
||
|
|
||
7 years ago
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
7 years ago
|
if (![self ensureJobTempDir]) {
|
||
|
OWSProdLogAndFail(@"%@ Could not create jobTempDirPath.", self.logTag);
|
||
|
return completion(NO);
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
if (![OWSBackupJob generateRandomDatabaseKeySpecWithKeychainKey:kOWSBackup_ExportDatabaseKeySpec]) {
|
||
|
OWSProdLogAndFail(@"%@ Could not generate database key spec for export.", self.logTag);
|
||
7 years ago
|
return completion(NO);
|
||
|
}
|
||
7 years ago
|
|
||
|
NSString *exportDatabaseDirPath = [self.jobTempDirPath stringByAppendingPathComponent:@"Database"];
|
||
|
|
||
7 years ago
|
if (![OWSFileSystem ensureDirectoryExists:exportDatabaseDirPath]) {
|
||
7 years ago
|
OWSProdLogAndFail(@"%@ Could not create exportDatabaseDirPath.", self.logTag);
|
||
7 years ago
|
return completion(NO);
|
||
|
}
|
||
|
BackupStorageKeySpecBlock keySpecBlock = ^{
|
||
7 years ago
|
NSData *_Nullable databaseKeySpec =
|
||
|
[OWSBackupJob loadDatabaseKeySpecWithKeychainKey:kOWSBackup_ExportDatabaseKeySpec];
|
||
|
if (!databaseKeySpec) {
|
||
|
OWSProdLogAndFail(@"%@ Could not load database keyspec for export.", self.logTag);
|
||
|
}
|
||
|
return databaseKeySpec;
|
||
7 years ago
|
};
|
||
|
self.backupStorage =
|
||
|
[[OWSBackupStorage alloc] initBackupStorageWithDatabaseDirPath:exportDatabaseDirPath keySpecBlock:keySpecBlock];
|
||
|
if (!self.backupStorage) {
|
||
7 years ago
|
OWSProdLogAndFail(@"%@ Could not create backupStorage.", self.logTag);
|
||
7 years ago
|
return completion(NO);
|
||
|
}
|
||
|
|
||
|
// TODO: Do we really need to run these registrations on the main thread?
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
[self.backupStorage runSyncRegistrations];
|
||
|
[self.backupStorage runAsyncRegistrationsWithCompletion:^{
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
completion(YES);
|
||
|
});
|
||
|
}];
|
||
|
});
|
||
|
}
|
||
|
|
||
|
- (BOOL)exportDatabase
|
||
|
{
|
||
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
7 years ago
|
YapDatabaseConnection *_Nullable tempDBConnection = self.backupStorage.newDatabaseConnection;
|
||
|
if (!tempDBConnection) {
|
||
|
OWSProdLogAndFail(@"%@ Could not create tempDBConnection.", self.logTag);
|
||
|
return NO;
|
||
|
}
|
||
|
YapDatabaseConnection *_Nullable primaryDBConnection = self.primaryStorage.newDatabaseConnection;
|
||
|
if (!primaryDBConnection) {
|
||
|
OWSProdLogAndFail(@"%@ Could not create primaryDBConnection.", self.logTag);
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
7 years ago
|
__block unsigned long long copiedThreads = 0;
|
||
|
__block unsigned long long copiedInteractions = 0;
|
||
|
__block unsigned long long copiedEntities = 0;
|
||
7 years ago
|
__block unsigned long long copiedAttachments = 0;
|
||
|
|
||
7 years ago
|
self.attachmentFilePathMap = [NSMutableDictionary new];
|
||
7 years ago
|
|
||
7 years ago
|
[primaryDBConnection readWithBlock:^(YapDatabaseReadTransaction *srcTransaction) {
|
||
|
[tempDBConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *dstTransaction) {
|
||
7 years ago
|
// Copy threads.
|
||
|
[srcTransaction
|
||
|
enumerateKeysAndObjectsInCollection:[TSThread collection]
|
||
|
usingBlock:^(NSString *key, id object, BOOL *stop) {
|
||
|
if (self.isComplete) {
|
||
|
*stop = YES;
|
||
|
return;
|
||
|
}
|
||
|
if (![object isKindOfClass:[TSThread class]]) {
|
||
7 years ago
|
OWSProdLogAndFail(
|
||
|
@"%@ unexpected class: %@", self.logTag, [object class]);
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
TSThread *thread = object;
|
||
|
[thread saveWithTransaction:dstTransaction];
|
||
|
copiedThreads++;
|
||
|
copiedEntities++;
|
||
|
}];
|
||
|
|
||
|
// Copy interactions.
|
||
|
[srcTransaction
|
||
|
enumerateKeysAndObjectsInCollection:[TSInteraction collection]
|
||
|
usingBlock:^(NSString *key, id object, BOOL *stop) {
|
||
|
if (self.isComplete) {
|
||
|
*stop = YES;
|
||
|
return;
|
||
|
}
|
||
|
if (![object isKindOfClass:[TSInteraction class]]) {
|
||
7 years ago
|
OWSProdLogAndFail(
|
||
|
@"%@ unexpected class: %@", self.logTag, [object class]);
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
// Ignore disappearing messages.
|
||
|
if ([object isKindOfClass:[TSMessage class]]) {
|
||
|
TSMessage *message = object;
|
||
|
if (message.isExpiringMessage) {
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
TSInteraction *interaction = object;
|
||
|
// Ignore dynamic interactions.
|
||
|
if (interaction.isDynamicInteraction) {
|
||
|
return;
|
||
|
}
|
||
|
[interaction saveWithTransaction:dstTransaction];
|
||
|
copiedInteractions++;
|
||
|
copiedEntities++;
|
||
|
}];
|
||
|
|
||
7 years ago
|
// Copy attachments.
|
||
|
[srcTransaction
|
||
|
enumerateKeysAndObjectsInCollection:[TSAttachmentStream collection]
|
||
|
usingBlock:^(NSString *key, id object, BOOL *stop) {
|
||
|
if (self.isComplete) {
|
||
|
*stop = YES;
|
||
|
return;
|
||
|
}
|
||
|
if (![object isKindOfClass:[TSAttachment class]]) {
|
||
|
OWSProdLogAndFail(
|
||
|
@"%@ unexpected class: %@", self.logTag, [object class]);
|
||
|
return;
|
||
|
}
|
||
|
if ([object isKindOfClass:[TSAttachmentStream class]]) {
|
||
|
TSAttachmentStream *attachmentStream = object;
|
||
|
NSString *_Nullable filePath = attachmentStream.filePath;
|
||
|
if (filePath) {
|
||
7 years ago
|
OWSAssert(attachmentStream.uniqueId.length > 0);
|
||
|
self.attachmentFilePathMap[attachmentStream.uniqueId] = filePath;
|
||
7 years ago
|
}
|
||
|
}
|
||
|
TSAttachment *attachment = object;
|
||
|
[attachment saveWithTransaction:dstTransaction];
|
||
|
copiedAttachments++;
|
||
|
copiedEntities++;
|
||
|
}];
|
||
7 years ago
|
}];
|
||
|
}];
|
||
|
|
||
|
// TODO: Should we do a database checkpoint?
|
||
|
|
||
|
DDLogInfo(@"%@ copiedThreads: %llu", self.logTag, copiedThreads);
|
||
|
DDLogInfo(@"%@ copiedMessages: %llu", self.logTag, copiedInteractions);
|
||
|
DDLogInfo(@"%@ copiedEntities: %llu", self.logTag, copiedEntities);
|
||
7 years ago
|
DDLogInfo(@"%@ copiedAttachments: %llu", self.logTag, copiedAttachments);
|
||
7 years ago
|
|
||
|
[self.backupStorage logFileSizes];
|
||
|
|
||
7 years ago
|
// Capture the list of files to save.
|
||
|
self.databaseFilePaths = [@[
|
||
|
self.backupStorage.databaseFilePath,
|
||
|
self.backupStorage.databaseFilePath_WAL,
|
||
|
self.backupStorage.databaseFilePath_SHM,
|
||
|
] mutableCopy];
|
||
|
|
||
|
// Close the database.
|
||
7 years ago
|
tempDBConnection = nil;
|
||
7 years ago
|
self.backupStorage = nil;
|
||
|
|
||
|
return YES;
|
||
|
}
|
||
|
|
||
7 years ago
|
- (void)saveToCloud:(OWSBackupJobCompletion)completion
|
||
7 years ago
|
{
|
||
7 years ago
|
OWSAssert(completion);
|
||
|
|
||
7 years ago
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
|
self.databaseRecordMap = [NSMutableDictionary new];
|
||
7 years ago
|
self.attachmentRecordMap = [NSMutableDictionary new];
|
||
7 years ago
|
|
||
|
[self saveNextFileToCloud:completion];
|
||
|
}
|
||
|
|
||
7 years ago
|
- (void)saveNextFileToCloud:(OWSBackupJobCompletion)completion
|
||
7 years ago
|
{
|
||
7 years ago
|
OWSAssert(completion);
|
||
|
|
||
7 years ago
|
if (self.isComplete) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
CGFloat progress
|
||
|
= (self.databaseRecordMap.count / (CGFloat)(self.databaseRecordMap.count + self.databaseFilePaths.count));
|
||
|
[self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_UPLOAD",
|
||
|
@"Indicates that the backup export data is being uploaded.")
|
||
|
progress:@(progress)];
|
||
|
|
||
7 years ago
|
__weak OWSBackupExportJob *weakSelf = self;
|
||
7 years ago
|
|
||
|
if (self.databaseFilePaths.count > 0) {
|
||
|
NSString *filePath = self.databaseFilePaths.lastObject;
|
||
|
[self.databaseFilePaths removeLastObject];
|
||
|
// Database files are encrypted and can be safely stored unencrypted in the cloud.
|
||
|
// TODO: Security review.
|
||
7 years ago
|
[OWSBackupAPI saveEphemeralDatabaseFileToCloudWithFileUrl:[NSURL fileURLWithPath:filePath]
|
||
7 years ago
|
success:^(NSString *recordName) {
|
||
7 years ago
|
// Ensure that we continue to work off the main thread.
|
||
7 years ago
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
7 years ago
|
OWSBackupExportJob *strongSelf = weakSelf;
|
||
7 years ago
|
if (!strongSelf) {
|
||
|
return;
|
||
|
}
|
||
|
strongSelf.databaseRecordMap[recordName] = [filePath lastPathComponent];
|
||
|
[strongSelf saveNextFileToCloud:completion];
|
||
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
|
// Database files are critical so any error uploading them is unrecoverable.
|
||
|
completion(error);
|
||
|
}];
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (self.attachmentFilePathMap.count > 0) {
|
||
|
NSString *attachmentId = self.attachmentFilePathMap.allKeys.lastObject;
|
||
|
NSString *attachmentFilePath = self.attachmentFilePathMap[attachmentId];
|
||
|
[self.attachmentFilePathMap removeObjectForKey:attachmentId];
|
||
|
|
||
|
// OWSAttachmentExport is used to lazily write an encrypted copy of the
|
||
|
// attachment to disk.
|
||
|
OWSAttachmentExport *attachmentExport = [OWSAttachmentExport new];
|
||
7 years ago
|
attachmentExport.delegate = self.delegate;
|
||
7 years ago
|
attachmentExport.jobTempDirPath = self.jobTempDirPath;
|
||
7 years ago
|
attachmentExport.attachmentId = attachmentId;
|
||
|
attachmentExport.attachmentFilePath = attachmentFilePath;
|
||
|
|
||
|
[OWSBackupAPI savePersistentFileOnceToCloudWithFileId:attachmentId
|
||
|
fileUrlBlock:^{
|
||
|
[attachmentExport prepareForUpload];
|
||
7 years ago
|
if (attachmentExport.tempFilePath.length < 1) {
|
||
|
DDLogError(@"%@ attachment export missing temp file path", self.logTag);
|
||
|
return (NSURL *)nil;
|
||
|
}
|
||
|
if (attachmentExport.relativeFilePath.length < 1) {
|
||
|
DDLogError(@"%@ attachment export missing relative file path", self.logTag);
|
||
7 years ago
|
return (NSURL *)nil;
|
||
|
}
|
||
|
return [NSURL fileURLWithPath:attachmentExport.tempFilePath];
|
||
|
}
|
||
7 years ago
|
success:^(NSString *recordName) {
|
||
7 years ago
|
// Ensure that we continue to work off the main thread.
|
||
7 years ago
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
7 years ago
|
OWSBackupExportJob *strongSelf = weakSelf;
|
||
7 years ago
|
if (!strongSelf) {
|
||
|
return;
|
||
|
}
|
||
7 years ago
|
strongSelf.attachmentRecordMap[recordName] = attachmentExport.relativeFilePath;
|
||
7 years ago
|
[strongSelf saveNextFileToCloud:completion];
|
||
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
7 years ago
|
// Ensure that we continue to work off the main thread.
|
||
7 years ago
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
// Attachment files are non-critical so any error uploading them is recoverable.
|
||
|
[weakSelf saveNextFileToCloud:completion];
|
||
|
});
|
||
|
}];
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (!self.manifestFilePath) {
|
||
|
if (![self writeManifestFile]) {
|
||
|
completion(OWSErrorWithCodeDescription(OWSErrorCodeExportBackupFailed,
|
||
|
NSLocalizedString(@"BACKUP_EXPORT_ERROR_COULD_NOT_EXPORT",
|
||
|
@"Error indicating the a backup export could not export the user's data.")));
|
||
|
return;
|
||
|
}
|
||
|
OWSAssert(self.manifestFilePath);
|
||
|
|
||
7 years ago
|
[OWSBackupAPI upsertManifestFileToCloudWithFileUrl:[NSURL fileURLWithPath:self.manifestFilePath]
|
||
7 years ago
|
success:^(NSString *recordName) {
|
||
7 years ago
|
// Ensure that we continue to work off the main thread.
|
||
7 years ago
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
7 years ago
|
OWSBackupExportJob *strongSelf = weakSelf;
|
||
7 years ago
|
if (!strongSelf) {
|
||
|
return;
|
||
|
}
|
||
|
strongSelf.manifestRecordName = recordName;
|
||
|
[strongSelf saveNextFileToCloud:completion];
|
||
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
|
// The manifest file is critical so any error uploading them is unrecoverable.
|
||
|
completion(error);
|
||
|
}];
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// All files have been saved to the cloud.
|
||
|
completion(nil);
|
||
|
}
|
||
|
|
||
|
- (BOOL)writeManifestFile
|
||
|
{
|
||
|
OWSAssert(self.databaseRecordMap.count > 0);
|
||
7 years ago
|
OWSAssert(self.attachmentRecordMap);
|
||
7 years ago
|
OWSAssert(self.jobTempDirPath.length > 0);
|
||
|
|
||
|
NSData *_Nullable databaseKeySpec =
|
||
|
[OWSBackupJob loadDatabaseKeySpecWithKeychainKey:kOWSBackup_ExportDatabaseKeySpec];
|
||
|
if (databaseKeySpec.length < 1) {
|
||
|
OWSProdLogAndFail(@"%@ Could not load database keyspec for export.", self.logTag);
|
||
|
return nil;
|
||
|
}
|
||
7 years ago
|
|
||
|
NSDictionary *json = @{
|
||
7 years ago
|
kOWSBackup_ManifestKey_DatabaseFiles : self.databaseRecordMap,
|
||
|
kOWSBackup_ManifestKey_AttachmentFiles : self.attachmentRecordMap,
|
||
7 years ago
|
// JSON doesn't support byte arrays.
|
||
7 years ago
|
kOWSBackup_ManifestKey_DatabaseKeySpec : databaseKeySpec.base64EncodedString,
|
||
7 years ago
|
};
|
||
|
NSError *error;
|
||
|
NSData *_Nullable jsonData =
|
||
|
[NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:&error];
|
||
|
if (!jsonData || error) {
|
||
7 years ago
|
OWSProdLogAndFail(@"%@ error encoding manifest file: %@", self.logTag, error);
|
||
7 years ago
|
return NO;
|
||
|
}
|
||
7 years ago
|
self.manifestFilePath =
|
||
|
[OWSBackupJob encryptDataAsTempFile:jsonData jobTempDirPath:self.jobTempDirPath delegate:self.delegate];
|
||
|
return self.manifestFilePath != nil;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
- (void)cleanUpCloud:(OWSBackupJobCompletion)completion
|
||
7 years ago
|
{
|
||
|
OWSAssert(completion);
|
||
|
|
||
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
7 years ago
|
[self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_CLEAN_UP",
|
||
|
@"Indicates that the cloud is being cleaned up.")
|
||
|
progress:nil];
|
||
|
|
||
7 years ago
|
// Now that our backup export has successfully completed,
|
||
|
// we try to clean up the cloud. We can safely delete any
|
||
|
// records not involved in this backup export.
|
||
|
NSMutableSet<NSString *> *activeRecordNames = [NSMutableSet new];
|
||
|
|
||
|
OWSAssert(self.databaseRecordMap.count > 0);
|
||
|
[activeRecordNames addObjectsFromArray:self.databaseRecordMap.allKeys];
|
||
|
|
||
|
OWSAssert(self.attachmentRecordMap);
|
||
|
[activeRecordNames addObjectsFromArray:self.attachmentRecordMap.allKeys];
|
||
|
|
||
|
OWSAssert(self.manifestRecordName.length > 0);
|
||
|
[activeRecordNames addObject:self.manifestRecordName];
|
||
|
|
||
7 years ago
|
__weak OWSBackupExportJob *weakSelf = self;
|
||
7 years ago
|
[OWSBackupAPI fetchAllRecordNamesWithSuccess:^(NSArray<NSString *> *recordNames) {
|
||
|
// Ensure that we continue to work off the main thread.
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
NSMutableSet<NSString *> *obsoleteRecordNames = [NSMutableSet new];
|
||
|
[obsoleteRecordNames addObjectsFromArray:recordNames];
|
||
|
[obsoleteRecordNames minusSet:activeRecordNames];
|
||
|
|
||
|
DDLogVerbose(@"%@ recordNames: %zd - activeRecordNames: %zd = obsoleteRecordNames: %zd",
|
||
|
self.logTag,
|
||
|
recordNames.count,
|
||
|
activeRecordNames.count,
|
||
|
obsoleteRecordNames.count);
|
||
|
|
||
7 years ago
|
[weakSelf deleteRecordsFromCloud:[obsoleteRecordNames.allObjects mutableCopy]
|
||
|
deletedCount:0
|
||
|
completion:completion];
|
||
7 years ago
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
|
// Ensure that we continue to work off the main thread.
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
// Cloud cleanup is non-critical so any error is recoverable.
|
||
|
completion(nil);
|
||
|
});
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
- (void)deleteRecordsFromCloud:(NSMutableArray<NSString *> *)obsoleteRecordNames
|
||
7 years ago
|
deletedCount:(NSUInteger)deletedCount
|
||
7 years ago
|
completion:(OWSBackupJobCompletion)completion
|
||
7 years ago
|
{
|
||
|
OWSAssert(obsoleteRecordNames);
|
||
|
OWSAssert(completion);
|
||
|
|
||
|
DDLogVerbose(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
|
||
|
|
||
|
if (obsoleteRecordNames.count < 1) {
|
||
|
// No more records to delete; cleanup is complete.
|
||
|
completion(nil);
|
||
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
CGFloat progress = (obsoleteRecordNames.count / (CGFloat)(obsoleteRecordNames.count + deletedCount));
|
||
|
[self updateProgressWithDescription:NSLocalizedString(@"BACKUP_EXPORT_PHASE_CLEAN_UP",
|
||
|
@"Indicates that the cloud is being cleaned up.")
|
||
|
progress:@(progress)];
|
||
|
|
||
7 years ago
|
NSString *recordName = obsoleteRecordNames.lastObject;
|
||
|
[obsoleteRecordNames removeLastObject];
|
||
|
|
||
7 years ago
|
__weak OWSBackupExportJob *weakSelf = self;
|
||
7 years ago
|
[OWSBackupAPI deleteRecordFromCloudWithRecordName:recordName
|
||
|
success:^{
|
||
|
// Ensure that we continue to work off the main thread.
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
7 years ago
|
[weakSelf deleteRecordsFromCloud:obsoleteRecordNames
|
||
|
deletedCount:deletedCount + 1
|
||
|
completion:completion];
|
||
7 years ago
|
});
|
||
|
}
|
||
|
failure:^(NSError *error) {
|
||
|
// Ensure that we continue to work off the main thread.
|
||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||
|
// Cloud cleanup is non-critical so any error is recoverable.
|
||
7 years ago
|
[weakSelf deleteRecordsFromCloud:obsoleteRecordNames
|
||
|
deletedCount:deletedCount + 1
|
||
|
completion:completion];
|
||
7 years ago
|
});
|
||
|
}];
|
||
|
}
|
||
|
|
||
7 years ago
|
@end
|
||
|
|
||
|
NS_ASSUME_NONNULL_END
|