github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/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 || dragonfly || freebsd
     6  // +build darwin 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  }