Understanding CHMOD
P at draigBrady.com
P at draigBrady.com
Fri Feb 13 06:44:51 EST 2004
Fuzzyman wrote:
> 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
--
Pádraig Brady - http://www.pixelbeat.org
More information about the Python-list
mailing list