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)


111
112
113
114
# File 'lib/inspec/utils/parser.rb', line 111

def includes_whitespaces?(mount_line)
  ws = mount_line.match(/^(.+)\son\s(.+)\stype\s.*$/)
  ws.captures[0].include?(" ") || ws.captures[1].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
# 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
    type_split = mount_line.split(" type ")
    fs_path = type_split[0]
    other_opts = type_split[1]
    fs, path = fs_path.match(%r{^(.+?)\son\s(/.+?)$}).captures
    mount = [fs, "on", path, "type"]
    mount.concat(other_opts.scan(/\S+/))
  else
    # ... otherwise we just split the fields by whitespaces
    mount = mount_line.scan(/\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