Module: Wright::Util::File Private

Defined in:
lib/wright/util/file.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Various file methods.

Constant Summary collapse

USER_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  'u' => 04700,
  'g' => 02070,
  'o' => 01007,
  'a' => 07777
}
MODE_MAP =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  'r' => 0444,
  'w' => 0222,
  'x' => 0111,
  's' => 06000,
  't' => 01000
}

Class Method Summary collapse

Class Method Details

.apply_user_mode_masks(base_mode_i, user_mask, op, mode_mask) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


114
115
116
117
118
119
120
121
122
123
# File 'lib/wright/util/file.rb', line 114

def self.apply_user_mode_masks(base_mode_i, user_mask, op, mode_mask)
  case op
  when '='
    (base_mode_i & ~user_mask) | (user_mask & mode_mask)
  when '+'
    base_mode_i | (user_mask & mode_mask)
  when '-'
    base_mode_i & ~(user_mask & mode_mask)
  end
end

.expand_tilde_path(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Expands tilde symbols in file paths. Path elements other than the first one are left alone.

Examples:

Wright::Util::File.expand_tilde_path('~root/foo')# => "/root/foo"


Wright::Util::File.expand_tilde_path('~root/foo/..')# => "/root/foo/.."


Wright::Util::File.expand_tilde_path('../foo/bar')# => "../foo/bar"

Parameters:

  • path (String)

    the file path

Returns:

  • (String)

    the expanded path


228
229
230
231
232
233
# File 'lib/wright/util/file.rb', line 228

def self.expand_tilde_path(path)
  return path unless path.start_with?('~')

  first, *rest = path.split(::File::SEPARATOR)
  ::File.join(::File.expand_path(first), rest)
end

.file_group(path) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a file's owner.

Examples:

FileUtils.touch('foo')
FileUtils.chown(0, 0, 'foo')
Wright::Util::File.file_group('foo')# => 0


Wright::Util::File.file_group('nonexistent')# => nil

Parameters:

  • path (String)

    the file's path

Returns:

  • (Integer)

    the file owner's uid or nil if the file does not exist.


208
209
210
# File 'lib/wright/util/file.rb', line 208

def self.file_group(path)
  ::File.exist?(path) ? ::File.stat(path).gid : nil
end

.file_mode(path) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a file's current mode.

Examples:

FileUtils.touch('foo')
FileUtils.chmod(0644, 'foo')
Wright::Util::File.file_mode('foo').to_s(8)# => "644"

Parameters:

  • path (String)

    the file's path

Returns:

  • (Integer)

    the file mode as an integer or nil if the file does not exist


170
171
172
# File 'lib/wright/util/file.rb', line 170

def self.file_mode(path)
  ::File.exist?(path) ? (::File.stat(path).mode & 07777) : nil
end

.file_owner(path) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a file's owner.

Examples:

FileUtils.touch('foo')
FileUtils.chown(0, 0, 'foo')
Wright::Util::File.file_owner('foo')# => 0


Wright::Util::File.file_owner('nonexistent')# => nil

Parameters:

  • path (String)

    the file's path

Returns:

  • (Integer)

    the file owner's uid or nil if the file does not exist


189
190
191
# File 'lib/wright/util/file.rb', line 189

def self.file_owner(path)
  ::File.exist?(path) ? ::File.stat(path).uid : nil
end

.ln_sfn(target, link_name) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Creates symlinks without descending into directories.

If the file denoted by link_name is a symlink to a directory, ln_sfn does not descend into it. Behaves similar to GNU ln(1) or OpenBSD ln(1) when using ln -sfn target link_name.

Parameters:

  • target (String)

    the link target

  • link_name (String)

    the link name


245
246
247
248
249
250
# File 'lib/wright/util/file.rb', line 245

def self.ln_sfn(target, link_name)
  if ::File.symlink?(link_name) && ::File.directory?(link_name)
    FileUtils.rm(link_name)
  end
  FileUtils.ln_sf(target, link_name)
end

.mode_clause_to_i(mode_clause, base_mode_i, is_directory) ⇒ Integer (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a single symbolic mode clause to an integer mode value.

Examples:

Wright::Util::File.mode_clause_to_i('g+r', 0600, false).to_s(8)# => "640"


Wright::Util::File.mode_clause_to_i('+rw', 0600, false).to_s(8)# => "666"

Parameters:

  • mode_clause (String)

    the symbolic mode clause

  • base_mode_i (Integer)

    the integer base mode

  • is_directory (Bool)

    denotes whether the mode_clause should be treated as a symbolic directory mode clause

Returns:

  • (Integer)

    the mode clause as an integer


104
105
106
107
108
109
110
111
# File 'lib/wright/util/file.rb', line 104

def self.mode_clause_to_i(mode_clause, base_mode_i, is_directory)
  mode_clause = "a#{mode_clause}" if mode_clause =~ /\A[+-=]/
  who, op, perm = mode_clause.split(/([+-=])/)
  perm ||= ''
  user_mask = user_mask(who)
  mode_mask = mode_mask(perm, is_directory)
  apply_user_mode_masks(base_mode_i, user_mask, op, mode_mask)
end

.mode_mask(mode, is_directory) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


56
57
58
59
# File 'lib/wright/util/file.rb', line 56

def self.mode_mask(mode, is_directory)
  mode.gsub!('X', 'x') if is_directory
  mode.chars.reduce(0) { |a, e| a | MODE_MAP[e].to_i }
end

.numeric_mode_to_i(mode) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a numeric mode string to an integer mode.

Examples:

Wright::Util::File.numeric_mode_to_i('0600').to_s(8)# => "600"


Wright::Util::File.numeric_mode_to_i('644').to_s(8)# => "644"


Wright::Util::File.numeric_mode_to_i(0644).to_s(8)# => "644"


Wright::Util::File.numeric_mode_to_i('invalid_mode').to_s(8)# => nil

Parameters:

  • mode (String, #to_i)

    the numeric mode string

Returns:

  • (Integer)

    the mode in integer form or nil if the mode could not be converted


145
146
147
148
# File 'lib/wright/util/file.rb', line 145

def self.numeric_mode_to_i(mode)
  return mode.to_i unless mode.is_a?(String)
  mode =~ /\A[0-7]{3,4}\Z/ ? mode.to_i(8) : nil
end

.symbolic_mode?(mode_str) ⇒ Boolean (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

150
151
152
153
154
155
# File 'lib/wright/util/file.rb', line 150

def self.symbolic_mode?(mode_str)
  return true if mode_str.empty?
  mode_fragment = /([augo]*[+-=][rwxXst]*)/
  mode_re = /\A#{mode_fragment}(,#{mode_fragment})*\Z/
  !(mode_str =~ mode_re).nil?
end

.symbolic_mode_to_i(mode, base_mode, filetype = :file) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a symbolic mode string to an integer mode value.

Examples:

Wright::Util::File.symbolic_mode_to_i('u=rw,go=r', 0400).to_s(8)# => "644"


Wright::Util::File.symbolic_mode_to_i('u=rw,g+r', 0200).to_s(8)# => "640"

Parameters:

  • mode (String)

    the symbolic mode string

  • base_mode (Integer)

    the base mode

  • filetype (Symbol) (defaults to: :file)

    the filetype

Returns:

  • (Integer)

    the integer mode


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/wright/util/file.rb', line 76

def self.symbolic_mode_to_i(mode, base_mode, filetype = :file)
  is_directory = (filetype == :directory)
  unless symbolic_mode?(mode)
    fail ArgumentError, "Invalid file mode \"#{mode}\""
  end
  mode_i = base_mode
  mode.split(/,/).each do |mode_clause|
    mode_i = mode_clause_to_i(mode_clause, mode_i, is_directory)
  end
  mode_i
end

.user_mask(target) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


42
43
44
# File 'lib/wright/util/file.rb', line 42

def self.user_mask(target)
  target.chars.reduce(0) { |a, e| a | USER_MAP[e] }
end