Understanding CHMOD

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzzyman

    Understanding CHMOD

    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


  • Paul Rubin

    #2
    Re: Understanding CHMOD

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

    Comment

    • Ben Finney

      #3
      Re: Understanding CHMOD

      On 13 Feb 2004 00:55:30 -0800, Fuzzyman wrote:[color=blue]
      > 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 :-([/color]

      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/>

      Comment

      • Tim Daneliuk

        #4
        Re: Understanding CHMOD

        Fuzzyman wrote:
        [color=blue]
        > 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 :-([/color]

        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:



        [color=blue]
        >
        > 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[/color]


        --
        ----------------------------------------------------------------------------
        Tim Daneliuk tundra@tundrawa re.com
        PGP Key: http://www.tundraware.com/PGP/

        Comment

        • P@draigBrady.com

          #5
          Re: Understanding CHMOD

          Fuzzyman wrote:[color=blue]
          > 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 :-([/color]

          agreed
          [color=blue]
          > 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 ?[/color]

          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.ar gv[1]
          mode=stat.S_IMO DE(os.lstat(fil ename)[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+wha t.lower()
          else:
          perms=perms+"-"
          print perms + " " + filename

          --
          Pádraig Brady - http://www.pixelbeat.org

          Comment

          • Fuzzyman

            #6
            Re: Understanding CHMOD

            [snip..][color=blue]
            >
            > 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.ar gv[1]
            > mode=stat.S_IMO DE(os.lstat(fil ename)[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+wha t.lower()
            > else:
            > perms=perms+"-"
            > print perms + " " + filename[/color]

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

            Fuzzy


            Comment

            • Fuzzyman

              #7
              Re: Understanding CHMOD

              Tim Daneliuk <tundra@tundraw are.com> wrote in message news:<bfhuf1-b0u.ln1@eskimo. tundraware.com> ...[color=blue]
              > Fuzzyman wrote:
              >[color=green]
              > > 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 :-([/color]
              >
              > 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/[/color]

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

              Fuzzy


              [color=blue]
              >
              >[color=green]
              > >
              > > 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[/color][/color]

              Comment

              Working...