Class: Train::File
- Inherits:
-
Object
- Object
- Train::File
- Defined in:
- lib/train/file.rb,
lib/train/file/local.rb,
lib/train/file/remote.rb,
lib/train/file/local/unix.rb,
lib/train/file/remote/aix.rb,
lib/train/file/remote/qnx.rb,
lib/train/file/remote/unix.rb,
lib/train/file/remote/linux.rb,
lib/train/file/local/windows.rb,
lib/train/file/remote/windows.rb
Defined Under Namespace
Constant Summary collapse
- DATA_FIELDS =
interface methods: these fields should be implemented by every backend File
%w{ exist? mode owner group uid gid content mtime size selinux_label path }.freeze
Instance Method Summary collapse
- #block_device? ⇒ Boolean
- #character_device? ⇒ Boolean
- #directory? ⇒ Boolean
- #file? ⇒ Boolean
-
#file_version ⇒ Object
file_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes.
-
#initialize(backend, path, follow_symlink = true) ⇒ File
constructor
A new instance of File.
- #md5sum ⇒ Object
-
#mounted? ⇒ Boolean
if the OS-specific file class supports inquirying as to whether the file/device is mounted, the #mounted method should return a command object whose stdout will not be nil if indeed the device is mounted.
- #owned_by?(sth) ⇒ Boolean
- #path ⇒ Object
- #pipe? ⇒ Boolean
-
#product_version ⇒ Object
product_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes.
-
#sanitize_filename(_path) ⇒ Object
This method gets override by particular os class.
- #sha256sum ⇒ Object
- #socket? ⇒ Boolean
- #source ⇒ Object
- #source_path ⇒ Object
- #symlink? ⇒ Boolean
- #to_json ⇒ Object
- #type ⇒ Object
- #version?(version) ⇒ Boolean
Constructor Details
#initialize(backend, path, follow_symlink = true) ⇒ File
Returns a new instance of File.
14 15 16 17 18 19 20 |
# File 'lib/train/file.rb', line 14 def initialize(backend, path, follow_symlink = true) @backend = backend @path = path || '' @follow_symlink = follow_symlink sanitize_filename(path) end |
Instance Method Details
#block_device? ⇒ Boolean
98 99 100 |
# File 'lib/train/file.rb', line 98 def block_device? type.to_s == 'block_device' end |
#character_device? ⇒ Boolean
102 103 104 |
# File 'lib/train/file.rb', line 102 def character_device? type.to_s == 'character_device' end |
#directory? ⇒ Boolean
118 119 120 |
# File 'lib/train/file.rb', line 118 def directory? type.to_s == 'directory' end |
#file? ⇒ Boolean
110 111 112 |
# File 'lib/train/file.rb', line 110 def file? type.to_s == 'file' end |
#file_version ⇒ Object
file_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes. Since this field is returned for all file objects, the acceptable default value is nil
89 90 91 |
# File 'lib/train/file.rb', line 89 def file_version nil end |
#md5sum ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/train/file.rb', line 51 def md5sum res = Digest::MD5.new res.update(content) res.hexdigest rescue TypeError => _ nil end |
#mounted? ⇒ Boolean
if the OS-specific file class supports inquirying as to whether the file/device is mounted, the #mounted method should return a command object whose stdout will not be nil if indeed the device is mounted.
if the OS-specific file class does not support checking for mount status, the method should not be implemented and this method will return false.
145 146 147 148 149 |
# File 'lib/train/file.rb', line 145 def mounted? return false unless respond_to?(:mounted) !mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.empty? end |
#owned_by?(sth) ⇒ Boolean
126 127 128 |
# File 'lib/train/file.rb', line 126 def owned_by?(sth) owner == sth end |
#path ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/train/file.rb', line 130 def path if symlink? && @follow_symlink link_path else @path end end |
#pipe? ⇒ Boolean
106 107 108 |
# File 'lib/train/file.rb', line 106 def pipe? type.to_s == 'pipe' end |
#product_version ⇒ Object
product_version is primarily used by Windows operating systems only and will be overwritten in Windows-related classes. Since this field is returned for all file objects, the acceptable default value is nil
82 83 84 |
# File 'lib/train/file.rb', line 82 def product_version nil end |
#sanitize_filename(_path) ⇒ Object
This method gets override by particular os class.
23 24 25 |
# File 'lib/train/file.rb', line 23 def sanitize_filename(_path) nil end |
#sha256sum ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/train/file.rb', line 59 def sha256sum res = Digest::SHA256.new res.update(content) res.hexdigest rescue TypeError => _ nil end |
#socket? ⇒ Boolean
114 115 116 |
# File 'lib/train/file.rb', line 114 def socket? type.to_s == 'socket' end |
#source ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/train/file.rb', line 67 def source if @follow_symlink self.class.new(@backend, @path, false) else self end end |
#source_path ⇒ Object
75 76 77 |
# File 'lib/train/file.rb', line 75 def source_path @path end |
#symlink? ⇒ Boolean
122 123 124 |
# File 'lib/train/file.rb', line 122 def symlink? source.type.to_s == 'symlink' end |
#to_json ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/train/file.rb', line 39 def to_json res = Hash[DATA_FIELDS.map { |x| [x, method(x).call] }] # additional fields provided as input res['type'] = type res['follow_symlink'] = @follow_symlink res end |
#type ⇒ Object
47 48 49 |
# File 'lib/train/file.rb', line 47 def type :unknown end |
#version?(version) ⇒ Boolean
93 94 95 96 |
# File 'lib/train/file.rb', line 93 def version?(version) product_version == version or file_version == version end |