[英]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.