检测应用程序是否是从OS X上的只读文件系统启动的

Tho*_* S. 8 java macos dmg

我想知道用户是否从只读文件系统(如.dmg)启动了基于Java的应用程序,因此自动更新等功能将能够显示有意义的信息,而不是因错误而中止.我首先想到检查.app的路径就足够了(当从.dmg启动时它就像这样/Volumes/MyApp 1.2.3/MyApp.app,但是这不起作用,因为用户可能已经在不同的分区上安装了应用程序.我还可以检查其他什么?

Ken*_*ses 5

您可以使用-[NSURL getResourceValue:forKey:error:]密钥NSURLVolumeIsReadOnlyKey.您可以将此应用于返回的应用包URL [[NSBundle mainBundle] bundleURL].所以:

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
}
Run Code Online (Sandbox Code Playgroud)


Jan*_*sen 1

您还可以通过查询与您的路径关联的路径,直接从 Java 检查某个路径是否指向只读目录中的某些内容FileStore

 File classpathRoot = new File(MyClass.class.getClassLoader().getResource("").getPath());
 /* getPath() actually returns a String instead of a Path object, 
  * so we need to take this little detour */
 Path yourAppPath = classpathRoot.toPath();
 boolean isReadOnly = Files.getFileStore(yourAppPath).isReadOnly();
Run Code Online (Sandbox Code Playgroud)