Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if Directory is a Mount Point in Objective C in OSX

I am writing a little command line utility in Objective C which will check if a given path is a mount point, and if not, would mount a network share to it. I was going to write this in bash, but opted to try to learn Objective C instead. I am looking for Objective C equivalent of something like this:

mount | grep some_path

Basically a function I can use to test if a given path is currently used as a mount point. Any help would be appreciated. Thank you!

like image 786
Bogdan Avatar asked Sep 25 '13 03:09

Bogdan


1 Answers

After some research I ended up using this code, in case any one needs it in the future:

        NSArray * keys = [NSArray arrayWithObjects:NSURLVolumeURLForRemountingKey, nil];
        NSArray * mountPaths = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:keys options:0];

        NSError * error;
        NSURL * remount;

        for (NSURL * mountPath in mountPaths) {
            [mountPath getResourceValue:&remount forKey:NSURLVolumeURLForRemountingKey error:&error];
            if(remount){
                if ([[[NSURL URLWithString:share] host] isEqualToString:[remount host]] && [[[NSURL URLWithString:share] path] isEqualToString:[remount path]]) {
                    printf("Already mounted at %s\n", [[mountPath path] UTF8String]);
                    return 0;
                }
            }
        }

Note, the NSURL share is passed into the function as the path to the remote share. Filtering by the remount key gives you a list of mountpoint for remote filesystems, as local filesystems do not have that key set.

like image 198
Bogdan Avatar answered Sep 30 '22 06:09

Bogdan