Kyle Fransham
Kyle Fransham

Reputation: 33

python creating tmp directory permissions

I'm using a python script to create a copy of a linux filesystem. I'm having trouble with the permissions on the created /tmp directory. The /tmp directory should have 1777 permissions, i.e.:

ls -l /
drwxrwxrwt  17 root     root 16384 2011-03-01 09:50 tmp

when I do the following,

os.mkdir('/mnt/tmp',1777)

I get strange permissions:

ls -l /
d-wxr----t 2 root root  4096 2011-03-01 09:53 tmp

Then I wondered about umask and chmod, so I tried this:

os.mkdir('/mnt/tmp')
old_mask=os.umask(0000)
os.chmod('/mnt/tmp',1777)
os.umask(old_mask)

but I still get unexpected permissions:

ls -l /
d-wxrwS--t 2 root root  4096 2011-03-01 09:57 tmp

However, what DOES give me the correct permissions of the created directory is the following:

os.mkdir('/mnt/tmp')
os.system("chmod 1777 /mnt/tmp")

I should note that I'm running this script through sudo, but there is no mention of any umask settings in /etc/sudoers. Running it as the actual root user makes no difference. It is impossible to run it as a normal user, since I'm making a copy of the FS, which has to include the files accessible only to root.

Any ideas here? Any help would be greatly appreciated.

Upvotes: 3

Views: 3372

Answers (2)

Sven Marnach
Sven Marnach

Reputation: 602175

You should provide the permissions as an octal number. In Python 2.x, simply use 01777 instead of 1777. In Python 3.x, use 0o1777.

Upvotes: 9

0x6adb015
0x6adb015

Reputation: 7811

Your permission should be in octal (777 in octal is 511 in decimal).

In Python, like in C, 0555 is 555 in base 8 (octal). If you want 1777 in octal, use 01777 in your code.

Upvotes: 1

Related Questions