如何使用std ::文件系统查看我是否有写入访问?

发布于 2025-01-20 00:36:55 字数 1110 浏览 2 评论 0原文

我想使用 std::filesystem 来查询提供给我的函数的磁盘文件夹路径。我想知道我是否有该文件夹的写入权限。但我想在不实际尝试写入文件夹的情况下执行此操作。

最后一个要求部分是因为

  • 我不想更改文件夹上文件系统的最后写入时间
  • 我想学习如何仅通过以某种与平台无关的方式查询文件系统来做到这一点(如果可能的话

)出现 std::filesystem::status()是我想要的功能。但是,该函数返回的权限标志包括 owner_writegroup_writeothers_write

我如何知道其中哪一个适用于我?我不知道我的应用程序/用户是所有者、组还是“其他”。是否有某种 can_i_write_to_this_folder() 函数在某处运行?

与此相关的我能找到的唯一问题没有涉及的答案。

[编辑] 人们似乎认为我正在寻找一些不切实际的可写性保证。我不是。我并不认为任何文件系统功能都是万无一失的。我不会将某些关键任务系统建立在这个决定的基础上。

出现并为我们提供了所有 directory_iterator 和其他好东西时,它们所宣传的目的是有用的。所以当我发现这是真的时,我评估了它们并使用了它们。就像我对 std::atomic<> 和参数包、std::variant 以及许多其他东西所做的那样。我想我们都同意,多年来添加了一些东西,但事实证明并不是那么好。

现在,该库宣传了这个函数 filesystem::status(),它旨在告诉我们(除其他外)文件或文件夹的权限。我想知道这个“访问”信息是否有任何实际用途。

共识似乎是事实并非如此。

I want to use std::filesystem to query about a disk folder path that was given to my function. I want to find out if I have write access to the folder. But I want to do it without actually trying to write to the folder.

This last requirement is partly because

  • I don't want to change the filesystem's last-written-to time on the folder
  • I want to learn how to do this merely by querying the filesystem in some sort of platform-independent fashion, if possible

It would appear that std::filesystem::status() is the function that I want. However the permissions flags returned by that function include owner_write, group_write and others_write.

How do I tell which of these applies to me? I don't know if my application/user is the owner, the group or the "other". Is there some sort of can_i_write_to_this_folder() function kicking around somewhere?

The only question I could find related to this has no answer that involves <filesystem>.

[Edit] People appear to be under the impression that I'm looking for some unrealistic guarantee of write-ability. I'm not. I'm not under the illusion that any filesystem function is foolproof. I'm not basing some mission-critical system upon this decision

When <filesystem> came around and gave us all directory_iterator and other goodies, their advertised purpose was useful. So I evaluated them and used them, when I found that to be true. Just as I did for std::atomic<> and parameter packs and std::variant and a host of other things. I think we can all agree there are a few things added over the years that have proved not to be that great.

Now the library advertises this function, filesystem::status() that purports to tell us (among other things) the permissions on a file or folder. I want to find out if this "access" information is of any practical use.

The consensus seems to be that it is not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

终陌 2025-01-27 00:36:55

如果您可以与“不知道”成为可能性,可以检查以查看所有许可位是否相同,如果是一样,则无论哪种状态都可以指示您可以做什么。

If you can live with "don't know" being a possibility, you can check to see if all the permission bits are the same, and if they are, then whichever state they are indicates what you can do.

后知后觉 2025-01-27 00:36:55

正如 使用 boost 获取文件的所有者和组如何确定文件的所有者或使用 boost 文件系统的目录?,无法使用此库获取文件的所有者。这确实意味着了解所有者/组/其他方面的文件权限并不是很有用。

对于 POSIX 系统,您可以按照上面评论中的建议使用 access ,对于 Windows...我不知道。也许可以使用 GetSecurityInfo 获取文件的所有者,并使用 LookupAccountSid 获取当前用户并进行比较。

As answered for boost::filesystem (which was the basis for std::filesystem) in Get file's owner and group using boost and How can I determine the owner of a file or directory using boost filesystem?, there is no way to get the file's owner using this library. Which does mean that knowing the file permissions in terms of owner/group/other is not very useful.

For POSIX systems you can use access as suggested in a comment above, and for Windows ... I dunno . Maybe get the file's owner with GetSecurityInfo and get the current user with LookupAccountSid and compare them.

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