From dd54b40bedbf7731cf88016c7a973afb0dedcf32 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Wed, 27 Mar 2019 13:15:26 -0400 Subject: [PATCH] Respond to CR. --- SignalServiceKit/src/Util/OWSFileSystem.m | 29 +++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/SignalServiceKit/src/Util/OWSFileSystem.m b/SignalServiceKit/src/Util/OWSFileSystem.m index 327a04d28..36d4a8f07 100644 --- a/SignalServiceKit/src/Util/OWSFileSystem.m +++ b/SignalServiceKit/src/Util/OWSFileSystem.m @@ -5,6 +5,7 @@ #import "OWSFileSystem.h" #import "OWSError.h" #import "TSConstants.h" +#import NS_ASSUME_NONNULL_BEGIN @@ -373,6 +374,7 @@ void ClearOldTemporaryDirectoriesSync(void) // Ignore the "current" temp directory. NSString *currentTempDirName = OWSTemporaryDirectory().lastPathComponent; + NSDate *thresholdDate = CurrentAppContext().appLaunchTime; NSString *dirPath = NSTemporaryDirectory(); NSError *error; NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:&error]; @@ -388,11 +390,30 @@ void ClearOldTemporaryDirectoriesSync(void) if ([fileName isEqualToString:currentTempDirName]) { continue; } + + NSString *filePath = [dirPath stringByAppendingPathComponent:fileName]; + + // Delete files with either: + // + // a) "ows_temp" name prefix. + // b) modified time before app launch time. if (![fileName hasPrefix:@"ows_temp"]) { - continue; + NSError *error; + NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error]; + if (!attributes || error) { + // This is fine; the file may have been deleted since we found it. + OWSLogError(@"Could not get attributes of file or directory at: %@", filePath); + continue; + } + // Don't delete files which were created in the last N minutes. + NSDate *creationDate = attributes.fileModificationDate; + if ([creationDate isAfterDate:thresholdDate]) { + OWSLogInfo(@"Skipping file due to age: %f", fabs([creationDate timeIntervalSinceNow])); + continue; + } } - NSString *filePath = [dirPath stringByAppendingPathComponent:fileName]; - OWSLogVerbose(@"Clearing old temp directory: %@", filePath); + + OWSLogVerbose(@"Removing temp file or directory: %@", filePath); if (![OWSFileSystem deleteFile:filePath]) { // This can happen if the app launches before the phone is unlocked. // Clean up will occur when app becomes active. @@ -407,7 +428,7 @@ void ClearOldTemporaryDirectories(void) { // We use the lowest priority queue for this, and wait N seconds // to avoid interfering with app startup. - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10.f * NSEC_PER_SEC)), + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.f * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ ClearOldTemporaryDirectoriesSync();