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