os.mkdir and mode

Nick Craig-Wood nick at craig-wood.com
Mon Dec 4 06:30:04 EST 2006


Martin v. Löwis <martin at v.loewis.de> wrote:
>  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

Yes you are right of course.  I didn't think that statment through did
I!

>  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.

That makes sense - the odd sequence above is one of those Unix
workarounds then...

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list