File Permissions

In the last lesson you must have noticed functions related to file permissions. This lesson will talk about them.

Permission Functions

We have two major functions related to file permissions:

  • std::filesystem::status() and
  • std::filesystem::permissions()

The first one returns file_status which contains information about the file type and also its permissions.

And you can use the second function to modify the file permissions. For example, to change a file to be read-only.

std::filesystem::perms

File permissions - std::filesystem::perms - it’s an enum class that represents the following values:

Name Value (octal) POSIX macro Notes
none 0000 There are no permissions set for the file
owner_read 0400 S_IRUSR Read permission, owner
owner_write 0200 S_IWUSR Write permission, owner
owner_exec 0100 S_IXUSR Execute/search permission, owner
owner_all 0700 S_IRWXU Read, write, execute/search for owner
group_read 0040 S_IRGRP Read permission, group
group_write 0020 S_IWGRP Write permission, group
group_exec 0010 S_IXGRP Execute/search permission, group
group_all 0070 S_IRWXG Read, write, execute/search by group
others_read 0004 S_IROTH Read permission, others
others_write 0002 S_IWOTH Write permission, others
others_exec 0001 S_IXOTH Execute/search permission, others
others_all 0007 S_IRWXO Read, write, execute/search for others
all 0777 owner_all | group_all | others_all
set_uid 04000 S_ISUID Set-user-ID on execution
set_gid 02000 S_ISGID Set-group-ID on execution
sticky_bit 01000 S_ISVTX Operating system dependent
mask 07777 all | set_uid | set_gid | sticky_bit
unknown 0xFFFF The permissions are not known

Get hands-on with 1400+ tech skills courses.