Understanding CHMOD

F

Fuzzyman

Ok.... so I might be a windoze user trying to program CGIs for a Linux
server.... but Python doesn't seem to go out of it's way to make
understanding file attributes difficult. The python manual is
appalling in this are a :-(

Anyway - I think I've finally worked out that the correct way to get
(rather than set) the mode of a file is :

from stat import *
S_IMODE(os.stat(filepath)[ST_MODE])

Obvious huh !

The result will be some bitmasked combination of the following ?

statlist = [S_ISUID, S_ISGID, S_ENFMT, S_ISVTX, S_IREAD, S_IWRITE,
S_IEXEC, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG,
S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH]

Which mean ??????

Having obtained a result from S_IMODE(os.stat(filepath)[ST_MODE]), how
do I work out what it means ?

Thanks.

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
P

Paul Rubin

What's your question? I think you need to look at the chmod(1) and
chmod(2) Linux man pages, not a Python manual.
 
B

Ben Finney

Ok.... so I might be a windoze user trying to program CGIs for a Linux
server.... but Python doesn't seem to go out of it's way to make
understanding file attributes difficult. The python manual is
appalling in this are a :-(

The library reference seems to detail all the points that were confusing
you. The documentation for the 'stat' module in particular seems pretty
explicit:

<http://www.python.org/doc/current/lib/module-stat.html>
 
T

Tim Daneliuk

Fuzzyman said:
Ok.... so I might be a windoze user trying to program CGIs for a Linux
server.... but Python doesn't seem to go out of it's way to make
understanding file attributes difficult. The python manual is
appalling in this are a :-(

These are Unix-style permissions and the right place to
get more info would be a Unix OS man page for 'chmod'.

As it happens, last year, I wrote a Python program that
examines file attributes portably across Win32 and Unix.
You can have a look at the code that does this (among many
other things) at:

http://www.tundraware.com/Software/twander/

Anyway - I think I've finally worked out that the correct way to get
(rather than set) the mode of a file is :

from stat import *
S_IMODE(os.stat(filepath)[ST_MODE])

Obvious huh !

The result will be some bitmasked combination of the following ?

statlist = [S_ISUID, S_ISGID, S_ENFMT, S_ISVTX, S_IREAD, S_IWRITE,
S_IEXEC, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG,
S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH]

Which mean ??????

Having obtained a result from S_IMODE(os.stat(filepath)[ST_MODE]), how
do I work out what it means ?

Thanks.

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
P

P

Fuzzyman said:
Ok.... so I might be a windoze user trying to program CGIs for a Linux
server.... but Python doesn't seem to go out of it's way to make
understanding file attributes difficult. The python manual is
appalling in this are a :-(
agreed

Anyway - I think I've finally worked out that the correct way to get
(rather than set) the mode of a file is :

from stat import *
S_IMODE(os.stat(filepath)[ST_MODE])

Obvious huh !

The result will be some bitmasked combination of the following ?

statlist = [S_ISUID, S_ISGID, S_ENFMT, S_ISVTX, S_IREAD, S_IWRITE,
S_IEXEC, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG,
S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH]

Which mean ??????

Having obtained a result from S_IMODE(os.stat(filepath)[ST_MODE]), how
do I work out what it means ?

These are the basic access permissions.

S_IRGRP
S_IROTH
S_IRUSR

S_IWGRP
S_IWOTH
S_IWUSR

S_IXGRP
S_IXOTH
S_IXUSR

There are some shortcut (confusing IMHO) entries:

S_IEXEC = S_IXUSR
S_IWRITE = S_IWUSR
S_IREAD = S_IRUSR

S_IRWXG = stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
S_IRWXO = stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH
S_IRWXU = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR

The rest are "extended" attribute bits
and file type bits.

Here's a simplified ls access listing in python:

#!/usr/bin/env python

import sys
import stat
import os

filename=sys.argv[1]
mode=stat.S_IMODE(os.lstat(filename)[stat.ST_MODE])
perms="-"
for who in "USR", "GRP", "OTH":
for what in "R", "W", "X":
if mode & getattr(stat,"S_I"+what+who):
perms=perms+what.lower()
else:
perms=perms+"-"
print perms + " " + filename
 
F

Fuzzyman

[snip..]
These are the basic access permissions.

S_IRGRP
S_IROTH
S_IRUSR

S_IWGRP
S_IWOTH
S_IWUSR

S_IXGRP
S_IXOTH
S_IXUSR

There are some shortcut (confusing IMHO) entries:

S_IEXEC = S_IXUSR
S_IWRITE = S_IWUSR
S_IREAD = S_IRUSR

S_IRWXG = stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
S_IRWXO = stat.S_IROTH | stat.S_IWOTH | stat.S_IXOTH
S_IRWXU = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR

The rest are "extended" attribute bits
and file type bits.

Here's a simplified ls access listing in python:

#!/usr/bin/env python

import sys
import stat
import os

filename=sys.argv[1]
mode=stat.S_IMODE(os.lstat(filename)[stat.ST_MODE])
perms="-"
for who in "USR", "GRP", "OTH":
for what in "R", "W", "X":
if mode & getattr(stat,"S_I"+what+who):
perms=perms+what.lower()
else:
perms=perms+"-"
print perms + " " + filename

Thanks for your help !
I always find it slightly surprising when I get a reply that actually
answers the question ;-)

Fuzzy

http://www.voidspace.org.uk/atlantibots/pyhtonutils.html
 
F

Fuzzyman

Tim Daneliuk said:
These are Unix-style permissions and the right place to
get more info would be a Unix OS man page for 'chmod'.

As it happens, last year, I wrote a Python program that
examines file attributes portably across Win32 and Unix.
You can have a look at the code that does this (among many
other things) at:

http://www.tundraware.com/Software/twander/

Thanks very much.
I'll have a look - much appreciated.

Fuzzy

http://www.voidspace.org.uk/atlantibots/pyhtonutils.html
Anyway - I think I've finally worked out that the correct way to get
(rather than set) the mode of a file is :

from stat import *
S_IMODE(os.stat(filepath)[ST_MODE])

Obvious huh !

The result will be some bitmasked combination of the following ?

statlist = [S_ISUID, S_ISGID, S_ENFMT, S_ISVTX, S_IREAD, S_IWRITE,
S_IEXEC, S_IRWXU, S_IRUSR, S_IWUSR, S_IXUSR, S_IRWXG,
S_IRGRP, S_IWGRP, S_IXGRP, S_IRWXO, S_IROTH, S_IWOTH, S_IXOTH]

Which mean ??????

Having obtained a result from S_IMODE(os.stat(filepath)[ST_MODE]), how
do I work out what it means ?

Thanks.

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top