Skip to content

Commit

Permalink
[#1830] Check that sysfs directory exists before reading (#1831)
Browse files Browse the repository at this point in the history
Signed-off-by: David Crosby <[email protected]>
  • Loading branch information
dafyddcrosby authored Aug 13, 2024
1 parent 48e3eef commit b218d3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
26 changes: 14 additions & 12 deletions lib/ohai/plugins/filesystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/unit/plugins/linux/filesystem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit b218d3f

Please sign in to comment.