如何在C++中使用std::filesystem检查读取和写入权限。

huangapple go评论115阅读模式
英文:

How to check for read and write permission in C++ using std::filesystem

问题

I have a unix codebase that is using access(path, W_OK) and access(path, R_OK), available in unistd.h, in order to see if the file is writeable/readable.
Examples at the end of the page here.

我有一个Unix代码库,使用unistd.h 中的 access(path, W_OK) 和 access(path, R_OK) 来检查文件是否可写/可读。示例在此处页面的末尾。

I have seen that Windows offer the same function, under the name of _access, but I wanted an implementation leveraging only std::filesystem available in C++17.

我注意到Windows提供了相同的函数,以_access的名称,但我想要一个仅使用C++17中可用的std::filesystem来实现的版本。

I thought about using std::filesystem::perms and perform a manual check for both read and write, but how can I perfectly mimic the checks made by access?

我考虑使用std::filesystem::perms并执行手动检查以进行读取和写入,但如何完美地模拟access所进行的检查?

英文:

I have a unix codebase that is using access(path, W_OK) and access(path, R_OK), available in unistd.h, in order to see if the file is writeable/readable.
Examples at the end of the page here.

I have seen that Windows offer the same function, under the name of _access, but I wanted an implementation leveraging only std::filesystem available in C++17.

I thought about using std::filesystem::perms and perform a manual check for both read and write, but how can I pefectly mimick the checks made by access?

答案1

得分: 2

请参考cppreference中的参考文档。

如果你想要检查权限,可以使用以下代码:

std::filesystem::status("filename").permissions()

这将返回权限列表。你可以使用类似以下的方法:

#include <filesystem>
#define F_OK 0
#define R_OK 1
#define W_OK 2
#define X_OK 3
namespace fs = std::filesystem;

int access(const fs::path& path, int amode){
    auto a = fs::status(path).permissions();
    switch(amode){
    case F_OK:
        return fs::perms::unknown != a ? 0 : -1;
    case R_OK:
        return (fs::perms::owner_read & a) == fs::perms::owner_read ? 0 : -1;
    case W_OK:
        return (fs::perms::owner_read & a) == fs::perms::owner_read && (fs::perms::owner_write & a) == fs::perms::owner_write ? 0 : -1;
    case X_OK:
        return (fs::perms::owner_read & a) == fs::perms::owner_read && (fs::perms::owner_exec & a) == fs::perms::owner_exec ? 0 : -1;
    default:
        return -1;
    }
}

这可能不会按照你的期望工作,因为你甚至不知道文件所有者是你还是我,或者我是否与群组所有者在同一组。你需要检查每种情况,例如,"read"是否可操作,无论文件是否具有owner_read,以及您是否是文件所有者,或者文件是否具有group_read,以及您是否与文件所有者在同一组,或者文件是否具有others_read。这使事情变得更加复杂。

你需要检查类似的内容,我猜这很复杂。

或者,你可以在Unix上轻松使用fstat,在Windows上使用GetFileAttributes,并在你的代码中检查系统是否是Unix或Windows。

英文:

See the reference cppreference
If you want to check you can use this:

std::filesystem::status(&quot;filename&quot;).permissions()

That will give you list of permissions. And you can use some approach like this:

#include &lt;filesystem&gt;
#define F_OK 0
#define R_OK 1
#define W_OK 2
#define X_OK 3
namespace fs = std::filesystem;

int access(const fs::path&amp; path, int amode){
    auto a=fs::status(path).permissions();
    switch(amode){
    case F_OK:
        return fs::perms::unknown!=a? 0 : -1;
    case R_OK:
        return (fs::perms::owner_read &amp; a) == fs::perms::owner_read ? 0 : -1;
    case W_OK:
        return (fs::perms::owner_read &amp; a) == fs::perms::owner_read  &amp;&amp; (fs::perms::owner_write &amp; a) == fs::perms::owner_write ? 0 : -1;
    case X_OK:
        return (fs::perms::owner_read &amp; a) == fs::perms::owner_read  &amp;&amp; (fs::perms::owner_exec &amp; a) == fs::perms::owner_exec ? 0 : -1;
    default:
        return -1;
    }
}

This might not work as you expected because I don't even controlled file owner is me or Am I in same group with group owner. You need to check each case, for example," read can be operate, whether file have owner_read and you are the file owner or file have group_read and you are same group with file owner or file have others_read"
It makes something more complicated.

You need to check something like that and it is so complex I guess.

Or you can easily use fstat for Unix and GetFileAttributes for Windows and in your code just check whether your system is Unix or Windows using directives.

huangapple
  • 本文由 发表于 2023年3月9日 20:33:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定