1
0
mirror of https://github.com/rclone/rclone.git synced 2025-12-06 00:03:32 +00:00

vfs,mount,cmount: use About to return the correct disk total/used/free

Disks total, used, free now shows correctly for mount and cmount (eg
`df` for Unix or in the Windows explorer).
This commit is contained in:
Nick Craig-Wood
2018-04-17 23:19:34 +01:00
parent ef3bcec76c
commit 2b855751fc
4 changed files with 106 additions and 5 deletions

View File

@@ -24,6 +24,7 @@ import (
"os"
"path"
"strings"
"sync"
"sync/atomic"
"time"
@@ -165,11 +166,14 @@ var (
// VFS represents the top level filing system
type VFS struct {
f fs.Fs
root *Dir
Opt Options
cache *cache
cancel context.CancelFunc
f fs.Fs
root *Dir
Opt Options
cache *cache
cancel context.CancelFunc
usageMu sync.Mutex
usageTime time.Time
usage *fs.Usage
}
// Options is options for creating the vfs
@@ -452,3 +456,40 @@ func (vfs *VFS) Rename(oldName, newName string) error {
}
return nil
}
// Statfs returns into about the filing system if known
//
// The values will be -1 if they aren't known
//
// This information is cached for the DirCacheTime interval
func (vfs *VFS) Statfs() (total, used, free int64) {
// defer log.Trace("/", "")("total=%d, used=%d, free=%d", &total, &used, &free)
vfs.usageMu.Lock()
defer vfs.usageMu.Unlock()
total, used, free = -1, -1, -1
doAbout := vfs.f.Features().About
if doAbout == nil {
return
}
if vfs.usageTime.IsZero() || time.Since(vfs.usageTime) >= vfs.Opt.DirCacheTime {
var err error
vfs.usage, err = doAbout()
vfs.usageTime = time.Now()
if err != nil {
fs.Errorf(vfs.f, "Statfs failed: %v", err)
return
}
}
if u := vfs.usage; u != nil {
if u.Total != nil {
total = *u.Total
}
if u.Free != nil {
free = *u.Free
}
if u.Used != nil {
used = *u.Used
}
}
return
}