Skip to content

Commit

Permalink
use DiskUsage struct
Browse files Browse the repository at this point in the history
  • Loading branch information
kbkpbot committed Feb 2, 2025
1 parent 60320e6 commit 6cd8253
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
7 changes: 7 additions & 0 deletions vlib/os/os.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -1073,3 +1073,10 @@ pub fn error_win32(e SystemError) IError {
panic('Win32 API not available on this platform.')
}
}

pub struct DiskUsage {
pub:
total u64
available u64
used u64
}
12 changes: 6 additions & 6 deletions vlib/os/os_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,8 @@ struct C.statvfs {
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) {
pub fn disk_usage(path string) !DiskUsage {
mpath := if path == '' { '.' } else { path }
defer { unsafe { mpath.free() } }
mut vfs := C.statvfs{}
Expand All @@ -559,5 +555,9 @@ pub fn disk_usage(path string) !(u64, u64, u64) {
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)
return DiskUsage{
total: f_bsize * f_blocks
available: f_bsize * f_bavail
used: f_bsize * (f_blocks - f_bfree)
}
}
8 changes: 4 additions & 4 deletions vlib/os/os_test.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,8 @@ fn test_mkdir_at_file_dst() {
}

fn test_disk_usage() {
total, available, used := os.disk_usage('.')!
assert total > 0
assert available > 0
assert used > 0
usage := os.disk_usage('.')!
assert usage.total > 0
assert usage.available > 0
assert usage.used > 0
}
12 changes: 6 additions & 6 deletions vlib/os/os_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,7 @@ pub fn page_size() int {
}

// disk_usage returns disk usage of `path`
// Examples:
// ```v
// total,available,used := os.disk_usage('.')
// ```
pub fn disk_usage(path string) !(u64, u64, u64) {
pub fn disk_usage(path string) !DiskUsage {
mut free_bytes_available_to_caller := u64(0)
mut total := u64(0)
mut available := u64(0)
Expand All @@ -621,5 +617,9 @@ pub fn disk_usage(path string) !(u64, u64, u64) {
if ret == false {
return error('can\`t get disk usage of path')
}
return total, available, total - available
return DiskUsage{
total: total
available: available
used: total - available
}
}

0 comments on commit 6cd8253

Please sign in to comment.