Module: Wright::Util::File

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

Overview

Internal: Various file methods.

Class Method Summary collapse

Class Method Details

.expand_tilde_path(path) ⇒ Object

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

path - The file path.

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"

Returns the expanded String path.



236
237
238
239
240
241
# File 'lib/wright/util/file.rb', line 236

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) ⇒ Object

Internal: Get a file’s owner.

path - The file’s path.

Examples

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

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

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



215
216
217
# File 'lib/wright/util/file.rb', line 215

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

.file_mode(path) ⇒ Object

Internal: Get a file’s current mode.

path - The file’s path.

Examples

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

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



175
176
177
# File 'lib/wright/util/file.rb', line 175

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

.file_owner(path) ⇒ Object

Internal: Get a file’s owner.

path - The file’s path.

Examples

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

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

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



195
196
197
# File 'lib/wright/util/file.rb', line 195

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

.ln_sfn(target, link_name) ⇒ Object

Internal: Creates a link named link_name to target.

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 to link_name”.

Returns nothing.



250
251
252
253
254
255
# File 'lib/wright/util/file.rb', line 250

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

.numeric_mode_to_i(mode) ⇒ Object

Internal: Convert a numeric mode string to an integer mode.

mode - The numeric mode string.

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

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



149
150
151
152
# File 'lib/wright/util/file.rb', line 149

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_to_i(mode, base_mode, filetype = :file) ⇒ Object

Internal: Convert a symbolic mode string to an integer mode value.

mode - The symbolic mode string. base_mode - The integer base mode. filetype - The filetype. Defaults to :file.

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"

Returns the integer mode.



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

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