I need to check the last three numbers from the octal notation which is
Loftz
(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”])
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.