2015-07-15 13:15:18 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Vulcano Security GmbH
|
|
|
|
# license: All rights reserved
|
|
|
|
|
2015-08-30 00:14:17 +00:00
|
|
|
module Vulcano::Resources
|
|
|
|
class File < Vulcano.resource(1)
|
|
|
|
name 'file'
|
2015-08-29 07:44:16 +00:00
|
|
|
|
2015-08-30 00:14:17 +00:00
|
|
|
def initialize(path)
|
|
|
|
@path = path
|
2015-08-30 02:33:15 +00:00
|
|
|
@file = vulcano.file(@path)
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
|
2015-08-30 00:14:17 +00:00
|
|
|
%w{
|
2015-09-18 10:35:32 +00:00
|
|
|
type exist? file? block_device? character_device? socket? directory?
|
2015-09-05 14:07:54 +00:00
|
|
|
symlink? pipe? mode mode? owner owned_by? group grouped_into? link_target
|
2015-09-22 17:31:47 +00:00
|
|
|
link_path linked_to? content mtime size selinux_label mounted? immutable?
|
2015-09-05 14:07:54 +00:00
|
|
|
product_version file_version version? md5sum sha256sum
|
2015-09-03 14:17:52 +00:00
|
|
|
}.each do |m|
|
|
|
|
define_method m.to_sym do |*args|
|
|
|
|
@file.method(m.to_sym).call(*args)
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
end
|
|
|
|
|
2015-09-09 16:52:27 +00:00
|
|
|
def contain(*_)
|
2015-09-03 21:24:42 +00:00
|
|
|
fail ' not yet implemented '
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
def readable?(by_owner, by_user)
|
|
|
|
if by_user.nil?
|
|
|
|
m = unix_mode_mask(by_owner, 'r') ||
|
2015-09-03 21:24:42 +00:00
|
|
|
fail("#{by_owner} is not a valid unix owner.")
|
2015-09-05 14:07:54 +00:00
|
|
|
(@file.mask & m) != 0
|
2015-09-01 22:50:52 +00:00
|
|
|
else
|
|
|
|
# TODO: REMOVE THIS FALLBACK
|
|
|
|
Specinfra::Runner.check_file_is_accessible_by_user(@path, by_user, 'r')
|
|
|
|
end
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
def writable?(by_owner, by_user)
|
|
|
|
if by_user.nil?
|
|
|
|
m = unix_mode_mask(by_owner, 'w') ||
|
2015-09-03 21:24:42 +00:00
|
|
|
fail("#{by_owner} is not a valid unix owner.")
|
2015-09-05 14:07:54 +00:00
|
|
|
(@file.mask & m) != 0
|
2015-08-30 00:14:17 +00:00
|
|
|
else
|
2015-09-01 22:50:52 +00:00
|
|
|
# TODO: REMOVE THIS FALLBACK
|
|
|
|
Specinfra::Runner.check_file_is_accessible_by_user(@path, by_user, 'w')
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-06-21 09:23:30 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
def executable?(by_owner, by_user)
|
|
|
|
if by_user.nil?
|
|
|
|
m = unix_mode_mask(by_owner, 'x') ||
|
2015-09-03 21:24:42 +00:00
|
|
|
fail("#{by_owner} is not a valid unix owner.")
|
2015-09-05 14:07:54 +00:00
|
|
|
(@file.mask & m) != 0
|
2015-08-30 00:14:17 +00:00
|
|
|
else
|
2015-09-01 22:50:52 +00:00
|
|
|
# TODO: REMOVE THIS FALLBACK
|
|
|
|
Specinfra::Runner.check_file_is_accessible_by_user(@path, by_user, 'x')
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
end
|
|
|
|
|
2015-08-30 00:14:17 +00:00
|
|
|
def to_s
|
2015-09-03 14:17:52 +00:00
|
|
|
"Path '#{@path}'"
|
2015-08-30 00:14:17 +00:00
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
end
|
|
|
|
end
|