user482813
user482813

Reputation: 53

C++17/20 - Use <filesystem> to determine if file is executable

I want to know if there is a way to determine if a file in a directory is executable or not using the new C++17/20 #include <filesystem>. I do not want to use Boost. I know how this can be done with stat, st_mode and S_IXUSR but I haven't found a way to do it with pure C++17/20.

Upvotes: 1

Views: 1004

Answers (1)

michaeldel
michaeldel

Reputation: 2385

Check execution permissions, that is owner_exec, group_exec and other_exec attributes of the corresponding std::experimental::filesystem::permissions struct. Given a filename, it can be retrieved with

namespace fs = std::experimental::filesystem;

// ...

const auto permissions = fs::status("file.txt").permissions();

Check these according what you know about current user (is the current user owner of file, in file's user group).

Upvotes: 2

Related Questions