MCPcopy Create free account
hub / github.com/NotAShelf/microfetch / get_root_disk_usage

Function get_root_disk_usage

crates/lib/src/system.rs:60–98  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

58#[cfg_attr(feature = "hotpath", hotpath::measure)]
59#[allow(clippy::cast_precision_loss)]
60pub fn get_root_disk_usage() -> Result<String, Error> {
61 let mut vfs = MaybeUninit::<StatfsBuf>::uninit();
62 let path = b"/\0";
63
64 if unsafe { sys_statfs(path.as_ptr(), vfs.as_mut_ptr()) } != 0 {
65 return Err(Error::last_os_error());
66 }
67
68 let vfs = unsafe { vfs.assume_init() };
69 #[allow(clippy::cast_sign_loss)]
70 let block_size = vfs.f_bsize as u64;
71 let total_blocks = vfs.f_blocks;
72 let available_blocks = vfs.f_bavail;
73
74 let total_size = block_size * total_blocks;
75 let used_size = total_size - (block_size * available_blocks);
76
77 let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0);
78 let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0);
79 let usage = (used_size / total_size) * 100.0;
80
81 let no_color = crate::colors::is_no_color();
82 let colors = Colors::new(no_color);
83
84 let mut result = String::with_capacity(64);
85
86 // Manual float formatting
87 write_float(&mut result, used_size, 2);
88 result.push_str(" GiB / ");
89 write_float(&mut result, total_size, 2);
90 result.push_str(" GiB (");
91 result.push_str(colors.cyan);
92 write_float(&mut result, usage, 0);
93 result.push('%');
94 result.push_str(colors.reset);
95 result.push(')');
96
97 Ok(result)
98}
99
100/// Write a float to string with specified decimal places
101#[allow(

Callers 1

runFunction · 0.85

Calls 3

sys_statfsFunction · 0.85
is_no_colorFunction · 0.85
write_floatFunction · 0.85

Tested by

no test coverage detected