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-01 22:50:52 +00:00
|
|
|
type exists? file? block_device? character_device? socket? directory?
|
|
|
|
symlink? pipe?
|
|
|
|
mode mode? owner owned_by? group grouped_into? link_target linked_to?
|
|
|
|
content mtime ctime size selinux_label
|
|
|
|
mounted? immutable? product_version file_version version?
|
|
|
|
md5sum sha256sum
|
2015-08-30 00:14:17 +00:00
|
|
|
}.each do |name|
|
|
|
|
define_method name.to_sym do |*args|
|
|
|
|
@file.method(name.to_sym).call(*args)
|
|
|
|
end
|
2015-08-29 07:44:16 +00:00
|
|
|
end
|
|
|
|
|
2015-08-30 00:14:17 +00:00
|
|
|
def contain(pattern, from, to)
|
|
|
|
raise ' not yet implemented '
|
|
|
|
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') ||
|
|
|
|
raise("#{by_owner} is not a valid unix owner.")
|
|
|
|
( @file.mask & m ) != 0
|
|
|
|
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') ||
|
|
|
|
raise("#{by_owner} is not a valid unix owner.")
|
|
|
|
( @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') ||
|
|
|
|
raise("#{by_owner} is not a valid unix owner.")
|
|
|
|
( @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
|
|
|
|
'Path "#{@path}"'
|
|
|
|
end
|
2015-09-01 22:50:52 +00:00
|
|
|
|
2015-08-29 07:44:16 +00:00
|
|
|
end
|
|
|
|
end
|