Project

file_mode

0.0
No commit activity in last 3 years
No release in over 3 years
Provides class methods and mixin to convert UNIX file permissions between long listing and 4-digit octal formats
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

file_mode¶ ↑

Author

Martin Carpenter

Email

mcarpenter@free.fr

Copyright

Copyright © Martin Carpenter 2014

About¶ ↑

The file_mode gem assists with manipulation of four-octal-digit UNIX file modes by providing readable method names rather than error-prone bit twiddles. It also provides methods to convert to and from standard ls(1)-style long listing formats. The class methods can be used standalone or this module can be mixed in to a class that has an integer :mode attribute.

Examples¶ ↑

Class method use¶ ↑

require 'file_mode'

FileMode.user_readable?(0o644)
=> true
FileMode.setuid?(0o4755)
=> true
FileMode.int_to_listing(0o644)
=> "rw-r--r--"
FileMode.listing_to_str('rwsr-xr-x')
=> "4755"

Mixin use¶ ↑

require 'file_mode'

class FileLikeThing
  include FileMode
  attr_accessor :mode
end

flt = FileLikeThing.new
flt.mode = 0o0644
flt.user_readable?
=> true
flt.setuid?
=> false

Mode formats¶ ↑

The module offers three forms of mode representation:

  • integer (eg 0o0644 or 420 decimal)

  • string (‘0644’)

  • listing (‘rw-r–r–’)

Listing formats are not necessarily identical across UNIX-like operating systems, or even across applications on the same operating system. A case in point is Solaris which shows mandatory file lock permission as ‘l’ with standard ls(1) and ‘L’ with XPG-compatible ls(1).

This module aims to take any standard format as input (listing_to_… methods) and targets Linux/GNU coreutils for output(…_to_listing methods).

Some conversion gotchas¶ ↑

Beware the following standard ruby gotchas when making conversions using #to_i and a solitary leading zero to specify octal mode:

644.to_i
=> 644       # ok
'644'.to_i
=> 644       # ok
0644.to_i
=> 420       # ok
'0644'.to_i
=> 644       # ! leading zero is ignored
'0o644'.to_i
=> 0         # ! parsing stops at 'o'

Unit tests¶ ↑

Unit tests are provided particularly for the conversion between the ls(1)-style long-listing format and integer representations. Since the long-listing format can vary between systems a script bin/create_listing_test.rb is provided to generate unit test files.