[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Has anyone used File.stat to check permissions

Peter Loftus

11/25/2007 9:29:00 PM

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

3 Answers

yermej

11/25/2007 10:36:00 PM

0

On Nov 25, 3:29 pm, Peter Loftus <lof...@gmail.com> 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-....

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

11/25/2007 11:21:00 PM

0

On Nov 25, 4:36 pm, yermej <yer...@gmail.com> wrote:
> On Nov 25, 3:29 pm, Peter Loftus <lof...@gmail.com> 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-....
>
> 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

Robert Klemme

11/26/2007 9:42:00 AM

0

2007/11/26, yermej <yermej@gmail.com>:
> On Nov 25, 4:36 pm, yermej <yer...@gmail.com> wrote:
> > On Nov 25, 3:29 pm, Peter Loftus <lof...@gmail.com> 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-....
> >
> > 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