Has anyone used File.stat to check permissions

P

Peter Loftus

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
 
Y

yermej

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

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
 
Y

yermej

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

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
 
R

Robert Klemme

2007/11/26 said:
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

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top