Class: Inspec::Resources::FileResource

Inherits:
Object
  • Object
show all
Includes:
MountParser
Defined in:
lib/resources/file.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

Bond, Directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MountParser

#parse_mount_options

Constructor Details

#initialize(path) ⇒ FileResource

Returns a new instance of FileResource.



26
27
28
# File 'lib/resources/file.rb', line 26

def initialize(path)
  @file = inspec.backend.file(path)
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



25
26
27
# File 'lib/resources/file.rb', line 25

def file
  @file
end

#mount_optionsObject (readonly)

Returns the value of attribute mount_options.



25
26
27
# File 'lib/resources/file.rb', line 25

def mount_options
  @mount_options
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

#contentObject



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

Returns:

  • (Boolean)


64
65
66
67
68
# File 'lib/resources/file.rb', line 64

def executable?(by_usergroup, by_specific_user)
  return false unless exist?

  file_permission_granted?('x', by_usergroup, by_specific_user)
end

#mounted?(expected_options = nil, identical = false) ⇒ Boolean

Returns:

  • (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?(expected_options = nil, identical = false)
  mounted = file.mounted

  # return if no additional parameters have been provided
  return file.mounted? if expected_options.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('#{source_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 ||= parse_mount_options(mounted.stdout, true)

  if identical
    # check if the options should be identical
    @mount_options == expected_options
  else
    # otherwise compare the selected values
    @mount_options.contains(expected_options)
  end
end

#readable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
# File 'lib/resources/file.rb', line 52

def readable?(by_usergroup, by_specific_user)
  return false unless exist?

  file_permission_granted?('r', by_usergroup, by_specific_user)
end

#sgidObject



98
99
100
# File 'lib/resources/file.rb', line 98

def sgid
  (mode & 02000) > 0
end

#stickyObject



102
103
104
# File 'lib/resources/file.rb', line 102

def sticky
  (mode & 01000) > 0
end

#suidObject



94
95
96
# File 'lib/resources/file.rb', line 94

def suid
  (mode & 04000) > 0
end

#to_sObject



106
107
108
# File 'lib/resources/file.rb', line 106

def to_s
  "File #{source_path}"
end

#writable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/resources/file.rb', line 58

def writable?(by_usergroup, by_specific_user)
  return false unless exist?

  file_permission_granted?('w', by_usergroup, by_specific_user)
end