开发者

Check for writing permissions to file in Windows/Linux

开发者 https://www.devze.com 2023-01-17 21:18 出处:网络
I would like to know how to check if I have write permissions to a folder. I\'m writing a C++ project and I should print some data to a result.txt file, but I need to know if I have permissions or no

I would like to know how to check if I have write permissions to a folder.

I'm writing a C++ project and I should print some data to a result.txt file, but I need to know if I have permissions or not.

Is the check different between Linux and Windows? Because my project should 开发者_如何学Gorun on Linux and currently I'm working in Visual Studio.


The portable way to check permissions is to try to open the file and check if that succeeded. If not, and errno (from the header <cerrno> is set to the value EACCES [yes, with one S], then you did not have sufficient permissions. This should work on both Unix/Linux and Windows. Example for stdio:

FILE *fp = fopen("results.txt", "w");
if (fp == NULL) {
    if (errno == EACCES)
        cerr << "Permission denied" << endl;
    else
        cerr << "Something went wrong: " << strerror(errno) << endl;
}

Iostreams will work a bit differently. AFAIK, they do not guarantee to set errno on both platforms, or report more specific errors than just "failure".

As Jerry Coffin wrote, don't rely on separate access test functions since your program will be prone to race conditions and security holes.


About the only reasonable thing to do is try to create the file, and if it fails, tell the user there was a problem. Any attempt at testing ahead of time, and only trying to create the file if you'll be able to create and write to it is open to problems from race conditions (had permission when you checked, but it was removed by the time you tried to use it, or vice versa) and corner cases (e.g., you have permission to create a file in that directory, but attempting to write there will exceed your disk quota). The only way to know is to try...


The most correct way to actually test for file write permission is to attempt to write to the file. The reason for this is because different platforms expose write permissions in very different ways. Even worse, just because the operating system tells you that you can (or cannot) write to a file, it might actually be lying, for instance, on a unix system, the file modes might allow writing, but the file is on read only media, or conversely, the file might actually be a character device created by the kernel for the processes' own use, so even though its filemodes are set to all zeroes, the kernel allows that process (and only that process) to muck with it all it likes.


Similar to the accepted answer but using the non-deprecated fopen_s function as well as modern C++ and append open mode to avoid destroying the file contents:

bool is_file_writable(const std::filesystem::path &file_path)
{
    FILE* file_handle;
    errno_t file_open_error;
    if ((file_open_error = fopen_s(&file_handle, file_path.string().c_str(), "a")) != 0)
    {
        return false;
    }

    fclose(file_handle);
    return true;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消