简体   繁体   English

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

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

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

You can use -[NSURL getResourceValue:forKey:error:] with the key NSURLVolumeIsReadOnlyKey . 您可以使用-[NSURL getResourceValue:forKey:error:]和密钥NSURLVolumeIsReadOnlyKey You would apply this to the app bundle URL as returned by [[NSBundle mainBundle] bundleURL] . 您可以将此应用于[[NSBundle mainBundle] bundleURL]返回的应用程序包URL。 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
}

If OSX is POSIX compliant, to determine if filesystem is mounted R/O, You can use statvfs() or fstatvfs() , returned struct statvfs field f_flag should have ST_RDONLY bit set for R/O filesystem. 如果OSX与POSIX兼容,要确定文件系统是否已挂载R / O,您可以使用statvfs()fstatvfs() ,返回的struct statvfs字段f_flag应该为R / O文件系统设置ST_RDONLY位。

As it was pointed in comments, check if this information is correctly provided by OS. 正如评论中指出的那样,检查操作系统是否正确提供了此信息。

JNA and this question may be usefull for Java. JNA这个问题可能对Java有用。

A few more ideas, which may be usefull here ( access() , open() , utime() ). 还有一些想法,可能在这里很有用( access()open()utime() )。

OS X specific statfs() may be used too, but this function is not portable (Linux and *BSD have slightly different statfs() functions). OS X特定的statfs()也可以使用,但是这个函数不可移植(Linux和* BSD的statfs()函数略有不同)。

You can also check directly from Java whether a certain path points to something within a read-only directory by querying the FileStore associated with your path: 您还可以通过查询与您的路径关联的FileStore ,直接从Java检查某个路径是否指向只读目录中的某些内容:

 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio中的只读文件系统 - Read-only file system in android studio 为什么 System.getProperty("java.io.tmpdir") 在 Mac OS X Catalina 10.15 上返回只读目录 - Why does System.getProperty("java.io.tmpdir") return a read-only directory on Mac OS X Catalina 10.15 尝试写入文件,获取FileNotFoundException(只读文件系统) - Trying to write to a file, getting FileNotFoundException (Read-only file system) Android打开失败:EROFS(只读文件系统)错误 - Android open failed: EROFS (Read-only file system) error java.io.IOException:只读文件系统 - java.io.IOException: Read-only file system java.io.FileNotFoundException: (只读文件系统) Mac - java.io.FileNotFoundException: (Read-only file system) Mac 为什么文件是只读的? - Why is the file Read-Only? 创建只读文件 - Create a read-only file Android文件系统:java.io.FileNotFoundException:/ savedArticlesFile:打开失败:EROFS(只读文件系统) - Android file system: java.io.FileNotFoundException: /savedArticlesFile: open failed: EROFS (Read-only file system) 使用getFilesDir()写入文件给出'只读文件系统'错误(Android) - Writing to file using getFilesDir() giving 'Read-only file system' error (Android)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM