Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: add disk_usage() #23634

Merged
merged 6 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions vlib/builtin/cfns.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -513,3 +513,5 @@ fn C.WrappedNSLog(str &u8)
// absolute value
@[trusted]
fn C.abs(number int) int

fn C.GetDiskFreeSpaceExA(path charptr, free_bytes_available_to_caller &u64, total_number_of_bytes &u64, total_number_of_free_bytes &u64) bool
31 changes: 31 additions & 0 deletions vlib/os/os_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import strings
#include <fcntl.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include <utime.h>

// path_separator is the platform specific separator string, used between the folders
Expand Down Expand Up @@ -530,3 +531,33 @@ fn C.sysconf(name int) i64
pub fn page_size() int {
return int(C.sysconf(C._SC_PAGESIZE))
}

struct C.statvfs {
f_bsize usize
f_blocks usize
f_bfree usize
f_bavail usize
}

fn C.statvfs(path charptr, vfs &C.statvfs) int

// disk_usage returns disk usage of `path`
// Examples:
// ```v
// total,available,used := os.disk_usage('.')
// ```
@[manualfree]
pub fn disk_usage(path string) !(u64, u64, u64) {
kbkpbot marked this conversation as resolved.
Show resolved Hide resolved
mpath := if path == '' { '.' } else { path }
defer { unsafe { mpath.free() } }
mut vfs := C.statvfs{}
ret := C.statvfs(mpath.str, &vfs)
if ret == -1 {
return error('can\`t get disk usage of path')
spytheman marked this conversation as resolved.
Show resolved Hide resolved
}
f_bsize := u64(vfs.f_bsize)
f_blocks := u64(vfs.f_blocks)
f_bavail := u64(vfs.f_bavail)
f_bfree := u64(vfs.f_bfree)
return f_bsize * f_blocks, f_bsize * f_bavail, f_bsize * (f_blocks - f_bfree)
}
7 changes: 7 additions & 0 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -1110,3 +1110,10 @@ fn test_mkdir_at_file_dst() {
}
assert false
}

fn test_disk_usage() {
total, available, used := os.disk_usage('.')!
assert total > 0
assert available > 0
assert used > 0
}
22 changes: 22 additions & 0 deletions vlib/os/os_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -601,3 +601,25 @@ pub fn page_size() int {
C.GetSystemInfo(&sinfo)
return int(sinfo.dwPageSize)
}

// disk_usage returns disk usage of `path`
// Examples:
// ```v
// total,available,used := os.disk_usage('.')
// ```
pub fn disk_usage(path string) !(u64, u64, u64) {
mut free_bytes_available_to_caller := u64(0)
mut total := u64(0)
mut available := u64(0)
mut ret := false
if path == '.' || path == '' {
ret = C.GetDiskFreeSpaceExA(0, &free_bytes_available_to_caller, &total, &available)
} else {
ret = C.GetDiskFreeSpaceExA(path.str, &free_bytes_available_to_caller, &total,
&available)
}
if ret == false {
return error('can\`t get disk usage of path')
spytheman marked this conversation as resolved.
Show resolved Hide resolved
}
return total, available, total - available
}
Loading