rc: add core/du to measure local disk usage

This commit is contained in:
Nick Craig-Wood 2023-09-05 10:54:01 +01:00
parent 0fb36562dd
commit 39f910a65d
2 changed files with 71 additions and 0 deletions

View File

@ -12,7 +12,9 @@ import (
"time"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/lib/diskusage"
)
func init() {
@ -620,3 +622,54 @@ func rcBackend(ctx context.Context, in rc.Params) (out rc.Params, err error) {
out["result"] = result
return out, nil
}
// This should really be in fs/rc/internal.go but can't go there due
// to a circular dependency on config.
func init() {
rc.Add(rc.Call{
Path: "core/du",
Fn: rcDu,
Title: "Returns disk usage of a locally attached disk.",
Help: `
This returns the disk usage for the local directory passed in as dir.
If the directory is not passed in, it defaults to the directory
pointed to by --cache-dir.
- dir - string (optional)
Returns:
` + "```" + `
{
"dir": "/",
"info": {
"Available": 361769115648,
"Free": 361785892864,
"Total": 982141468672
}
}
` + "```" + `
`,
})
}
// Terminates app
func rcDu(ctx context.Context, in rc.Params) (out rc.Params, err error) {
dir, err := in.GetString("dir")
if rc.IsErrParamNotFound(err) {
dir = config.GetCacheDir()
} else if err != nil {
return nil, err
}
info, err := diskusage.New(dir)
if err != nil {
return nil, err
}
out = rc.Params{
"dir": dir,
"info": info,
}
return out, nil
}

View File

@ -15,6 +15,7 @@ import (
"github.com/rclone/rclone/fs/operations"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/fstest"
"github.com/rclone/rclone/lib/diskusage"
"github.com/rclone/rclone/lib/rest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@ -577,3 +578,20 @@ func TestRcCommand(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), errTxt)
}
// operations/command: Runs a backend command
func TestRcDu(t *testing.T) {
ctx := context.Background()
_, call := rcNewRun(t, "core/du")
in := rc.Params{}
out, err := call.Fn(ctx, in)
if err == diskusage.ErrUnsupported {
t.Skip(err)
}
assert.NotEqual(t, "", out["dir"])
info := out["info"].(diskusage.Info)
assert.True(t, info.Total != 0)
assert.True(t, info.Total > info.Free)
assert.True(t, info.Total > info.Available)
assert.True(t, info.Free >= info.Available)
}