Class: Inspec::Resources::File

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) ⇒ File

Returns a new instance of File.



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

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



23
24
25
# File 'lib/resources/file.rb', line 23

def file
  @file
end

#mount_optionsObject (readonly)

Returns the value of attribute mount_options.



23
24
25
# File 'lib/resources/file.rb', line 23

def mount_options
  @mount_options
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/resources/file.rb', line 23

def path
  @path
end

Instance Method Details

#contain(*_) ⇒ Object



40
41
42
# File 'lib/resources/file.rb', line 40

def contain(*_)
  fail 'Contain is not supported. Please use standard RSpec matchers.'
end

#executable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/resources/file.rb', line 56

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)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/resources/file.rb', line 62

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('#{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)


44
45
46
47
48
# File 'lib/resources/file.rb', line 44

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

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

#to_sObject



86
87
88
# File 'lib/resources/file.rb', line 86

def to_s
  "File #{path}"
end

#writable?(by_usergroup, by_specific_user) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/resources/file.rb', line 50

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

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