From 3506e0db7f3834c2f367d5f4b160e73b63b5e875 Mon Sep 17 00:00:00 2001 From: utox39 Date: Sun, 9 Mar 2025 21:21:53 +0100 Subject: [PATCH] feat(macos): add disk info --- src/macos/macos.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/macos/macos.zig b/src/macos/macos.zig index fc5b223..eda6213 100644 --- a/src/macos/macos.zig +++ b/src/macos/macos.zig @@ -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)), + }; +}