Class: Inspec::Resources::FileResource
- Inherits:
-
Object
- Object
- Inspec::Resources::FileResource
- Includes:
- MountParser
- Defined in:
- lib/resources/file.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#mount_options ⇒ Object
readonly
Returns the value of attribute mount_options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #contain(*_) ⇒ Object
- #content ⇒ Object
- #executable?(by_usergroup, by_specific_user) ⇒ Boolean
-
#gid ⇒ Object
TODO: This is temporary and must be moved to train.
-
#initialize(path) ⇒ FileResource
constructor
A new instance of FileResource.
- #mounted?(expected_options = nil, identical = false) ⇒ Boolean
- #readable?(by_usergroup, by_specific_user) ⇒ Boolean
- #to_s ⇒ Object
-
#uid ⇒ Object
TODO: This is temporary and must be moved to train.
- #writable?(by_usergroup, by_specific_user) ⇒ Boolean
Methods included from MountParser
Constructor Details
#initialize(path) ⇒ FileResource
Returns a new instance of FileResource.
26 27 28 29 |
# File 'lib/resources/file.rb', line 26 def initialize(path) @path = path @file = inspec.backend.file(@path) end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
25 26 27 |
# File 'lib/resources/file.rb', line 25 def file @file end |
#mount_options ⇒ Object (readonly)
Returns the value of attribute mount_options.
25 26 27 |
# File 'lib/resources/file.rb', line 25 def @mount_options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
25 26 27 |
# File 'lib/resources/file.rb', line 25 def path @path end |
Instance Method Details
#contain(*_) ⇒ Object
48 49 50 |
# File 'lib/resources/file.rb', line 48 def contain(*_) fail 'Contain is not supported. Please use standard RSpec matchers.' end |
#content ⇒ Object
42 43 44 45 46 |
# File 'lib/resources/file.rb', line 42 def content res = file.content return nil if res.nil? res.force_encoding('utf-8') end |
#executable?(by_usergroup, by_specific_user) ⇒ Boolean
64 65 66 67 68 |
# File 'lib/resources/file.rb', line 64 def executable?(by_usergroup, by_specific_user) return false unless exist? ('x', by_usergroup, by_specific_user) end |
#gid ⇒ Object
TODO: This is temporary and must be moved to train
102 103 104 105 106 |
# File 'lib/resources/file.rb', line 102 def gid res = inspec.command('stat '+Shellwords.escape(@path)+' -c %u') return nil if res.exit_status != 0 || res.stdout.empty? res.stdout.to_i end |
#mounted?(expected_options = nil, identical = false) ⇒ Boolean
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/resources/file.rb', line 70 def mounted?( = nil, identical = false) mounted = file.mounted # return if no additional parameters have been provided return file.mounted? if .nil? # deprecation warning, this functionality will be removed in future version warn "[DEPRECATION] `be_mounted.with and be_mounted.only_with` are deprecated. Please use `mount('#{path}')` instead." # we cannot read mount data on non-Linux systems return nil if !inspec.os.linux? # parse content if we are on linux @mount_options ||= (mounted.stdout, true) if identical # check if the options should be identical @mount_options == else # otherwise compare the selected values @mount_options.contains() end end |
#readable?(by_usergroup, by_specific_user) ⇒ Boolean
52 53 54 55 56 |
# File 'lib/resources/file.rb', line 52 def readable?(by_usergroup, by_specific_user) return false unless exist? ('r', by_usergroup, by_specific_user) end |
#to_s ⇒ Object
108 109 110 |
# File 'lib/resources/file.rb', line 108 def to_s "File #{path}" end |
#uid ⇒ Object
TODO: This is temporary and must be moved to train
95 96 97 98 99 |
# File 'lib/resources/file.rb', line 95 def uid res = inspec.command('stat '+Shellwords.escape(@path)+' -c %u') return nil if res.exit_status != 0 || res.stdout.empty? res.stdout.to_i end |
#writable?(by_usergroup, by_specific_user) ⇒ Boolean
58 59 60 61 62 |
# File 'lib/resources/file.rb', line 58 def writable?(by_usergroup, by_specific_user) return false unless exist? ('w', by_usergroup, by_specific_user) end |