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

发布于 2024-10-15 17:43:28 字数 988 浏览 1 评论 0原文

如果我有 Linux 或 Mac OS X 等 POSIX 系统,那么确定路径是否位于只读文件系统上的最佳且最便携的方法是什么?我可以立即想到 4 种方法:

  • open(2) 使用 O_WRONLY 的文件 - 您需要想出一个唯一的文件名并还传入 O_CREATO_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 with O_WRONLY - You would need to come up with a unique filename and also pass in O_CREAT and O_EXCL. If it fails and you have an errno of EROFS 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 could unlink(2) it immediately after creating it.

  • 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. However, the spec for statvfs(3) makes it clear that applications cannot depend on any of the fields containing valid information. It would seem there's a decent possibility ST_RDONLY might not be set for a read-only filesystem.

  • 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. 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 of EROFS.

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不放开 2024-10-22 17:43:28

您还可以popen命令mount并检查输出,查找您的文件系统并查看它是否包含文本" (ro,".

但同样,这不一定是可移植的。

我的选择是根本不担心文件系统是否以只读方式安装,只需尝试创建文件,如果失败,请告诉用户错误是什么。当然,让他们选择将其保存在其他地方,

无论如何,你确实必须做这样的事情,因为在任何情况下,即使测试和执行之间存在很小的差距,你也可能会发现情况发生了变化(可能没有达到这种程度)。使整个文件系统只读,但谁知道呢,也许有(或将来)有一个文件系统允许这样做)。

You could also popen the command mount 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).

一个人的旅程 2024-10-22 17:43:28
utime(path, NULL);

如果您有写权限,那么这将为您提供 ROFS,或者(如果允许)只需更新目录上的 mtime,这基本上是无害的。

utime(path, NULL);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文