Based on what I’ve found here: https://en.cppreference.com/w/cpp/filesystem/permissions I’ve tried to programmatically set the folder permission to write, in order to be able to programmatically create a subfolder.
<code> void demo_perms(std::filesystem::perms p)
{
using std::filesystem::perms;
auto show = [=](char op, perms perm)
{
std::cout << (perms::none == (perm & p) ? '-' : op);
};
show('r', perms::owner_read);
show('w', perms::owner_write);
show('x', perms::owner_exec);
show('r', perms::group_read);
show('w', perms::group_write);
show('x', perms::group_exec);
show('r', perms::others_read);
show('w', perms::others_write);
show('x', perms::others_exec);
std::cout << 'n';
}
cwd = std::filesystem::current_path();
std::filesystem::permissions(
cwd,
std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
std::filesystem::perm_options::add
);
demo_perms(std::filesystem::status(cwd).permissions());
if(!std::filesystem::exists(graspdatafolderpath))
{
std::filesystem::create_directory("/GraspData");
}
</code>
<code> void demo_perms(std::filesystem::perms p)
{
using std::filesystem::perms;
auto show = [=](char op, perms perm)
{
std::cout << (perms::none == (perm & p) ? '-' : op);
};
show('r', perms::owner_read);
show('w', perms::owner_write);
show('x', perms::owner_exec);
show('r', perms::group_read);
show('w', perms::group_write);
show('x', perms::group_exec);
show('r', perms::others_read);
show('w', perms::others_write);
show('x', perms::others_exec);
std::cout << 'n';
}
cwd = std::filesystem::current_path();
std::filesystem::permissions(
cwd,
std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
std::filesystem::perm_options::add
);
demo_perms(std::filesystem::status(cwd).permissions());
if(!std::filesystem::exists(graspdatafolderpath))
{
std::filesystem::create_directory("/GraspData");
}
</code>
void demo_perms(std::filesystem::perms p)
{
using std::filesystem::perms;
auto show = [=](char op, perms perm)
{
std::cout << (perms::none == (perm & p) ? '-' : op);
};
show('r', perms::owner_read);
show('w', perms::owner_write);
show('x', perms::owner_exec);
show('r', perms::group_read);
show('w', perms::group_write);
show('x', perms::group_exec);
show('r', perms::others_read);
show('w', perms::others_write);
show('x', perms::others_exec);
std::cout << 'n';
}
cwd = std::filesystem::current_path();
std::filesystem::permissions(
cwd,
std::filesystem::perms::owner_all | std::filesystem::perms::group_all,
std::filesystem::perm_options::add
);
demo_perms(std::filesystem::status(cwd).permissions());
if(!std::filesystem::exists(graspdatafolderpath))
{
std::filesystem::create_directory("/GraspData");
}
Ouput:
<code> rwxrwxr-x
Unhandled standard exception of type "NSt10filesystem7__cxx1116filesystem_errorE" with message "filesystem error: cannot create directory: Permission denied [/GraspData]"; terminating the application
</code>
<code> rwxrwxr-x
Unhandled standard exception of type "NSt10filesystem7__cxx1116filesystem_errorE" with message "filesystem error: cannot create directory: Permission denied [/GraspData]"; terminating the application
</code>
rwxrwxr-x
Unhandled standard exception of type "NSt10filesystem7__cxx1116filesystem_errorE" with message "filesystem error: cannot create directory: Permission denied [/GraspData]"; terminating the application
But, as, you can see, what I’ve tried, does not work
How to make it work?
8