module Inspec::Utils::LinuxMountParser

Public Instance Methods

includes_whitespaces?(mount_line) click to toggle source

Device-/Sharename or Mountpoint includes whitespaces?

# 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) click to toggle source

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

# 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