1

I try to create new directory and set its permissions (using at most c++11 and without boost) so user, group and others can list files inside read them and write new files (linux environment).

#include <sys/stat.h>
#include <sys/types.h>

int main(void) {
  const char* path = "/tmp/newDir";
  mode_t process_mask = umask(0);
  int syscall_status = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
  umask(process_mask);
  return syscall_status;
}

This code is based on the man (2) page of mkdir (and umask). However the created directory has rwxr-xr-x permissions (no write for group and others). I also tried using chmod syscall on the directory but it didn't solve the problem. Other sources in stackoverflow treated files (rather than folders), and trying to apply the file-methods on my directory didn't work as well.

Also, I want to avoid calling system() from stdlib, this is the last option I'll use if I don't find a solution (security considerations).

Ron U
  • 498
  • 1
  • 5
  • 21
  • 2
    I strongly recommend Boost.Filesystem. Or, if you have C++17, `` – AndyG Sep 24 '19 at 13:08
  • At what privilege level are you running the program? – ryyker Sep 24 '19 at 13:09
  • @AndyG I can't use boost and compile with c++11 (can't newer). – Ron U Sep 24 '19 at 13:15
  • @ryyker user level, and I have rwx permissions for the parent dir /tmp/ – Ron U Sep 24 '19 at 13:16
  • @RonU: My sympathies about your C++ version :-). The inability to use Boost isn't the worst thing overall; it tends to creep into your codebase and bloat your headers anyway. – AndyG Sep 24 '19 at 13:16
  • @RonU Have you verified the mkdir call is succeeding? You could be unknowingly letting an EEXIST slide. – Petr Skocik Sep 24 '19 at 14:18
  • Possible duplicate of [Fixing file permissions after modifying in C++?](https://stackoverflow.com/q/9287048/608639), [How to set file permissions (cross platform) in C++?](https://stackoverflow.com/q/592448/608639), etc. – jww Sep 24 '19 at 15:17
  • @jww I updated the question to explain how it is different – Ron U Sep 24 '19 at 18:11
  • It looks like you have ACL applied. Which is not strange as you do that on /tmp folder which shared by all users. umask alone can't breach that – Swift - Friday Pie Sep 24 '19 at 18:43

1 Answers1

1
char* path = "/tmp/newDir";

Besides the syntax error, this is ill-formed since C++11. Prior to that, this would be using a deprecated conversion. String literals are const in C++ -> Use pointer to const.

Other than that, the program is correct assuming a POSIX system. If it fails, then you can check errno to see why. If you don't get all permissions: Check if the parent directory has a default ACL; that would override umask.

A portable way of creating a directory in C++ is std::filesystem::create_directory and a way of setting permissions is std::filesystem::permissions.

ryyker
  • 22,849
  • 3
  • 43
  • 87
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • the path variable is just something I wrote to focus the question, in the actual code it is a const string from somewhere else and there are no typos ofcourse :) . The std::filesystem won't help me because I'm using c++11 (i wrote that in the comments but I'll edit the question for further clarity) – Ron U Sep 24 '19 at 18:05
  • @RonU you should look at ACL, that's the most important part of this. https://linuxconfig.org/how-to-manage-acls-on-linux – Swift - Friday Pie Sep 24 '19 at 18:49
  • @Swift-FridayPie when I run 'chmod 777 /tmp/newDir' through the terminal (with the same user that runs the program) it grants the required permissions to the dir , so does it make sense that the ACL is blocking me? – Ron U Sep 25 '19 at 05:13
  • @RonU https://unix.stackexchange.com/questions/328806/acl-named-group-permissions-not-overriding-file-permissions-why – Swift - Friday Pie Sep 25 '19 at 06:17