2015-09-08 10:59:36 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
require 'shellwords'
|
|
|
|
|
|
|
|
class Vulcano::Plugins::Backend
|
|
|
|
class LinuxFile < FileCommon
|
2015-09-16 09:22:40 +00:00
|
|
|
attr_reader :path
|
2015-09-08 10:59:36 +00:00
|
|
|
def initialize(backend, path)
|
|
|
|
@backend = backend
|
|
|
|
@path = path
|
|
|
|
@spath = Shellwords.escape(@path)
|
|
|
|
@stat = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def content
|
|
|
|
@content ||= @backend.run_command(
|
|
|
|
"cat #{@spath} 2>/dev/null || echo -n").stdout
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?
|
|
|
|
@exists ||= (
|
|
|
|
@backend.
|
|
|
|
run_command("test -e #{@spath}").
|
|
|
|
exit_status == 0
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-09-15 10:28:36 +00:00
|
|
|
def link_target
|
|
|
|
return @link_target unless @link_target.nil?
|
|
|
|
return @link_target = nil if link_path.nil?
|
|
|
|
@link_target = @backend.file(link_path)
|
|
|
|
end
|
|
|
|
|
2015-09-15 10:18:07 +00:00
|
|
|
def link_path
|
|
|
|
return nil unless symlink?
|
|
|
|
@link_path ||= (
|
2015-09-08 10:59:36 +00:00
|
|
|
@backend.
|
2015-09-10 11:00:04 +00:00
|
|
|
run_command("readlink #{@spath}").stdout.chomp
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mounted?
|
|
|
|
@mounted ||= (
|
|
|
|
!@backend.
|
|
|
|
run_command("mount | grep -- ' on #{@spath}'").
|
|
|
|
stdout.empty?
|
2015-09-08 10:59:36 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
%w{
|
2015-09-15 09:34:09 +00:00
|
|
|
type mode owner group mtime size selinux_label
|
2015-09-08 10:59:36 +00:00
|
|
|
}.each do |field|
|
|
|
|
define_method field.to_sym do
|
|
|
|
stat[field.to_sym]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 09:34:09 +00:00
|
|
|
def product_version
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_version
|
|
|
|
nil
|
|
|
|
end
|
2015-09-08 10:59:36 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
TYPES = {
|
|
|
|
socket: 00140000,
|
|
|
|
symlink: 00120000,
|
|
|
|
file: 00100000,
|
|
|
|
block_device: 00060000,
|
|
|
|
directory: 00040000,
|
|
|
|
character_device: 00020000,
|
2015-09-09 07:42:46 +00:00
|
|
|
pipe: 00010000,
|
2015-09-08 10:59:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def stat
|
|
|
|
return @stat unless @stat.nil?
|
2015-09-15 09:34:09 +00:00
|
|
|
res = @backend.run_command("stat #{@spath} 2>/dev/null --printf '%s\n%f\n%U\n%u\n%G\n%g\n%X\n%Y\n%C'")
|
2015-09-08 10:59:36 +00:00
|
|
|
|
2015-09-15 09:34:09 +00:00
|
|
|
# ignore the exit_code: it is != 0 if selinux labels are not supported
|
|
|
|
# on the system.
|
2015-09-08 10:59:36 +00:00
|
|
|
|
|
|
|
fields = res.stdout.split("\n")
|
2015-09-15 09:34:09 +00:00
|
|
|
if fields.length != 9
|
2015-09-08 10:59:36 +00:00
|
|
|
return @stat = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
tmask = fields[1].to_i(16)
|
|
|
|
type = TYPES.find { |_, mask| mask & tmask == mask }
|
|
|
|
type ||= [:unknown]
|
|
|
|
|
|
|
|
@stat = {
|
|
|
|
type: type[0],
|
|
|
|
mode: tmask & 00777,
|
|
|
|
owner: fields[2],
|
2015-09-10 11:00:04 +00:00
|
|
|
group: fields[4],
|
|
|
|
mtime: fields[7].to_i,
|
|
|
|
size: fields[0].to_i,
|
2015-09-15 09:34:09 +00:00
|
|
|
selinux_label: fields[8],
|
2015-09-08 10:59:36 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|