feat(macos): add disk info
This commit is contained in:
@@ -5,6 +5,7 @@ const c_sysctl = @cImport(@cInclude("sys/sysctl.h"));
|
||||
const c_iokit = @cImport(@cInclude("IOKit/IOKitLib.h"));
|
||||
const c_core_foundation = @cImport(@cInclude("CoreFoundation/CoreFoundation.h"));
|
||||
const c_mach = @cImport(@cInclude("mach/mach.h"));
|
||||
const c_statvfs = @cImport(@cInclude("sys/statvfs.h"));
|
||||
|
||||
/// Structure representing system uptime in days, hours, and minutes.
|
||||
pub const SystemUptime = struct {
|
||||
@@ -29,6 +30,13 @@ pub const RamInfo = struct {
|
||||
ram_usage_percentage: u8,
|
||||
};
|
||||
|
||||
pub const DiskInfo = struct {
|
||||
disk_path: []const u8,
|
||||
disk_size: f64,
|
||||
disk_usage: f64,
|
||||
disk_usage_percentage: u8,
|
||||
};
|
||||
|
||||
/// Returns the current logged-in uesr's username.
|
||||
/// Uses the environment variable `USER`.
|
||||
/// The caller is responsible for freeing the allocated memory.
|
||||
@@ -256,3 +264,23 @@ pub fn getRamInfo() !RamInfo {
|
||||
|
||||
return ram_info;
|
||||
}
|
||||
|
||||
pub fn getDiskSize(disk_path: []const u8) !DiskInfo {
|
||||
var stat: c_statvfs.struct_statvfs = undefined;
|
||||
if (c_statvfs.statvfs(disk_path.ptr, &stat) != 0) {
|
||||
return error.StatvfsFailed;
|
||||
}
|
||||
|
||||
const total_size = stat.f_blocks * stat.f_frsize;
|
||||
const free_size = stat.f_bavail * stat.f_frsize;
|
||||
const used_size = total_size - free_size;
|
||||
|
||||
const used_size_percentage = (used_size * 100) / total_size;
|
||||
|
||||
return DiskInfo{
|
||||
.disk_path = disk_path,
|
||||
.disk_size = @as(f64, @floatFromInt(total_size)) / 1e9,
|
||||
.disk_usage = @as(f64, @floatFromInt(used_size)) / 1e9,
|
||||
.disk_usage_percentage = @as(u8, @intCast(used_size_percentage)),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user