Module: FileMode
- Extended by:
- ClassMethods
- Defined in:
- lib/file_mode.rb
Overview
Module for UNIX octal file modes. Classes that mixin this module must have a mode attribute that is the integer representation of the mode.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- OCTAL_DIGIT_TO_LISTING =
Mapping between a single octal digit and its permissions listing.
{ 0 => '---', 1 => '--x', 2 => '-w-', 3 => '-wx', 4 => 'r--', 5 => 'r-x', 6 => 'rw-', 7 => 'rwx' }
Class Method Summary collapse
-
.int_to_listing(int) ⇒ Object
Convert an integer into its permissions listing string.
-
.int_to_str(int) ⇒ Object
Convert an integer mode to a 4 digit octal string.
-
.listing_to_int(listing) ⇒ Object
Convert a permissions listing string to its integer equivalent.
-
.listing_to_str(listing) ⇒ Object
Convert permissions listing string to a string of its octal representation.
-
.str_to_int(str) ⇒ Object
Convert an octal string to an integer.
-
.str_to_listing(str) ⇒ Object
Convert an octal string into its permissions listing string.
Methods included from ClassMethods
group_executable?, group_readable?, group_writable?, other_executable?, other_readable?, other_writable?, setgid?, setuid?, sticky?, user_executable?, user_readable?, user_writable?
Class Method Details
.int_to_listing(int) ⇒ Object
Convert an integer into its permissions listing string. Listing strings may vary between operating systems (and even between applications on the same operating system). The listing output here matches the ouput from Linux/GNU coreutils ls(1).
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/file_mode.rb', line 120 def self.int_to_listing(int) perms = [ [6, 00700], [3, 00070], [0, 00007] ].map do |bitshift, mask| OCTAL_DIGIT_TO_LISTING[(int & mask) >> bitshift] end.join if 0 != int & 04000 # setuid perms[2] = perms[2] == 'x' ? 's' : 'S' end if 0 != int & 02000 # setgid perms[5] = perms[5] == 'x' ? 's' : 'S' end if 0 != int & 01000 # sticky perms[8] = perms[8] == 'x' ? 't' : 'T' end perms end |
.int_to_str(int) ⇒ Object
Convert an integer mode to a 4 digit octal string.
78 79 80 |
# File 'lib/file_mode.rb', line 78 def self.int_to_str(int) int.to_s(8).rjust(4, '0') end |
.listing_to_int(listing) ⇒ Object
Convert a permissions listing string to its integer equivalent.
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/file_mode.rb', line 88 def self.listing_to_int(listing) int = listing.chars.map do |c| # l/L are for mandatory file locking on Solaris. %w{ - S T l L }.include?(c) ? 0 : 1 end.join.to_i(2) int += 04000 if %w{ s S }.include?(listing[2]) # setuid int += 02000 if %w{ s S l L }.include?(listing[5]) # setgid int += 01000 if %w{ t T }.include?(listing[8]) # sticky int end |
.listing_to_str(listing) ⇒ Object
Convert permissions listing string to a string of its octal representation.
100 101 102 |
# File 'lib/file_mode.rb', line 100 def self.listing_to_str(listing) int_to_str(listing_to_int(listing)) end |
.str_to_int(str) ⇒ Object
Convert an octal string to an integer.
83 84 85 |
# File 'lib/file_mode.rb', line 83 def self.str_to_int(str) str.to_i(8) end |
.str_to_listing(str) ⇒ Object
Convert an octal string into its permissions listing string.
137 138 139 |
# File 'lib/file_mode.rb', line 137 def self.str_to_listing(str) int_to_listing(str.to_i(8)) end |