确定文件系统是否以只读方式挂载的最佳 POSIX 方法
如果我有 Linux 或 Mac OS X 等 POSIX 系统,那么确定路径是否位于只读文件系统上的最佳且最便携的方法是什么?我可以立即想到 4 种方法:
open(2)
使用O_WRONLY
的文件 - 您需要想出一个唯一的文件名并还传入O_CREAT
和O_EXCL
。如果失败并且您的错误号为EROFS
,那么您就知道它是一个只读文件系统。这会产生实际创建一个您不关心的文件的烦人副作用,但您可以在创建它后立即unlink(2)
它。statvfs(3)
- 返回的struct statvfs
的字段之一是f_flag
,标志之一是ST_RDONLY 用于只读文件系统。但是,
statvfs(3)
的规范明确指出应用程序不能依赖任何包含有效信息的字段。似乎很有可能没有为只读文件系统设置ST_RDONLY
。access(2)
- 如果您知道挂载点,则可以将access(2)
与W_OK
标志一起使用,只要您正在以具有挂载点写入权限的用户身份运行。即,要么您是 root,要么它是使用您的 UID 作为安装参数安装的。您将得到返回值 -1 和错误号EROFS
。解析
/etc/mtab
或/proc/mounts
- 似乎不可移植。例如,Mac OS X 似乎没有这些。即使系统确实有 /etc/mtab ,我也不确定操作系统之间的字段是否一致,或者只读的挂载选项(Linux 上的 ro )是否一致可移植。
我还缺少其他方式吗?如果您需要知道文件系统是否以只读方式安装,您会怎么做?
If I have a POSIX system like Linux or Mac OS X, what's the best and most portable way to determine if a path is on a read-only filesystem? I can think of 4 ways off the top of my head:
open(2)
a file withO_WRONLY
- You would need to come up with a unique filename and also pass inO_CREAT
andO_EXCL
. If it fails and you have an errno ofEROFS
then you know it's a read-only filesystem. This would have the annoying side effect of actually creating a file you didn't care about, but you couldunlink(2)
it immediately after creating it.statvfs(3)
- One of the fields of the returnedstruct statvfs
isf_flag
, and one of the flags isST_RDONLY
for a read-only filesystem. However, the spec forstatvfs(3)
makes it clear that applications cannot depend on any of the fields containing valid information. It would seem there's a decent possibilityST_RDONLY
might not be set for a read-only filesystem.access(2)
- If you know the mount point, you can useaccess(2)
with theW_OK
flag as long as you are running as a user who would have write access to the mountpoint. Ie, either you are root or it was mounted with your UID as a mount parameter. You would get a return value of -1 and an errno ofEROFS
.Parsing
/etc/mtab
or/proc/mounts
- Doesn't seem portable. Mac OS X seems to have neither of these, for example. Even if the system did have/etc/mtab
I'm not sure the fields are consistent between OSes or if the mount options for read-only (ro
on Linux) are portable.
Are there other ways I'm missing? If you needed to know if a filesystem was mounted read-only, how would you do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以
popen
命令mount
并检查输出,查找您的文件系统并查看它是否包含文本" (ro,"
.但同样,这不一定是可移植的。
我的选择是根本不担心文件系统是否以只读方式安装,只需尝试创建文件,如果失败,请告诉用户错误是什么。当然,让他们选择将其保存在其他地方,
无论如何,你确实必须做这样的事情,因为在任何情况下,即使测试和执行之间存在很小的差距,你也可能会发现情况发生了变化(可能没有达到这种程度)。使整个文件系统只读,但谁知道呢,也许有(或将来)有一个文件系统允许这样做)。
You could also
popen
the commandmount
and examine the output looking for your file system and seeing if it held the text" (ro,"
.But again, that's not necessarily portable.
My option would be to not worry about whether the file system was mounted read only at all. Just try and create your file and, if it fails, tell the user what the error was. And, of course, give them the option of saving it somewhere else.
You really have to do that sort of thing anyway since, in any scenario where there's even a small gap between testing and doing, you may find the situation changes (probably not to the extent of making an entire file system read only but, who knows, maybe there is (or will be in the future) a file system that allows this).
如果您有写权限,那么这将为您提供 ROFS,或者(如果允许)只需更新目录上的 mtime,这基本上是无害的。
If you have write perms, then that will give you ROFS or -- if permitted -- simply update the mtime on the directory, which is basically harmless.