Repository navigation
Expand file tree
/
Copy pathFs.m
More file actions
193 lines (175 loc) · 8.86 KB
/
Copy pathFs.m
File metadata and controls
193 lines (175 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Fs.m
// Keybase
//
// Created by Michael Maxim on 9/5/18.
// Copyright © 2018 Keybase. All rights reserved.
//
#import "Fs.h"
@implementation FsHelper
- (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString
{
NSURL * URL = [NSURL fileURLWithPath: filePathString];
NSError * error = nil;
BOOL success = [URL setResourceValue: @YES forKey: NSURLIsExcludedFromBackupKey error: &error];
if(!success){
NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
}
return success;
}
- (void) createBackgroundReadableDirectory:(NSString*) path setAllFiles:(BOOL)setAllFiles
{
NSFileManager* fm = [NSFileManager defaultManager];
NSError* error = nil;
NSLog(@"creating background readable directory: path: %@ setAllFiles: %d", path, setAllFiles);
// Setting NSFileProtectionCompleteUntilFirstUserAuthentication makes the
// directory accessible as long as the user has unlocked the phone once. The
// files are still stored on the disk encrypted (note for the chat database,
// it means we are encrypting it twice), and are inaccessible otherwise.
NSDictionary* noProt = [NSDictionary dictionaryWithObject:NSFileProtectionCompleteUntilFirstUserAuthentication forKey:NSFileProtectionKey];
[fm createDirectoryAtPath:path withIntermediateDirectories:YES
attributes:noProt
error:nil];
if (![fm setAttributes:noProt ofItemAtPath:path error:&error]) {
NSLog(@"Error setting file attributes on path: %@ error: %@", path, error);
}
if (!setAllFiles) {
NSLog(@"setAllFiles is false, so returning now");
return;
} else {
NSLog(@"setAllFiles is true charging forward");
}
// If the caller wants us to set everything in the directory, then let's do it now (one level down at least)
NSArray<NSString*>* contents = [fm contentsOfDirectoryAtPath:path error:&error];
if (contents == nil) {
NSLog(@"Error listing directory contents: %@", error);
} else {
for (NSString* file in contents) {
NSString* filePath = [NSString stringWithFormat:@"%@/%@", path, file];
if (![fm setAttributes:noProt ofItemAtPath:filePath error:&error]) {
NSLog(@"Error setting file attributes on file: %@ error: %@", file, error);
}
}
}
}
- (BOOL) maybeMigrateDirectory:(NSString*)source dest:(NSString*)dest {
NSError* error = nil;
NSFileManager* fm = [NSFileManager defaultManager];
// Always do this move in case it doesn't work on previous attempts.
NSArray<NSString*>* sourceContents = [fm contentsOfDirectoryAtPath:source error:&error];
if (nil == sourceContents) {
NSLog(@"Error listing app contents directory: %@", error);
return NO;
} else {
for (NSString* file in sourceContents) {
BOOL isDirectory = NO;
NSString* path = [NSString stringWithFormat:@"%@/%@", source, file];
NSString* destPath = [NSString stringWithFormat:@"%@/%@", dest, file];
if ([fm fileExistsAtPath:path isDirectory:&isDirectory] && isDirectory) {
NSLog(@"skipping directory: %@", file);
continue;
}
if (![fm moveItemAtPath:path toPath:destPath error:&error]) {
if ([error code] == NSFileWriteFileExistsError) {
// Just charge forward if the file is there already
continue;
}
NSLog(@"Error moving file: %@ error: %@", file, error);
return NO;
}
}
}
return YES;
}
+ (NSString*) getAppKeybasePath {
return [@"~/Library/Application Support/Keybase" stringByExpandingTildeInPath];
}
+ (NSString*) getEraseableKVPath {
return [[FsHelper getAppKeybasePath] stringByAppendingPathComponent:@"eraseablekvstore/device-eks"];
}
- (NSString*) setupAppHome:(NSString*)home sharedHome:(NSString*)sharedHome {
NSURL* tempUrl = [[NSFileManager defaultManager] temporaryDirectory];
// workaround a problem where iOS dyld3 loader crashes if accessing .closure files
// with complete data protection on
NSString* dyldDir = [NSString stringWithFormat:@"%@/com.apple.dyld", [tempUrl path]];
// Setup all directories
NSString* appKeybasePath = [FsHelper getAppKeybasePath];
NSString* appEraseableKVPath = [FsHelper getEraseableKVPath];
[self createBackgroundReadableDirectory:dyldDir setAllFiles:YES];
[self createBackgroundReadableDirectory:appKeybasePath setAllFiles:YES];
[self createBackgroundReadableDirectory:appEraseableKVPath setAllFiles:YES];
[self addSkipBackupAttributeToItemAtPath:appKeybasePath];
return home;
}
- (NSString*) setupSharedHome:(NSString*) home sharedHome:(NSString*)sharedHome {
NSString* appKeybasePath = [FsHelper getAppKeybasePath];
NSString* appEraseableKVPath = [FsHelper getEraseableKVPath];
NSString* sharedKeybasePath = [NSString stringWithFormat:@"%@/Library/Application Support/Keybase", sharedHome];
NSString* sharedEraseableKVPath = [sharedKeybasePath stringByAppendingPathComponent:@"eraseablekvstore/device-eks"];
[self createBackgroundReadableDirectory:sharedKeybasePath setAllFiles:YES];
[self createBackgroundReadableDirectory:sharedEraseableKVPath setAllFiles:YES];
[self addSkipBackupAttributeToItemAtPath:sharedKeybasePath];
if (![self maybeMigrateDirectory:appKeybasePath dest:sharedKeybasePath]) {
return home;
}
if (![self maybeMigrateDirectory:appEraseableKVPath dest:sharedEraseableKVPath]) {
return home;
}
return sharedHome;
}
- (NSDictionary*) setupFs:(BOOL)skipLogFile setupSharedHome:(BOOL)setupSharedHome {
NSString* home = NSHomeDirectory();
NSURL* sharedURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.keybase"];
NSString* sharedHome = [sharedURL relativePath];
home = [self setupAppHome:home sharedHome:sharedHome];
if (setupSharedHome) {
sharedHome = [self setupSharedHome:home sharedHome:sharedHome];
}
NSString* appKeybasePath = [FsHelper getAppKeybasePath];
// Setup app level directories
NSString* levelDBPath = [appKeybasePath stringByAppendingPathComponent:@"keybase.leveldb"];
NSString* chatLevelDBPath = [appKeybasePath stringByAppendingPathComponent:@"keybase.chat.leveldb"];
NSString* kbfsBlockCacheDBPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_block_cache"];
NSString* kbfsBlockMetaDBPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_block_metadata"];
NSString* kbfsConflictsDBPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_conflicts"];
NSString* kbfsFavoritesDBPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_favorites"];
NSString* kbfsJournalPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_journal"];
NSString* kbfsMDCachePath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_md_cache"];
NSString* kbfsQuotaCachePath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_quota_cache"];
NSString* kbfsSyncCachePath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_sync_cache"];
NSString* kbfsSettingsPath = [appKeybasePath stringByAppendingPathComponent:@"kbfs_settings"];
NSString* kbfsSyncedTlfsPath = [appKeybasePath stringByAppendingPathComponent:@"synced_tlf_config"];
NSString* oldLogPath = [@"~/Library/Caches/Keybase" stringByExpandingTildeInPath];
// Put logs in a subdir that is entirely background readable
NSString* logPath = [oldLogPath stringByAppendingPathComponent:@"logs"];
NSString* serviceLogFile = skipLogFile ? @"" : [logPath stringByAppendingPathComponent:@"ios.log"];
if (!skipLogFile) {
// cleanup old log files
NSFileManager* fm = [NSFileManager defaultManager];
NSArray<NSString*>* logs = @[@"ios.log", @"ios.log.ek"];
for (NSString* file in logs) {
NSString* oldPath = [oldLogPath stringByAppendingPathComponent:file];
[fm removeItemAtPath:oldPath error:NULL];
}
}
// Create LevelDB and log directories with a slightly lower data protection
// mode so we can use them in the background
[self createBackgroundReadableDirectory:chatLevelDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:levelDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsBlockCacheDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsBlockMetaDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsConflictsDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsFavoritesDBPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsJournalPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsMDCachePath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsQuotaCachePath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsSyncCachePath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsSettingsPath setAllFiles:YES];
[self createBackgroundReadableDirectory:kbfsSyncedTlfsPath setAllFiles:YES];
[self createBackgroundReadableDirectory:logPath setAllFiles:YES];
return @{@"home": home,
@"sharedHome": sharedHome,
@"logFile": serviceLogFile
};
}
@end