Sha256: 814821b128e9f060b347546488cc1454ad353022fbaf36d0dfe38c40baaa5014

Contents?: true

Size: 1.33 KB

Versions: 3

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Linux
      class Disk < BaseResolver
        @log = Facter::Log.new(self)
        @semaphore = Mutex.new
        @fact_list ||= {}
        DIR = '/sys/block'
        FILE_PATHS = { model: 'device/model', size: 'size', vendor: 'device/vendor' }.freeze
        class << self
          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { read_facts(fact_name) }
          end

          def read_facts(fact_name)
            @fact_list[:disks] = {}
            build_disks_hash

            FILE_PATHS.each do |key, file|
              @fact_list[:disks].each do |disk, value|
                file_path = File.join(DIR, disk, file)

                next unless File.exist?(file_path)

                result = File.read(file_path).strip

                # Linux always considers sectors to be 512 bytes long independently of the devices real block size.
                value[key] = file =~ /size/ ? result.to_i * 512 : result
              end
            end

            @fact_list[fact_name]
          end

          def build_disks_hash
            directories = Dir.entries(DIR).reject { |dir| dir =~ /\.+/ }
            directories.each { |disk| @fact_list[:disks].merge!(disk => {}) }
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
facter-4.0.9.pre lib/resolvers/disk_resolver.rb
facter-4.0.8.pre lib/resolvers/disk_resolver.rb
facter-4.0.7.pre lib/resolvers/disk_resolver.rb