简体   繁体   English

最好的POSIX方法来确定文件系统是否以只读方式挂载

[英]Best POSIX way to determine if a filesystem is mounted read only

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? 如果我有像Linux或Mac OS X这样的POSIX系统,那么确定路径是否在只读文件系统上的最佳和最便携的方法是什么? I can think of 4 ways off the top of my head: 我可以想到4个方面:

  • open(2) a file with O_WRONLY - You would need to come up with a unique filename and also pass in O_CREAT and O_EXCL . 使用O_WRONLY open(2)文件 - 您需要提供唯一的文件名并传入O_CREATO_EXCL If it fails and you have an errno of EROFS then you know it's a read-only filesystem. 如果它失败并且你有一个EROFS的错误,那么你知道它是一个只读的文件系统。 This would have the annoying side effect of actually creating a file you didn't care about, but you could unlink(2) it immediately after creating it. 这会产生令人讨厌的副作用,实际上创建一个你不关心的文件,但你可以在创建它后立即unlink(2)

  • statvfs(3) - One of the fields of the returned struct statvfs is f_flag , and one of the flags is ST_RDONLY for a read-only filesystem. statvfs(3) - 返回的struct statvfs一个字段是f_flag ,其中一个标志是ST_RDONLY用于只读文件系统。 However, the spec for statvfs(3) makes it clear that applications cannot depend on any of the fields containing valid information. 但是, statvfs(3)的规范清楚地表明应用程序不能依赖包含有效信息的任何字段。 It would seem there's a decent possibility ST_RDONLY might not be set for a read-only filesystem. 似乎有可能不会为只读文件系统设置ST_RDONLY

  • access(2) - If you know the mount point, you can use access(2) with the W_OK flag as long as you are running as a user who would have write access to the mountpoint. access(2) - 如果您知道挂载点,只要您作为具有对挂载点具有写访问权限的用户运行,就可以将access(2)W_OK标志一起使用。 Ie, either you are root or it was mounted with your UID as a mount parameter. 即,您是root用户还是使用您的UID挂载作为挂载参数。 You would get a return value of -1 and an errno of EROFS . 您将获得-1的返回值和EROFS的错误。

  • Parsing /etc/mtab or /proc/mounts - Doesn't seem portable. 解析/etc/mtab/proc/mounts - 似乎不可移植。 Mac OS X seems to have neither of these, for example. 例如,Mac OS X似乎没有这些。 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. 即使系统确实有/etc/mtab我也不确定操作系统之间的字段是否一致,或者只读(Linux上的ro )的挂载选项是否可移植。

Are there other ways I'm missing? 我还有其他方法吗? If you needed to know if a filesystem was mounted read-only, how would you do it? 如果你需要知道文件系统是否以只读方式挂载,你会怎么做?

utime(path, NULL);

如果你有写权限,那么这将给你ROFS或 - 如果允许 - 只需更新目录上的mtime,这基本上是无害的。

You could also popen the command mount and examine the output looking for your file system and seeing if it held the text " (ro," . 您也可以popen的命令mount和检查输出寻找你的文件系统,看它是否持有文本" (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). 无论如何,你真的必须这样做,因为在测试和执行之间甚至存在小差距的任何情况下,你可能会发现情况发生了变化(可能没有达到使整个文件系统只读的程度但是,谁知道,也许存在(或将来)文件系统允许这样做。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM