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.
738 lines
30 KiB
Matlab
738 lines
30 KiB
Matlab
7 years ago
|
//
|
||
6 years ago
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
||
7 years ago
|
//
|
||
|
|
||
|
#import "OWSOrphanDataCleaner.h"
|
||
7 years ago
|
#import "DateUtil.h"
|
||
5 years ago
|
#import <SignalCoreKit/NSDate+OWS.h>
|
||
5 years ago
|
#import <SignalUtilitiesKit/OWSProfileManager.h>
|
||
4 years ago
|
#import <SessionMessagingKit/OWSUserProfile.h>
|
||
4 years ago
|
#import <SessionMessagingKit/AppReadiness.h>
|
||
5 years ago
|
#import <SignalUtilitiesKit/AppVersion.h>
|
||
4 years ago
|
#import <SessionUtilitiesKit/SessionUtilitiesKit.h>
|
||
4 years ago
|
#import <SessionUtilitiesKit/OWSFileSystem.h>
|
||
4 years ago
|
#import <SessionMessagingKit/OWSPrimaryStorage.h>
|
||
4 years ago
|
#import <SessionMessagingKit/TSAttachmentStream.h>
|
||
4 years ago
|
#import <SessionMessagingKit/TSInteraction.h>
|
||
|
#import <SessionMessagingKit/TSMessage.h>
|
||
|
#import <SessionMessagingKit/TSQuotedMessage.h>
|
||
4 years ago
|
#import <SessionMessagingKit/TSThread.h>
|
||
|
#import <SessionMessagingKit/YapDatabaseTransaction+OWS.h>
|
||
5 years ago
|
#import <SignalUtilitiesKit/SignalUtilitiesKit-Swift.h>
|
||
7 years ago
|
#import <YapDatabase/YapDatabase.h>
|
||
|
|
||
|
NS_ASSUME_NONNULL_BEGIN
|
||
|
|
||
7 years ago
|
// LOG_ALL_FILE_PATHS can be used to determine if there are other kinds of files
|
||
|
// that we're not cleaning up.
|
||
|
//#define LOG_ALL_FILE_PATHS
|
||
|
|
||
7 years ago
|
#define ENABLE_ORPHAN_DATA_CLEANER
|
||
7 years ago
|
|
||
7 years ago
|
NSString *const OWSOrphanDataCleaner_Collection = @"OWSOrphanDataCleaner_Collection";
|
||
|
NSString *const OWSOrphanDataCleaner_LastCleaningVersionKey = @"OWSOrphanDataCleaner_LastCleaningVersionKey";
|
||
7 years ago
|
NSString *const OWSOrphanDataCleaner_LastCleaningDateKey = @"OWSOrphanDataCleaner_LastCleaningDateKey";
|
||
7 years ago
|
|
||
|
@interface OWSOrphanData : NSObject
|
||
|
|
||
|
@property (nonatomic) NSSet<NSString *> *interactionIds;
|
||
|
@property (nonatomic) NSSet<NSString *> *attachmentIds;
|
||
|
@property (nonatomic) NSSet<NSString *> *filePaths;
|
||
|
|
||
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
|
@implementation OWSOrphanData
|
||
|
|
||
|
@end
|
||
|
|
||
|
#pragma mark -
|
||
|
|
||
|
typedef void (^OrphanDataBlock)(OWSOrphanData *);
|
||
|
|
||
|
@implementation OWSOrphanDataCleaner
|
||
|
|
||
|
// Unlike CurrentAppContext().isMainAppAndActive, this method can be safely
|
||
|
// invoked off the main thread.
|
||
|
+ (BOOL)isMainAppAndActive
|
||
|
{
|
||
|
return CurrentAppContext().reportedApplicationState == UIApplicationStateActive;
|
||
|
}
|
||
|
|
||
|
+ (void)printPaths:(NSArray<NSString *> *)paths label:(NSString *)label
|
||
|
{
|
||
|
for (NSString *path in [paths sortedArrayUsingSelector:@selector(compare:)]) {
|
||
7 years ago
|
OWSLogDebug(@"%@: %@", label, path);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
+ (long long)fileSizeOfFilePath:(NSString *)filePath
|
||
|
{
|
||
|
NSError *error;
|
||
|
NSNumber *fileSize = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error][NSFileSize];
|
||
|
if (error) {
|
||
|
if ([error.domain isEqualToString:NSCocoaErrorDomain] && error.code == 260) {
|
||
7 years ago
|
OWSLogWarn(@"can't find size of missing file.");
|
||
|
OWSLogDebug(@"can't find size of missing file: %@", filePath);
|
||
7 years ago
|
} else {
|
||
7 years ago
|
OWSFailDebug(@"attributesOfItemAtPath: %@ error: %@", filePath, error);
|
||
7 years ago
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
return fileSize.longLongValue;
|
||
|
}
|
||
|
|
||
|
+ (nullable NSNumber *)fileSizeOfFilePathsSafe:(NSArray<NSString *> *)filePaths
|
||
|
{
|
||
|
long long result = 0;
|
||
|
for (NSString *filePath in filePaths) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
result += [self fileSizeOfFilePath:filePath];
|
||
|
}
|
||
|
return @(result);
|
||
|
}
|
||
|
|
||
|
+ (nullable NSSet<NSString *> *)filePathsInDirectorySafe:(NSString *)dirPath
|
||
|
{
|
||
|
NSMutableSet *filePaths = [NSMutableSet new];
|
||
|
if (![[NSFileManager defaultManager] fileExistsAtPath:dirPath]) {
|
||
|
return filePaths;
|
||
|
}
|
||
|
NSError *error;
|
||
|
NSArray<NSString *> *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:&error];
|
||
|
if (error) {
|
||
7 years ago
|
OWSFailDebug(@"contentsOfDirectoryAtPath error: %@", error);
|
||
7 years ago
|
return [NSSet new];
|
||
|
}
|
||
|
for (NSString *fileName in fileNames) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
NSString *filePath = [dirPath stringByAppendingPathComponent:fileName];
|
||
|
BOOL isDirectory;
|
||
|
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
|
||
|
if (isDirectory) {
|
||
|
NSSet<NSString *> *_Nullable dirPaths = [self filePathsInDirectorySafe:filePath];
|
||
|
if (!dirPaths) {
|
||
|
return nil;
|
||
|
}
|
||
|
[filePaths unionSet:dirPaths];
|
||
|
} else {
|
||
|
[filePaths addObject:filePath];
|
||
|
}
|
||
|
}
|
||
|
return filePaths;
|
||
|
}
|
||
|
|
||
|
// This method finds (but does not delete):
|
||
|
//
|
||
|
// * Orphan TSInteractions (with no thread).
|
||
|
// * Orphan TSAttachments (with no message).
|
||
|
// * Orphan attachment files (with no corresponding TSAttachment).
|
||
|
// * Orphan profile avatars.
|
||
|
// * Temporary files (all).
|
||
|
//
|
||
|
// It also finds (we don't clean these up).
|
||
|
//
|
||
|
// * Missing attachment files (cannot be cleaned up).
|
||
|
// These are attachments which have no file on disk. They should be extremely rare -
|
||
|
// the only cases I have seen are probably due to debugging.
|
||
|
// They can't be cleaned up - we don't want to delete the TSAttachmentStream or
|
||
|
// its corresponding message. Better that the broken message shows up in the
|
||
|
// conversation view.
|
||
|
+ (void)findOrphanDataWithRetries:(NSInteger)remainingRetries
|
||
|
databaseConnection:(YapDatabaseConnection *)databaseConnection
|
||
|
success:(OrphanDataBlock)success
|
||
|
failure:(dispatch_block_t)failure
|
||
|
{
|
||
7 years ago
|
OWSAssertDebug(databaseConnection);
|
||
7 years ago
|
|
||
|
if (remainingRetries < 1) {
|
||
7 years ago
|
OWSLogInfo(@"Aborting orphan data search.");
|
||
7 years ago
|
dispatch_async(self.workQueue, ^{
|
||
|
failure();
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Wait until the app is active...
|
||
|
[CurrentAppContext() runNowOrWhenMainAppIsActive:^{
|
||
|
// ...but perform the work off the main thread.
|
||
|
dispatch_async(self.workQueue, ^{
|
||
|
OWSOrphanData *_Nullable orphanData = [self findOrphanDataSync:databaseConnection];
|
||
|
if (orphanData) {
|
||
|
success(orphanData);
|
||
|
} else {
|
||
|
[self findOrphanDataWithRetries:remainingRetries - 1
|
||
|
databaseConnection:databaseConnection
|
||
|
success:success
|
||
|
failure:failure];
|
||
|
}
|
||
|
});
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
// Returns nil on failure, usually indicating that the search
|
||
|
// aborted due to the app resigning active. This method is extremely careful to
|
||
|
// abort if the app resigns active, in order to avoid 0xdead10cc crashes.
|
||
|
+ (nullable OWSOrphanData *)findOrphanDataSync:(YapDatabaseConnection *)databaseConnection
|
||
|
{
|
||
7 years ago
|
OWSAssertDebug(databaseConnection);
|
||
7 years ago
|
|
||
|
__block BOOL shouldAbort = NO;
|
||
|
|
||
|
#ifdef LOG_ALL_FILE_PATHS
|
||
|
{
|
||
|
NSString *documentDirPath = [OWSFileSystem appDocumentDirectoryPath];
|
||
|
NSArray<NSString *> *_Nullable allDocumentFilePaths =
|
||
|
[self filePathsInDirectorySafe:documentDirPath].allObjects;
|
||
|
allDocumentFilePaths = [allDocumentFilePaths sortedArrayUsingSelector:@selector(compare:)];
|
||
|
NSString *attachmentsFolder = [TSAttachmentStream attachmentsFolder];
|
||
|
for (NSString *filePath in allDocumentFilePaths) {
|
||
|
if ([filePath hasPrefix:attachmentsFolder]) {
|
||
|
continue;
|
||
|
}
|
||
7 years ago
|
OWSLogVerbose(@"non-attachment file: %@", filePath);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
{
|
||
|
NSString *documentDirPath = [OWSFileSystem appSharedDataDirectoryPath];
|
||
|
NSArray<NSString *> *_Nullable allDocumentFilePaths =
|
||
|
[self filePathsInDirectorySafe:documentDirPath].allObjects;
|
||
|
allDocumentFilePaths = [allDocumentFilePaths sortedArrayUsingSelector:@selector(compare:)];
|
||
|
NSString *attachmentsFolder = [TSAttachmentStream attachmentsFolder];
|
||
|
for (NSString *filePath in allDocumentFilePaths) {
|
||
|
if ([filePath hasPrefix:attachmentsFolder]) {
|
||
|
continue;
|
||
|
}
|
||
7 years ago
|
OWSLogVerbose(@"non-attachment file: %@", filePath);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
// We treat _all_ temp files as orphan files. This is safe
|
||
|
// because temp files only need to be retained for the
|
||
|
// a single launch of the app. Since our "date threshold"
|
||
|
// for deletion is relative to the current launch time,
|
||
|
// all temp files currently in use should be safe.
|
||
7 years ago
|
NSArray<NSString *> *_Nullable tempFilePaths = [self getTempFilePaths];
|
||
7 years ago
|
if (!tempFilePaths || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
|
#ifdef LOG_ALL_FILE_PATHS
|
||
|
{
|
||
|
NSDateFormatter *dateFormatter = [NSDateFormatter new];
|
||
|
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
|
||
|
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
|
||
|
|
||
|
tempFilePaths = [tempFilePaths sortedArrayUsingSelector:@selector(compare:)];
|
||
|
for (NSString *filePath in tempFilePaths) {
|
||
|
NSError *error;
|
||
|
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
|
||
|
if (!attributes || error) {
|
||
7 years ago
|
OWSLogDebug(@"Could not get attributes of file at: %@", filePath);
|
||
|
OWSFailDebug(@"Could not get attributes of file");
|
||
7 years ago
|
continue;
|
||
|
}
|
||
7 years ago
|
OWSLogVerbose(
|
||
|
@"temp file: %@, %@", filePath, [dateFormatter stringFromDate:attributes.fileModificationDate]);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
NSString *legacyAttachmentsDirPath = TSAttachmentStream.legacyAttachmentsDirPath;
|
||
|
NSString *sharedDataAttachmentsDirPath = TSAttachmentStream.sharedDataAttachmentsDirPath;
|
||
|
NSSet<NSString *> *_Nullable legacyAttachmentFilePaths = [self filePathsInDirectorySafe:legacyAttachmentsDirPath];
|
||
|
if (!legacyAttachmentFilePaths || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
NSSet<NSString *> *_Nullable sharedDataAttachmentFilePaths =
|
||
|
[self filePathsInDirectorySafe:sharedDataAttachmentsDirPath];
|
||
|
if (!sharedDataAttachmentFilePaths || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
|
NSString *legacyProfileAvatarsDirPath = OWSUserProfile.legacyProfileAvatarsDirPath;
|
||
|
NSString *sharedDataProfileAvatarsDirPath = OWSUserProfile.sharedDataProfileAvatarsDirPath;
|
||
|
NSSet<NSString *> *_Nullable legacyProfileAvatarsFilePaths =
|
||
|
[self filePathsInDirectorySafe:legacyProfileAvatarsDirPath];
|
||
|
if (!legacyProfileAvatarsFilePaths || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
NSSet<NSString *> *_Nullable sharedDataProfileAvatarFilePaths =
|
||
|
[self filePathsInDirectorySafe:sharedDataProfileAvatarsDirPath];
|
||
|
if (!sharedDataProfileAvatarFilePaths || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
7 years ago
|
NSMutableSet<NSString *> *allOnDiskFilePaths = [NSMutableSet new];
|
||
|
[allOnDiskFilePaths unionSet:legacyAttachmentFilePaths];
|
||
|
[allOnDiskFilePaths unionSet:sharedDataAttachmentFilePaths];
|
||
|
[allOnDiskFilePaths unionSet:legacyProfileAvatarsFilePaths];
|
||
|
[allOnDiskFilePaths unionSet:sharedDataProfileAvatarFilePaths];
|
||
|
[allOnDiskFilePaths addObjectsFromArray:tempFilePaths];
|
||
7 years ago
|
|
||
|
NSSet<NSString *> *profileAvatarFilePaths = [OWSUserProfile allProfileAvatarFilePaths];
|
||
|
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
7 years ago
|
NSNumber *_Nullable totalFileSize = [self fileSizeOfFilePathsSafe:allOnDiskFilePaths.allObjects];
|
||
7 years ago
|
|
||
|
if (!totalFileSize || !self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
7 years ago
|
NSUInteger fileCount = allOnDiskFilePaths.count;
|
||
7 years ago
|
|
||
|
// Attachments
|
||
|
__block int attachmentStreamCount = 0;
|
||
|
NSMutableSet<NSString *> *allAttachmentFilePaths = [NSMutableSet new];
|
||
|
NSMutableSet<NSString *> *allAttachmentIds = [NSMutableSet new];
|
||
|
// Threads
|
||
|
__block NSSet *threadIds;
|
||
|
// Messages
|
||
|
NSMutableSet<NSString *> *orphanInteractionIds = [NSMutableSet new];
|
||
6 years ago
|
NSMutableSet<NSString *> *allMessageAttachmentIds = [NSMutableSet new];
|
||
7 years ago
|
[databaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
||
|
[transaction
|
||
|
enumerateKeysAndObjectsInCollection:TSAttachmentStream.collection
|
||
|
usingBlock:^(NSString *key, TSAttachment *attachment, BOOL *stop) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
shouldAbort = YES;
|
||
|
*stop = YES;
|
||
|
return;
|
||
|
}
|
||
|
if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
|
||
|
return;
|
||
|
}
|
||
|
[allAttachmentIds addObject:attachment.uniqueId];
|
||
|
|
||
|
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
|
||
|
attachmentStreamCount++;
|
||
7 years ago
|
NSString *_Nullable filePath = [attachmentStream originalFilePath];
|
||
7 years ago
|
if (filePath) {
|
||
|
[allAttachmentFilePaths addObject:filePath];
|
||
|
} else {
|
||
7 years ago
|
OWSFailDebug(@"attachment has no file path.");
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
[allAttachmentFilePaths
|
||
|
addObjectsFromArray:attachmentStream.allThumbnailPaths];
|
||
7 years ago
|
}];
|
||
|
|
||
|
if (shouldAbort) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
threadIds = [NSSet setWithArray:[transaction allKeysInCollection:TSThread.collection]];
|
||
|
|
||
|
[transaction
|
||
|
enumerateKeysAndObjectsInCollection:TSMessage.collection
|
||
|
usingBlock:^(NSString *key, TSInteraction *interaction, BOOL *stop) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
shouldAbort = YES;
|
||
|
*stop = YES;
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
if (interaction.uniqueThreadId.length < 1
|
||
7 years ago
|
|| ![threadIds containsObject:interaction.uniqueThreadId]) {
|
||
|
[orphanInteractionIds addObject:interaction.uniqueId];
|
||
|
}
|
||
|
|
||
|
if (![interaction isKindOfClass:[TSMessage class]]) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
TSMessage *message = (TSMessage *)interaction;
|
||
6 years ago
|
[allMessageAttachmentIds addObjectsFromArray:message.allAttachmentIds];
|
||
7 years ago
|
}];
|
||
|
}];
|
||
|
if (shouldAbort) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
7 years ago
|
OWSLogDebug(@"fileCount: %zu", fileCount);
|
||
|
OWSLogDebug(@"totalFileSize: %lld", totalFileSize.longLongValue);
|
||
|
OWSLogDebug(@"attachmentStreams: %d", attachmentStreamCount);
|
||
|
OWSLogDebug(@"attachmentStreams with file paths: %zu", allAttachmentFilePaths.count);
|
||
7 years ago
|
|
||
7 years ago
|
NSMutableSet<NSString *> *orphanFilePaths = [allOnDiskFilePaths mutableCopy];
|
||
7 years ago
|
[orphanFilePaths minusSet:allAttachmentFilePaths];
|
||
|
[orphanFilePaths minusSet:profileAvatarFilePaths];
|
||
|
NSMutableSet<NSString *> *missingAttachmentFilePaths = [allAttachmentFilePaths mutableCopy];
|
||
7 years ago
|
[missingAttachmentFilePaths minusSet:allOnDiskFilePaths];
|
||
7 years ago
|
|
||
7 years ago
|
OWSLogDebug(@"orphan file paths: %zu", orphanFilePaths.count);
|
||
|
OWSLogDebug(@"missing attachment file paths: %zu", missingAttachmentFilePaths.count);
|
||
7 years ago
|
|
||
|
[self printPaths:orphanFilePaths.allObjects label:@"orphan file paths"];
|
||
|
[self printPaths:missingAttachmentFilePaths.allObjects label:@"missing attachment file paths"];
|
||
|
|
||
7 years ago
|
OWSLogDebug(@"attachmentIds: %zu", allAttachmentIds.count);
|
||
6 years ago
|
OWSLogDebug(@"allMessageAttachmentIds: %zu", allMessageAttachmentIds.count);
|
||
7 years ago
|
|
||
|
NSMutableSet<NSString *> *orphanAttachmentIds = [allAttachmentIds mutableCopy];
|
||
6 years ago
|
[orphanAttachmentIds minusSet:allMessageAttachmentIds];
|
||
|
NSMutableSet<NSString *> *missingAttachmentIds = [allMessageAttachmentIds mutableCopy];
|
||
7 years ago
|
[missingAttachmentIds minusSet:allAttachmentIds];
|
||
|
|
||
7 years ago
|
OWSLogDebug(@"orphan attachmentIds: %zu", orphanAttachmentIds.count);
|
||
|
OWSLogDebug(@"missing attachmentIds: %zu", missingAttachmentIds.count);
|
||
|
OWSLogDebug(@"orphan interactions: %zu", orphanInteractionIds.count);
|
||
7 years ago
|
|
||
|
OWSOrphanData *result = [OWSOrphanData new];
|
||
|
result.interactionIds = [orphanInteractionIds copy];
|
||
|
result.attachmentIds = [orphanAttachmentIds copy];
|
||
|
result.filePaths = [orphanFilePaths copy];
|
||
|
return result;
|
||
|
}
|
||
|
|
||
6 years ago
|
+ (BOOL)shouldAuditOnLaunch:(YapDatabaseConnection *)databaseConnection {
|
||
7 years ago
|
OWSAssertIsOnMainThread();
|
||
|
|
||
7 years ago
|
#ifndef ENABLE_ORPHAN_DATA_CLEANER
|
||
6 years ago
|
return NO;
|
||
7 years ago
|
#endif
|
||
|
|
||
|
__block NSString *_Nullable lastCleaningVersion;
|
||
|
__block NSDate *_Nullable lastCleaningDate;
|
||
|
[databaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
||
7 years ago
|
lastCleaningVersion = [transaction stringForKey:OWSOrphanDataCleaner_LastCleaningVersionKey
|
||
|
inCollection:OWSOrphanDataCleaner_Collection];
|
||
|
lastCleaningDate = [transaction dateForKey:OWSOrphanDataCleaner_LastCleaningDateKey
|
||
|
inCollection:OWSOrphanDataCleaner_Collection];
|
||
7 years ago
|
}];
|
||
|
|
||
6 years ago
|
// Clean up once per app version.
|
||
7 years ago
|
NSString *currentAppVersion = AppVersion.sharedInstance.currentAppVersion;
|
||
6 years ago
|
if (!lastCleaningVersion || ![lastCleaningVersion isEqualToString:currentAppVersion]) {
|
||
|
OWSLogVerbose(@"Performing orphan data cleanup; new version: %@.", currentAppVersion);
|
||
|
return YES;
|
||
|
}
|
||
|
|
||
|
// Clean up once per N days.
|
||
|
if (lastCleaningDate) {
|
||
|
#ifdef DEBUG
|
||
|
BOOL shouldAudit = [DateUtil dateIsOlderThanToday:lastCleaningDate];
|
||
|
#else
|
||
|
BOOL shouldAudit = [DateUtil dateIsOlderThanOneWeek:lastCleaningDate];
|
||
|
#endif
|
||
|
|
||
|
if (shouldAudit) {
|
||
|
OWSLogVerbose(@"Performing orphan data cleanup; time has passed.");
|
||
|
}
|
||
|
return shouldAudit;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// Has never audited before.
|
||
|
return NO;
|
||
|
}
|
||
|
|
||
|
+ (void)auditOnLaunchIfNecessary {
|
||
|
OWSAssertIsOnMainThread();
|
||
|
|
||
|
OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager];
|
||
6 years ago
|
YapDatabaseConnection *databaseConnection = [primaryStorage newDatabaseConnection];
|
||
6 years ago
|
|
||
|
if (![self shouldAuditOnLaunch:databaseConnection]) {
|
||
7 years ago
|
return;
|
||
|
}
|
||
7 years ago
|
|
||
|
// If we want to be cautious, we can disable orphan deletion using
|
||
|
// flag - the cleanup will just be a dry run with logging.
|
||
6 years ago
|
BOOL shouldRemoveOrphans = YES;
|
||
7 years ago
|
[self auditAndCleanup:shouldRemoveOrphans databaseConnection:databaseConnection completion:nil];
|
||
7 years ago
|
}
|
||
|
|
||
|
+ (void)auditAndCleanup:(BOOL)shouldRemoveOrphans
|
||
|
{
|
||
6 years ago
|
[self auditAndCleanup:shouldRemoveOrphans
|
||
|
completion:^ {
|
||
|
}];
|
||
7 years ago
|
}
|
||
|
|
||
|
+ (void)auditAndCleanup:(BOOL)shouldRemoveOrphans completion:(dispatch_block_t)completion
|
||
|
{
|
||
|
OWSPrimaryStorage *primaryStorage = [OWSPrimaryStorage sharedManager];
|
||
6 years ago
|
YapDatabaseConnection *databaseConnection = [primaryStorage newDatabaseConnection];
|
||
7 years ago
|
|
||
|
[self auditAndCleanup:shouldRemoveOrphans databaseConnection:databaseConnection completion:completion];
|
||
7 years ago
|
}
|
||
|
|
||
|
// We use the lowest priority possible.
|
||
|
+ (dispatch_queue_t)workQueue
|
||
|
{
|
||
|
return dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
|
||
|
}
|
||
|
|
||
7 years ago
|
+ (void)auditAndCleanup:(BOOL)shouldRemoveOrphans
|
||
|
databaseConnection:(YapDatabaseConnection *)databaseConnection
|
||
|
completion:(nullable dispatch_block_t)completion
|
||
7 years ago
|
{
|
||
|
OWSAssertIsOnMainThread();
|
||
7 years ago
|
OWSAssertDebug(databaseConnection);
|
||
7 years ago
|
|
||
|
if (!AppReadiness.isAppReady) {
|
||
7 years ago
|
OWSFailDebug(@"can't audit orphan data until app is ready.");
|
||
7 years ago
|
return;
|
||
|
}
|
||
|
if (!CurrentAppContext().isMainApp) {
|
||
7 years ago
|
OWSFailDebug(@"can't audit orphan data in app extensions.");
|
||
7 years ago
|
return;
|
||
|
}
|
||
7 years ago
|
if (CurrentAppContext().isRunningTests) {
|
||
|
OWSLogVerbose(@"Ignoring audit orphan data in tests.");
|
||
|
return;
|
||
|
}
|
||
7 years ago
|
|
||
|
// Orphan cleanup has two risks:
|
||
|
//
|
||
|
// * As a long-running process that involves access to the
|
||
|
// shared data container, it could cause 0xdead10cc.
|
||
|
// * It could accidentally delete data still in use,
|
||
|
// e.g. a profile avatar which has been saved to disk
|
||
|
// but whose OWSUserProfile hasn't been saved yet.
|
||
|
//
|
||
|
// To prevent 0xdead10cc, the cleaner continually checks
|
||
|
// whether the app has resigned active. If so, it aborts.
|
||
|
// Each phase (search, re-search, processing) retries N times,
|
||
|
// then gives up until the next app launch.
|
||
|
//
|
||
|
// To prevent accidental data deletion, we take the following
|
||
|
// measures:
|
||
|
//
|
||
|
// * Only cleanup data of the following types (which should
|
||
|
// include all relevant app data): profile avatar,
|
||
|
// attachment, temporary files (including temporary
|
||
|
// attachments).
|
||
|
// * We don't delete any data created more recently than N seconds
|
||
|
// _before_ when the app launched. This prevents any stray data
|
||
|
// currently in use by the app from being accidentally cleaned
|
||
|
// up.
|
||
|
const NSInteger kMaxRetries = 3;
|
||
|
[self findOrphanDataWithRetries:kMaxRetries
|
||
|
databaseConnection:databaseConnection
|
||
|
success:^(OWSOrphanData *orphanData) {
|
||
|
[self processOrphans:orphanData
|
||
|
remainingRetries:kMaxRetries
|
||
|
databaseConnection:databaseConnection
|
||
|
shouldRemoveOrphans:shouldRemoveOrphans
|
||
|
success:^{
|
||
7 years ago
|
OWSLogInfo(@"Completed orphan data cleanup.");
|
||
7 years ago
|
|
||
5 years ago
|
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||
7 years ago
|
[transaction setObject:AppVersion.sharedInstance.currentAppVersion
|
||
|
forKey:OWSOrphanDataCleaner_LastCleaningVersionKey
|
||
|
inCollection:OWSOrphanDataCleaner_Collection];
|
||
|
[transaction setDate:[NSDate new]
|
||
|
forKey:OWSOrphanDataCleaner_LastCleaningDateKey
|
||
|
inCollection:OWSOrphanDataCleaner_Collection];
|
||
5 years ago
|
}];
|
||
7 years ago
|
|
||
|
if (completion) {
|
||
|
completion();
|
||
|
}
|
||
7 years ago
|
}
|
||
|
failure:^{
|
||
7 years ago
|
OWSLogInfo(@"Aborting orphan data cleanup.");
|
||
7 years ago
|
if (completion) {
|
||
|
completion();
|
||
|
}
|
||
7 years ago
|
}];
|
||
|
}
|
||
|
failure:^{
|
||
7 years ago
|
OWSLogInfo(@"Aborting orphan data cleanup.");
|
||
7 years ago
|
if (completion) {
|
||
|
completion();
|
||
|
}
|
||
7 years ago
|
}];
|
||
|
}
|
||
|
|
||
|
// Returns NO on failure, usually indicating that orphan processing
|
||
|
// aborted due to the app resigning active. This method is extremely careful to
|
||
|
// abort if the app resigns active, in order to avoid 0xdead10cc crashes.
|
||
|
+ (void)processOrphans:(OWSOrphanData *)orphanData
|
||
|
remainingRetries:(NSInteger)remainingRetries
|
||
|
databaseConnection:(YapDatabaseConnection *)databaseConnection
|
||
|
shouldRemoveOrphans:(BOOL)shouldRemoveOrphans
|
||
|
success:(dispatch_block_t)success
|
||
|
failure:(dispatch_block_t)failure
|
||
|
{
|
||
7 years ago
|
OWSAssertDebug(databaseConnection);
|
||
|
OWSAssertDebug(orphanData);
|
||
7 years ago
|
|
||
|
if (remainingRetries < 1) {
|
||
7 years ago
|
OWSLogInfo(@"Aborting orphan data audit.");
|
||
7 years ago
|
dispatch_async(self.workQueue, ^{
|
||
|
failure();
|
||
|
});
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Wait until the app is active...
|
||
|
[CurrentAppContext() runNowOrWhenMainAppIsActive:^{
|
||
|
// ...but perform the work off the main thread.
|
||
|
dispatch_async(self.workQueue, ^{
|
||
|
if ([self processOrphansSync:orphanData
|
||
|
databaseConnection:databaseConnection
|
||
|
shouldRemoveOrphans:shouldRemoveOrphans]) {
|
||
|
success();
|
||
|
return;
|
||
|
} else {
|
||
|
[self processOrphans:orphanData
|
||
|
remainingRetries:remainingRetries - 1
|
||
|
databaseConnection:databaseConnection
|
||
|
shouldRemoveOrphans:shouldRemoveOrphans
|
||
|
success:success
|
||
|
failure:failure];
|
||
|
}
|
||
|
});
|
||
|
}];
|
||
|
}
|
||
|
|
||
|
// Returns NO on failure, usually indicating that orphan processing
|
||
|
// aborted due to the app resigning active. This method is extremely careful to
|
||
|
// abort if the app resigns active, in order to avoid 0xdead10cc crashes.
|
||
|
+ (BOOL)processOrphansSync:(OWSOrphanData *)orphanData
|
||
|
databaseConnection:(YapDatabaseConnection *)databaseConnection
|
||
|
shouldRemoveOrphans:(BOOL)shouldRemoveOrphans
|
||
|
{
|
||
7 years ago
|
OWSAssertDebug(databaseConnection);
|
||
|
OWSAssertDebug(orphanData);
|
||
7 years ago
|
|
||
|
__block BOOL shouldAbort = NO;
|
||
|
|
||
6 years ago
|
// We need to avoid cleaning up new files that are still in the process of
|
||
7 years ago
|
// being created/written, so we don't clean up anything recent.
|
||
|
const NSTimeInterval kMinimumOrphanAgeSeconds = CurrentAppContext().isRunningTests ? 0.f : 15 * kMinuteInterval;
|
||
7 years ago
|
NSDate *appLaunchTime = CurrentAppContext().appLaunchTime;
|
||
7 years ago
|
NSTimeInterval thresholdTimestamp = appLaunchTime.timeIntervalSince1970 - kMinimumOrphanAgeSeconds;
|
||
|
NSDate *thresholdDate = [NSDate dateWithTimeIntervalSince1970:thresholdTimestamp];
|
||
5 years ago
|
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
||
7 years ago
|
NSUInteger interactionsRemoved = 0;
|
||
|
for (NSString *interactionId in orphanData.interactionIds) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
shouldAbort = YES;
|
||
|
return;
|
||
|
}
|
||
|
TSInteraction *_Nullable interaction =
|
||
|
[TSInteraction fetchObjectWithUniqueID:interactionId transaction:transaction];
|
||
|
if (!interaction) {
|
||
|
// This could just be a race condition, but it should be very unlikely.
|
||
7 years ago
|
OWSLogWarn(@"Could not load interaction: %@", interactionId);
|
||
7 years ago
|
continue;
|
||
|
}
|
||
|
// Don't delete interactions which were created in the last N minutes.
|
||
5 years ago
|
NSDate *creationDate = [NSDate ows_dateWithMillisecondsSince1970:interaction.timestamp];
|
||
|
if ([creationDate isAfterDate:thresholdDate]) {
|
||
|
OWSLogInfo(@"Skipping orphan interaction due to age: %f", fabs(creationDate.timeIntervalSinceNow));
|
||
|
continue;
|
||
7 years ago
|
}
|
||
7 years ago
|
OWSLogInfo(@"Removing orphan message: %@", interaction.uniqueId);
|
||
7 years ago
|
interactionsRemoved++;
|
||
|
if (!shouldRemoveOrphans) {
|
||
|
continue;
|
||
|
}
|
||
|
[interaction removeWithTransaction:transaction];
|
||
|
}
|
||
7 years ago
|
OWSLogInfo(@"Deleted orphan interactions: %zu", interactionsRemoved);
|
||
7 years ago
|
|
||
|
NSUInteger attachmentsRemoved = 0;
|
||
|
for (NSString *attachmentId in orphanData.attachmentIds) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
shouldAbort = YES;
|
||
|
return;
|
||
|
}
|
||
|
TSAttachment *_Nullable attachment =
|
||
|
[TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
|
||
|
if (!attachment) {
|
||
|
// This can happen on launch since we sync contacts/groups, especially if you have a lot of attachments
|
||
|
// to churn through, it's likely it's been deleted since starting this job.
|
||
7 years ago
|
OWSLogWarn(@"Could not load attachment: %@", attachmentId);
|
||
7 years ago
|
continue;
|
||
|
}
|
||
|
if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
|
||
|
continue;
|
||
|
}
|
||
|
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
|
||
|
// Don't delete attachments which were created in the last N minutes.
|
||
|
NSDate *creationDate = attachmentStream.creationTimestamp;
|
||
|
if ([creationDate isAfterDate:thresholdDate]) {
|
||
7 years ago
|
OWSLogInfo(@"Skipping orphan attachment due to age: %f", fabs(creationDate.timeIntervalSinceNow));
|
||
7 years ago
|
continue;
|
||
|
}
|
||
7 years ago
|
OWSLogInfo(@"Removing orphan attachmentStream: %@", attachmentStream.uniqueId);
|
||
7 years ago
|
attachmentsRemoved++;
|
||
|
if (!shouldRemoveOrphans) {
|
||
|
continue;
|
||
|
}
|
||
|
[attachmentStream removeWithTransaction:transaction];
|
||
|
}
|
||
7 years ago
|
OWSLogInfo(@"Deleted orphan attachments: %zu", attachmentsRemoved);
|
||
5 years ago
|
}];
|
||
7 years ago
|
|
||
|
if (shouldAbort) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
|
NSUInteger filesRemoved = 0;
|
||
|
NSArray<NSString *> *filePaths = [orphanData.filePaths.allObjects sortedArrayUsingSelector:@selector(compare:)];
|
||
|
for (NSString *filePath in filePaths) {
|
||
|
if (!self.isMainAppAndActive) {
|
||
|
return nil;
|
||
|
}
|
||
|
|
||
|
NSError *error;
|
||
|
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:&error];
|
||
|
if (!attributes || error) {
|
||
7 years ago
|
// This is fine; the file may have been deleted since we found it.
|
||
|
OWSLogWarn(@"Could not get attributes of file at: %@", filePath);
|
||
7 years ago
|
continue;
|
||
|
}
|
||
|
// Don't delete files which were created in the last N minutes.
|
||
|
NSDate *creationDate = attributes.fileModificationDate;
|
||
|
if ([creationDate isAfterDate:thresholdDate]) {
|
||
6 years ago
|
OWSLogInfo(@"Skipping file due to age: %f", fabs([creationDate timeIntervalSinceNow]));
|
||
7 years ago
|
continue;
|
||
|
}
|
||
6 years ago
|
OWSLogInfo(@"Deleting file: %@", filePath);
|
||
7 years ago
|
filesRemoved++;
|
||
|
if (!shouldRemoveOrphans) {
|
||
|
continue;
|
||
|
}
|
||
|
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
|
||
|
if (error) {
|
||
7 years ago
|
OWSLogDebug(@"Could not remove orphan file at: %@", filePath);
|
||
|
OWSFailDebug(@"Could not remove orphan file");
|
||
7 years ago
|
}
|
||
|
}
|
||
6 years ago
|
OWSLogInfo(@"Deleted orphan files: %zu", filesRemoved);
|
||
7 years ago
|
|
||
|
return YES;
|
||
|
}
|
||
|
|
||
7 years ago
|
+ (nullable NSArray<NSString *> *)getTempFilePaths
|
||
|
{
|
||
7 years ago
|
NSString *dir1 = OWSTemporaryDirectory();
|
||
7 years ago
|
NSArray<NSString *> *_Nullable paths1 = [[self filePathsInDirectorySafe:dir1].allObjects mutableCopy];
|
||
|
|
||
7 years ago
|
NSString *dir2 = OWSTemporaryDirectoryAccessibleAfterFirstAuth();
|
||
7 years ago
|
NSArray<NSString *> *_Nullable paths2 = [[self filePathsInDirectorySafe:dir2].allObjects mutableCopy];
|
||
|
|
||
|
if (paths1 && paths2) {
|
||
|
return [paths1 arrayByAddingObjectsFromArray:paths2];
|
||
|
} else {
|
||
|
return nil;
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
@end
|
||
|
|
||
|
NS_ASSUME_NONNULL_END
|