python 3.x - python3 os.mkdir() does not enforce correct mode -
is bug or feature ?
when create directory os.mkdir
(idem pathlib.path.mkdir
) , explicit mode, permissions of created directory not fit. if enforce os.chmod
again, works...
>>> import sys, os >>> sys.version '3.4.3 (default, feb 27 2015, 18:13:37) \n[gcc 4.4.5]' >>> os.mkdir('truite', mode=0o2770) >>> oct(os.stat('truite').st_mode) '0o40750' >>> os.chmod('truite', 0o2770) >>> oct(os.stat('truite').st_mode) '0o42770'
since wanted able make directory parents , mode o2770, here code (pth
pathlib.path
object) :
def make_shared_dir(pth) : if not pth.parent.is_dir() : make_shared_dir(pth.parent) if not pth.is_dir() : pth.mkdir() pth.chmod(0o2770)
it's feature.
the documentation mentions it, albeit briefly:
os.mkdir(path[, mode])
create directory named path numeric mode mode. default mode 0777 (octal). on systems, mode ignored. where used, current umask value first masked out.
a umask per-process setting limits permissions applied newly created files/directories.
a umask number (usually presented in octal) permissions themselves, bit set in umask disallowed in resulting permissions. example, if umask 0o022
(a common default), no newly created file/directory ever have group or world write permissions.
permissions on newly created files/directories restricted last 3 octal digits (i.e. 0o777
). if umask 0o022
, behaves if 0o7022
. why setgid bit dropped.
by way, not python thing; it's unix thing. doesn't apply python, programs in ubuntu. can check current umask entering umask
command in terminal. in fact, umask enforced linux kernel when performs mkdir()
systemcall python (and every other program) invokes create directory.
you can see in the documentation mkdir()
: man 2 mkdir
:
the argument mode specifies permissions use. modified process's umask in usual way: permissions of created directory (mode & ~umask & 0777).
the reason is, of course, security. umask, users have great deal of control on default permissions (to e.g. block default read permissions world or group). if want files sticky, setuid, setgid, group-writable, or world-writable (assuming 0o022
umask), need explicitly using chmod.
Comments
Post a Comment