github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/vfs/disk_usage_unix.go (about) 1 // Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 //go:build darwin || openbsd || dragonfly || freebsd 6 // +build darwin openbsd dragonfly freebsd 7 8 package vfs 9 10 import "golang.org/x/sys/unix" 11 12 func (defaultFS) GetDiskUsage(path string) (DiskUsage, error) { 13 stat := unix.Statfs_t{} 14 if err := unix.Statfs(path, &stat); err != nil { 15 return DiskUsage{}, err 16 } 17 18 freeBytes := uint64(stat.Bsize) * uint64(stat.Bfree) 19 availBytes := uint64(stat.Bsize) * uint64(stat.Bavail) 20 totalBytes := uint64(stat.Bsize) * uint64(stat.Blocks) 21 return DiskUsage{ 22 AvailBytes: availBytes, 23 TotalBytes: totalBytes, 24 UsedBytes: totalBytes - freeBytes, 25 }, nil 26 }