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

On Tue, Jan 29, 2013 at 4:06 PM, Rob M. [email protected]
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?

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 01/30/2013 11:06 AM, Rob M. wrote:

Rob

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.

OK. That’s what I had assumed.

Thanks,

Rob

OK, so I’ve attached what I have…I think it works correctly :slight_smile:

enjoy,

Rob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs