By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

Understanding CHMOD

P: n/a
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/atlantib...thonutils.html
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
What's your question? I think you need to look at the chmod(1) and
chmod(2) Linux man pages, not a Python manual.
Jul 18 '05 #2

P: n/a
On 13 Feb 2004 00:55:30 -0800, 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 :-(


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>

--
\ "There is no reason anyone would want a computer in their |
`\ home." -- Ken Olson, president, chairman and founder of |
_o__) Digital Equipment Corp., 1977 |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3

P: n/a
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 :-(
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/atlantib...thonutils.html

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundraware.com
PGP Key: http://www.tundraware.com/PGP/
Jul 18 '05 #4

P: n/a
P
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

Jul 18 '05 #5

P: n/a
[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/atlantib...htonutils.html
Jul 18 '05 #6

P: n/a
Tim Daneliuk <tu****@tundraware.com> wrote in message news:<bf************@eskimo.tundraware.com>...
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 :-(
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/atlantib...htonutils.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/atlantib...thonutils.html

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.