Linux mode bits

Hi,

Is there a class available for handling the Linux mode bits? i.e.
Assuming I have the access mode of a file as a number, e.g. 33188
(octal: 0100644), can I test the mode bits? In Python I can use the
'stat' module which has all of the bits defined as well as tests like:
S_ISREG(). Is there something analogous in Ruby?

Thanks,

Rob

···

--
Posted via http://www.ruby-forum.com/.

minimally, FIle.stat(filename).mode will give the file's mode bits as a Fixnum:

1.9.3-head :007 > "%o" % File.stat('.emacs').mode
=> "100644"

···

On Tue, Jan 29, 2013 at 4:06 PM, Rob Marshall <lists@ruby-forum.com> wrote:

Is there a class available for handling the Linux mode bits? i.e.
Assuming I have the access mode of a file as a number, e.g. 33188
(octal: 0100644), can I test the mode bits? In Python I can use the
'stat' module which has all of the bits defined as well as tests like:
S_ISREG(). Is there something analogous in Ruby?

Sadly the symbolic constants are not exposed (afaik). You can define your own which should be relatively stable if you are just working in a linux environment. For instance (but obviously not complete)

# ruby, why you no expose these for my platform?!!
# ---
S_IFMT = 0170000 # bit mask for the file type bit fields
S_IFDIR = 0040000 # directory

S_ISUID = 0004000 # set UID bit
S_ISGID = 0002000 # set-group-ID bit
S_ISVTX = 0001000 # sticky bit

S_IRUSR = 00400 # owner has read permission
S_IWUSR = 00200 # owner has write permission
S_IXUSR = 00100 # owner has execute permission

S_IRGRP = 00040 # group has read permission
S_IWGRP = 00020 # group has write permission
S_IXGRP = 00010 # group has execute permission

S_IROTH = 00004 # others have read permission
S_IWOTH = 00002 # others have write permission
S_IXOTH = 00001 # others have execute permission

S_IMODE = 07777 # mask for user adjustable mode bits

File.lstat('foo').mode & S_IFMT == S_IFDIR
File.lstat('foo').mode & S_IMODE == S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP

You could wrap it up in a nice module or class if you wanted.

···

On 01/30/2013 11:06 AM, Rob Marshall wrote:

Hi,

Is there a class available for handling the Linux mode bits? i.e.
Assuming I have the access mode of a file as a number, e.g. 33188
(octal: 0100644), can I test the mode bits? In Python I can use the
'stat' module which has all of the bits defined as well as tests like:
S_ISREG(). Is there something analogous in Ruby?

Thanks,

Rob

OK. That's what I had assumed.

Thanks,

Rob

···

--
Posted via http://www.ruby-forum.com/.

OK, so I've attached what I have...I think it works correctly :slight_smile:

enjoy,

Rob

Attachments:
http://www.ruby-forum.com/attachment/8083/FileStat.rb

···

--
Posted via http://www.ruby-forum.com/.