Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Has anyone used File.stat to check permissions

Reply
Thread Tools

Has anyone used File.stat to check permissions

 
 
Peter Loftus
Guest
Posts: n/a
 
      11-25-2007
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/.

 
Reply With Quote
 
 
 
 
yermej
Guest
Posts: n/a
 
      11-25-2007
On Nov 25, 3:29 pm, Peter Loftus <(E-Mail Removed)> 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([-3..-1] == "644"

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

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

Jeremy
 
Reply With Quote
 
 
 
 
yermej
Guest
Posts: n/a
 
      11-25-2007
On Nov 25, 4:36 pm, yermej <(E-Mail Removed)> wrote:
> On Nov 25, 3:29 pm, Peter Loftus <(E-Mail Removed)> 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([-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
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-26-2007
2007/11/26, yermej <(E-Mail Removed)>:
> On Nov 25, 4:36 pm, yermej <(E-Mail Removed)> wrote:
> > On Nov 25, 3:29 pm, Peter Loftus <(E-Mail Removed)> 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([-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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Check if value has already been used Chris Gallagher Ruby 3 12-19-2007 03:27 PM
In-depth documenation on User Permissions, Group Permissions, ACLs, DCLs etc. Curt K ASP .Net 0 11-03-2006 04:54 PM
How check is app has file write security permissions? Ed Sutton ASP .Net 4 10-03-2006 05:07 PM
ASPX file returning obscur runtime error - after changing permissions to a subweb (.net app) to different permissions than on its parent ? Isabelle ASP .Net 0 08-11-2004 02:04 PM
Re: Permissions - giving "everyone" full permissions is bad ? Scott Allen ASP .Net 0 07-13-2004 08:54 PM



Advertisments