github.com/searKing/golang/go@v1.2.117/os/disk_unix.go (about) 1 // Copyright 2022 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build !windows && (android || darwin || dragonfly || freebsd || linux || ios) 6 7 package os 8 9 import ( 10 "syscall" 11 ) 12 13 // DiskUsage returns total and free bytes available in a directory, e.g. `/`. 14 func DiskUsage(path string) (total int64, free int64, avail int64, inodes int64, inodesFree int64, err error) { 15 var st syscall.Statfs_t 16 if err := syscall.Statfs(path, &st); err != nil { 17 return 0, 0, 0, 0, 0, err 18 } 19 reservedBlocks := int64(st.Bfree) - int64(st.Bavail) 20 // Bsize uint64 /* file system block size */ 21 // Frsize uint64 /* fundamental fs block size */ 22 // Blocks uint64 /* number of blocks (unit f_frsize) */ 23 // Bfree uint64 /* free blocks in file system */ 24 // Bavail uint64 /* free blocks for non-root */ 25 // Files uint64 /* total file inodes */ 26 // Ffree uint64 /* free file inodes */ 27 // Favail uint64 /* free file inodes for to non-root */ 28 // Fsid uint64 /* file system id */ 29 // Flag uint64 /* bit mask of f_flag values */ 30 // Namemax uint64 /* maximum filename length */ 31 return int64(st.Bsize) * (int64(st.Blocks) - reservedBlocks), int64(st.Bsize) * int64(st.Bfree), int64(st.Bsize) * int64(st.Bavail), int64(st.Files), int64(st.Ffree), nil 32 }