Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting whether an application was launched from a read-only file system on OS X

Tags:

java

macos

dmg

I want to know whether the user launched our Java-based application from a read-only file system like from a .dmg, so functions like auto-update will be able to show meaningful information instead of aborting with an error. I first thought checking the .app's path would be sufficient (when launched from a .dmg it is something like /Volumes/MyApp 1.2.3/MyApp.app, but this won't work, because the user might have installed the application on a different partition. What other things may I check?

like image 968
Thomas S. Avatar asked Dec 25 '14 10:12

Thomas S.


1 Answers

You can use -[NSURL getResourceValue:forKey:error:] with the key NSURLVolumeIsReadOnlyKey. You would apply this to the app bundle URL as returned by [[NSBundle mainBundle] bundleURL]. So:

NSBundle* bundle = [NSBundle mainBundle];
NSURL* bundleURL = bundle.bundleURL;
NSNumber* readOnly;
NSError* error;
if ([bundleURL getResourceValue:&readOnly forKey:NSURLVolumeIsReadOnlyKey error:&error])
{
    BOOL isReadOnly = [readOnly boolValue];
    // act on isReadOnly value
}
else
{
    // Handle error
}
like image 156
Ken Thomases Avatar answered Oct 17 '22 05:10

Ken Thomases