forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iOSFS.m
450 lines (349 loc) · 14.6 KB
/
iOSFS.m
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
//
// iOSFS.m
// iSH
//
// Created by Noah Peeters on 26.10.19.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <sys/stat.h>
#include "SceneDelegate.h"
#include "iOSFS.h"
#include "kernel/fs.h"
#include "kernel/errno.h"
#include "fs/path.h"
#include "fs/real.h"
const NSFileCoordinatorWritingOptions NSFileCoordinatorWritingForCreating = NSFileCoordinatorWritingForMerging;
@interface DirectoryPickerDelegate : NSObject <UIDocumentPickerDelegate>
@property NSArray<NSURL *> *urls;
@property lock_t lock;
@property cond_t cond;
@end
@implementation DirectoryPickerDelegate
- (instancetype)init {
if (self = [super init]) {
lock_init(&_lock);
cond_init(&_cond);
}
return self;
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
[self documentPicker:controller didPickDocumentsAtURLs:@[]];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
lock(&_lock);
self.urls = urls;
notify(&_cond);
unlock(&_lock);
}
- (NSArray<NSURL *> *)waitForUrls {
lock(&_lock);
while (_urls == nil)
wait_for(&_cond, &_lock, NULL);
unlock(&_lock);
return _urls;
}
- (void)dealloc {
cond_destroy(&_cond);
}
@end
NSURL *url_for_mount(struct mount *mount) {
return (__bridge NSURL *) mount->data;
}
NSURL *url_for_path_in_mount(struct mount *mount, const char *path) {
return [[url_for_mount(mount) URLByAppendingPathComponent:[NSString stringWithUTF8String:path]] URLByStandardizingPath];
}
const char *path_for_url_in_mount(struct mount *mount, NSURL *url, const char *fallback) {
NSString *mount_path = [[url_for_mount(mount) URLByStandardizingPath] path];
NSString *url_path = [[url URLByStandardizingPath] path];
// The `/private` prefix is a special case as described in the documentation of `URLByStandardizingPath`.
if ([mount_path hasPrefix:@"/private/"]) mount_path = [mount_path substringFromIndex:8];
if ([url_path hasPrefix:@"/private/"]) url_path = [url_path substringFromIndex:8];
if (![url_path hasPrefix:mount_path])
return fallback;
NSURL *new_url = [NSURL fileURLWithPath:[url_path substringFromIndex:[mount_path length]]];
return [new_url ? [new_url path] : @"" cStringUsingEncoding:NSUTF8StringEncoding];
}
static int iosfs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat);
const struct fd_ops iosfs_fdops;
static int posixErrorFromNSError(NSError *error) {
if (error != nil) {
NSError *underlyingError = [error.userInfo objectForKey:NSUnderlyingErrorKey];
if (underlyingError) {
return -(int)underlyingError.code
;
} else {
return _EPERM;
}
}
return 0;
}
static int combine_error(NSError *coordinatorError, int err) {
int posix_error = posixErrorFromNSError(coordinatorError);
return posix_error ? posix_error : err;
}
static struct fd *iosfs_open(struct mount *mount, const char *path, int flags, int mode) {
NSURL *url = url_for_path_in_mount(mount, path);
struct statbuf stats;
int err = iosfs_stat(mount, path, &stats);
if (err == 0 && S_ISREG(stats.mode)) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
__block NSError *error;
__block struct fd *fd;
__block dispatch_semaphore_t file_opened = dispatch_semaphore_create(0);
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
void (^operation)(NSURL *url) = ^(NSURL *url) {
fd = realfs_open(mount, path_for_url_in_mount(mount, url, path), flags, mode);
fd->ops = &iosfs_fdops;
if (IS_ERR(fd)) {
dispatch_semaphore_signal(file_opened);
} else {
dispatch_semaphore_t file_closed = dispatch_semaphore_create(0);
fd->data = (__bridge void *) file_closed;
dispatch_semaphore_signal(file_opened);
dispatch_semaphore_wait(file_closed, DISPATCH_TIME_FOREVER);
}
};
if (!(flags & O_WRONLY_) && !(flags & O_RDWR_)) {
[coordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:operation];
} else if (flags & O_CREAT_) {
[coordinator coordinateWritingItemAtURL:url options:NSFileCoordinatorWritingForCreating error:&error byAccessor:operation];
} else {
[coordinator coordinateWritingItemAtURL:url options:NSFileCoordinatorWritingForMerging error:&error byAccessor:operation];
}
});
dispatch_semaphore_wait(file_opened, DISPATCH_TIME_FOREVER);
int posix_error = posixErrorFromNSError(error);
return posix_error ? ERR_PTR(posix_error) : fd;
}
struct fd *fd = realfs_open(mount, path, flags, mode);
fd->ops = &iosfs_fdops;
return fd;
}
int iosfs_close(struct fd *fd) {
int err = realfs.close(fd);
return err;
}
static int iosfs_ask_for_url(NSURL **url) {
TerminalViewController *terminalViewController = currentTerminalViewController;
if (!terminalViewController)
return _ENODEV;
DirectoryPickerDelegate *pickerDelegate = [DirectoryPickerDelegate new];
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[ @"public.folder" ] inMode:UIDocumentPickerModeOpen];
picker.delegate = pickerDelegate;
[terminalViewController presentViewController:picker animated:true completion:nil];
});
NSArray<NSURL *> *urls = [pickerDelegate waitForUrls];
if (urls.count == 0)
return _ENODEV;
*url = urls[0];
return 0;
}
static int iosfs_mount(struct mount *mount) {
NSURL *url;
int err = iosfs_ask_for_url(&url);
if (err)
return err;
// Overwrite url & base path
mount->data = (void *)CFBridgingRetain(url);
mount->source = strdup([[url path] UTF8String]);
if ([url startAccessingSecurityScopedResource] == NO) {
CFBridgingRelease(mount->data);
return _EPERM;
}
return realfs.mount(mount);
}
static int iosfs_umount(struct mount *mount) {
NSURL *url = url_for_mount(mount);
[url stopAccessingSecurityScopedResource];
CFBridgingRelease(mount->data);
return 0;
}
static int iosfs_rename(struct mount *mount, const char *src, const char *dst) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *src_url = url_for_path_in_mount(mount, src);
NSURL *dst_url = url_for_path_in_mount(mount, dst);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:src_url options:NSFileCoordinatorWritingForMoving error:&error byAccessor:^(NSURL *url) {
[coordinator itemAtURL:url willMoveToURL:dst_url];
err = realfs.rename(mount, path_for_url_in_mount(mount, url, src), dst);
[coordinator itemAtURL:url didMoveToURL:dst_url];
}];
return combine_error(error, err);
}
static int iosfs_symlink(struct mount *mount, const char *target, const char *link) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *dst_url = url_for_path_in_mount(mount, link);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:dst_url options:NSFileCoordinatorWritingForCreating error:&error byAccessor:^(NSURL *url) {
err = realfs.symlink(mount, path_for_url_in_mount(mount, url, target), link);
}];
return combine_error(error, err);
}
static int iosfs_mknod(struct mount *mount, const char *path, mode_t_ mode, dev_t_ dev) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingForCreating error:&error byAccessor:^(NSURL *url) {
err = realfs.mknod(mount, path_for_url_in_mount(mount, url, path), mode, dev);
}];
return combine_error(error, err);
}
static int iosfs_setattr(struct mount *mount, const char *path, struct attr attr) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingContentIndependentMetadataOnly error:&error byAccessor:^(NSURL *url) {
err = realfs.setattr(mount, path_for_url_in_mount(mount, url, path), attr);
}];
return combine_error(error, err);
}
static int iosfs_fsetattr(struct fd *fd, struct attr attr) {
return realfs.fsetattr(fd, attr);
}
static ssize_t iosfs_readlink(struct mount *mount, const char *path, char *buf, size_t bufsize) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block ssize_t size;
[coordinator coordinateReadingItemAtURL:in_url options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *url) {
size = realfs.readlink(mount, path_for_url_in_mount(mount, url, path), buf, bufsize);
}];
int posix_error = posixErrorFromNSError(error);
return posix_error ? posix_error : size;
}
static int iosfs_getpath(struct fd *fd, char *buf) {
return realfs.getpath(fd, buf);
}
static int iosfs_link(struct mount *mount, const char *src, const char *dst) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *dst_url = url_for_path_in_mount(mount, dst);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:dst_url options:NSFileCoordinatorWritingForCreating error:&error byAccessor:^(NSURL *url) {
err = realfs.link(mount, src, path_for_url_in_mount(mount, url, dst));
}];
return combine_error(error, err);
}
static int iosfs_unlink(struct mount *mount, const char *path) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingForDeleting error:&error byAccessor:^(NSURL *url) {
err = realfs.unlink(mount, path_for_url_in_mount(mount, url, path));
}];
return combine_error(error, err);
}
static int iosfs_rmdir(struct mount *mount, const char *path) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingForDeleting error:&error byAccessor:^(NSURL *url) {
err = realfs.rmdir(mount, path_for_url_in_mount(mount, url, path));
}];
return combine_error(error, err);
}
static int iosfs_stat(struct mount *mount, const char *path, struct statbuf *fake_stat) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateReadingItemAtURL:in_url options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *url) {
err = realfs.stat(mount, path_for_url_in_mount(mount, url, path), fake_stat);
}];
return combine_error(error, err);
}
static int iosfs_fstat(struct fd *fd, struct statbuf *fake_stat) {
int err = realfs.fstat(fd, fake_stat);
return err;
}
static int iosfs_utime(struct mount *mount, const char *path, struct timespec atime, struct timespec mtime) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingContentIndependentMetadataOnly error:&error byAccessor:^(NSURL *url) {
err = realfs.utime(mount, path_for_url_in_mount(mount, url, path), atime, mtime);
}];
return combine_error(error, err);
}
static int iosfs_mkdir(struct mount *mount, const char *path, mode_t_ mode) {
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSURL *in_url = url_for_path_in_mount(mount, path);
NSError *error;
__block int err;
[coordinator coordinateWritingItemAtURL:in_url options:NSFileCoordinatorWritingForCreating error:&error byAccessor:^(NSURL *url) {
err = realfs.mkdir(mount, path_for_url_in_mount(mount, url, path), mode);
}];
return combine_error(error, err);
}
static int iosfs_flock(struct fd *fd, int operation) {
return realfs.flock(fd, operation);
}
const struct fs_ops iosfs = {
.name = "ios", .magic = 0x7265616d,
.mount = iosfs_mount,
.umount = iosfs_umount,
.statfs = realfs_statfs,
.open = iosfs_open,
.readlink = iosfs_readlink,
.link = iosfs_link,
.unlink = iosfs_unlink,
.rmdir = iosfs_rmdir,
.rename = iosfs_rename,
.symlink = iosfs_symlink,
.mknod = iosfs_mknod,
.close = iosfs_close,
.stat = iosfs_stat,
.fstat = iosfs_fstat,
.setattr = iosfs_setattr,
.fsetattr = iosfs_fsetattr,
.utime = iosfs_utime,
.getpath = iosfs_getpath,
.flock = iosfs_flock,
.mkdir = iosfs_mkdir,
};
const struct fs_ops iosfs_unsafe = {
.name = "ios-unsafe", .magic = 0x7265616e,
.mount = iosfs_mount,
.umount = iosfs_umount,
.statfs = realfs_statfs,
.open = realfs_open,
.readlink = realfs_readlink,
.link = realfs_link,
.unlink = realfs_unlink,
.rmdir = realfs_rmdir,
.rename = realfs_rename,
.symlink = realfs_symlink,
.mknod = realfs_mknod,
.close = realfs_close,
.stat = realfs_stat,
.fstat = realfs_fstat,
.setattr = realfs_setattr,
.fsetattr = realfs_fsetattr,
.utime = realfs_utime,
.getpath = realfs_getpath,
.flock = realfs_flock,
.mkdir = realfs_mkdir,
};
const struct fd_ops iosfs_fdops = {
.read = realfs_read,
.write = realfs_write,
.readdir = realfs_readdir,
.telldir = realfs_telldir,
.seekdir = realfs_seekdir,
.lseek = realfs_lseek,
.mmap = realfs_mmap,
.poll = realfs_poll,
.fsync = realfs_fsync,
.close = iosfs_close,
.getflags = realfs_getflags,
.setflags = realfs_setflags,
};