Has anyone used File.stat to check permissions

Discussion in 'Ruby' started by Peter Loftus, Nov 25, 2007.

  1. Peter Loftus

    Peter Loftus Guest

    In ruby API there is an example

    s = File.stat("testfile")
    sprintf("%o", s.mode) #=> "100644"

    I need to check the last three numbers from the octal notation which is
    "644"
    which means:
    rw- for user
    r-- for group
    r-- for other

    Does anyone know how i could check just those three numbers and leave
    the rest

    Regards
    Loftz
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Loftus, Nov 25, 2007
    #1
    1. Advertising

  2. Peter Loftus

    yermej Guest

    On Nov 25, 3:29 pm, Peter Loftus <> wrote:
    > In ruby API there is an example
    >
    > s = File.stat("testfile")
    > sprintf("%o", s.mode) #=> "100644"
    >
    > I need to check the last three numbers from the octal notation which is
    > "644"
    > which means:
    > rw- for user
    > r-- for group
    > r-- for other
    >
    > Does anyone know how i could check just those three numbers and leave
    > the rest
    >
    > Regards
    > Loftz
    > --
    > Posted viahttp://www.ruby-forum.com/.


    If you're wanting to compare to the String "644":

    s.mode.to_s(8)[-3..-1] == "644"

    otherwise, you can mask using bitwise & and compare to the Fixnum
    0644:

    (s.mode & 0xFFF) == 0644
    (s.mode & 07777) == 0644

    Jeremy
     
    yermej, Nov 25, 2007
    #2
    1. Advertising

  3. Peter Loftus

    yermej Guest

    On Nov 25, 4:36 pm, yermej <> wrote:
    > On Nov 25, 3:29 pm, Peter Loftus <> wrote:
    >
    >
    >
    > > In ruby API there is an example

    >
    > > s = File.stat("testfile")
    > > sprintf("%o", s.mode) #=> "100644"

    >
    > > I need to check the last three numbers from the octal notation which is
    > > "644"
    > > which means:
    > > rw- for user
    > > r-- for group
    > > r-- for other

    >
    > > Does anyone know how i could check just those three numbers and leave
    > > the rest

    >
    > > Regards
    > > Loftz
    > > --
    > > Posted viahttp://www.ruby-forum.com/.

    >
    > If you're wanting to compare to the String "644":
    >
    > s.mode.to_s(8)[-3..-1] == "644"
    >
    > otherwise, you can mask using bitwise & and compare to the Fixnum
    > 0644:
    >
    > (s.mode & 0xFFF) == 0644
    > (s.mode & 07777) == 0644
    >
    > Jeremy


    Sorry, those masks will probably work in most cases, but I guess you
    should really use 0777 or 0x1FF to only get the ugo fields. Otherwise,
    there might be problems if something is setuid or has the sticky bit
    set.

    Jeremy
     
    yermej, Nov 25, 2007
    #3
  4. 2007/11/26, yermej <>:
    > On Nov 25, 4:36 pm, yermej <> wrote:
    > > On Nov 25, 3:29 pm, Peter Loftus <> wrote:
    > >
    > >
    > >
    > > > In ruby API there is an example

    > >
    > > > s = File.stat("testfile")
    > > > sprintf("%o", s.mode) #=> "100644"

    > >
    > > > I need to check the last three numbers from the octal notation which is
    > > > "644"
    > > > which means:
    > > > rw- for user
    > > > r-- for group
    > > > r-- for other

    > >
    > > > Does anyone know how i could check just those three numbers and leave
    > > > the rest

    > >
    > > > Regards
    > > > Loftz
    > > > --
    > > > Posted viahttp://www.ruby-forum.com/.

    > >
    > > If you're wanting to compare to the String "644":
    > >
    > > s.mode.to_s(8)[-3..-1] == "644"
    > >
    > > otherwise, you can mask using bitwise & and compare to the Fixnum
    > > 0644:
    > >
    > > (s.mode & 0xFFF) == 0644
    > > (s.mode & 07777) == 0644
    > >
    > > Jeremy

    >
    > Sorry, those masks will probably work in most cases, but I guess you
    > should really use 0777 or 0x1FF to only get the ugo fields. Otherwise,
    > there might be problems if something is setuid or has the sticky bit
    > set.


    I don't see why it should be necessary to go through string
    representation. You can simply use bit operations:

    s = File.stat("testfile")
    puts "all set!" if s.mode & 0644 != 0

    Peter, or did you mean "set" and not "check"? Then you need to
    actually set the mode, for example

    # untested
    require 'fileutils'
    s = File.stat("testfile")
    FileUtils.chmod(s | 0644, ["testfile"])

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 26, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,712
    Pete Becker
    Jun 2, 2005
  2. Patrick Useldinger

    os.stat('<filename>')[stat.ST_INO] on Windows

    Patrick Useldinger, Feb 27, 2005, in forum: Python
    Replies:
    6
    Views:
    1,178
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Mar 3, 2005
  3. Magesh
    Replies:
    3
    Views:
    444
    Gordon Burditt
    Oct 5, 2007
  4. Rolf Krüger
    Replies:
    2
    Views:
    616
    Ian Collins
    Mar 12, 2008
  5. ruck
    Replies:
    10
    Views:
    1,278
Loading...

Share This Page