Module: Inspec::Utils::LinuxMountParser

Included in:
Resources::FileResource, Resources::LinuxMounts
Defined in:
lib/inspec/utils/parser.rb

Instance Method Summary collapse

Instance Method Details

#includes_whitespaces?(mount_line) ⇒ Boolean

Device-/Sharename or Mountpoint includes whitespaces?

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/inspec/utils/parser.rb', line 119

def includes_whitespaces?(mount_line)
  # Split the mount_line by " on "
  parts = mount_line.split(" on ")
  # Check if either part contains spaces
  parts.any? { |part| part.include?(" ") }
end

#parse_mount_options(mount_line, compatibility = false) ⇒ Object

this parses the output of mount command (only tested on linux) this method expects only one line of the mount output



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/inspec/utils/parser.rb', line 71

def parse_mount_options(mount_line, compatibility = false)
  if includes_whitespaces?(mount_line)
    # Device-/Sharenames and Mountpoints including whitespaces require special treatment:
    # We use the keyword ' type ' to split up and rebuild the desired array of fields
    # Split the mount line by the keyword ' type '
    fs_path, other_opts = mount_line.split(" type ", 2)

    # Manually split fs_path into the filesystem and path parts
    fs, path = fs_path.split(" on ", 2)

    # Start building the mount array
    mount = [fs, "on", path, "type"]

    # Split the remaining options by spaces
    other_opts = other_opts.split(/\s+/)

    # Concatenate the options to the mount array
    mount.concat(other_opts)
  else
    # If no whitespace, simply split by spaces
    mount = mount_line.split(/\s+/)
  end

  # parse device and type
  mount_options = { device: mount[0], type: mount[4] }

  if compatibility == false
    # parse options as array
    mount_options[:options] = mount[5].gsub(/\(|\)/, "").split(",")
  else
    Inspec.deprecate(:mount_parser_serverspec_compat, "Parsing mount options in this fashion is deprecated")
    mount_options[:options] = {}
    mount[5].gsub(/\(|\)/, "").split(",").each do |option|
      name, val = option.split("=")
      if val.nil?
        val = true
      elsif val =~ /^\d+$/
        # parse numbers
        val = val.to_i
      end
      mount_options[:options][name.to_sym] = val
    end
  end

  mount_options
end