473,473 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 24490
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. Löwis <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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Salmo Bytes | last post by:
I have a script that wants to mirror a directory structure, reading from location1 and writing (mkdir) at location2. This code works fine on a my own desktop test box. But fails at 'mkdir' when...
2
by: Paul Schmidinger | last post by:
Hi, I created a directory with PHP mkdir (mode 0777). Then I try to move_ulpoaded_file() to move a file into this directory. I get an error: Warning: move_uploaded_file(): SAFE MODE...
8
by: Sue | last post by:
AccessXP in Access2000 Mode: In my code I use the MkDir method to create a folder and then I want to use the transfertext method to create a delimited text file in that folder. MkDir runs and...
1
by: joelbyrd | last post by:
I'm trying to do a mkdir(), but I'm getting permission denied error, even though I've set the mode to 0777: mkdir($full_path, 0777). I also couldn't do a chmod successfully. The code is...
5
by: eoindeb | last post by:
I am trying to create a directory on Solaris using the mkdir() function. This works fine when I pass a string literal ("/etc/hosts") to mkdir, but if I try passing a directory pointer to mkdir, it...
30
by: MikeC | last post by:
Good People, I'm writing a backup utility that uses a chdir() to go into the source directory (in which the files reside that I want to back up), so I don't want to use chdir() to get into the...
3
by: Cris | last post by:
OK, I do this call on a linux system: if(!file_exists("../pages/".$_POST."/")) { $dirname = "/home/u2/sss/sss/html/pages/".$_POST.""; mkdir($dirname, $mode); } and get this:
4
by: cclough | last post by:
Hello, I have run into a problem using the mkdir function on a windows 03 server running IIS v6 with PHP 5.0. I am trying to simply create a folder within my site. Safe Mode is set to "off" and...
2
by: _q_u_a_m_i_s's | last post by:
Hy, i encountered a weird problem on a server running php5, and apache. Seems like i cannot create folders that end with "/". for example: mkdir("test/") will fail mkdir("test") will work Is...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.