From cb4b44b8f41f368ee68cdc1456470e3f1f6e65c8 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Thu, 4 Jan 2018 18:17:53 -0500 Subject: [PATCH] Lock databases during backup export. --- Signal/src/util/OWSBackup.m | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/Signal/src/util/OWSBackup.m b/Signal/src/util/OWSBackup.m index 640f21bf9..e011e5cbd 100644 --- a/Signal/src/util/OWSBackup.m +++ b/Signal/src/util/OWSBackup.m @@ -188,16 +188,22 @@ NS_ASSUME_NONNULL_BEGIN backupDirPath:backupDirPath]) { return; } - if (![self copyDirectory:OWSFileSystem.appDocumentDirectoryPath - dstDirName:@"appDocumentDirectoryPath" - backupDirPath:backupDirPath]) { - return; - } - if (![self copyDirectory:OWSFileSystem.appSharedDataDirectoryPath - dstDirName:@"appSharedDataDirectoryPath" - backupDirPath:backupDirPath]) { - return; - } + // Use a read/write transaction to acquire a file lock on the database files. + // + // TODO: If we use multiple database files, lock them too. + [TSStorageManager.sharedManager.newDatabaseConnection + readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { + if (![self copyDirectory:OWSFileSystem.appDocumentDirectoryPath + dstDirName:@"appDocumentDirectoryPath" + backupDirPath:backupDirPath]) { + return; + } + if (![self copyDirectory:OWSFileSystem.appSharedDataDirectoryPath + dstDirName:@"appSharedDataDirectoryPath" + backupDirPath:backupDirPath]) { + return; + } + }]; if (![self zipDirectory:backupDirPath dstFilePath:backupZipPath]) { return; }