os.mkdir and mode

"Martin v. Löwis" martin at v.loewis.de
Sat Dec 2 13:29:05 EST 2006


Nick Craig-Wood schrieb:
> So it looks like python mkdir() is applying the umask where as
> /bin/mkdir doesn't.  From man 2 mkdir

Actually, mkdir(1) has no chance to not apply the umask: it also
has to use mkdir(2), which is implemented in the OS kernel, and
that applies the umask. Try

strace mkdir -m770 test

to see how mkdir solves this problem; the relevant fragment
is this:

umask(0)                                = 022
mkdir("test", 0770)                     = 0
chmod("test", 0770)                     = 0

So it does *both* set the umask to 0, and then apply chmod.

Looking at the source, I see that it invokes umask(0) not to
clear the umask, but to find out what the old value was.
It then invokes chmod to set any "special" bits (s, t) that
might be specified, as mkdir(2) isn't required (by POSIX spec)
to honor them.

Regards,
Martin



More information about the Python-list mailing list