Repository navigation
Expand file tree
/
Copy pathinternal.go
More file actions
707 lines (607 loc) · 17.9 KB
/
Copy pathinternal.go
File metadata and controls
707 lines (607 loc) · 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Define the internal rc functions
package rc
import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"runtime"
"slices"
"strings"
"time"
"github.com/adrg/xdg"
"github.com/coreos/go-semver/semver"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/obscure"
"github.com/rclone/rclone/lib/atexit"
"github.com/rclone/rclone/lib/buildinfo"
"github.com/rclone/rclone/lib/debug"
)
func init() {
Add(Call{
Path: "rc/noopauth",
Fn: rcNoop,
Title: "Echo the input to the output parameters requiring auth",
Help: `
This echoes the input parameters to the output parameters for testing
purposes. It can be used to check that rclone is still alive and to
check that parameter passing is working properly.`,
})
Add(Call{
Path: "rc/noop",
NoAuth: true,
Fn: rcNoop,
Title: "Echo the input to the output parameters",
Help: `
This echoes the input parameters to the output parameters for testing
purposes. It can be used to check that rclone is still alive and to
check that parameter passing is working properly.`,
})
}
// Echo the input to the output parameters
func rcNoop(ctx context.Context, in Params) (out Params, err error) {
return in, nil
}
func init() {
Add(Call{
Path: "rc/error",
NoAuth: true,
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.`,
})
}
// Return an error regardless
func rcError(ctx context.Context, in Params) (out Params, err error) {
return nil, fmt.Errorf("arbitrary error on input %+v", in)
}
func init() {
Add(Call{
Path: "rc/panic",
Fn: rcPanic,
Title: "This returns an error by panicking",
Help: `
This returns an error with the input as part of its error string.
Useful for testing error handling.`,
})
}
// Return an error regardless
func rcPanic(ctx context.Context, in Params) (out Params, err error) {
panic(fmt.Sprintf("arbitrary error on input %+v", in))
}
func init() {
Add(Call{
Path: "rc/fatal",
Fn: rcFatal,
Title: "This returns an fatal error",
Help: `
This returns an error with the input as part of its error string.
Useful for testing error handling.`,
})
}
// Return an error regardless
func rcFatal(ctx context.Context, in Params) (out Params, err error) {
fs.Fatalf(nil, "arbitrary error on input %+v", in)
return nil, nil
}
func init() {
Add(Call{
Path: "rc/list",
NoAuth: true,
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.`,
})
}
// List the registered commands
func rcList(ctx context.Context, in Params) (out Params, err error) {
out = make(Params)
out["commands"] = Calls.List()
return out, nil
}
func init() {
Add(Call{
Path: "core/pid",
Fn: rcPid,
Title: "Return PID of current process",
Help: `
This returns PID of current process.
Useful for stopping rclone process.`,
})
}
// Return PID of current process
func rcPid(ctx context.Context, in Params) (out Params, err error) {
out = make(Params)
out["pid"] = os.Getpid()
return out, nil
}
func init() {
Add(Call{
Path: "core/memstats",
Fn: rcMemStats,
Title: "Returns the memory statistics",
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
`,
})
}
// Return the memory statistics
func rcMemStats(ctx context.Context, in Params) (out Params, err error) {
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
}
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.
`,
})
}
// Do a garbage collection run
func rcGc(ctx context.Context, in Params) (out Params, err error) {
runtime.GC()
return nil, nil
}
func init() {
Add(Call{
Path: "core/version",
NoAuth: true,
Fn: rcVersion,
Title: "Shows the current version of rclone, Go and the OS.",
Help: `
This shows the current versions of rclone, Go and the OS:
- version - rclone version, e.g. "v1.71.2"
- decomposed - version number as [major, minor, patch]
- isGit - boolean - true if this was compiled from the git version
- isBeta - boolean - true if this is a beta version
- os - OS in use as according to Go GOOS (e.g. "linux")
- osKernel - OS Kernel version (e.g. "6.8.0-86-generic (x86_64)")
- osVersion - OS Version (e.g. "ubuntu 24.04 (64 bit)")
- osArch - cpu architecture in use (e.g. "arm64 (ARMv8 compatible)")
- arch - cpu architecture in use according to Go GOARCH (e.g. "arm64")
- goVersion - version of Go runtime in use (e.g. "go1.25.0")
- linking - type of rclone executable (static or dynamic)
- goTags - space separated build tags or "none"
`,
})
}
// Return version info
func rcVersion(ctx context.Context, in Params) (out Params, err error) {
version, err := semver.NewVersion(fs.Version[1:])
if err != nil {
return nil, err
}
linking, tagString := buildinfo.GetLinkingAndTags()
osVersion, osKernel := buildinfo.GetOSVersion()
if osVersion == "" {
osVersion = "unknown"
}
if osKernel == "" {
osKernel = "unknown"
}
out = Params{
"version": fs.Version,
"decomposed": version.Slice(),
"isGit": strings.HasSuffix(fs.Version, "-DEV"),
"isBeta": version.PreRelease != "",
"os": runtime.GOOS,
"osVersion": osVersion,
"osKernel": osKernel,
"osArch": buildinfo.GetArch(),
"arch": runtime.GOARCH,
"goVersion": runtime.Version(),
"linking": linking,
"goTags": tagString,
}
return out, nil
}
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
func rcObscure(ctx context.Context, in Params) (out Params, err error) {
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
}
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
}
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
`,
})
}
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
`,
})
}
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
}
func init() {
Add(Call{
Path: "debug/set-soft-memory-limit",
Fn: rcSetSoftMemoryLimit,
Title: "Call runtime/debug.SetMemoryLimit for setting a soft memory limit for the runtime.",
Help: `
SetMemoryLimit provides the runtime with a soft memory limit.
The runtime undertakes several processes to try to respect this memory limit, including
adjustments to the frequency of garbage collections and returning memory to the underlying
system more aggressively. This limit will be respected even if GOGC=off (or, if SetGCPercent(-1) is executed).
The input limit is provided as bytes, and includes all memory mapped, managed, and not
released by the Go runtime. Notably, it does not account for space used by the Go binary
and memory external to Go, such as memory managed by the underlying system on behalf of
the process, or memory managed by non-Go code inside the same process.
Examples of excluded memory sources include: OS kernel memory held on behalf of the process,
memory allocated by C code, and memory mapped by syscall.Mmap (because it is not managed by the Go runtime).
A zero limit or a limit that's lower than the amount of memory used by the Go runtime may cause
the garbage collector to run nearly continuously. However, the application may still make progress.
The memory limit is always respected by the Go runtime, so to effectively disable this behavior,
set the limit very high. math.MaxInt64 is the canonical value for disabling the limit, but values
much greater than the available memory on the underlying system work just as well.
See https://go.dev/doc/gc-guide for a detailed guide explaining the soft memory limit in more detail,
as well as a variety of common use-cases and scenarios.
SetMemoryLimit returns the previously set memory limit. A negative input does not adjust the limit,
and allows for retrieval of the currently set memory limit.
Parameters:
- mem-limit - int
`,
})
}
func rcSetSoftMemoryLimit(ctx context.Context, in Params) (out Params, err error) {
memLimit, err := in.GetInt64("mem-limit")
if err != nil {
return nil, err
}
oldMemLimit := debug.SetMemoryLimit(memLimit)
out = Params{
"existing-mem-limit": oldMemLimit,
}
return out, nil
}
func init() {
Add(Call{
Path: "debug/set-gc-percent",
Fn: rcSetGCPercent,
Title: "Call runtime/debug.SetGCPercent for setting the garbage collection target percentage.",
Help: `
SetGCPercent sets the garbage collection target percentage: a collection is triggered
when the ratio of freshly allocated data to live data remaining after the previous collection
reaches this percentage. SetGCPercent returns the previous setting. The initial setting is the
value of the GOGC environment variable at startup, or 100 if the variable is not set.
This setting may be effectively reduced in order to maintain a memory limit.
A negative percentage effectively disables garbage collection, unless the memory limit is reached.
See https://pkg.go.dev/runtime/debug#SetMemoryLimit for more details.
Parameters:
- gc-percent - int
`,
})
}
func rcSetGCPercent(ctx context.Context, in Params) (out Params, err error) {
gcPercent, err := in.GetInt64("gc-percent")
if err != nil {
return nil, err
}
oldGCPercent := debug.SetGCPercent(int(gcPercent))
out = Params{
"existing-gc-percent": oldGCPercent,
}
return out, nil
}
func init() {
Add(Call{
Path: "core/command",
Fn: rcRunCommand,
NeedsRequest: true,
NeedsResponse: true,
Title: "Run a rclone terminal command over rc.",
Help: `This takes the following parameters:
- command - a string with the command name.
- arg - a list of arguments for the backend command.
- opt - a map of string to string of options.
- returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
- Defaults to "COMBINED_OUTPUT" if not set.
- The STREAM returnTypes will write the output to the body of the HTTP message.
- The COMBINED_OUTPUT will write the output to the "result" parameter.
Returns:
- result - result from the backend command.
- Only set when using returnType "COMBINED_OUTPUT".
- error - set if rclone exits with an error code.
- returnType - one of ("COMBINED_OUTPUT", "STREAM", "STREAM_ONLY_STDOUT", "STREAM_ONLY_STDERR").
Example:
rclone rc core/command command=ls -a mydrive:/ -o max-depth=1
rclone rc core/command -a ls -a mydrive:/ -o max-depth=1
Returns:
` + "```" + `
{
"error": false,
"result": "<Raw command line output>"
}
OR
{
"error": true,
"result": "<Raw command line output>"
}
` + "```" + `
`,
})
}
// rcRunCommand runs an rclone command with the given args and flags
func rcRunCommand(ctx context.Context, in Params) (out Params, err error) {
command, err := in.GetString("command")
if err != nil {
command = ""
}
var opt = map[string]string{}
err = in.GetStructMissingOK("opt", &opt)
if err != nil {
return nil, err
}
var arg = []string{}
err = in.GetStructMissingOK("arg", &arg)
if err != nil {
return nil, err
}
returnType, err := in.GetString("returnType")
if err != nil {
returnType = "COMBINED_OUTPUT"
}
var httpResponse http.ResponseWriter
httpResponse, err = in.GetHTTPResponseWriter()
if err != nil {
return nil, fmt.Errorf("response object is required\n%w", err)
}
var allArgs = []string{}
if command != "" {
// Add the command e.g.: ls to the args
allArgs = append(allArgs, command)
}
// Add all from arg
allArgs = append(allArgs, arg...)
// Add flags to args for e.g. --max-depth 1 comes in as { max-depth 1 }.
// Convert it to [ max-depth, 1 ] and append to args list
for key, value := range opt {
if len(key) == 1 {
allArgs = append(allArgs, "-"+key)
} else {
allArgs = append(allArgs, "--"+key)
}
allArgs = append(allArgs, value)
}
// Get the path for the current executable which was used to run rclone.
ex, err := os.Executable()
if err != nil {
return nil, err
}
cmd := exec.CommandContext(ctx, ex, allArgs...)
if returnType == "COMBINED_OUTPUT" {
// Run the command and get the output for error and stdout combined.
out, err := cmd.CombinedOutput()
if err != nil {
return Params{
"result": string(out),
"error": true,
}, nil
}
return Params{
"result": string(out),
"error": false,
}, nil
} else if returnType == "STREAM_ONLY_STDOUT" {
cmd.Stdout = httpResponse
} else if returnType == "STREAM_ONLY_STDERR" {
cmd.Stderr = httpResponse
} else if returnType == "STREAM" {
cmd.Stdout = httpResponse
cmd.Stderr = httpResponse
} else {
return nil, fmt.Errorf("unknown returnType %q", returnType)
}
err = cmd.Run()
return nil, err
}
func init() {
Add(Call{
Path: "core/disks",
Fn: rcDisks,
Title: "List the local disks",
Help: `This does not take any parameters
This call is for rclone GUI programs to enumerate local disks and
important directories for doing transfers to and from. The list
returned will include the root directory and the user's home directory
and any mounted disks. The returned items should be usable directly as
remotes.
Returns:
- disks
- This is an array of strings of local disk names
`,
})
}
func mountOK(path string) bool {
if runtime.GOOS == "darwin" {
if strings.HasPrefix(path, "/Volumes/") {
return true
}
} else if runtime.GOOS == "windows" {
return true
} else { // Linux and all other unices
// Fedora/Arch/openSUSE standard
if strings.HasPrefix(path, "/run/media/") {
return true
}
// Ubuntu/Debian standard
if strings.HasPrefix(path, "/media/") {
return true
}
// Traditional unix standard
if strings.HasPrefix(path, "/mnt/") {
return true
}
}
return false
}
// Disks returns likely local disks and some other useful positions
func rcDisks(ctx context.Context, in Params) (out Params, err error) {
disks := []string{}
add := func(s string) {
if s != "/" {
s, _ = strings.CutSuffix(s, "/&qu
5053
ot;)
}
if !slices.Contains(disks, s) {
disks = append(disks, s)
}
}
// Add home directory
home, err := os.UserHomeDir()
if err == nil {
add(home)
}
// Add root directory
if runtime.GOOS != "windows" {
add("/")
}
// Add mount points
for _, mount := range getMounts() {
if mountOK(mount) {
add(mount)
}
}
// Add user directories
add(xdg.UserDirs.Desktop)
add(xdg.UserDirs.Download)
add(xdg.UserDirs.Documents)
add(xdg.UserDirs.Music)
add(xdg.UserDirs.Pictures)
add(xdg.UserDirs.Videos)
out = Params{
"disks": disks,
}
return out, nil
}