Class: Train::File
- Inherits:
-
Object
show all
- 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
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: Local, Remote
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
Constructor Details
#initialize(backend, path, follow_symlink = true) ⇒ File
Returns a new instance of File.
12
13
14
15
16
17
18
|
# File 'lib/train/file.rb', line 12
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
80
81
82
|
# File 'lib/train/file.rb', line 80
def block_device?
type.to_s == 'block_device'
end
|
#character_device? ⇒ Boolean
84
85
86
|
# File 'lib/train/file.rb', line 84
def character_device?
type.to_s == 'character_device'
end
|
#directory? ⇒ Boolean
100
101
102
|
# File 'lib/train/file.rb', line 100
def directory?
type.to_s == 'directory'
end
|
#file? ⇒ Boolean
92
93
94
|
# File 'lib/train/file.rb', line 92
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
71
72
73
|
# File 'lib/train/file.rb', line 71
def file_version
nil
end
|
#md5sum ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/train/file.rb', line 133
def md5sum
return perform_checksum_ruby(:md5) if defined?(@ruby_checksum_fallback)
checksum = if @backend.os.family == 'windows'
perform_checksum_windows(:md5)
else
@md5_command ||= case @backend.os.family
when 'darwin'
'md5 -r'
when 'solaris'
'digest -a md5'
else
'md5sum'
end
perform_checksum_unix(@md5_command)
end
checksum || perform_checksum_ruby(:md5)
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.
127
128
129
130
131
|
# File 'lib/train/file.rb', line 127
def mounted?
return false unless respond_to?(:mounted)
!mounted.nil? && !mounted.stdout.nil? && !mounted.stdout.empty?
end
|
#owned_by?(sth) ⇒ Boolean
108
109
110
|
# File 'lib/train/file.rb', line 108
def owned_by?(sth)
owner == sth
end
|
#path ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/train/file.rb', line 112
def path
if symlink? && @follow_symlink
link_path
else
@path
end
end
|
#pipe? ⇒ Boolean
88
89
90
|
# File 'lib/train/file.rb', line 88
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
64
65
66
|
# File 'lib/train/file.rb', line 64
def product_version
nil
end
|
#sanitize_filename(_path) ⇒ Object
This method gets override by particular os class.
21
22
23
|
# File 'lib/train/file.rb', line 21
def sanitize_filename(_path)
nil
end
|
#sha256sum ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/train/file.rb', line 155
def sha256sum
return perform_checksum_ruby(:sha256) if defined?(@ruby_checksum_fallback)
checksum = if @backend.os.family == 'windows'
perform_checksum_windows(:sha256)
else
@sha256_command ||= case @backend.os.family
when 'darwin', 'hpux', 'qnx'
'shasum -a 256'
when 'solaris'
'digest -a sha256'
else
'sha256sum'
end
perform_checksum_unix(@sha256_command)
end
checksum || perform_checksum_ruby(:sha256)
end
|
#socket? ⇒ Boolean
96
97
98
|
# File 'lib/train/file.rb', line 96
def socket?
type.to_s == 'socket'
end
|
#source ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/train/file.rb', line 49
def source
if @follow_symlink
self.class.new(@backend, @path, false)
else
self
end
end
|
#source_path ⇒ Object
57
58
59
|
# File 'lib/train/file.rb', line 57
def source_path
@path
end
|
#symlink? ⇒ Boolean
104
105
106
|
# File 'lib/train/file.rb', line 104
def symlink?
source.type.to_s == 'symlink'
end
|
#to_json ⇒ Object
37
38
39
40
41
42
43
|
# File 'lib/train/file.rb', line 37
def to_json
res = Hash[DATA_FIELDS.map { |x| [x, method(x).call] }]
res['type'] = type
res['follow_symlink'] = @follow_symlink
res
end
|
#type ⇒ Object
45
46
47
|
# File 'lib/train/file.rb', line 45
def type
:unknown
end
|
#version?(version) ⇒ Boolean
75
76
77
78
|
# File 'lib/train/file.rb', line 75
def version?(version)
product_version == version or
file_version == version
end
|