diskusage.go 1.32 KB
Newer Older
1
// Copyright 2021 The go-ethereum Authors
2
// This file is part of go-ethereum.
3
//
4 5
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
6 7 8
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// go-ethereum is distributed in the hope that it will be useful,
10 11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
13
//
14 15
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16

17
//go:build !windows && !openbsd
18
// +build !windows,!openbsd
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

package utils

import (
	"fmt"

	"golang.org/x/sys/unix"
)

func getFreeDiskSpace(path string) (uint64, error) {
	var stat unix.Statfs_t
	if err := unix.Statfs(path, &stat); err != nil {
		return 0, fmt.Errorf("failed to call Statfs: %v", err)
	}

	// Available blocks * size per block = available space in bytes
35
	var bavail = stat.Bavail
36
	// nolint:staticcheck
37 38 39 40 41 42 43
	if stat.Bavail < 0 {
		// FreeBSD can have a negative number of blocks available
		// because of the grace limit.
		bavail = 0
	}
	//nolint:unconvert
	return uint64(bavail) * uint64(stat.Bsize), nil
44
}