github.com/searKing/golang/go@v1.2.74/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 // +build !windows 7 // +build android darwin dragonfly freebsd linux ios 8 9 package os 10 11 import ( 12 "syscall" 13 ) 14 15 // DiskUsage returns total and free bytes available in a directory, e.g. `/`. 16 func DiskUsage(path string) (total int64, free int64, avail int64, inodes int64, inodesFree int64, err error) { 17 var st syscall.Statfs_t 18 if err := syscall.Statfs(path, &st); err != nil { 19 return 0, 0, 0, 0, 0, err 20 } 21 reservedBlocks := int64(st.Bfree) - int64(st.Bavail) 22 // Bsize uint64 /* file system block size */ 23 // Frsize uint64 /* fundamental fs block size */ 24 // Blocks uint64 /* number of blocks (unit f_frsize) */ 25 // Bfree uint64 /* free blocks in file system */ 26 // Bavail uint64 /* free blocks for non-root */ 27 // Files uint64 /* total file inodes */ 28 // Ffree uint64 /* free file inodes */ 29 // Favail uint64 /* free file inodes for to non-root */ 30 // Fsid uint64 /* file system id */ 31 // Flag uint64 /* bit mask of f_flag values */ 32 // Namemax uint64 /* maximum filename length */ 33 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 34 }