From 46a89e89f0f8d2708e4c5a52b4a104390649cd19 Mon Sep 17 00:00:00 2001
From: Matthew Chen <charlesmchen@gmail.com>
Date: Mon, 5 Mar 2018 13:32:13 -0300
Subject: [PATCH] Sketch out OWSBackupStorage.

---
 Pods                                          |   2 +-
 .../src/Storage/OWSBackupStorage.h            |  21 +-
 .../src/Storage/OWSBackupStorage.m            | 192 ++++++------------
 3 files changed, 68 insertions(+), 147 deletions(-)

diff --git a/Pods b/Pods
index aae15b5fb..93e79025c 160000
--- a/Pods
+++ b/Pods
@@ -1 +1 @@
-Subproject commit aae15b5fb1fc718f19b862390b61f540e45a36ee
+Subproject commit 93e79025cf285042cb397f3f4d1e0d52c68b9ecc
diff --git a/SignalServiceKit/src/Storage/OWSBackupStorage.h b/SignalServiceKit/src/Storage/OWSBackupStorage.h
index 85623096c..7b405da13 100644
--- a/SignalServiceKit/src/Storage/OWSBackupStorage.h
+++ b/SignalServiceKit/src/Storage/OWSBackupStorage.h
@@ -10,27 +10,16 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-// void runSyncRegistrationsForStorage(OWSStorage *storage);
-// void runAsyncRegistrationsForStorage(OWSStorage *storage);
-
 @interface OWSBackupStorage : OWSStorage
 
 - (instancetype)init NS_UNAVAILABLE;
 
-//+ (instancetype)sharedManager NS_SWIFT_NAME(shared());
-//
+- (instancetype)initStorage NS_UNAVAILABLE;
+
+- (instancetype)initBackupStorageWithdatabaseDirPath:(NSString *)databaseDirPath
+                                     databaseKeySpec:(NSData *)databaseKeySpec NS_DESIGNATED_INITIALIZER;
+
 - (YapDatabaseConnection *)dbConnection;
-//
-//+ (nullable NSError *)migrateToSharedData;
-//
-//+ (NSString *)databaseFilePath;
-//
-//+ (NSString *)legacyDatabaseFilePath;
-//+ (NSString *)legacyDatabaseFilePath_SHM;
-//+ (NSString *)legacyDatabaseFilePath_WAL;
-//+ (NSString *)sharedDataDatabaseFilePath;
-//+ (NSString *)sharedDataDatabaseFilePath_SHM;
-//+ (NSString *)sharedDataDatabaseFilePath_WAL;
 
 @end
 
diff --git a/SignalServiceKit/src/Storage/OWSBackupStorage.m b/SignalServiceKit/src/Storage/OWSBackupStorage.m
index 8e279baf7..ac777053c 100644
--- a/SignalServiceKit/src/Storage/OWSBackupStorage.m
+++ b/SignalServiceKit/src/Storage/OWSBackupStorage.m
@@ -3,28 +3,11 @@
 //
 
 #import "OWSBackupStorage.h"
-
-//#import "AppContext.h"
-//#import "OWSAnalytics.h"
-//#import "OWSBatchMessageProcessor.h"
-//#import "OWSDisappearingMessagesFinder.h"
-//#import "OWSFailedAttachmentDownloadsJob.h"
-//#import "OWSFailedMessagesJob.h"
-//#import "OWSFileSystem.h"
-//#import "OWSIncomingMessageFinder.h"
-//#import "OWSMessageReceiver.h"
+#import "OWSFileSystem.h"
 #import "OWSStorage+Subclass.h"
 
-//#import "TSDatabaseSecondaryIndexes.h"
-//#import "TSDatabaseView.h"
-
 NS_ASSUME_NONNULL_BEGIN
 
-NSString *const OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory
-    = @"OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory";
-
-#pragma mark -
-
 @interface OWSBackupStorage ()
 
 @property (nonatomic, readonly, nullable) YapDatabaseConnection *dbConnection;
@@ -32,34 +15,32 @@ NSString *const OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory
 @property (atomic) BOOL areAsyncRegistrationsComplete;
 @property (atomic) BOOL areSyncRegistrationsComplete;
 
+@property (nonatomic, readonly) NSString *databaseDirPath;
+@property (nonatomic, readonly) NSData *databaseKeySpec;
+
 @end
 
 #pragma mark -
 
 @implementation OWSBackupStorage
 
-//+ (instancetype)sharedManager
-//{
-//    static OWSBackupStorage *sharedManager = nil;
-//    static dispatch_once_t onceToken;
-//    dispatch_once(&onceToken, ^{
-//        sharedManager = [[self alloc] initStorage];
-//
-//#if TARGET_OS_IPHONE
-//        [OWSBackupStorage protectFiles];
-//#endif
-//    });
-//    return sharedManager;
-//}
-
-- (instancetype)initStorage
+@synthesize databaseKeySpec = _databaseKeySpec;
+
+- (instancetype)initBackupStorageWithdatabaseDirPath:(NSString *)databaseDirPath
+                                     databaseKeySpec:(NSData *)databaseKeySpec
 {
+    OWSAssert(databaseDirPath.length > 0);
+    OWSAssert(databaseKeySpec.length > 0);
+    OWSAssert([OWSFileSystem ensureDirectoryExists:databaseDirPath]);
+
     self = [super initStorage];
 
     if (self) {
-        _dbConnection = self.newDatabaseConnection;
+        [self protectFiles];
 
-        OWSSingletonAssert();
+        _dbConnection = self.newDatabaseConnection;
+        _databaseDirPath = databaseDirPath;
+        _databaseKeySpec = databaseKeySpec;
     }
 
     return self;
@@ -68,7 +49,6 @@ NSString *const OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory
 - (void)resetStorage
 {
     _dbConnection = nil;
-    //    _dbReadWriteConnection = nil;
 
     [super resetStorage];
 }
@@ -113,108 +93,60 @@ NSString *const OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory
                                        }];
 }
 
-+ (void)protectFiles
+- (void)protectFiles
 {
-    // TODO:
-
-    //    DDLogInfo(
-    //        @"%@ Database file size: %@", self.logTag, [OWSFileSystem
-    //        fileSizeOfPath:self.sharedDataDatabaseFilePath]);
-    //    DDLogInfo(
-    //        @"%@ \t SHM file size: %@", self.logTag, [OWSFileSystem
-    //        fileSizeOfPath:self.sharedDataDatabaseFilePath_SHM]);
-    //    DDLogInfo(
-    //        @"%@ \t WAL file size: %@", self.logTag, [OWSFileSystem
-    //        fileSizeOfPath:self.sharedDataDatabaseFilePath_WAL]);
-    //
-    //    // Protect the entire new database directory.
-    //    [OWSFileSystem protectFileOrFolderAtPath:self.sharedDataDatabaseDirPath];
-}
+    DDLogInfo(@"%@ Database file size: %@", self.logTag, [OWSFileSystem fileSizeOfPath:self.databaseFilePath]);
+    DDLogInfo(@"%@ \t SHM file size: %@", self.logTag, [OWSFileSystem fileSizeOfPath:self.databaseFilePath_SHM]);
+    DDLogInfo(@"%@ \t WAL file size: %@", self.logTag, [OWSFileSystem fileSizeOfPath:self.databaseFilePath_WAL]);
 
-//+ (NSString *)legacyDatabaseDirPath
-//{
-//    return [OWSFileSystem appDocumentDirectoryPath];
-//}
-//
-//+ (NSString *)sharedDataDatabaseDirPath
-//{
-//    NSString *databaseDirPath = [[OWSFileSystem appSharedDataDirectoryPath]
-//    stringByAppendingPathComponent:@"database"];
-//
-//    if (![OWSFileSystem ensureDirectoryExists:databaseDirPath]) {
-//        OWSRaiseException(
-//            OWSBackupStorageExceptionName_CouldNotCreateDatabaseDirectory, @"Could not create new database
-//            directory");
-//    }
-//    return databaseDirPath;
-//}
+    // Protect the entire new database directory.
+    [OWSFileSystem protectFileOrFolderAtPath:self.databaseDirPath];
+}
 
 + (NSString *)databaseFilename
 {
     return @"SignalBackup.sqlite";
 }
 
-//+ (NSString *)databaseFilename_SHM
-//{
-//    return [self.databaseFilename stringByAppendingString:@"-shm"];
-//}
-//
-//+ (NSString *)databaseFilename_WAL
-//{
-//    return [self.databaseFilename stringByAppendingString:@"-wal"];
-//}
-//
-//+ (NSString *)legacyDatabaseFilePath
-//{
-//    return [self.legacyDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename];
-//}
-//
-//+ (NSString *)legacyDatabaseFilePath_SHM
-//{
-//    return [self.legacyDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename_SHM];
-//}
-//
-//+ (NSString *)legacyDatabaseFilePath_WAL
-//{
-//    return [self.legacyDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename_WAL];
-//}
-//
-//+ (NSString *)sharedDataDatabaseFilePath
-//{
-//    return [self.sharedDataDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename];
-//}
-//
-//+ (NSString *)sharedDataDatabaseFilePath_SHM
-//{
-//    return [self.sharedDataDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename_SHM];
-//}
-//
-//+ (NSString *)sharedDataDatabaseFilePath_WAL
-//{
-//    return [self.sharedDataDatabaseDirPath stringByAppendingPathComponent:self.databaseFilename_WAL];
-//}
-
-//+ (NSString *)databaseFilePath
-//{
-//    DDLogVerbose(@"%@ databasePath: %@", self.logTag, OWSBackupStorage.sharedDataDatabaseFilePath);
-//
-//    return self.sharedDataDatabaseFilePath;
-//}
-//
-//- (NSString *)databaseFilePath
-//{
-//    return OWSBackupStorage.databaseFilePath;
-//}
-
-//+ (YapDatabaseConnection *)dbReadConnection
-//{
-//    return OWSBackupStorage.sharedManager.dbReadConnection;
-//}
-//
-//+ (YapDatabaseConnection *)dbReadWriteConnection
-//{
-//    return OWSBackupStorage.sharedManager.dbReadWriteConnection;
-//}
+- (NSString *)databaseFilename
+{
+    return OWSBackupStorage.databaseFilename;
+}
+
+- (NSString *)databaseFilename_SHM
+{
+    return [self.databaseFilename stringByAppendingString:@"-shm"];
+}
+
+- (NSString *)databaseFilename_WAL
+{
+    return [self.databaseFilename stringByAppendingString:@"-wal"];
+}
+
+- (NSString *)databaseFilePath
+{
+    return [self.databaseDirPath stringByAppendingPathComponent:self.databaseFilename];
+}
+
+- (NSString *)databaseFilePath_SHM
+{
+    return [self.databaseDirPath stringByAppendingPathComponent:self.databaseFilename_SHM];
+}
+
+- (NSString *)databaseFilePath_WAL
+{
+    return [self.databaseDirPath stringByAppendingPathComponent:self.databaseFilename_WAL];
+}
+
+- (NSData *)databaseKeySpec
+{
+    return self.databaseKeySpec;
+}
+
+- (void)ensureDatabaseKeySpecExists
+{
+    // Do nothing.
+}
 
 @end