From b218d3f5d8ff772e2b98a044bfe97791d3b02f53 Mon Sep 17 00:00:00 2001 From: "David T. Crosby" Date: Tue, 13 Aug 2024 14:06:39 -0600 Subject: [PATCH] [#1830] Check that sysfs directory exists before reading (#1831) Signed-off-by: David Crosby --- lib/ohai/plugins/filesystem.rb | 26 ++++++++++++---------- spec/unit/plugins/linux/filesystem_spec.rb | 1 + 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index f6cb6a2b6..a97fb7231 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -276,19 +276,21 @@ def collect_btrfs_data(entry) if entry[:fs_type] == "btrfs" && entry["uuid"] uuid = entry["uuid"] alloc = "/sys/fs/btrfs/#{uuid}/allocation" - %w{data metadata system}.each do |bg_type| - dir = "#{alloc}/#{bg_type}" - %w{single dup}.each do |raid| - if file_exist?("#{dir}/#{raid}") - btrfs["raid"] = raid + if Dir.exist?(alloc) + %w{data metadata system}.each do |bg_type| + dir = "#{alloc}/#{bg_type}" + %w{single dup}.each do |raid| + if file_exist?("#{dir}/#{raid}") + btrfs["raid"] = raid + end + end + logger.trace("Plugin Filesystem: reading btrfs allocation files at #{dir}") + btrfs["allocation"] ||= Mash.new + btrfs["allocation"][bg_type] ||= Mash.new + %w{total_bytes bytes_used}.each do |field| + bytes = file_read("#{dir}/#{field}").chomp.to_i + btrfs["allocation"][bg_type][field] = "#{bytes}" end - end - logger.trace("Plugin Filesystem: reading btrfs allocation files at #{dir}") - btrfs["allocation"] ||= Mash.new - btrfs["allocation"][bg_type] ||= Mash.new - %w{total_bytes bytes_used}.each do |field| - bytes = file_read("#{dir}/#{field}").chomp.to_i - btrfs["allocation"][bg_type][field] = "#{bytes}" end end end diff --git a/spec/unit/plugins/linux/filesystem_spec.rb b/spec/unit/plugins/linux/filesystem_spec.rb index 1cbbe4469..dac141878 100644 --- a/spec/unit/plugins/linux/filesystem_spec.rb +++ b/spec/unit/plugins/linux/filesystem_spec.rb @@ -72,6 +72,7 @@ btrfs_sysfs_base = "/sys/fs/btrfs/d6efda02-1b73-453c-8c74-7d8dee78fa5e/allocation" %w{data metadata system}.each do |bg_type| + allow(Dir).to receive(:exist?).with(btrfs_sysfs_base).and_return(true) allow(File).to receive(:exist?).with("#{btrfs_sysfs_base}/#{bg_type}/single").and_return(true) allow(File).to receive(:exist?).with("#{btrfs_sysfs_base}/#{bg_type}/dup").and_return(false) %w{total_bytes bytes_used}.each do |field|