os.mkdir and mode

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

    os.mkdir and mode

    How do I do the following unix command:

    mkdir -m770 test

    with the os.mkdir command. Using os.mkdir(mode=0 770) ends with the
    incorrect permissions.

    Thanks,

    VJ

  • Peter Otten

    #2
    Re: os.mkdir and mode

    vj wrote:
    How do I do the following unix command:
    >
    mkdir -m770 test
    >
    with the os.mkdir command. Using os.mkdir(mode=0 770) ends with the
    incorrect permissions.
    mkdir() works just like its C equivalent, see


    "Where it is used, the current umask value is first masked out."

    Use os.chmod() after os.mkdir() to get the desired permissions.

    Peter

    Comment

    • Nick Craig-Wood

      #3
      Re: os.mkdir and mode

      vj <vinjvinj@gmail .comwrote:
      How do I do the following unix command:
      >
      mkdir -m770 test
      >
      with the os.mkdir command. Using os.mkdir(mode=0 770) ends with the
      incorrect permissions.
      You mean :-

      $ python -c 'import os; os.mkdir("test" , 0770)'
      $ stat test/
      File: `test/'
      Size: 4096 Blocks: 8 IO Block: 4096 directory
      Device: 806h/2054d Inode: 2453906 Links: 2
      Access: (0750/drwxr-x---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
      Access: 2006-12-02 09:42:59.000000 000 +0000
      Modify: 2006-12-02 09:42:59.000000 000 +0000
      Change: 2006-12-02 09:42:59.000000 000 +0000

      vs

      $ rmdir test
      $ mkdir -m770 test
      $ stat test/
      File: `test/'
      Size: 4096 Blocks: 8 IO Block: 4096 directory
      Device: 806h/2054d Inode: 2453906 Links: 2
      Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
      Access: 2006-12-02 09:43:23.000000 000 +0000
      Modify: 2006-12-02 09:43:23.000000 000 +0000
      Change: 2006-12-02 09:43:23.000000 000 +0000
      $ umask
      0022
      $

      So it looks like python mkdir() is applying the umask where as
      /bin/mkdir doesn't. From man 2 mkdir

      mkdir() attempts to create a directory named pathname.

      The parameter mode specifies the permissions to use. It is modified by
      the process's umask in the usual way: the permissions of the created
      directory are (mode & ~umask & 0777). Other mode bits of the created
      directory depend on the operating system. For Linux, see below.

      So python follows C rather than shell. Seems reasonable.

      To fix your problem, reset your umask thus :-

      $ rmdir test
      $ python -c 'import os; os.umask(0); os.mkdir("test" , 0770)'
      $ stat test
      File: `test'
      Size: 4096 Blocks: 8 IO Block: 4096 directory
      Device: 806h/2054d Inode: 2453906 Links: 2
      Access: (0770/drwxrwx---) Uid: ( 518/ ncw) Gid: ( 518/ ncw)
      Access: 2006-12-02 09:48:04.000000 000 +0000
      Modify: 2006-12-02 09:48:04.000000 000 +0000
      Change: 2006-12-02 09:48:04.000000 000 +0000
      $

      --
      Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

      Comment

      • Nick Craig-Wood

        #4
        Re: os.mkdir and mode

        Peter Otten <__peter__@web. dewrote:
        vj wrote:
        >
        How do I do the following unix command:

        mkdir -m770 test

        with the os.mkdir command. Using os.mkdir(mode=0 770) ends with the
        incorrect permissions.
        >
        mkdir() works just like its C equivalent, see

        >
        "Where it is used, the current umask value is first masked out."
        >
        Use os.chmod() after os.mkdir() to get the desired permissions.
        I think you meant use os.umask(0) before the os.mkdir() ?

        --
        Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

        Comment

        • Peter Otten

          #5
          Re: os.mkdir and mode

          Nick Craig-Wood wrote:
          Peter Otten <__peter__@web. dewrote:
          > vj wrote:
          >>
          How do I do the following unix command:
          >
          mkdir -m770 test
          >
          with the os.mkdir command. Using os.mkdir(mode=0 770) ends with the
          incorrect permissions.
          >>
          > mkdir() works just like its C equivalent, see
          > http://docs.python.org/dev/lib/os-file-dir.html:
          >>
          > "Where it is used, the current umask value is first masked out."
          >>
          > Use os.chmod() after os.mkdir() to get the desired permissions.
          >
          I think you meant use os.umask(0) before the os.mkdir() ?
          No, I didn't. What is the difference/advantage of that approach?

          Peter

          Comment

          • vj

            #6
            Re: os.mkdir and mode

            To fix your problem, reset your umask thus :-

            Thanks for the detailed reply. Your fix works like a charm.

            VJ

            Comment

            • Martin v. Löwis

              #7
              Re: os.mkdir and mode

              Nick Craig-Wood schrieb:
              So it looks like python mkdir() is applying the umask where as
              /bin/mkdir doesn't. From man 2 mkdir
              Actually, mkdir(1) has no chance to not apply the umask: it also
              has to use mkdir(2), which is implemented in the OS kernel, and
              that applies the umask. Try

              strace mkdir -m770 test

              to see how mkdir solves this problem; the relevant fragment
              is this:

              umask(0) = 022
              mkdir("test", 0770) = 0
              chmod("test", 0770) = 0

              So it does *both* set the umask to 0, and then apply chmod.

              Looking at the source, I see that it invokes umask(0) not to
              clear the umask, but to find out what the old value was.
              It then invokes chmod to set any "special" bits (s, t) that
              might be specified, as mkdir(2) isn't required (by POSIX spec)
              to honor them.

              Regards,
              Martin

              Comment

              • Nick Craig-Wood

                #8
                Re: os.mkdir and mode

                Peter Otten <__peter__@web. dewrote:
                "Where it is used, the current umask value is first masked out."
                >
                Use os.chmod() after os.mkdir() to get the desired permissions.
                I think you meant use os.umask(0) before the os.mkdir() ?
                >
                No, I didn't. What is the difference/advantage of that approach?
                If you use use os.umask(0) then the os.mkdir(dir, perms) will create
                the directory with exactly those permissions, no chmod needed.

                --
                Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

                Comment

                • Nick Craig-Wood

                  #9
                  Re: os.mkdir and mode

                  Martin v. Löwis <martin@v.loewi s.dewrote:
                  Nick Craig-Wood schrieb:
                  So it looks like python mkdir() is applying the umask where as
                  /bin/mkdir doesn't. From man 2 mkdir
                  >
                  Actually, mkdir(1) has no chance to not apply the umask: it also
                  has to use mkdir(2), which is implemented in the OS kernel, and
                  that applies the umask. Try
                  Yes you are right of course. I didn't think that statment through did
                  I!
                  strace mkdir -m770 test
                  >
                  to see how mkdir solves this problem; the relevant fragment
                  is this:
                  >
                  umask(0) = 022
                  mkdir("test", 0770) = 0
                  chmod("test", 0770) = 0
                  >
                  So it does *both* set the umask to 0, and then apply chmod.
                  >
                  Looking at the source, I see that it invokes umask(0) not to
                  clear the umask, but to find out what the old value was.
                  It then invokes chmod to set any "special" bits (s, t) that
                  might be specified, as mkdir(2) isn't required (by POSIX spec)
                  to honor them.
                  That makes sense - the odd sequence above is one of those Unix
                  workarounds then...

                  --
                  Nick Craig-Wood <nick@craig-wood.com-- http://www.craig-wood.com/nick

                  Comment

                  Working...