pull/1837/merge
Gustavo Caso 1 day ago committed by GitHub
commit cc251a8cda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -71,6 +71,31 @@ func Usage(path string) (*UsageStat, error) {
return UsageWithContext(context.Background(), path)
}
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
type result struct {
usage *UsageStat
err error
}
resultCh := make(chan result, 1)
go func() {
usage, err := getUsage(ctx, path)
resultCh <- result{usage: usage, err: err}
}()
select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-resultCh:
if res.err != nil {
return nil, res.err
}
return res.usage, nil
}
}
// Partitions returns disk partitions. If all is false, returns
// physical devices only (e.g. hard disks, cd-rom drives, USB keys)
// and ignore all others (e.g. memory partitions such as /dev/shm)

@ -44,7 +44,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
return ret, err
}
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
f, err := perfstat.FileSystemStat()
if err != nil {
return nil, err

@ -84,7 +84,7 @@ func getFsType(stat unix.Statfs_t) string {
return FSType[int(stat.Vfstype)]
}
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
func getUsage(ctx context.Context, path string) (*UsageStat, error) {
out, err := invoke.CommandWithContext(ctx, "df", "-v")
if err != nil {
return nil, err

@ -17,7 +17,7 @@ func PartitionsWithContext(_ context.Context, _ bool) ([]PartitionStat, error) {
return []PartitionStat{}, common.ErrNotImplementedError
}
func UsageWithContext(_ context.Context, _ string) (*UsageStat, error) {
func getUsage(_ context.Context, _ string) (*UsageStat, error) {
return nil, common.ErrNotImplementedError
}

@ -102,7 +102,7 @@ func IOCountersWithContext(_ context.Context, _ ...string) (map[string]IOCounter
return ret, common.ErrNotImplementedError
}
func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := Statvfs{}
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h

@ -122,7 +122,7 @@ func parseDiskstats(buf []byte) (Diskstats, error) {
return ds, nil
}
func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {

@ -199,7 +199,7 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
return ret, nil
}
func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
statvfs := unix.Statvfs_t{}
if err := unix.Statvfs(path, &statvfs); err != nil {
return nil, fmt.Errorf("unable to call statvfs(2) on %q: %w", path, err)

@ -10,7 +10,7 @@ import (
"golang.org/x/sys/unix"
)
func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
stat := unix.Statfs_t{}
err := unix.Statfs(path, &stat)
if err != nil {

@ -55,7 +55,7 @@ func init() {
}
}
func UsageWithContext(_ context.Context, path string) (*UsageStat, error) {
func getUsage(_ context.Context, path string) (*UsageStat, error) {
lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0)

Loading…
Cancel
Save