forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathFileProviderExtension.m
More file actions
791 lines (724 loc) · 34.5 KB
/
Copy pathFileProviderExtension.m
File metadata and controls
791 lines (724 loc) · 34.5 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
//
// FileProviderExtension.m
// iSHFiles
//
// Created by Theodore Dubois on 9/20/18.
//
#import "FileProviderExtension.h"
#import "FileProviderItem.h"
#import "FileProviderEnumerator.h"
#import "NSError+ISHErrno.h"
#import "../AppGroup.h"
#include "fs/fake-db.h"
static NSNumber *ISHFileProviderDurationMilliseconds(NSTimeInterval start) {
return @((NSInteger) ((NSDate.date.timeIntervalSinceReferenceDate - start) * 1000.0));
}
static NSString *const kISHFileProviderDiagnosticsDirectory = @"Diagnostics";
static NSString *const kISHFileProviderDiagnosticsBreadcrumbsFile = @"breadcrumbs.json";
static dispatch_queue_t ISHFileProviderBreadcrumbQueue(void) {
static dispatch_queue_t queue;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("app.ish.iSH-AOK.FileProvider.Diagnostics", DISPATCH_QUEUE_SERIAL);
});
return queue;
}
static NSURL *ISHFileProviderDiagnosticsDirectoryURL(void) {
NSURL *baseURL = ContainerURL();
if (baseURL == nil) {
baseURL = [NSFileManager.defaultManager URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask].firstObject;
}
if (baseURL == nil)
return nil;
return [baseURL URLByAppendingPathComponent:kISHFileProviderDiagnosticsDirectory isDirectory:YES];
}
static NSURL *ISHFileProviderBreadcrumbsURL(void) {
NSURL *directoryURL = ISHFileProviderDiagnosticsDirectoryURL();
if (directoryURL == nil)
return nil;
return [directoryURL URLByAppendingPathComponent:kISHFileProviderDiagnosticsBreadcrumbsFile isDirectory:NO];
}
static NSString *ISHFileProviderISO8601StringFromDate(NSDate *date) {
if (date == nil)
return nil;
static NSISO8601DateFormatter *formatter;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
formatter = [NSISO8601DateFormatter new];
formatter.formatOptions = NSISO8601DateFormatWithInternetDateTime;
});
return [formatter stringFromDate:date];
}
void ISHFileProviderRecordBreadcrumb(NSString *event, NSDictionary<NSString *, id> *details) {
if (event.length == 0)
return;
dispatch_async(ISHFileProviderBreadcrumbQueue(), ^{
NSURL *directoryURL = ISHFileProviderDiagnosticsDirectoryURL();
NSURL *breadcrumbsURL = ISHFileProviderBreadcrumbsURL();
if (directoryURL == nil || breadcrumbsURL == nil)
return;
[NSFileManager.defaultManager createDirectoryAtURL:directoryURL
withIntermediateDirectories:YES
attributes:nil
error:nil];
NSData *existingData = [NSData dataWithContentsOfURL:breadcrumbsURL];
NSMutableArray<NSDictionary<NSString *, id> *> *breadcrumbs = [NSMutableArray array];
if (existingData.length > 0) {
id existingObject = [NSJSONSerialization JSONObjectWithData:existingData options:NSJSONReadingMutableContainers error:nil];
if ([existingObject isKindOfClass:[NSArray class]])
[breadcrumbs addObjectsFromArray:existingObject];
}
NSMutableDictionary<NSString *, id> *entry = [NSMutableDictionary dictionary];
entry[@"timestamp"] = ISHFileProviderISO8601StringFromDate([NSDate date]) ?: @"";
entry[@"event"] = event;
if (details.count != 0)
entry[@"details"] = details;
[breadcrumbs addObject:entry];
const NSUInteger maxBreadcrumbs = 200;
if (breadcrumbs.count > maxBreadcrumbs) {
NSRange overflow = NSMakeRange(0, breadcrumbs.count - maxBreadcrumbs);
[breadcrumbs removeObjectsInRange:overflow];
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:breadcrumbs options:NSJSONWritingPrettyPrinted error:nil];
if (jsonData != nil)
[jsonData writeToURL:breadcrumbsURL options:NSDataWritingAtomic error:nil];
});
}
static NSMutableDictionary<NSString *, id> *ISHFileProviderDetails(NSString * _Nullable domainIdentifier) {
NSMutableDictionary<NSString *, id> *details = [NSMutableDictionary dictionary];
if (domainIdentifier.length != 0)
details[@"domain"] = domainIdentifier;
return details;
}
static NSString *ISHFileProviderCleanupDefaultsKey(NSString * _Nullable domainIdentifier) {
NSString *suffix = domainIdentifier.length != 0 ? domainIdentifier : @"default";
return [NSString stringWithFormat:@"ISHFileProviderLastCleanup.%@", suffix];
}
static int ISHFileProviderAcquireRootLock(NSString *domainIdentifier, BOOL exclusive) {
NSError *error = nil;
int fd = ISHAppGroupAcquireNamedLock(@"root", domainIdentifier, exclusive, &error);
if (fd < 0) {
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(domainIdentifier);
details[@"mode"] = exclusive ? @"exclusive" : @"shared";
details[@"error"] = error.localizedDescription ?: @"unknown";
ISHFileProviderRecordBreadcrumb(@"fileprovider.rootlock.failed", details);
}
return fd;
}
// Lists the valid installed roots in the shared app-group container. Mirrors the
// app-side RootURLLooksValid check (a "data" directory plus a "meta.db").
static NSArray<NSString *> *ISHFileProviderInstalledRootNames(void) {
NSURL *rootsDir = [ContainerURL() URLByAppendingPathComponent:@"roots" isDirectory:YES];
if (rootsDir == nil)
return @[];
NSArray<NSURL *> *entries = [NSFileManager.defaultManager contentsOfDirectoryAtURL:rootsDir
includingPropertiesForKeys:nil
options:0
error:nil];
NSMutableArray<NSString *> *names = [NSMutableArray array];
for (NSURL *entry in entries) {
NSURL *data = [entry URLByAppendingPathComponent:@"data" isDirectory:YES];
NSURL *meta = [entry URLByAppendingPathComponent:@"meta.db" isDirectory:NO];
BOOL isDir = NO;
if ([NSFileManager.defaultManager fileExistsAtPath:data.path isDirectory:&isDir] && isDir &&
[NSFileManager.defaultManager fileExistsAtPath:meta.path])
[names addObject:entry.lastPathComponent];
}
[names sortUsingSelector:@selector(localizedStandardCompare:)];
return names;
}
@interface ISHFileProviderMount ()
@property (nonatomic) struct fakefs_mount storage;
@property (nonatomic, readwrite) NSString *rootName;
@property (nonatomic, readwrite) NSURL *rootURL;
@property (nonatomic, readwrite) NSRecursiveLock *ioLock;
@end
@implementation ISHFileProviderMount
- (nullable instancetype)initWithRootName:(NSString *)rootName error:(NSError **)error {
self = [super init];
if (self == nil)
return nil;
_storage.root_fd = -1;
NSURL *container = ContainerURL();
NSURL *fs_dir = [[container URLByAppendingPathComponent:@"roots"]
URLByAppendingPathComponent:rootName];
_rootName = [rootName copy];
_rootURL = [fs_dir URLByAppendingPathComponent:@"data"];
_ioLock = [[NSRecursiveLock alloc] init];
_storage.source = strdup(_rootURL.fileSystemRepresentation);
_storage.root_fd = open(_storage.source, O_RDONLY | O_DIRECTORY);
if (_storage.root_fd < 0) {
if (error != nil)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
free((void *) _storage.source);
_storage.source = NULL;
return nil;
}
int err = fake_db_init(&_storage.db,
[fs_dir URLByAppendingPathComponent:@"meta.db"].fileSystemRepresentation,
_storage.root_fd);
if (err < 0) {
NSLog(@"error opening root: %d", err);
close(_storage.root_fd);
_storage.root_fd = -1;
free((void *) _storage.source);
_storage.source = NULL;
if (error != nil)
*error = [NSError errorWithISHErrno:err itemIdentifier:NSFileProviderRootContainerItemIdentifier];
return nil;
}
return self;
}
- (struct fakefs_mount *)mount {
return &_storage;
}
- (void)dealloc {
if (_storage.source != NULL) {
free((void *) _storage.source);
_storage.source = NULL;
}
if (_storage.root_fd >= 0) {
close(_storage.root_fd);
_storage.root_fd = -1;
}
if (_storage.db.db != NULL)
fake_db_deinit(&_storage.db);
}
@end
@interface FileProviderExtension () {
}
// One open fakefs mount per installed root, opened lazily. Replaces the old
// single-mount-per-domain model now that one domain hosts every root.
@property (nonatomic) NSMutableDictionary<NSString *, ISHFileProviderMount *> *mountsByRoot;
@end
@implementation FileProviderExtension
- (nullable ISHFileProviderMount *)mountForRootName:(NSString *)rootName error:(NSError **)error {
if (rootName.length == 0) {
if (error != nil)
*error = [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil];
return nil;
}
@synchronized (self) {
if (self.mountsByRoot == nil)
self.mountsByRoot = [NSMutableDictionary dictionary];
ISHFileProviderMount *mountOwner = self.mountsByRoot[rootName];
if (mountOwner != nil)
return mountOwner;
ISHFileProviderRecordBreadcrumb(@"fileprovider.mount.create.begin", ISHFileProviderDetails(rootName));
NSTimeInterval start = NSDate.date.timeIntervalSinceReferenceDate;
mountOwner = [[ISHFileProviderMount alloc] initWithRootName:rootName error:error];
if (mountOwner == nil) {
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(rootName);
details[@"duration_ms"] = ISHFileProviderDurationMilliseconds(start);
if (error != nil && *error != nil)
details[@"error"] = (*error).localizedDescription ?: @"unknown";
ISHFileProviderRecordBreadcrumb(@"fileprovider.mount.create.failed", details);
return nil;
}
self.mountsByRoot[rootName] = mountOwner;
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(rootName);
details[@"duration_ms"] = ISHFileProviderDurationMilliseconds(start);
ISHFileProviderRecordBreadcrumb(@"fileprovider.mount.create.end", details);
return mountOwner;
}
}
- (NSArray<NSString *> *)installedRootNames {
return ISHFileProviderInstalledRootNames();
}
// Periodically removes materialized files whose backing inode no longer exists.
// Runs at most hourly; triggered when the domain root is enumerated.
- (void)maybeCleanupStorage {
NSString *cleanupKey = ISHFileProviderCleanupDefaultsKey(@"all");
NSDate *lastCleanup = [NSUserDefaults.standardUserDefaults objectForKey:cleanupKey] ?: NSDate.distantPast;
if ([NSDate.date timeIntervalSinceDate:lastCleanup] <= 60 * 60 /* 1 hour */)
return;
[self cleanupStorage];
[NSUserDefaults.standardUserDefaults setObject:NSDate.date forKey:cleanupKey];
}
- (NSURL *)storageURL {
NSURL *storage = NSFileProviderManager.defaultManager.documentStorageURL;
if (self.domain != nil)
storage = [storage URLByAppendingPathComponent:self.domain.pathRelativeToDocumentStorage isDirectory:YES];
return storage;
}
- (nullable NSFileProviderItem)itemForIdentifier:(NSFileProviderItemIdentifier)identifier error:(NSError * _Nullable *)error {
NSTimeInterval start = NSDate.date.timeIntervalSinceReferenceDate;
NSString *rootName = ISHFileProviderRootNameForIdentifier(identifier);
if (rootName == nil) {
// The domain root container: a synthetic folder listing the roots.
return [[FileProviderItem alloc] initWithIdentifier:NSFileProviderRootContainerItemIdentifier mountOwner:nil error:error];
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:error];
if (mountOwner == nil) {
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(rootName);
details[@"identifier"] = identifier ?: @"";
details[@"duration_ms"] = ISHFileProviderDurationMilliseconds(start);
if (error != nil && *error != nil)
details[@"error"] = (*error).localizedDescription ?: @"unknown";
ISHFileProviderRecordBreadcrumb(@"fileprovider.item.lookup.failed", details);
return nil;
}
NSFileProviderItemIdentifier inner = ISHFileProviderInnerIdentifier(identifier);
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, NO);
@try {
NSLog(@"item for id %@", identifier);
NSError *err;
FileProviderItem *item = [[FileProviderItem alloc] initWithIdentifier:inner mountOwner:mountOwner error:&err];
if (item == nil) {
if (error != nil)
*error = err;
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(rootName);
details[@"identifier"] = identifier ?: @"";
details[@"duration_ms"] = ISHFileProviderDurationMilliseconds(start);
details[@"error"] = err.localizedDescription ?: @"unknown";
ISHFileProviderRecordBreadcrumb(@"fileprovider.item.lookup.failed", details);
return nil;
}
NSNumber *duration = ISHFileProviderDurationMilliseconds(start);
if (duration.integerValue >= 200) {
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(rootName);
details[@"identifier"] = identifier ?: @"";
details[@"duration_ms"] = duration;
ISHFileProviderRecordBreadcrumb(@"fileprovider.item.lookup.slow", details);
}
return item;
} @finally {
ISHAppGroupReleaseLock(rootLockFd);
}
}
- (nullable NSURL *)URLForItemWithPersistentIdentifier:(NSFileProviderItemIdentifier)identifier {
if ([identifier isEqualToString:NSFileProviderRootContainerItemIdentifier])
return self.storageURL;
FileProviderItem *item = [self itemForIdentifier:identifier error:nil];
if (item == nil)
return nil;
NSURL *url = [self.storageURL URLByAppendingPathComponent:identifier isDirectory:YES];
url = [url URLByAppendingPathComponent:item.path.lastPathComponent isDirectory:NO];
NSLog(@"url for id %@ = %@", identifier, url);
return url;
}
- (nullable NSFileProviderItemIdentifier)persistentIdentifierForItemAtURL:(NSURL *)url {
if ([url.URLByDeletingLastPathComponent isEqual:NSFileProviderManager.defaultManager.documentStorageURL]) {
return NSFileProviderRootContainerItemIdentifier;
}
NSString *identifier = url.pathComponents[url.pathComponents.count - 2];
// Identifiers are now "<root>" or "<root>+<inner>" (a single path component),
// so the old "must be a decimal inode or virt_" check no longer applies.
if (identifier.length == 0)
return nil;
NSLog(@"id for url %@ = %@", url, identifier);
return identifier;
}
- (BOOL)enhanceSanityOfURL:(NSURL *)url error:(NSError **)error {
NSURL *dir = url.URLByDeletingLastPathComponent;
NSFileManager *manager = NSFileManager.defaultManager;
BOOL isDir;
if ([manager fileExistsAtPath:dir.path isDirectory:&isDir] && !isDir)
[manager removeItemAtURL:dir error:nil];
return [manager createDirectoryAtURL:dir
withIntermediateDirectories:YES
attributes:nil
error:error];
}
- (void)providePlaceholderAtURL:(NSURL *)url completionHandler:(void (^)(NSError * _Nullable error))completionHandler {
NSError *err;
FileProviderItem *item = [self itemForIdentifier:[self persistentIdentifierForItemAtURL:url] error:&err];
if (item == nil) {
completionHandler(err);
return;
}
if (![self enhanceSanityOfURL:url error:&err]) {
completionHandler(err);
return;
}
if (![NSFileProviderManager writePlaceholderAtURL:[NSFileProviderManager placeholderURLForURL:url]
withMetadata:item
error:&err]) {
completionHandler(err);
return;
}
completionHandler(nil);
}
- (void)startProvidingItemAtURL:(NSURL *)url completionHandler:(void (^)(NSError *))completionHandler {
// Should ensure that the actual file is in the position returned by URLForItemWithIdentifier:, then call the completion handler
NSError *err;
FileProviderItem *item = [self itemForIdentifier:[self persistentIdentifierForItemAtURL:url] error:&err];
if (item == nil) {
completionHandler(err);
return;
}
if (![self enhanceSanityOfURL:url error:&err]) {
completionHandler(err);
return;
}
[item loadToURL:url];
completionHandler(nil);
}
- (void)itemChangedAtURL:(NSURL *)url {
FileProviderItem *item = [self itemForIdentifier:[self persistentIdentifierForItemAtURL:url] error:nil];
if (item == nil)
return;
[item saveFromURL:url];
}
#pragma mark - Action helpers
// FIXME: not dry enough
// These helpers operate on a specific root's mount, resolved by the caller from
// the parent/item identifier.
- (BOOL)doCreateDirectoryAt:(NSString *)path mount:(struct fakefs_mount *)mount inode:(ino_t *)inode error:(NSError **)error {
NSURL *url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:mount->source]] URLByAppendingPathComponent:path];
if (![NSFileManager.defaultManager createDirectoryAtURL:url
withIntermediateDirectories:NO
attributes:@{NSFilePosixPermissions: @0777}
error:error]) {
return NO;
}
db_begin_write(&mount->db);
struct ish_stat stat;
NSString *parentPath = [path substringToIndex:[path rangeOfString:@"/" options:NSBackwardsSearch].location];
if (!path_read_stat(&mount->db, parentPath.fileSystemRepresentation, &stat, NULL)) {
db_rollback(&mount->db);
[NSFileManager.defaultManager removeItemAtURL:url error:nil];
if (error != nil)
*error = [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil];
return NO;
}
stat.mode = (stat.mode & ~S_IFMT) | S_IFDIR;
path_create(&mount->db, path.fileSystemRepresentation, &stat);
if (inode != NULL)
*inode = path_get_inode(&mount->db, path.fileSystemRepresentation);
db_commit(&mount->db);
return YES;
}
- (BOOL)doCreateFileAt:(NSString *)path importFrom:(NSURL *)importURL mount:(struct fakefs_mount *)mount inode:(ino_t *)inode error:(NSError **)error {
NSURL *url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:mount->source]] URLByAppendingPathComponent:path];
if (![NSFileManager.defaultManager copyItemAtURL:importURL
toURL:url
error:error]) {
return NO;
}
db_begin_write(&mount->db);
struct ish_stat stat;
NSString *parentPath = [path substringToIndex:[path rangeOfString:@"/" options:NSBackwardsSearch].location];
if (!path_read_stat(&mount->db, parentPath.fileSystemRepresentation, &stat, NULL)) {
db_rollback(&mount->db);
[NSFileManager.defaultManager removeItemAtURL:url error:nil];
if (error != nil)
*error = [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil];
return NO;
}
stat.mode = (stat.mode & ~S_IFMT & ~0111) | S_IFREG;
path_create(&mount->db, path.fileSystemRepresentation, &stat);
if (inode != NULL)
*inode = path_get_inode(&mount->db, path.fileSystemRepresentation);
db_commit(&mount->db);
return YES;
}
- (NSString *)pathOfItemWithIdentifier:(NSFileProviderItemIdentifier)identifier error:(NSError **)error {
FileProviderItem *parent = [self itemForIdentifier:identifier error:error];
if (parent == nil)
return nil;
return parent.path;
}
#pragma mark - Actions
/* TODO: implement the actions for items here
each of the actions follows the same pattern:
- make a note of the change in the local model
- schedule a server request as a background task to inform the server of the change
- call the completion block with the modified item in its post-modification state
*/
- (void)createDirectoryWithName:(NSString *)directoryName inParentItemIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier completionHandler:(void (^)(NSFileProviderItem _Nullable, NSError * _Nullable))completionHandler {
NSError *error;
NSString *rootName = ISHFileProviderRootNameForIdentifier(parentItemIdentifier);
if (rootName == nil) {
// The top level lists roots; you can't create a directory directly there.
completionHandler(nil, [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil]);
return;
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:&error];
if (mountOwner == nil) {
completionHandler(nil, error);
return;
}
NSString *parentPath = [self pathOfItemWithIdentifier:parentItemIdentifier error:&error];
if (parentPath == nil) {
completionHandler(nil, error);
return;
}
ino_t inode;
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, YES);
[mountOwner.ioLock lock];
BOOL worked = [self doCreateDirectoryAt:[parentPath stringByAppendingFormat:@"/%@", directoryName] mount:mountOwner.mount inode:&inode error:&error];
[mountOwner.ioLock unlock];
ISHAppGroupReleaseLock(rootLockFd);
if (!worked) {
completionHandler(nil, error);
return;
}
NSFileProviderItemIdentifier newIdentifier = ISHFileProviderComposeIdentifier(rootName, [NSString stringWithFormat:@"%lu", (unsigned long) inode]);
FileProviderItem *item = [self itemForIdentifier:newIdentifier error:&error];
if (item == nil)
completionHandler(nil, error);
else
completionHandler(item, nil);
}
- (void)importDocumentAtURL:(NSURL *)fileURL toParentItemIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier completionHandler:(void (^)(NSFileProviderItem _Nullable, NSError * _Nullable))completionHandler {
NSError *error;
NSString *rootName = ISHFileProviderRootNameForIdentifier(parentItemIdentifier);
if (rootName == nil) {
completionHandler(nil, [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil]);
return;
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:&error];
if (mountOwner == nil) {
completionHandler(nil, error);
return;
}
NSString *parentPath = [self pathOfItemWithIdentifier:parentItemIdentifier error:&error];
if (parentPath == nil) {
completionHandler(nil, error);
return;
}
[fileURL startAccessingSecurityScopedResource];
BOOL isDir;
assert([NSFileManager.defaultManager fileExistsAtPath:fileURL.path isDirectory:&isDir] && !isDir);
ino_t inode;
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, YES);
[mountOwner.ioLock lock];
BOOL worked = [self doCreateFileAt:[parentPath stringByAppendingFormat:@"/%@", fileURL.lastPathComponent]
importFrom:fileURL
mount:mountOwner.mount
inode:&inode
error:&error];
[mountOwner.ioLock unlock];
ISHAppGroupReleaseLock(rootLockFd);
[fileURL stopAccessingSecurityScopedResource];
if (!worked) {
completionHandler(nil, error);
return;
}
NSFileProviderItemIdentifier newIdentifier = ISHFileProviderComposeIdentifier(rootName, [NSString stringWithFormat:@"%lu", (unsigned long) inode]);
FileProviderItem *item = [self itemForIdentifier:newIdentifier error:&error];
if (item == nil)
completionHandler(nil, error);
else
completionHandler(item, nil);
}
- (NSString *)pathFromURL:(NSURL *)url mount:(struct fakefs_mount *)mount {
NSURL *root = [NSURL fileURLWithPath:[NSString stringWithUTF8String:mount->source]];
assert([url.path hasPrefix:root.path]);
NSString *path = [url.path substringFromIndex:root.path.length];
assert([path hasPrefix:@"/"]);
if ([path hasSuffix:@"/"])
path = [path substringToIndex:path.length - 1];
return path;
}
- (BOOL)doDelete:(NSString *)path mount:(struct fakefs_mount *)mount itemIdentifier:(NSFileProviderItemIdentifier)identifier error:(NSError **)error {
NSURL *url = [[NSURL fileURLWithPath:[NSString stringWithUTF8String:mount->source]] URLByAppendingPathComponent:path];
NSDirectoryEnumerator<NSURL *> *enumerator = [NSFileManager.defaultManager enumeratorAtURL:url
includingPropertiesForKeys:nil
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
errorHandler:nil];
for (NSURL *suburl in enumerator) {
if (![self doDelete:[self pathFromURL:suburl mount:mount] mount:mount itemIdentifier:identifier error:error])
return NO;
}
db_begin_write(&mount->db);
path_unlink(&mount->db, path.fileSystemRepresentation);
int err = unlinkat(mount->root_fd, fix_path(path.fileSystemRepresentation), 0);
if (err < 0)
err = unlinkat(mount->root_fd, fix_path(path.fileSystemRepresentation), AT_REMOVEDIR);
if (err < 0) {
db_rollback(&mount->db);
if (error != nil)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
return NO;
}
db_commit(&mount->db);
return YES;
}
- (void)deleteItemWithIdentifier:(NSFileProviderItemIdentifier)itemIdentifier completionHandler:(void (^)(NSError * _Nullable))completionHandler {
NSError *error;
NSString *rootName = ISHFileProviderRootNameForIdentifier(itemIdentifier);
if (rootName == nil || [ISHFileProviderInnerIdentifier(itemIdentifier) isEqualToString:NSFileProviderRootContainerItemIdentifier]) {
// Don't allow deleting the top level or an entire root through Files.
completionHandler([NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil]);
return;
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:&error];
if (mountOwner == nil) {
completionHandler(error);
return;
}
NSString *path = [self pathOfItemWithIdentifier:itemIdentifier error:&error];
if (path == nil) {
completionHandler(error);
return;
}
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, YES);
[mountOwner.ioLock lock];
BOOL worked = [self doDelete:path mount:mountOwner.mount itemIdentifier:itemIdentifier error:&error];
[mountOwner.ioLock unlock];
ISHAppGroupReleaseLock(rootLockFd);
if (!worked)
completionHandler(error);
else
completionHandler(nil);
}
- (BOOL)doRename:(NSString *)src to:(NSString *)dst mount:(struct fakefs_mount *)mount itemIdentifier:(NSFileProviderItemIdentifier)identifier error:(NSError **)error {
db_begin_write(&mount->db);
path_rename(&mount->db, src.fileSystemRepresentation, dst.fileSystemRepresentation);
int err = renameat(mount->root_fd, fix_path(src.fileSystemRepresentation), mount->root_fd, fix_path(dst.fileSystemRepresentation));
if (err < 0) {
db_rollback(&mount->db);
if (error != nil)
*error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:nil];
return NO;
}
db_commit(&mount->db);
return YES;
}
- (void)renameItemWithIdentifier:(NSFileProviderItemIdentifier)itemIdentifier toName:(NSString *)itemName completionHandler:(void (^)(NSFileProviderItem _Nullable, NSError * _Nullable))completionHandler {
NSError *error;
NSString *rootName = ISHFileProviderRootNameForIdentifier(itemIdentifier);
if (rootName == nil || [ISHFileProviderInnerIdentifier(itemIdentifier) isEqualToString:NSFileProviderRootContainerItemIdentifier]) {
completionHandler(nil, [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil]);
return;
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:&error];
if (mountOwner == nil) {
completionHandler(nil, error);
return;
}
FileProviderItem *item = [self itemForIdentifier:itemIdentifier error:&error];
if (item == nil) {
completionHandler(nil, error);
return;
}
NSString *dstPath = [item.path.stringByDeletingLastPathComponent stringByAppendingPathComponent:itemName];
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, YES);
[mountOwner.ioLock lock];
BOOL worked = [self doRename:item.path to:dstPath mount:mountOwner.mount itemIdentifier:itemIdentifier error:&error];
[mountOwner.ioLock unlock];
ISHAppGroupReleaseLock(rootLockFd);
if (!worked) {
completionHandler(nil, error);
return;
}
completionHandler(item, nil);
}
- (void)reparentItemWithIdentifier:(NSFileProviderItemIdentifier)itemIdentifier toParentItemWithIdentifier:(NSFileProviderItemIdentifier)parentItemIdentifier newName:(NSString *)newName completionHandler:(void (^)(NSFileProviderItem _Nullable, NSError * _Nullable))completionHandler {
NSError *error;
NSString *rootName = ISHFileProviderRootNameForIdentifier(itemIdentifier);
NSString *parentRootName = ISHFileProviderRootNameForIdentifier(parentItemIdentifier);
if (rootName == nil || parentRootName == nil || ![rootName isEqualToString:parentRootName]) {
// Moving across roots (or to/from the top level) isn't supported.
completionHandler(nil, [NSError errorWithDomain:NSFileProviderErrorDomain code:NSFileProviderErrorNoSuchItem userInfo:nil]);
return;
}
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:&error];
if (mountOwner == nil) {
completionHandler(nil, error);
return;
}
FileProviderItem *item = [self itemForIdentifier:itemIdentifier error:&error];
if (item == nil) {
completionHandler(nil, error);
return;
}
FileProviderItem *parent = [self itemForIdentifier:parentItemIdentifier error:&error];
if (parent == nil) {
completionHandler(nil, error);
return;
}
if (newName == nil)
newName = item.path.lastPathComponent;
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, YES);
[mountOwner.ioLock lock];
BOOL worked = [self doRename:item.path to:[parent.path stringByAppendingPathComponent:newName] mount:mountOwner.mount itemIdentifier:itemIdentifier error:&error];
[mountOwner.ioLock unlock];
ISHAppGroupReleaseLock(rootLockFd);
if (!worked) {
completionHandler(nil, error);
return;
}
completionHandler(item, nil);
}
#pragma mark - Enumeration
- (nullable id<NSFileProviderEnumerator>)enumeratorForContainerItemIdentifier:(NSFileProviderItemIdentifier)containerItemIdentifier error:(NSError **)error {
FileProviderItem *item = [self itemForIdentifier:containerItemIdentifier error:error];
if (item == nil)
return nil;
if (item.mountOwner == nil) // the domain root: opportune moment to GC storage
[self maybeCleanupStorage];
return [[FileProviderEnumerator alloc] initWithItem:item extension:self];
}
#pragma mark - Storage deletion
// According to an engineer I talked to at WWDC, -stopProvidingItemAtURL: is never ever called, so that can't be used to free up disk space.
// Solution for now is to periodically look for and delete files in file provider storage where the original is missing.
// TODO: Delete files in file provider storage when the original file is deleted
// TODO: Create hardlinks into file provider storage instead of copies
//
- (void)cleanupStorage {
NSTimeInterval start = NSDate.date.timeIntervalSinceReferenceDate;
NSFileManager *manager = NSFileManager.defaultManager;
NSArray<NSURL *> *storageDirs = [manager contentsOfDirectoryAtURL:self.storageURL includingPropertiesForKeys:nil options:0 error:nil];
NSUInteger removedCount = 0;
for (NSURL *dir in storageDirs) {
// Materialized storage dirs are named by the fully-scoped identifier,
// "<root>+<inode>". Root folders and virtual items have no inode and are
// left alone.
NSString *fullIdentifier = dir.lastPathComponent;
NSString *rootName = ISHFileProviderRootNameForIdentifier(fullIdentifier);
NSFileProviderItemIdentifier inner = ISHFileProviderInnerIdentifier(fullIdentifier);
inode_t inode = inner.longLongValue;
if (rootName == nil || inode == 0)
continue;
ISHFileProviderMount *mountOwner = [self mountForRootName:rootName error:nil];
if (mountOwner == nil)
continue;
int rootLockFd = ISHFileProviderAcquireRootLock(rootName, NO);
BOOL exists = inode_exists(&mountOwner.mount->db, inode);
ISHAppGroupReleaseLock(rootLockFd);
if (!exists) {
NSLog(@"removing dead inode %llu in %@", inode, rootName);
NSError *err;
if (![manager removeItemAtURL:dir error:&err])
NSLog(@"failed to remove dead inode: %@", err);
else
removedCount++;
}
}
NSNumber *duration = ISHFileProviderDurationMilliseconds(start);
if (removedCount != 0 || duration.integerValue >= 500) {
NSMutableDictionary<NSString *, id> *details = ISHFileProviderDetails(@"all");
details[@"duration_ms"] = duration;
details[@"removed"] = @(removedCount);
ISHFileProviderRecordBreadcrumb(@"fileprovider.cleanup", details);
}
}
// Dead code, leaving it here just in case
- (void)stopProvidingItemAtURL:(NSURL *)url {
FileProviderItem *item = [self itemForIdentifier:[self persistentIdentifierForItemAtURL:url] error:nil];
if (item == nil)
return;
[item saveFromURL:url];
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
[NSFileProviderManager writePlaceholderAtURL:[NSFileProviderManager placeholderURLForURL:url]
withMetadata:item
error:nil];
}
@end
void die(const char *msg, ...) {
va_list args;
va_start(args, msg);
[NSException raise:@"ish die" format:[NSString stringWithUTF8String:msg] arguments:args];
abort();
va_end(args);
}
void ish_printk(const char *msg, ...) {
va_list args;
va_start(args, msg);
NSLogv([NSString stringWithUTF8String:msg], args);
va_end(args);
}