From b4359b33dd29d1d0196cf59c6452bf7e1b415132 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 14 Feb 2018 20:35:32 -0800 Subject: [PATCH 1/2] Fix "lose messages received while in background" A moved legacy DB has the NSFileProtectionClassComplete, meaning it's never accessible while the device is locked. // FREEBIE --- SignalServiceKit/src/Util/OWSFileSystem.h | 2 + SignalServiceKit/src/Util/OWSFileSystem.m | 56 +++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/SignalServiceKit/src/Util/OWSFileSystem.h b/SignalServiceKit/src/Util/OWSFileSystem.h index d81d94fb3..db6643c11 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.h +++ b/SignalServiceKit/src/Util/OWSFileSystem.h @@ -9,6 +9,7 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)init NS_UNAVAILABLE; + (BOOL)protectFileOrFolderAtPath:(NSString *)path; ++ (BOOL)protectRecursiveContentsAtPath:(NSString *)path; + (NSString *)appDocumentDirectoryPath; @@ -36,6 +37,7 @@ NS_ASSUME_NONNULL_BEGIN + (nullable NSString *)writeDataToTemporaryFile:(NSData *)data fileExtension:(NSString *_Nullable)fileExtension; + (nullable NSNumber *)fileSizeOfPath:(NSString *)filePath; ++ (void)logAttributesOfItemAtPathRecursively:(NSString *)path; @end diff --git a/SignalServiceKit/src/Util/OWSFileSystem.m b/SignalServiceKit/src/Util/OWSFileSystem.m index bdb49465b..8b9eccb65 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.m +++ b/SignalServiceKit/src/Util/OWSFileSystem.m @@ -9,8 +9,34 @@ NS_ASSUME_NONNULL_BEGIN @implementation OWSFileSystem ++ (BOOL)protectRecursiveContentsAtPath:(NSString *)path +{ + BOOL isDirectory; + if (![NSFileManager.defaultManager fileExistsAtPath:path isDirectory:&isDirectory]) { + return NO; + } + + if (!isDirectory) { + [self protectFileOrFolderAtPath:path]; + } + NSString *dirPath = path; + + BOOL success = YES; + NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirPath]; + + for (NSString *relativePath in directoryEnumerator) { + NSString *filePath = [dirPath stringByAppendingPathComponent:relativePath]; + DDLogDebug(@"%@ path: %@ had attributes: %@", self.logTag, filePath, directoryEnumerator.fileAttributes); + + success = success && [self protectFileOrFolderAtPath:filePath]; + } + + return success; +} + + (BOOL)protectFileOrFolderAtPath:(NSString *)path { + DDLogVerbose(@"%@ protecting file at path: %@", self.logTag, path); if (![NSFileManager.defaultManager fileExistsAtPath:path]) { return NO; } @@ -32,6 +58,33 @@ NS_ASSUME_NONNULL_BEGIN return YES; } ++ (void)logAttributesOfItemAtPath:(NSString *)path +{ + NSDictionary *_Nullable attributes = [self attributesOfItemAtPath:path]; + DDLogDebug(@"%@ path: %@ has attributes: %@", self.logTag, path, attributes); +} + ++ (nullable NSDictionary *)attributesOfItemAtPath:(NSString *)path +{ + return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil]; +} + ++ (void)logAttributesOfItemAtPathRecursively:(NSString *)path +{ + BOOL isDirectory; + BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]; + OWSAssert(exists); + + if (isDirectory) { + NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:(NSString *)path]; + for (NSString *path in directoryEnumerator) { + DDLogDebug(@"%@ path: %@ has attributes: %@", self.logTag, path, directoryEnumerator.fileAttributes); + } + } else { + [self logAttributesOfItemAtPath:path]; + } +} + + (NSString *)appDocumentDirectoryPath { NSFileManager *fileManager = [NSFileManager defaultManager]; @@ -93,6 +146,9 @@ NS_ASSUME_NONNULL_BEGIN oldFilePath, newFilePath, fabs([startDate timeIntervalSinceNow])); + + // Ensure all files moved have the proper data protection class. + [self protectRecursiveContentsAtPath:newFilePath]; } + (BOOL)ensureDirectoryExists:(NSString *)dirPath From da15f245cfb09a29f6c9263d2f56558e00761d47 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Thu, 15 Feb 2018 06:20:53 -0800 Subject: [PATCH 2/2] CR: fix early return, assert on error inline functions which were only used once // FREEBIE --- SignalServiceKit/src/Util/OWSFileSystem.m | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/SignalServiceKit/src/Util/OWSFileSystem.m b/SignalServiceKit/src/Util/OWSFileSystem.m index 8b9eccb65..aaba7231b 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.m +++ b/SignalServiceKit/src/Util/OWSFileSystem.m @@ -17,7 +17,7 @@ NS_ASSUME_NONNULL_BEGIN } if (!isDirectory) { - [self protectFileOrFolderAtPath:path]; + return [self protectFileOrFolderAtPath:path]; } NSString *dirPath = path; @@ -58,17 +58,6 @@ NS_ASSUME_NONNULL_BEGIN return YES; } -+ (void)logAttributesOfItemAtPath:(NSString *)path -{ - NSDictionary *_Nullable attributes = [self attributesOfItemAtPath:path]; - DDLogDebug(@"%@ path: %@ has attributes: %@", self.logTag, path, attributes); -} - -+ (nullable NSDictionary *)attributesOfItemAtPath:(NSString *)path -{ - return [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil]; -} - + (void)logAttributesOfItemAtPathRecursively:(NSString *)path { BOOL isDirectory; @@ -81,7 +70,12 @@ NS_ASSUME_NONNULL_BEGIN DDLogDebug(@"%@ path: %@ has attributes: %@", self.logTag, path, directoryEnumerator.fileAttributes); } } else { - [self logAttributesOfItemAtPath:path]; + NSError *error; + NSDictionary *_Nullable attributes = + [[NSFileManager defaultManager] attributesOfItemAtPath:path error:error]; + OWSAssert(!error); + + DDLogDebug(@"%@ path: %@ has attributes: %@", self.logTag, path, attributes); } }