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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
|
package operations
import (
"context"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"net/http"
"path"
"strings"
"time"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config"
"github.com/rclone/rclone/fs/hash"
"github.com/rclone/rclone/fs/rc"
"github.com/rclone/rclone/lib/diskusage"
)
func init() {
rc.Add(rc.Call{
Path: "operations/list",
AuthRequired: true,
Fn: rcList,
Title: "List the given remote and path in JSON format",
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:"
- remote - a path within that remote e.g. "dir"
- opt - a dictionary of options to control the listing (optional)
- recurse - If set recurse directories
- noModTime - If set return modification time
- showEncrypted - If set show decrypted names
- showOrigIDs - If set show the IDs for each item if known
- showHash - If set return a dictionary of hashes
- noMimeType - If set don't show mime types
- dirsOnly - If set only show directories
- filesOnly - If set only show files
- metadata - If set return metadata of objects also
- hashTypes - array of strings of hash types to show if showHash set
Returns:
- list
- This is an array of objects as described in the lsjson command
See the [lsjson](/commands/rclone_lsjson/) command for more information on the above and examples.
`,
})
}
// List the directory
func rcList(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, remote, err := rc.GetFsAndRemote(ctx, in)
if err != nil {
return nil, err
}
var opt ListJSONOpt
err = in.GetStruct("opt", &opt)
if rc.NotErrParamNotFound(err) {
return nil, err
}
list := []*ListJSONItem{}
err = ListJSON(ctx, f, remote, &opt, func(item *ListJSONItem) error {
list = append(list, item)
return nil
})
if err != nil {
return nil, err
}
out = make(rc.Params)
out["list"] = list
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "operations/stat",
AuthRequired: true,
Fn: rcStat,
Title: "Give information about the supplied file or directory",
Help: `This takes the following parameters
- fs - a remote name string eg "drive:"
- remote - a path within that remote eg "dir"
- opt - a dictionary of options to control the listing (optional)
- see operations/list for the options
The result is
- item - an object as described in the lsjson command. Will be null if not found.
Note that if you are only interested in files then it is much more
efficient to set the filesOnly flag in the options.
See the [lsjson](/commands/rclone_lsjson/) command for more information on the above and examples.
`,
})
}
// List the directory
func rcStat(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, remote, err := rc.GetFsAndRemote(ctx, in)
if err != nil {
return nil, err
}
var opt ListJSONOpt
err = in.GetStruct("opt", &opt)
if rc.NotErrParamNotFound(err) {
return nil, err
}
item, err := StatJSON(ctx, f, remote, &opt)
if err != nil {
return nil, err
}
out = make(rc.Params)
out["item"] = item
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "operations/about",
AuthRequired: true,
Fn: rcAbout,
Title: "Return the space used on the remote",
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:"
The result is as returned from rclone about --json
See the [about](/commands/rclone_about/) command for more information on the above.
`,
})
}
// About the remote
func rcAbout(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, err := rc.GetFs(ctx, in)
if err != nil {
return nil, err
}
doAbout := f.Features().About
if doAbout == nil {
return nil, fmt.Errorf("%v doesn't support about", f)
}
u, err := doAbout(ctx)
if err != nil {
return nil, fmt.Errorf("about call failed: %w", err)
}
err = rc.Reshape(&out, u)
if err != nil {
return nil, fmt.Errorf("about Reshape failed: %w", err)
}
return out, nil
}
func init() {
for _, copy := range []bool{false, true} {
name := "Move"
if copy {
name = "Copy"
}
rc.Add(rc.Call{
Path: "operations/" + strings.ToLower(name) + "file",
AuthRequired: true,
Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
return rcMoveOrCopyFile(ctx, in, copy)
},
Title: name + " a file from source remote to destination remote",
Help: `This takes the following parameters:
- srcFs - a remote name string e.g. "drive:" for the source, "/" for local filesystem
- srcRemote - a path within that remote e.g. "file.txt" for the source
- dstFs - a remote name string e.g. "drive2:" for the destination, "/" for local filesystem
- dstRemote - a path within that remote e.g. "file2.txt" for the destination
`,
})
}
}
// Copy a file
func rcMoveOrCopyFile(ctx context.Context, in rc.Params, cp bool) (out rc.Params, err error) {
srcFs, srcRemote, err := rc.GetFsAndRemoteNamed(ctx, in, "srcFs", "srcRemote")
if err != nil {
return nil, err
}
dstFs, dstRemote, err := rc.GetFsAndRemoteNamed(ctx, in, "dstFs", "dstRemote")
if err != nil {
return nil, err
}
return nil, moveOrCopyFile(ctx, dstFs, srcFs, dstRemote, srcRemote, cp, false)
}
func init() {
for _, op := range []struct {
name string
title string
help string
noRemote bool
needsRequest bool
noCommand bool
}{
{name: "mkdir", title: "Make a destination directory or container"},
{name: "rmdir", title: "Remove an empty directory or container"},
{name: "purge", title: "Remove a directory or container and all of its contents"},
{name: "rmdirs", title: "Remove all the empty directories in the path", help: "- leaveRoot - boolean, set to true not to delete the root\n"},
{name: "delete", title: "Remove files in the path", noRemote: true},
{name: "deletefile", title: "Remove the single file pointed to"},
{name: "copyurl", title: "Copy the URL to the object", help: "- url - string, URL to read from\n - autoFilename - boolean, set to true to retrieve destination file name from url\n"},
{name: "uploadfile", title: "Upload file using multiform/form-data", help: "- each part in body represents a file to be uploaded\n", needsRequest: true, noCommand: true},
{name: "cleanup", title: "Remove trashed files in the remote or path", noRemote: true},
{name: "settier", title: "Changes storage tier or class on all files in the path", noRemote: true},
{name: "settierfile", title: "Changes storage tier or class on the single file pointed to", noCommand: true},
} {
var remote, command string
if !op.noRemote {
remote = "- remote - a path within that remote e.g. \"dir\"\n"
}
if !op.noCommand {
command = "See the [" + op.name + "](/commands/rclone_" + op.name + "/) command for more information on the above.\n"
}
rc.Add(rc.Call{
Path: "operations/" + op.name,
AuthRequired: true,
NeedsRequest: op.needsRequest,
Fn: func(ctx context.Context, in rc.Params) (rc.Params, error) {
return rcSingleCommand(ctx, in, op.name, op.noRemote)
},
Title: op.title,
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:"
` + remote + op.help + "\n" + command,
})
}
}
// Run a single command, e.g. Mkdir
func rcSingleCommand(ctx context.Context, in rc.Params, name string, noRemote bool) (out rc.Params, err error) {
var (
f fs.Fs
remote string
)
if noRemote {
f, err = rc.GetFs(ctx, in)
} else {
f, remote, err = rc.GetFsAndRemote(ctx, in)
}
if err != nil {
return nil, err
}
switch name {
case "mkdir":
return nil, Mkdir(ctx, f, remote)
case "rmdir":
return nil, Rmdir(ctx, f, remote)
case "purge":
return nil, Purge(ctx, f, remote)
case "rmdirs":
leaveRoot, err := in.GetBool("leaveRoot")
if rc.NotErrParamNotFound(err) {
return nil, err
}
return nil, Rmdirs(ctx, f, remote, leaveRoot)
case "delete":
return nil, Delete(ctx, f)
case "deletefile":
o, err := f.NewObject(ctx, remote)
if err != nil {
return nil, err
}
return nil, DeleteFile(ctx, o)
case "copyurl":
url, err := in.GetString("url")
if err != nil {
return nil, err
}
autoFilename, _ := in.GetBool("autoFilename")
noClobber, _ := in.GetBool("noClobber")
headerFilename, _ := in.GetBool("headerFilename")
_, err = CopyURL(ctx, f, remote, url, autoFilename, headerFilename, noClobber)
return nil, err
case "uploadfile":
var request *http.Request
request, err := in.GetHTTPRequest()
if err != nil {
return nil, err
}
contentType := request.Header.Get("Content-Type")
mediaType, params, err := mime.ParseMediaType(contentType)
if err != nil {
return nil, err
}
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(request.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err == io.EOF {
return nil, nil
}
if err != nil {
return nil, err
}
if p.FileName() != "" {
obj, err := Rcat(ctx, f, path.Join(remote, p.FileName()), p, time.Now(), nil)
if err != nil {
return nil, err
}
fs.Debugf(obj, "Upload Succeeded")
}
}
}
return nil, nil
case "cleanup":
return nil, CleanUp(ctx, f)
case "settier":
if !f.Features().SetTier {
return nil, fmt.Errorf("remote %s does not support settier", f.Name())
}
tier, err := in.GetString("tier")
if err != nil {
return nil, err
}
return nil, SetTier(ctx, f, tier)
case "settierfile":
if !f.Features().SetTier {
return nil, fmt.Errorf("remote %s does not support settier", f.Name())
}
tier, err := in.GetString("tier")
if err != nil {
return nil, err
}
o, err := f.NewObject(ctx, remote)
if err != nil {
return nil, err
}
return nil, SetTierFile(ctx, o, tier)
}
panic("unknown rcSingleCommand type")
}
func init() {
rc.Add(rc.Call{
Path: "operations/size",
AuthRequired: true,
Fn: rcSize,
Title: "Count the number of bytes and files in remote",
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:path/to/dir"
Returns:
- count - number of files
- bytes - number of bytes in those files
See the [size](/commands/rclone_size/) command for more information on the above.
`,
})
}
// Size a directory
func rcSize(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, err := rc.GetFs(ctx, in)
if err != nil {
return nil, err
}
count, bytes, sizeless, err := Count(ctx, f)
if err != nil {
return nil, err
}
out = make(rc.Params)
out["count"] = count
out["bytes"] = bytes
out["sizeless"] = sizeless
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "operations/publiclink",
AuthRequired: true,
Fn: rcPublicLink,
Title: "Create or retrieve a public link to the given file or folder.",
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:"
- remote - a path within that remote e.g. "dir"
- unlink - boolean - if set removes the link rather than adding it (optional)
- expire - string - the expiry time of the link e.g. "1d" (optional)
Returns:
- url - URL of the resource
See the [link](/commands/rclone_link/) command for more information on the above.
`,
})
}
// Make a public link
func rcPublicLink(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, remote, err := rc.GetFsAndRemote(ctx, in)
if err != nil {
return nil, err
}
unlink, _ := in.GetBool("unlink")
expire, err := in.GetDuration("expire")
if rc.IsErrParamNotFound(err) {
expire = time.Duration(fs.DurationOff)
} else if err != nil {
return nil, err
}
url, err := PublicLink(ctx, f, remote, fs.Duration(expire), unlink)
if err != nil {
return nil, err
}
out = make(rc.Params)
out["url"] = url
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "operations/fsinfo",
Fn: rcFsInfo,
Title: "Return information about the remote",
Help: `This takes the following parameters:
- fs - a remote name string e.g. "drive:"
This returns info about the remote passed in;
` + "```" + `
{
// optional features and whether they are available or not
"Features": {
"About": true,
"BucketBased": false,
"BucketBasedRootOK": false,
"CanHaveEmptyDirectories": true,
"CaseInsensitive": false,
"ChangeNotify": false,
"CleanUp": false,
"Command": true,
"Copy": false,
"DirCacheFlush": false,
"DirMove": true,
"Disconnect": false,
"DuplicateFiles": false,
"GetTier": false,
"IsLocal": true,
"ListR": false,
"MergeDirs": false,
"MetadataInfo": true,
"Move": true,
"OpenWriterAt": true,
"PublicLink": false,
"Purge": true,
"PutStream": true,
"PutUnchecked": false,
"ReadMetadata": true,
"ReadMimeType": false,
"ServerSideAcrossConfigs": false,
"SetTier": false,
"SetWrapper": false,
"Shutdown": false,
"SlowHash": true,
"SlowModTime": false,
"UnWrap": false,
"UserInfo": false,
"UserMetadata": true,
"WrapFs": false,
"WriteMetadata": true,
"WriteMimeType": false
},
// Names of hashes available
"Hashes": [
"md5",
"sha1",
"whirlpool",
"crc32",
"sha256",
"dropbox",
"mailru",
"quickxor"
],
"Name": "local", // Name as created
"Precision": 1, // Precision of timestamps in ns
"Root": "/", // Path as created
"String": "Local file system at /", // how the remote will appear in logs
// Information about the system metadata for this backend
"MetadataInfo": {
"System": {
"atime": {
"Help": "Time of last access",
"Type": "RFC 3339",
"Example": "2006-01-02T15:04:05.999999999Z07:00"
},
"btime": {
"Help": "Time of file birth (creation)",
"Type": "RFC 3339",
"Example": "2006-01-02T15:04:05.999999999Z07:00"
},
"gid": {
"Help": "Group ID of owner",
"Type": "decimal number",
"Example": "500"
},
"mode": {
"Help": "File type and mode",
"Type": "octal, unix style",
"Example": "0100664"
},
"mtime": {
"Help": "Time of last modification",
"Type": "RFC 3339",
"Example": "2006-01-02T15:04:05.999999999Z07:00"
},
"rdev": {
"Help": "Device ID (if special file)",
"Type": "hexadecimal",
"Example": "1abc"
},
"uid": {
"Help": "User ID of owner",
"Type": "decimal number",
"Example": "500"
}
},
"Help": "Textual help string\n"
}
}
` + "```" + `
This command does not have a command line equivalent so use this instead:
rclone rc --loopback operations/fsinfo fs=remote:
`,
})
}
// Fsinfo the remote
func rcFsInfo(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, err := rc.GetFs(ctx, in)
if err != nil {
return nil, err
}
info := GetFsInfo(f)
err = rc.Reshape(&out, info)
if err != nil {
return nil, fmt.Errorf("fsinfo Reshape failed: %w", err)
}
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "backend/command",
AuthRequired: true,
Fn: rcBackend,
Title: "Runs a backend command.",
Help: `This takes the following parameters:
- command - a string with the command name
- fs - a remote name string e.g. "drive:"
- arg - a list of arguments for the backend command
- opt - a map of string to string of options
Returns:
- result - result from the backend command
Example:
rclone rc backend/command command=noop fs=. -o echo=yes -o blue -a path1 -a path2
Returns
` + "```" + `
{
"result": {
"arg": [
"path1",
"path2"
],
"name": "noop",
"opt": {
"blue": "",
"echo": "yes"
}
}
}
` + "```" + `
Note that this is the direct equivalent of using this "backend"
command:
rclone backend noop . -o echo=yes -o blue path1 path2
Note that arguments must be preceded by the "-a" flag
See the [backend](/commands/rclone_backend/) command for more information.
`,
})
}
// Make a public link
func rcBackend(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, err := rc.GetFs(ctx, in)
if err != nil {
return nil, err
}
doCommand := f.Features().Command
if doCommand == nil {
return nil, fmt.Errorf("%v: doesn't support backend commands", f)
}
command, err := in.GetString("command")
if err != nil {
return nil, err
}
opt := map[string]string{}
err = in.GetStructMissingOK("opt", &opt)
if err != nil {
return nil, err
}
arg := []string{}
err = in.GetStructMissingOK("arg", &arg)
if err != nil {
return nil, err
}
result, err := doCommand(ctx, command, arg, opt)
if err != nil {
return nil, fmt.Errorf("command %q failed: %w", command, err)
}
out = make(rc.Params)
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
}
func init() {
rc.Add(rc.Call{
Path: "operations/check",
AuthRequired: true,
Fn: rcCheck,
Title: "check the source and destination are the same",
Help: `Checks the files in the source and destination match. It compares
sizes and hashes and logs a report of files that don't
match. It doesn't alter the source or destination.
This takes the following parameters:
- srcFs - a remote name string e.g. "drive:" for the source, "/" for local filesystem
- dstFs - a remote name string e.g. "drive2:" for the destination, "/" for local filesystem
- download - check by downloading rather than with hash
- checkFileHash - treat checkFileFs:checkFileRemote as a SUM file with hashes of given type
- checkFileFs - treat checkFileFs:checkFileRemote as a SUM file with hashes of given type
- checkFileRemote - treat checkFileFs:checkFileRemote as a SUM file with hashes of given type
- oneWay - check one way only, source files must exist on remote
- combined - make a combined report of changes (default false)
- missingOnSrc - report all files missing from the source (default true)
- missingOnDst - report all files missing from the destination (default true)
- match - report all matching files (default false)
- differ - report all non-matching files (default true)
- error - report all files with errors (hashing or reading) (default true)
If you supply the download flag, it will download the data from
both remotes and check them against each other on the fly. This can
be useful for remotes that don't support hashes or if you really want
to check all the data.
If you supply the size-only global flag, it will only compare the sizes not
the hashes as well. Use this for a quick check.
If you supply the checkFileHash option with a valid hash name, the
checkFileFs:checkFileRemote must point to a text file in the SUM
format. This treats the checksum file as the source and dstFs as the
destination. Note that srcFs is not used and should not be supplied in
this case.
Returns:
- success - true if no error, false otherwise
- status - textual summary of check, OK or text string
- hashType - hash used in check, may be missing
- combined - array of strings of combined report of changes
- missingOnSrc - array of strings of all files missing from the source
- missingOnDst - array of strings of all files missing from the destination
- match - array of strings of all matching files
- differ - array of strings of all non-matching files
- error - array of strings of all files with errors (hashing or reading)
`,
})
}
// Writer which writes into the slice provided
type stringWriter struct {
out *[]string
}
// Write writes len(p) bytes from p to the underlying data stream. It returns
// the number of bytes written from p (0 <= n <= len(p)) and any error
// encountered that caused the write to stop early. Write must return a non-nil
// error if it returns n < len(p). Write must not modify the slice data,
// even temporarily.
//
// Implementations must not retain p.
func (s stringWriter) Write(p []byte) (n int, err error) {
result := string(p)
result = strings.TrimSuffix(result, "\n")
*s.out = append(*s.out, result)
return len(p), nil
}
// Check two directories
func rcCheck(ctx context.Context, in rc.Params) (out rc.Params, err error) {
srcFs, err := rc.GetFsNamed(ctx, in, "srcFs")
if err != nil && !rc.IsErrParamNotFound(err) {
return nil, err
}
dstFs, err := rc.GetFsNamed(ctx, in, "dstFs")
if err != nil {
return nil, err
}
checkFileFs, checkFileRemote, err := rc.GetFsAndRemoteNamed(ctx, in, "checkFileFs", "checkFileRemote")
if err != nil && !rc.IsErrParamNotFound(err) {
return nil, err
}
checkFileHash, err := in.GetString("checkFileHash")
if err != nil && !rc.IsErrParamNotFound(err) {
return nil, err
}
checkFileSet := 0
if checkFileHash != "" {
checkFileSet++
}
if checkFileFs != nil {
checkFileSet++
}
if checkFileRemote != "" {
checkFileSet++
}
if checkFileSet > 0 && checkFileSet < 3 {
return nil, fmt.Errorf("need all of checkFileFs, checkFileRemote, checkFileHash to be set together")
}
var checkFileHashType hash.Type
if checkFileHash != "" {
if err := checkFileHashType.Set(checkFileHash); err != nil {
return nil, err
}
if srcFs != nil {
return nil, rc.NewErrParamInvalid(errors.New("only supply dstFs when using checkFileHash"))
}
} else if srcFs == nil {
return nil, rc.NewErrParamInvalid(errors.New("need srcFs parameter when not using checkFileHash"))
}
oneway, _ := in.GetBool("oneWay")
download, _ := in.GetBool("download")
opt := &CheckOpt{
Fsrc: srcFs,
Fdst: dstFs,
OneWay: oneway,
}
out = rc.Params{}
getOutput := func(name string, Default bool) io.Writer {
active, err := in.GetBool(name)
if err != nil {
active = Default
}
if !active {
return nil
}
result := []string{}
out[name] = &result
return stringWriter{&result}
}
opt.Combined = getOutput("combined", false)
opt.MissingOnSrc = getOutput("missingOnSrc", true)
opt.MissingOnDst = getOutput("missingOnDst", true)
opt.Match = getOutput("match", false)
opt.Differ = getOutput("differ", true)
opt.Error = getOutput("error", true)
if checkFileHash != "" {
out["hashType"] = checkFileHashType.String()
err = CheckSum(ctx, dstFs, checkFileFs, checkFileRemote, checkFileHashType, opt, download)
} else {
if download {
err = CheckDownload(ctx, opt)
} else {
out["hashType"] = srcFs.Hashes().Overlap(dstFs.Hashes()).GetOne().String()
err = Check(ctx, opt)
}
}
if err != nil {
out["status"] = err.Error()
out["success"] = false
} else {
out["status"] = "OK"
out["success"] = true
}
return out, nil
}
func init() {
rc.Add(rc.Call{
Path: "operations/hashsum",
AuthRequired: true,
Fn: rcHashsum,
Title: "Produces a hashsum file for all the objects in the path.",
Help: `Produces a hash file for all the objects in the path using the hash
named. The output is in the same format as the standard
md5sum/sha1sum tool.
This takes the following parameters:
- fs - a remote name string e.g. "drive:" for the source, "/" for local filesystem
- this can point to a file and just that file will be returned in the listing.
- hashType - type of hash to be used
- download - check by downloading rather than with hash (boolean)
- base64 - output the hashes in base64 rather than hex (boolean)
If you supply the download flag, it will download the data from the
remote and create the hash on the fly. This can be useful for remotes
that don't support the given hash or if you really want to check all
the data.
Note that if you wish to supply a checkfile to check hashes against
the current files then you should use operations/check instead of
operations/hashsum.
Returns:
- hashsum - array of strings of the hashes
- hashType - type of hash used
Example:
$ rclone rc --loopback operations/hashsum fs=bin hashType=MD5 download=true base64=true
{
"hashType": "md5",
"hashsum": [
"WTSVLpuiXyJO_kGzJerRLg== backend-versions.sh",
"v1b_OlWCJO9LtNq3EIKkNQ== bisect-go-rclone.sh",
"VHbmHzHh4taXzgag8BAIKQ== bisect-rclone.sh",
]
}
See the [hashsum](/commands/rclone_hashsum/) command for more information on the above.
`,
})
}
// Parse download, base64 and hashType parameters
func parseHashParameters(in rc.Params) (download bool, base64 bool, ht hash.Type, err error) {
download, _ = in.GetBool("download")
base64, _ = in.GetBool("base64")
hashType, err := in.GetString("hashType")
if err != nil {
return
}
err = ht.Set(hashType)
return
}
// Hashsum a directory
func rcHashsum(ctx context.Context, in rc.Params) (out rc.Params, err error) {
ctx, f, err := rc.GetFsNamedFileOK(ctx, in, "fs")
if err != nil {
return nil, err
}
download, base64, ht, err := parseHashParameters(in)
if err != nil {
return out, err
}
hashes := []string{}
err = HashLister(ctx, ht, base64, download, f, stringWriter{&hashes})
out = rc.Params{
"hashType": ht.String(),
"hashsum": hashes,
}
return out, err
}
func init() {
rc.Add(rc.Call{
Path: "operations/hashsumfile",
AuthRequired: true,
Fn: rcHashsumFile,
Title: "Produces a hash for a single file.",
Help: `Produces a hash for a single file using the hash named.
This takes the following parameters:
- fs - a remote name string e.g. "drive:"
- remote - a path within that remote e.g. "file.txt"
- hashType - type of hash to be used
- download - check by downloading rather than with hash (boolean)
- base64 - output the hashes in base64 rather than hex (boolean)
If you supply the download flag, it will download the data from the
remote and create the hash on the fly. This can be useful for remotes
that don't support the given hash or if you really want to read all
the data.
Returns:
- hash - hash for the file
- hashType - type of hash used
Example:
$ rclone rc --loopback operations/hashsumfile fs=/ remote=/bin/bash hashType=MD5 download=true base64=true
{
"hashType": "md5",
"hash": "MDMw-fG2YXs7Uz5Nz-H68A=="
}
See the [hashsum](/commands/rclone_hashsum/) command for more information on the above.
`,
})
}
// Hashsum a file
func rcHashsumFile(ctx context.Context, in rc.Params) (out rc.Params, err error) {
f, remote, err := rc.GetFsAndRemote(ctx, in)
if err != nil {
return nil, err
}
download, base64, ht, err := parseHashParameters(in)
if err != nil {
return out, err
}
o, err := f.NewObject(ctx, remote)
if err != nil {
return nil, err
}
sum, err := HashSum(ctx, ht, base64, download, o)
out = rc.Params{
"hashType": ht.String(),
"hash": sum,
}
return out, err
}
|