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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With