Skip to content

Commit

Permalink
Bit of a cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Apr 12, 2020
1 parent 52e24ca commit effa9a7
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions app/iOSFS.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

const NSFileCoordinatorWritingOptions NSFileCoordinatorWritingForCreating = NSFileCoordinatorWritingForMerging;

@interface DirectoryPickerDelegate : NSObject <UIDocumentPickerDelegate>
@interface DirectoryPicker : NSObject <UIDocumentPickerDelegate>

@property NSArray<NSURL *> *urls;
@property lock_t lock;
@property cond_t cond;

@end

@implementation DirectoryPickerDelegate
@implementation DirectoryPicker

- (instancetype)init {
if (self = [super init]) {
Expand All @@ -45,12 +45,33 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocum
unlock(&_lock);
}

- (NSArray<NSURL *> *)waitForUrls {
- (int)askForURL:(NSURL **)url {
TerminalViewController *terminalViewController = currentTerminalViewController;
if (!terminalViewController)
return _ENODEV;

dispatch_async(dispatch_get_main_queue(), ^(void) {
UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[ @"public.folder" ] inMode:UIDocumentPickerModeOpen];
picker.delegate = self;
[terminalViewController presentViewController:picker animated:true completion:nil];
});

lock(&_lock);
while (_urls == nil)
wait_for(&_cond, &_lock, NULL);
while (_urls == nil) {
int err = wait_for(&_cond, &_lock, NULL);
if (err < 0) {
unlock(&_lock);
return err;
}
}
NSArray<NSURL *> *urls = _urls;
_urls = nil;
unlock(&_lock);
return _urls;

if (urls.count == 0)
return _ENODEV;
*url = urls[0];
return 0;
}

- (void)dealloc {
Expand Down Expand Up @@ -89,8 +110,7 @@ static int posixErrorFromNSError(NSError *error) {
if (error != nil) {
NSError *underlyingError = [error.userInfo objectForKey:NSUnderlyingErrorKey];
if (underlyingError) {
return -(int)underlyingError.code
;
return -(int)underlyingError.code;
} else {
return _EPERM;
}
Expand Down Expand Up @@ -154,39 +174,24 @@ static int combine_error(NSError *coordinatorError, int err) {

int iosfs_close(struct fd *fd) {
int err = realfs.close(fd);
if (fd->data != NULL) {
dispatch_semaphore_t file_closed = (__bridge dispatch_semaphore_t) fd->data;
dispatch_semaphore_signal(file_closed);
}
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);
DirectoryPicker *picker = [DirectoryPicker new];
int err = [picker askForURL:&url];
if (err)
return err;

// Overwrite url & base path
mount->data = (void *)CFBridgingRetain(url);
free((void *) mount->source);
mount->source = strdup([[url path] UTF8String]);

if ([url startAccessingSecurityScopedResource] == NO) {
Expand Down

0 comments on commit effa9a7

Please sign in to comment.