diff options
-rw-r--r-- | src/cmd/dir.zig | 4 | ||||
-rw-r--r-- | src/statvfs_helper.c | 38 |
2 files changed, 35 insertions, 7 deletions
diff --git a/src/cmd/dir.zig b/src/cmd/dir.zig index 774bd8c..04f74dc 100644 --- a/src/cmd/dir.zig +++ b/src/cmd/dir.zig @@ -93,10 +93,6 @@ fn isLeapYear(year: u32) bool { const GetFreeDiskSpaceError = error{ NotImplemented, AccessDenied, OutOfMemory }; fn getFreeDiskSpace(path: []const u8) GetFreeDiskSpaceError!u64 { - if (@import("builtin").os.tag == .windows) { - return error.NotImplemented; - } - // Create null-terminated path for C function const allocator = std.heap.page_allocator; const null_terminated_path = try allocator.dupeZ(u8, path); diff --git a/src/statvfs_helper.c b/src/statvfs_helper.c index 8f6a4fe..b22d2d1 100644 --- a/src/statvfs_helper.c +++ b/src/statvfs_helper.c @@ -1,9 +1,41 @@ #include <stdint.h> #ifdef _WIN32 +#include <windows.h> +#include <string.h> +#include <stdio.h> + uint64_t statvfs_get_free_space_impl(const char* path) { - // Windows not implemented - (void)path; + ULARGE_INTEGER freeBytesAvailable; + ULARGE_INTEGER totalNumberOfBytes; + ULARGE_INTEGER totalNumberOfFreeBytes; + + // GetDiskFreeSpaceExA expects a directory path, so we need to extract the root directory + // For simplicity, we'll use the current directory if path is a file + char root_path[MAX_PATH]; + + // If path is already a root (like "C:" or "C:\"), use it directly + if (strlen(path) >= 2 && path[1] == ':') { + snprintf(root_path, sizeof(root_path), "%c:\\", path[0]); + } else { + // For relative paths or files, get the current directory + if (GetCurrentDirectoryA(sizeof(root_path), root_path) == 0) { + return 0; + } + // Ensure it ends with backslash for GetDiskFreeSpaceExA + size_t len = strlen(root_path); + if (len > 0 && root_path[len - 1] != '\\') { + if (len < sizeof(root_path) - 1) { + root_path[len] = '\\'; + root_path[len + 1] = '\0'; + } + } + } + + if (GetDiskFreeSpaceExA(root_path, &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)) { + return (uint64_t)freeBytesAvailable.QuadPart; + } + return 0; } #else @@ -16,4 +48,4 @@ uint64_t statvfs_get_free_space_impl(const char* path) { } return (uint64_t)stat.f_frsize * (uint64_t)stat.f_bavail; } -#endif
\ No newline at end of file +#endif |