diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-24 06:38:53 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2025-08-24 06:38:53 +0200 |
commit | 41a94a7f354a72af807e25607de65ee98f3d1b63 (patch) | |
tree | d1c5663e34c04d363a3e74a37ee1e21c457a0b00 /src/statvfs_helper.c | |
parent | 14ba60acfb81912e59d434c71be1fd33fc9a383b (diff) |
DIR: Add Windows-compatible statvfs.
Diffstat (limited to 'src/statvfs_helper.c')
-rw-r--r-- | src/statvfs_helper.c | 38 |
1 files changed, 35 insertions, 3 deletions
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 |