2018-03-05 19:44:16 +08:00
|
|
|
// Define the internal rc functions
|
|
|
|
|
|
|
|
package rc
|
|
|
|
|
2018-04-05 22:12:34 +08:00
|
|
|
import (
|
2019-06-17 16:34:30 +08:00
|
|
|
"context"
|
2018-04-05 22:12:34 +08:00
|
|
|
"os"
|
2018-04-24 03:10:28 +08:00
|
|
|
"runtime"
|
2019-10-17 22:04:22 +08:00
|
|
|
"time"
|
2018-04-05 22:12:34 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-10-17 22:04:22 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/config/obscure"
|
|
|
|
"github.com/rclone/rclone/fs/version"
|
2019-10-17 22:04:22 +08:00
|
|
|
"github.com/rclone/rclone/lib/atexit"
|
2018-04-05 22:12:34 +08:00
|
|
|
)
|
2018-03-05 19:44:16 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
2018-11-05 00:16:43 +08:00
|
|
|
Path: "rc/noopauth",
|
|
|
|
AuthRequired: true,
|
|
|
|
Fn: rcNoop,
|
|
|
|
Title: "Echo the input to the output parameters requiring auth",
|
2018-03-05 19:44:16 +08:00
|
|
|
Help: `
|
|
|
|
This echoes the input parameters to the output parameters for testing
|
2018-11-04 00:37:09 +08:00
|
|
|
purposes. It can be used to check that rclone is still alive and to
|
|
|
|
check that parameter passing is working properly.`,
|
|
|
|
})
|
|
|
|
Add(Call{
|
2018-11-05 00:16:43 +08:00
|
|
|
Path: "rc/noop",
|
|
|
|
Fn: rcNoop,
|
|
|
|
Title: "Echo the input to the output parameters",
|
2018-11-04 00:37:09 +08:00
|
|
|
Help: `
|
|
|
|
This echoes the input parameters to the output parameters for testing
|
2018-03-05 19:44:16 +08:00
|
|
|
purposes. It can be used to check that rclone is still alive and to
|
|
|
|
check that parameter passing is working properly.`,
|
|
|
|
})
|
2018-11-05 00:16:43 +08:00
|
|
|
}
|
|
|
|
|
2019-04-30 20:06:24 +08:00
|
|
|
// Echo the input to the output parameters
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcNoop(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:16:43 +08:00
|
|
|
return in, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-03-05 19:44:16 +08:00
|
|
|
Add(Call{
|
|
|
|
Path: "rc/error",
|
|
|
|
Fn: rcError,
|
|
|
|
Title: "This returns an error",
|
|
|
|
Help: `
|
|
|
|
This returns an error with the input as part of its error string.
|
|
|
|
Useful for testing error handling.`,
|
|
|
|
})
|
2018-11-05 00:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an error regardless
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcError(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:16:43 +08:00
|
|
|
return nil, errors.Errorf("arbitrary error on input %+v", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-03-05 19:44:16 +08:00
|
|
|
Add(Call{
|
|
|
|
Path: "rc/list",
|
|
|
|
Fn: rcList,
|
|
|
|
Title: "List all the registered remote control commands",
|
|
|
|
Help: `
|
|
|
|
This lists all the registered remote control commands as a JSON map in
|
|
|
|
the commands response.`,
|
|
|
|
})
|
2018-11-05 00:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// List the registered commands
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcList(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:16:43 +08:00
|
|
|
out = make(Params)
|
|
|
|
out["commands"] = Calls.List()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-05 22:12:34 +08:00
|
|
|
Add(Call{
|
2018-04-24 03:44:44 +08:00
|
|
|
Path: "core/pid",
|
2018-04-05 22:12:34 +08:00
|
|
|
Fn: rcPid,
|
|
|
|
Title: "Return PID of current process",
|
|
|
|
Help: `
|
|
|
|
This returns PID of current process.
|
|
|
|
Useful for stopping rclone process.`,
|
|
|
|
})
|
2018-11-05 00:16:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return PID of current process
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcPid(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:16:43 +08:00
|
|
|
out = make(Params)
|
|
|
|
out["pid"] = os.Getpid()
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-04-24 03:10:28 +08:00
|
|
|
Add(Call{
|
|
|
|
Path: "core/memstats",
|
|
|
|
Fn: rcMemStats,
|
2018-04-24 03:44:44 +08:00
|
|
|
Title: "Returns the memory statistics",
|
2018-04-24 03:10:28 +08:00
|
|
|
Help: `
|
|
|
|
This returns the memory statistics of the running program. What the values mean
|
|
|
|
are explained in the go docs: https://golang.org/pkg/runtime/#MemStats
|
|
|
|
|
|
|
|
The most interesting values for most people are:
|
|
|
|
|
|
|
|
* HeapAlloc: This is the amount of memory rclone is actually using
|
|
|
|
* HeapSys: This is the amount of memory rclone has obtained from the OS
|
|
|
|
* Sys: this is the total amount of memory requested from the OS
|
|
|
|
* It is virtual memory so may include unused memory
|
2018-05-11 20:53:13 +08:00
|
|
|
`,
|
|
|
|
})
|
2018-04-05 22:12:34 +08:00
|
|
|
}
|
2018-04-24 03:10:28 +08:00
|
|
|
|
|
|
|
// Return the memory statistics
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcMemStats(ctx context.Context, in Params) (out Params, err error) {
|
2018-04-24 03:10:28 +08:00
|
|
|
out = make(Params)
|
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
out["Alloc"] = m.Alloc
|
|
|
|
out["TotalAlloc"] = m.TotalAlloc
|
|
|
|
out["Sys"] = m.Sys
|
|
|
|
out["Mallocs"] = m.Mallocs
|
|
|
|
out["Frees"] = m.Frees
|
|
|
|
out["HeapAlloc"] = m.HeapAlloc
|
|
|
|
out["HeapSys"] = m.HeapSys
|
|
|
|
out["HeapIdle"] = m.HeapIdle
|
|
|
|
out["HeapInuse"] = m.HeapInuse
|
|
|
|
out["HeapReleased"] = m.HeapReleased
|
|
|
|
out["HeapObjects"] = m.HeapObjects
|
|
|
|
out["StackInuse"] = m.StackInuse
|
|
|
|
out["StackSys"] = m.StackSys
|
|
|
|
out["MSpanInuse"] = m.MSpanInuse
|
|
|
|
out["MSpanSys"] = m.MSpanSys
|
|
|
|
out["MCacheInuse"] = m.MCacheInuse
|
|
|
|
out["MCacheSys"] = m.MCacheSys
|
|
|
|
out["BuckHashSys"] = m.BuckHashSys
|
|
|
|
out["GCSys"] = m.GCSys
|
|
|
|
out["OtherSys"] = m.OtherSys
|
|
|
|
return out, nil
|
|
|
|
}
|
2018-05-11 20:53:13 +08:00
|
|
|
|
2018-11-05 00:16:43 +08:00
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/gc",
|
|
|
|
Fn: rcGc,
|
|
|
|
Title: "Runs a garbage collection.",
|
|
|
|
Help: `
|
|
|
|
This tells the go runtime to do a garbage collection run. It isn't
|
|
|
|
necessary to call this normally, but it can be useful for debugging
|
|
|
|
memory problems.
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-11 20:53:13 +08:00
|
|
|
// Do a garbage collection run
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcGc(ctx context.Context, in Params) (out Params, err error) {
|
2018-05-11 20:53:13 +08:00
|
|
|
runtime.GC()
|
2018-10-28 01:29:20 +08:00
|
|
|
return nil, nil
|
2018-05-11 20:53:13 +08:00
|
|
|
}
|
2018-11-05 00:16:43 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/version",
|
|
|
|
Fn: rcVersion,
|
|
|
|
Title: "Shows the current version of rclone and the go runtime.",
|
|
|
|
Help: `
|
|
|
|
This shows the current version of go and the go runtime
|
2019-10-27 18:32:56 +08:00
|
|
|
|
2018-11-05 00:16:43 +08:00
|
|
|
- version - rclone version, eg "v1.44"
|
|
|
|
- decomposed - version number as [major, minor, patch, subpatch]
|
|
|
|
- note patch and subpatch will be 999 for a git compiled version
|
|
|
|
- isGit - boolean - true if this was compiled from the git version
|
|
|
|
- os - OS in use as according to Go
|
|
|
|
- arch - cpu architecture in use according to Go
|
|
|
|
- goVersion - version of Go runtime in use
|
|
|
|
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return version info
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcVersion(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:16:43 +08:00
|
|
|
decomposed, err := version.New(fs.Version)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = Params{
|
|
|
|
"version": fs.Version,
|
|
|
|
"decomposed": decomposed,
|
|
|
|
"isGit": decomposed.IsGit(),
|
|
|
|
"os": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
"goVersion": runtime.Version(),
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2018-11-05 00:30:56 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/obscure",
|
|
|
|
Fn: rcObscure,
|
|
|
|
Title: "Obscures a string passed in.",
|
|
|
|
Help: `
|
|
|
|
Pass a clear string and rclone will obscure it for the config file:
|
|
|
|
- clear - string
|
|
|
|
|
|
|
|
Returns
|
|
|
|
- obscured - string
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return obscured string
|
2019-06-17 16:34:30 +08:00
|
|
|
func rcObscure(ctx context.Context, in Params) (out Params, err error) {
|
2018-11-05 00:30:56 +08:00
|
|
|
clear, err := in.GetString("clear")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
obscured, err := obscure.Obscure(clear)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
out = Params{
|
|
|
|
"obscured": obscured,
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
2019-10-17 22:04:22 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "core/quit",
|
|
|
|
Fn: rcQuit,
|
|
|
|
Title: "Terminates the app.",
|
|
|
|
Help: `
|
|
|
|
(optional) Pass an exit code to be used for terminating the app:
|
|
|
|
- exitCode - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminates app
|
|
|
|
func rcQuit(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
code, err := in.GetInt64("exitCode")
|
|
|
|
|
|
|
|
if IsErrParamInvalid(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if IsErrParamNotFound(err) {
|
|
|
|
code = 0
|
|
|
|
}
|
|
|
|
exitCode := int(code)
|
|
|
|
|
|
|
|
go func(exitCode int) {
|
|
|
|
time.Sleep(time.Millisecond * 1500)
|
|
|
|
atexit.Run()
|
|
|
|
os.Exit(exitCode)
|
|
|
|
}(exitCode)
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-10-26 23:27:20 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-mutex-profile-fraction",
|
|
|
|
Fn: rcSetMutexProfileFraction,
|
|
|
|
Title: "Set runtime.SetMutexProfileFraction for mutex profiling.",
|
|
|
|
Help: `
|
|
|
|
SetMutexProfileFraction controls the fraction of mutex contention
|
|
|
|
events that are reported in the mutex profile. On average 1/rate
|
|
|
|
events are reported. The previous rate is returned.
|
|
|
|
|
|
|
|
To turn off profiling entirely, pass rate 0. To just read the current
|
|
|
|
rate, pass rate < 0. (For n>1 the details of sampling may change.)
|
|
|
|
|
|
|
|
Once this is set you can look use this to profile the mutex contention:
|
|
|
|
|
|
|
|
go tool pprof http://localhost:5572/debug/pprof/mutex
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
|
|
- rate - int
|
|
|
|
|
|
|
|
Results
|
|
|
|
|
|
|
|
- previousRate - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminates app
|
|
|
|
func rcSetMutexProfileFraction(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
rate, err := in.GetInt64("rate")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
previousRate := runtime.SetMutexProfileFraction(int(rate))
|
|
|
|
out = make(Params)
|
|
|
|
out["previousRate"] = previousRate
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Add(Call{
|
|
|
|
Path: "debug/set-block-profile-rate",
|
|
|
|
Fn: rcSetBlockProfileRate,
|
|
|
|
Title: "Set runtime.SetBlockProfileRate for blocking profiling.",
|
|
|
|
Help: `
|
|
|
|
SetBlockProfileRate controls the fraction of goroutine blocking events
|
|
|
|
that are reported in the blocking profile. The profiler aims to sample
|
|
|
|
an average of one blocking event per rate nanoseconds spent blocked.
|
|
|
|
|
|
|
|
To include every blocking event in the profile, pass rate = 1. To turn
|
|
|
|
off profiling entirely, pass rate <= 0.
|
|
|
|
|
|
|
|
After calling this you can use this to see the blocking profile:
|
|
|
|
|
|
|
|
go tool pprof http://localhost:5572/debug/pprof/block
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
|
|
|
|
- rate - int
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Terminates app
|
|
|
|
func rcSetBlockProfileRate(ctx context.Context, in Params) (out Params, err error) {
|
|
|
|
rate, err := in.GetInt64("rate")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
runtime.SetBlockProfileRate(int(rate))
|
|
|
|
return nil, nil
|
|
|
|
}
|