Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

rc: add core/du to measure local disk usage

Nick Craig-Wood39f910a65d14
diff --git a/fs/operations/rc.go b/fs/operations/rc.go
index 16942bea3..66804ab00 100644
--- a/fs/operations/rc.go
+++ b/fs/operations/rc.go
@@ -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
+}
diff --git a/fs/operations/rc_test.go b/fs/operations/rc_test.go
index b73c63ff9..ebe0994c1 100644
--- a/fs/operations/rc_test.go
+++ b/fs/operations/rc_test.go
@@ -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)
+}