When creating a file or directory, the OS uses a default permission mode based on different sources (OS-default, inherited ACLs, ...?). I want to get the default permissions for a specific directory.
For example, if I create a file /tmp/test.txt, the file has mode 0o644
. AFAIK this comes from the default 0o666
permissions of the OS and from the umask 0o22
.
A directory /tmp/testdir would start with 0o755
as the default mode is 0o777
and the umask is 0o22
.
But is there a way to programmatically get the defaults, all inheritance and the umask to calculate the mode in effect?
I thought about creating a temporary file and a folder in this directory, read the permission flags and delete them. But is there a better way?
# Default file mask = 0o666, no ACL or other, umask = 0o22
get_default_file_mode("/tmp") # should return 0o644
# Default direcoty mask = 0o777
get_default_directory_mode("/tmp") # should return 0o755
Is there a way to achieve this without creating the file and folder explicitly?
(The programming language to be used is not that important)