468,505 Members | 1,569 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

os.mkdir and mode

vj
How do I do the following unix command:

mkdir -m770 test

with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
incorrect permissions.

Thanks,

VJ

Dec 2 '06 #1
8 23815
vj wrote:
How do I do the following unix command:

mkdir -m770 test

with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
incorrect permissions.
mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.

Peter
Dec 2 '06 #2
vj <vi******@gmail.comwrote:
How do I do the following unix command:

mkdir -m770 test

with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
incorrect permissions.
You mean :-

$ python -c 'import os; os.mkdir("test", 0770)'
$ stat test/
File: `test/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0750/drwxr-x---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:42:59.000000000 +0000
Modify: 2006-12-02 09:42:59.000000000 +0000
Change: 2006-12-02 09:42:59.000000000 +0000

vs

$ rmdir test
$ mkdir -m770 test
$ stat test/
File: `test/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:43:23.000000000 +0000
Modify: 2006-12-02 09:43:23.000000000 +0000
Change: 2006-12-02 09:43:23.000000000 +0000
$ umask
0022
$

So it looks like python mkdir() is applying the umask where as
/bin/mkdir doesn't. From man 2 mkdir

mkdir() attempts to create a directory named pathname.

The parameter mode specifies the permissions to use. It is modified by
the process's umask in the usual way: the permissions of the created
directory are (mode & ~umask & 0777). Other mode bits of the created
directory depend on the operating system. For Linux, see below.

So python follows C rather than shell. Seems reasonable.

To fix your problem, reset your umask thus :-

$ rmdir test
$ python -c 'import os; os.umask(0); os.mkdir("test", 0770)'
$ stat test
File: `test'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: 806h/2054d Inode: 2453906 Links: 2
Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
Access: 2006-12-02 09:48:04.000000000 +0000
Modify: 2006-12-02 09:48:04.000000000 +0000
Change: 2006-12-02 09:48:04.000000000 +0000
$

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 2 '06 #3
Peter Otten <__*******@web.dewrote:
vj wrote:
How do I do the following unix command:

mkdir -m770 test

with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
incorrect permissions.

mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.
I think you meant use os.umask(0) before the os.mkdir() ?

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 2 '06 #4
Nick Craig-Wood wrote:
Peter Otten <__*******@web.dewrote:
> vj wrote:
How do I do the following unix command:

mkdir -m770 test

with the os.mkdir command. Using os.mkdir(mode=0770) ends with the
incorrect permissions.

mkdir() works just like its C equivalent, see
http://docs.python.org/dev/lib/os-file-dir.html:

"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.

I think you meant use os.umask(0) before the os.mkdir() ?
No, I didn't. What is the difference/advantage of that approach?

Peter
Dec 2 '06 #5
vj
To fix your problem, reset your umask thus :-

Thanks for the detailed reply. Your fix works like a charm.

VJ

Dec 2 '06 #6
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
Dec 2 '06 #7
Peter Otten <__*******@web.dewrote:
"Where it is used, the current umask value is first masked out."

Use os.chmod() after os.mkdir() to get the desired permissions.
I think you meant use os.umask(0) before the os.mkdir() ?

No, I didn't. What is the difference/advantage of that approach?
If you use use os.umask(0) then the os.mkdir(dir, perms) will create
the directory with exactly those permissions, no chmod needed.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 4 '06 #8
Martin v. Lwis <ma****@v.loewis.dewrote:
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 <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 4 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Salmo Bytes | last post: by
2 posts views Thread by Paul Schmidinger | last post: by
8 posts views Thread by Sue | last post: by
1 post views Thread by joelbyrd | last post: by
5 posts views Thread by eoindeb | last post: by
30 posts views Thread by MikeC | last post: by
3 posts views Thread by Cris | last post: by
2 posts views Thread by _q_u_a_m_i_s's | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.