1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
module Train
class File
class Local < Train::File
%w{
exist? file? socket? directory? symlink? pipe? size basename
}.each do |m|
define_method m.to_sym do
::File.method(m.to_sym).call(@path)
end
end
def content
@content ||= ::File.read(@path, encoding: "UTF-8")
rescue StandardError => _
nil
end
def content=(new_content)
::File.open(@path, "w", encoding: "UTF-8") { |fp| fp.write(new_content) }
@content = new_content
end
def link_path
return nil unless symlink?
begin
@link_path ||= ::File.realpath(@path)
rescue Errno::ELOOP => _
# Leave it blank on symbolic loop, same as readlink
@link_path = ""
end
end
def shallow_link_path
return nil unless symlink?
@link_path ||= ::File.readlink(@path)
end
def block_device?
::File.blockdev?(@path)
end
def character_device?
::File.chardev?(@path)
end
def type
case ::File.ftype(@path)
when "blockSpecial"
:block_device
when "characterSpecial"
:character_device
when "link"
:symlink
when "fifo"
:pipe
else
::File.ftype(@path).to_sym
end
end
%w{
mode owner group uid gid mtime selinux_label
}.each do |field|
define_method field.to_sym do
stat[field.to_sym]
end
end
def mode?(sth)
mode == sth
end
def linked_to?(dst)
link_path == dst
end
end
end
end
# subclass requires are loaded after Train::File::Local is defined
# to avoid superclass mismatch errors
require_relative "local/unix"
require_relative "local/windows"
|