Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
// Package cmd implements the rclone command
|
2016-08-04 23:18:56 +02:00
|
|
|
//
|
2023-09-23 13:20:01 +02:00
|
|
|
// It is in a sub package so it's internals can be reused elsewhere
|
2016-08-04 23:18:56 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
// FIXME only attach the remote flags when using a remote???
|
|
|
|
// would probably mean bringing all the flags in to here? Or define some flagsets in fs...
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2016-08-04 23:18:56 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-04-11 15:16:55 +02:00
|
|
|
"os/exec"
|
2016-08-04 23:18:56 +02:00
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
2018-04-11 15:16:55 +02:00
|
|
|
"strconv"
|
2018-05-14 19:06:57 +02:00
|
|
|
"strings"
|
2018-10-03 22:46:18 +02:00
|
|
|
"sync"
|
2016-08-04 23:18:56 +02:00
|
|
|
"time"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/accounting"
|
|
|
|
"github.com/rclone/rclone/fs/cache"
|
2021-03-10 16:40:34 +01:00
|
|
|
"github.com/rclone/rclone/fs/config/configfile"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/configflags"
|
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
|
|
|
"github.com/rclone/rclone/fs/fspath"
|
|
|
|
fslog "github.com/rclone/rclone/fs/log"
|
2024-07-04 11:47:05 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc/rcserver"
|
2023-07-15 18:41:13 +02:00
|
|
|
fssync "github.com/rclone/rclone/fs/sync"
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/lib/atexit"
|
2021-03-21 13:13:09 +01:00
|
|
|
"github.com/rclone/rclone/lib/buildinfo"
|
2021-07-05 23:48:57 +02:00
|
|
|
"github.com/rclone/rclone/lib/exitcode"
|
2020-10-06 17:34:26 +02:00
|
|
|
"github.com/rclone/rclone/lib/terminal"
|
2018-10-13 15:41:15 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2016-08-04 23:18:56 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
2024-02-12 04:34:25 +01:00
|
|
|
cpuProfile = flags.StringP("cpuprofile", "", "", "Write cpu profile to file", "Debugging")
|
|
|
|
memProfile = flags.StringP("memprofile", "", "", "Write memory profile to file", "Debugging")
|
|
|
|
statsInterval = flags.DurationP("stats", "", time.Minute*1, "Interval between printing stats, e.g. 500ms, 60s, 5m (0 to disable)", "Logging")
|
|
|
|
version bool
|
2017-11-15 06:32:00 +01:00
|
|
|
// Errors
|
|
|
|
errorCommandNotFound = errors.New("command not found")
|
|
|
|
errorUncategorized = errors.New("uncategorized error")
|
|
|
|
errorNotEnoughArguments = errors.New("not enough arguments")
|
2018-12-29 18:34:58 +01:00
|
|
|
errorTooManyArguments = errors.New("too many arguments")
|
2017-11-15 06:32:00 +01:00
|
|
|
)
|
|
|
|
|
2016-08-05 18:12:27 +02:00
|
|
|
// ShowVersion prints the version to stdout
|
|
|
|
func ShowVersion() {
|
2021-04-05 20:53:09 +02:00
|
|
|
osVersion, osKernel := buildinfo.GetOSVersion()
|
|
|
|
if osVersion == "" {
|
|
|
|
osVersion = "unknown"
|
|
|
|
}
|
|
|
|
if osKernel == "" {
|
|
|
|
osKernel = "unknown"
|
|
|
|
}
|
|
|
|
|
2021-03-21 13:13:09 +01:00
|
|
|
linking, tagString := buildinfo.GetLinkingAndTags()
|
2021-04-05 20:53:09 +02:00
|
|
|
|
2022-10-22 17:23:49 +02:00
|
|
|
arch := buildinfo.GetArch()
|
|
|
|
|
2016-08-04 23:18:56 +02:00
|
|
|
fmt.Printf("rclone %s\n", fs.Version)
|
2021-04-05 20:53:09 +02:00
|
|
|
fmt.Printf("- os/version: %s\n", osVersion)
|
|
|
|
fmt.Printf("- os/kernel: %s\n", osKernel)
|
2021-03-21 13:13:09 +01:00
|
|
|
fmt.Printf("- os/type: %s\n", runtime.GOOS)
|
2022-10-22 17:23:49 +02:00
|
|
|
fmt.Printf("- os/arch: %s\n", arch)
|
2021-03-21 13:13:09 +01:00
|
|
|
fmt.Printf("- go/version: %s\n", runtime.Version())
|
|
|
|
fmt.Printf("- go/linking: %s\n", linking)
|
|
|
|
fmt.Printf("- go/tags: %s\n", tagString)
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 08:05:53 +02:00
|
|
|
// NewFsFile creates an Fs from a name but may point to a file.
|
2016-08-04 23:18:56 +02:00
|
|
|
//
|
2016-10-23 18:34:17 +02:00
|
|
|
// It returns a string with the file name if points to a file
|
2018-05-07 18:58:16 +02:00
|
|
|
// otherwise "".
|
2018-03-29 09:10:19 +02:00
|
|
|
func NewFsFile(remote string) (fs.Fs, string) {
|
2021-02-10 15:37:57 +01:00
|
|
|
_, fsPath, err := fspath.SplitFs(remote)
|
2016-08-04 23:18:56 +02:00
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := cache.Get(context.Background(), remote)
|
2016-10-23 18:34:17 +02:00
|
|
|
switch err {
|
|
|
|
case fs.ErrorIsFile:
|
2020-08-31 18:46:58 +02:00
|
|
|
cache.Pin(f) // pin indefinitely since it was on the CLI
|
2016-10-23 18:34:17 +02:00
|
|
|
return f, path.Base(fsPath)
|
|
|
|
case nil:
|
2020-08-31 18:46:58 +02:00
|
|
|
cache.Pin(f) // pin indefinitely since it was on the CLI
|
2016-10-23 18:34:17 +02:00
|
|
|
return f, ""
|
|
|
|
default:
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-10-23 18:34:17 +02:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
2020-05-25 08:05:53 +02:00
|
|
|
// newFsFileAddFilter creates an src Fs from a name
|
2016-10-23 18:34:17 +02:00
|
|
|
//
|
2018-05-07 18:58:16 +02:00
|
|
|
// This works the same as NewFsFile however it adds filters to the Fs
|
|
|
|
// to limit it to a single file if the remote pointed to a file.
|
|
|
|
func newFsFileAddFilter(remote string) (fs.Fs, string) {
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(context.Background())
|
2018-03-29 09:10:19 +02:00
|
|
|
f, fileName := NewFsFile(remote)
|
2016-10-23 18:34:17 +02:00
|
|
|
if fileName != "" {
|
2020-11-26 18:10:41 +01:00
|
|
|
if !fi.InActive() {
|
2022-06-08 22:54:39 +02:00
|
|
|
err := fmt.Errorf("can't limit to single files when using filters: %v", remote)
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2024-08-14 19:19:36 +02:00
|
|
|
log.Fatal(err.Error())
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
// Limit transfers to this file
|
2020-11-26 18:10:41 +01:00
|
|
|
err := fi.AddFile(fileName)
|
2016-10-23 18:34:17 +02:00
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-10-23 18:34:17 +02:00
|
|
|
log.Fatalf("Failed to limit to single file %q: %v", remote, err)
|
|
|
|
}
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2016-10-23 18:34:17 +02:00
|
|
|
return f, fileName
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 18:58:16 +02:00
|
|
|
// NewFsSrc creates a new src fs from the arguments.
|
|
|
|
//
|
|
|
|
// The source can be a file or a directory - if a file then it will
|
|
|
|
// limit the Fs to a single file.
|
|
|
|
func NewFsSrc(args []string) fs.Fs {
|
|
|
|
fsrc, _ := newFsFileAddFilter(args[0])
|
|
|
|
return fsrc
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFsDir creates an Fs from a name
|
2016-08-04 23:18:56 +02:00
|
|
|
//
|
|
|
|
// This must point to a directory
|
2018-05-07 18:58:16 +02:00
|
|
|
func newFsDir(remote string) fs.Fs {
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := cache.Get(context.Background(), remote)
|
2016-08-04 23:18:56 +02:00
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
2020-08-31 18:46:58 +02:00
|
|
|
cache.Pin(f) // pin indefinitely since it was on the CLI
|
2016-08-04 23:18:56 +02:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-05-07 18:58:16 +02:00
|
|
|
// NewFsDir creates a new Fs from the arguments
|
|
|
|
//
|
|
|
|
// The argument must point a directory
|
|
|
|
func NewFsDir(args []string) fs.Fs {
|
|
|
|
fdst := newFsDir(args[0])
|
|
|
|
return fdst
|
|
|
|
}
|
|
|
|
|
2016-08-04 23:18:56 +02:00
|
|
|
// NewFsSrcDst creates a new src and dst fs from the arguments
|
|
|
|
func NewFsSrcDst(args []string) (fs.Fs, fs.Fs) {
|
2018-05-07 18:58:16 +02:00
|
|
|
fsrc, _ := newFsFileAddFilter(args[0])
|
|
|
|
fdst := newFsDir(args[1])
|
2016-08-06 01:07:36 +02:00
|
|
|
return fsrc, fdst
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
|
2018-05-07 19:13:17 +02:00
|
|
|
// NewFsSrcFileDst creates a new src and dst fs from the arguments
|
|
|
|
//
|
|
|
|
// The source may be a file, in which case the source Fs and file name is returned
|
|
|
|
func NewFsSrcFileDst(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs) {
|
|
|
|
fsrc, srcFileName = NewFsFile(args[0])
|
|
|
|
fdst = newFsDir(args[1])
|
|
|
|
return fsrc, srcFileName, fdst
|
|
|
|
}
|
|
|
|
|
2016-10-23 18:34:17 +02:00
|
|
|
// NewFsSrcDstFiles creates a new src and dst fs from the arguments
|
|
|
|
// If src is a file then srcFileName and dstFileName will be non-empty
|
|
|
|
func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs, dstFileName string) {
|
2018-05-07 18:58:16 +02:00
|
|
|
fsrc, srcFileName = newFsFileAddFilter(args[0])
|
2016-10-23 18:34:17 +02:00
|
|
|
// If copying a file...
|
|
|
|
dstRemote := args[1]
|
2017-12-15 12:37:31 +01:00
|
|
|
// If file exists then srcFileName != "", however if the file
|
|
|
|
// doesn't exist then we assume it is a directory...
|
2016-10-23 18:34:17 +02:00
|
|
|
if srcFileName != "" {
|
2019-09-05 12:01:04 +02:00
|
|
|
var err error
|
|
|
|
dstRemote, dstFileName, err = fspath.Split(dstRemote)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Parsing %q failed: %v", args[1], err)
|
|
|
|
}
|
2016-10-23 18:34:17 +02:00
|
|
|
if dstRemote == "" {
|
2017-02-22 22:24:04 +01:00
|
|
|
dstRemote = "."
|
|
|
|
}
|
|
|
|
if dstFileName == "" {
|
|
|
|
log.Fatalf("%q is a directory", args[1])
|
2016-10-23 18:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
2020-11-05 16:18:51 +01:00
|
|
|
fdst, err := cache.Get(context.Background(), dstRemote)
|
2017-12-15 12:37:31 +01:00
|
|
|
switch err {
|
|
|
|
case fs.ErrorIsFile:
|
2019-11-18 15:13:02 +01:00
|
|
|
_ = fs.CountError(err)
|
2017-12-15 12:37:31 +01:00
|
|
|
log.Fatalf("Source doesn't exist or is a directory and destination is a file")
|
|
|
|
case nil:
|
|
|
|
default:
|
2019-11-18 15:13:02 +01:00
|
|
|
_ = fs.CountError(err)
|
2017-12-15 12:37:31 +01:00
|
|
|
log.Fatalf("Failed to create file system for destination %q: %v", dstRemote, err)
|
|
|
|
}
|
2020-08-31 18:46:58 +02:00
|
|
|
cache.Pin(fdst) // pin indefinitely since it was on the CLI
|
2016-10-23 18:34:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-03 21:42:35 +02:00
|
|
|
// NewFsDstFile creates a new dst fs with a destination file name from the arguments
|
|
|
|
func NewFsDstFile(args []string) (fdst fs.Fs, dstFileName string) {
|
2019-09-05 12:01:04 +02:00
|
|
|
dstRemote, dstFileName, err := fspath.Split(args[0])
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Parsing %q failed: %v", args[0], err)
|
|
|
|
}
|
2017-08-03 21:42:35 +02:00
|
|
|
if dstRemote == "" {
|
|
|
|
dstRemote = "."
|
|
|
|
}
|
|
|
|
if dstFileName == "" {
|
|
|
|
log.Fatalf("%q is a directory", args[0])
|
|
|
|
}
|
2018-05-07 18:58:16 +02:00
|
|
|
fdst = newFsDir(dstRemote)
|
2017-08-03 21:42:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-15 18:40:17 +01:00
|
|
|
// ShowStats returns true if the user added a `--stats` flag to the command line.
|
|
|
|
//
|
|
|
|
// This is called by Run to override the default value of the
|
|
|
|
// showStats passed in.
|
|
|
|
func ShowStats() bool {
|
|
|
|
statsIntervalFlag := pflag.Lookup("stats")
|
|
|
|
return statsIntervalFlag != nil && statsIntervalFlag.Changed
|
|
|
|
}
|
|
|
|
|
2016-08-04 23:18:56 +02:00
|
|
|
// Run the function with stats and retries if required
|
2016-12-04 17:52:24 +01:00
|
|
|
func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(context.Background())
|
2020-02-07 11:34:55 +01:00
|
|
|
var cmdErr error
|
2018-10-03 22:46:18 +02:00
|
|
|
stopStats := func() {}
|
2016-12-15 18:40:17 +01:00
|
|
|
if !showStats && ShowStats() {
|
|
|
|
showStats = true
|
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
if ci.Progress {
|
2018-06-30 17:05:31 +02:00
|
|
|
stopStats = startProgress()
|
|
|
|
} else if showStats {
|
2016-12-01 09:49:47 +01:00
|
|
|
stopStats = StartStats()
|
2016-12-04 17:52:24 +01:00
|
|
|
}
|
2018-05-13 20:24:56 +02:00
|
|
|
SigInfoHandler()
|
2024-02-12 04:34:25 +01:00
|
|
|
for try := 1; try <= ci.Retries; try++ {
|
2020-02-07 11:34:55 +01:00
|
|
|
cmdErr = f()
|
|
|
|
cmdErr = fs.CountError(cmdErr)
|
2019-07-18 12:13:54 +02:00
|
|
|
lastErr := accounting.GlobalStats().GetLastError()
|
2020-02-07 11:34:55 +01:00
|
|
|
if cmdErr == nil {
|
|
|
|
cmdErr = lastErr
|
2019-05-12 19:39:29 +02:00
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
if !Retry || !accounting.GlobalStats().Errored() {
|
2016-09-12 16:42:57 +02:00
|
|
|
if try > 1 {
|
2024-02-12 04:34:25 +01:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d succeeded", try, ci.Retries)
|
2016-09-12 16:42:57 +02:00
|
|
|
}
|
2016-08-04 23:18:56 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
if accounting.GlobalStats().HadFatalError() {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Errorf(nil, "Fatal error received - not attempting retries")
|
2016-08-04 23:18:56 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
if accounting.GlobalStats().Errored() && !accounting.GlobalStats().HadRetryError() {
|
2021-01-02 12:20:02 +01:00
|
|
|
fs.Errorf(nil, "Can't retry any of the errors - not attempting retries")
|
2016-08-04 23:18:56 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-18 12:13:54 +02:00
|
|
|
if retryAfter := accounting.GlobalStats().RetryAfter(); !retryAfter.IsZero() {
|
2022-06-08 22:25:17 +02:00
|
|
|
d := time.Until(retryAfter)
|
2019-03-21 12:24:13 +01:00
|
|
|
if d > 0 {
|
|
|
|
fs.Logf(nil, "Received retry after error - sleeping until %s (%v)", retryAfter.Format(time.RFC3339Nano), d)
|
|
|
|
time.Sleep(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if lastErr != nil {
|
2024-02-12 04:34:25 +01:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d failed with %d errors and: %v", try, ci.Retries, accounting.GlobalStats().GetErrors(), lastErr)
|
2016-08-04 23:18:56 +02:00
|
|
|
} else {
|
2024-02-12 04:34:25 +01:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d failed with %d errors", try, ci.Retries, accounting.GlobalStats().GetErrors())
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2024-02-12 04:34:25 +01:00
|
|
|
if try < ci.Retries {
|
2019-07-18 12:13:54 +02:00
|
|
|
accounting.GlobalStats().ResetErrors()
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2024-02-12 04:34:25 +01:00
|
|
|
if ci.RetriesInterval > 0 {
|
|
|
|
time.Sleep(ci.RetriesInterval)
|
2018-06-08 17:12:24 +02:00
|
|
|
}
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2018-10-03 22:46:18 +02:00
|
|
|
stopStats()
|
2019-07-18 12:13:54 +02:00
|
|
|
if showStats && (accounting.GlobalStats().Errored() || *statsInterval > 0) {
|
|
|
|
accounting.GlobalStats().Log()
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2018-03-09 18:15:48 +01:00
|
|
|
fs.Debugf(nil, "%d go routines active\n", runtime.NumGoroutine())
|
2018-04-11 15:16:55 +02:00
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
if ci.Progress && ci.ProgressTerminalTitle {
|
2020-10-06 17:34:26 +02:00
|
|
|
// Clear terminal title
|
|
|
|
terminal.WriteTerminalTitle("")
|
|
|
|
}
|
|
|
|
|
2018-04-11 15:16:55 +02:00
|
|
|
// dump all running go-routines
|
2020-11-05 12:33:32 +01:00
|
|
|
if ci.Dump&fs.DumpGoRoutines != 0 {
|
2020-02-07 11:34:55 +01:00
|
|
|
err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
2018-04-11 15:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to dump goroutines: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump open files
|
2020-11-05 12:33:32 +01:00
|
|
|
if ci.Dump&fs.DumpOpenFiles != 0 {
|
2018-04-11 15:16:55 +02:00
|
|
|
c := exec.Command("lsof", "-p", strconv.Itoa(os.Getpid()))
|
|
|
|
c.Stdout = os.Stdout
|
|
|
|
c.Stderr = os.Stderr
|
2020-02-07 11:34:55 +01:00
|
|
|
err := c.Run()
|
2018-04-11 15:16:55 +02:00
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(nil, "Failed to list open files: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 13:51:59 +02:00
|
|
|
// clear cache and shutdown backends
|
|
|
|
cache.Clear()
|
|
|
|
if lastErr := accounting.GlobalStats().GetLastError(); cmdErr == nil {
|
|
|
|
cmdErr = lastErr
|
|
|
|
}
|
|
|
|
|
2020-02-07 11:34:55 +01:00
|
|
|
// Log the final error message and exit
|
|
|
|
if cmdErr != nil {
|
|
|
|
nerrs := accounting.GlobalStats().GetErrors()
|
|
|
|
if nerrs <= 1 {
|
|
|
|
log.Printf("Failed to %s: %v", cmd.Name(), cmdErr)
|
|
|
|
} else {
|
|
|
|
log.Printf("Failed to %s with %d errors: last error was: %v", cmd.Name(), nerrs, cmdErr)
|
|
|
|
}
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2020-02-07 11:34:55 +01:00
|
|
|
resolveExitCode(cmdErr)
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckArgs checks there are enough arguments and prints a message if not
|
|
|
|
func CheckArgs(MinArgs, MaxArgs int, cmd *cobra.Command, args []string) {
|
|
|
|
if len(args) < MinArgs {
|
|
|
|
_ = cmd.Usage()
|
2018-12-29 18:34:58 +01:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Command %s needs %d arguments minimum: you provided %d non flag arguments: %q\n", cmd.Name(), MinArgs, len(args), args)
|
2017-11-15 06:32:00 +01:00
|
|
|
resolveExitCode(errorNotEnoughArguments)
|
2016-08-04 23:18:56 +02:00
|
|
|
} else if len(args) > MaxArgs {
|
|
|
|
_ = cmd.Usage()
|
2018-12-29 18:34:58 +01:00
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum: you provided %d non flag arguments: %q\n", cmd.Name(), MaxArgs, len(args), args)
|
|
|
|
resolveExitCode(errorTooManyArguments)
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 09:49:47 +01:00
|
|
|
// StartStats prints the stats every statsInterval
|
2016-08-04 23:18:56 +02:00
|
|
|
//
|
2018-10-03 22:46:18 +02:00
|
|
|
// It returns a func which should be called to stop the stats.
|
|
|
|
func StartStats() func() {
|
|
|
|
if *statsInterval <= 0 {
|
|
|
|
return func() {}
|
|
|
|
}
|
2016-08-04 23:18:56 +02:00
|
|
|
stopStats := make(chan struct{})
|
2018-10-03 22:46:18 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
ticker := time.NewTicker(*statsInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2019-07-18 12:13:54 +02:00
|
|
|
accounting.GlobalStats().Log()
|
2018-10-03 22:46:18 +02:00
|
|
|
case <-stopStats:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
2018-10-03 22:46:18 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
return func() {
|
|
|
|
close(stopStats)
|
|
|
|
wg.Wait()
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig is run by cobra after initialising the flags
|
|
|
|
func initConfig() {
|
2024-07-03 18:22:47 +02:00
|
|
|
// Set the global options from the flags
|
|
|
|
err := fs.GlobalOptionsInit()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to initialise global options: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:15:52 +01:00
|
|
|
ctx := context.Background()
|
|
|
|
ci := fs.GetConfig(ctx)
|
2020-09-18 17:37:54 +02:00
|
|
|
|
2017-02-10 14:28:06 +01:00
|
|
|
// Start the logger
|
2018-01-12 17:30:54 +01:00
|
|
|
fslog.InitLogging()
|
|
|
|
|
|
|
|
// Finish parsing any command line flags
|
2020-11-05 12:33:32 +01:00
|
|
|
configflags.SetFlags(ci)
|
2016-08-04 23:18:56 +02:00
|
|
|
|
2021-03-10 16:40:34 +01:00
|
|
|
// Load the config
|
2021-04-26 23:37:49 +02:00
|
|
|
configfile.Install()
|
2021-03-10 16:40:34 +01:00
|
|
|
|
2021-03-10 21:16:17 +01:00
|
|
|
// Start accounting
|
|
|
|
accounting.Start(ctx)
|
|
|
|
|
2023-02-10 21:13:33 +01:00
|
|
|
// Configure console
|
2021-01-24 12:28:39 +01:00
|
|
|
if ci.NoConsole {
|
2023-02-10 21:13:33 +01:00
|
|
|
// Hide the console window
|
2021-01-24 12:28:39 +01:00
|
|
|
terminal.HideConsole()
|
2023-02-10 21:13:33 +01:00
|
|
|
} else {
|
|
|
|
// Enable color support on stdout if possible.
|
|
|
|
// This enables virtual terminal processing on Windows 10,
|
|
|
|
// adding native support for ANSI/VT100 escape sequences.
|
|
|
|
terminal.EnableColorsStdout()
|
2021-01-24 12:28:39 +01:00
|
|
|
}
|
|
|
|
|
2016-08-04 23:18:56 +02:00
|
|
|
// Write the args for debug purposes
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)
|
2016-08-04 23:18:56 +02:00
|
|
|
|
2020-09-18 17:37:54 +02:00
|
|
|
// Inform user about systemd log support now that we have a logger
|
2020-12-17 12:55:27 +01:00
|
|
|
if fslog.Opt.LogSystemdSupport {
|
|
|
|
fs.Debugf("rclone", "systemd logging support activated")
|
2020-09-18 17:37:54 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 19:29:20 +02:00
|
|
|
// Start the remote control server if configured
|
2024-07-04 11:47:05 +02:00
|
|
|
_, err = rcserver.Start(context.Background(), &rc.Opt)
|
2018-11-01 18:20:04 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to start remote control: %v", err)
|
|
|
|
}
|
2018-03-05 12:44:16 +01:00
|
|
|
|
2016-08-04 23:18:56 +02:00
|
|
|
// Setup CPU profiling if desired
|
|
|
|
if *cpuProfile != "" {
|
2017-02-09 18:08:51 +01:00
|
|
|
fs.Infof(nil, "Creating CPU profile %q\n", *cpuProfile)
|
2016-08-04 23:18:56 +02:00
|
|
|
f, err := os.Create(*cpuProfile)
|
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-01-25 11:10:21 +01:00
|
|
|
atexit.Register(func() {
|
2017-02-20 17:33:45 +01:00
|
|
|
pprof.StopCPUProfile()
|
2024-04-07 09:05:45 +02:00
|
|
|
err := f.Close()
|
|
|
|
if err != nil {
|
|
|
|
err = fs.CountError(err)
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-20 17:33:45 +01:00
|
|
|
})
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup memory profiling if desired
|
|
|
|
if *memProfile != "" {
|
2018-01-25 11:10:21 +01:00
|
|
|
atexit.Register(func() {
|
2017-02-09 18:08:51 +01:00
|
|
|
fs.Infof(nil, "Saving Memory profile %q\n", *memProfile)
|
2016-08-04 23:18:56 +02:00
|
|
|
f, err := os.Create(*memProfile)
|
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = pprof.WriteHeapProfile(f)
|
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2019-11-18 15:13:02 +01:00
|
|
|
err = fs.CountError(err)
|
2016-08-04 23:18:56 +02:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-20 17:33:45 +01:00
|
|
|
})
|
2016-08-04 23:18:56 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 06:32:00 +01:00
|
|
|
|
|
|
|
func resolveExitCode(err error) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(context.Background())
|
2018-05-12 11:40:44 +02:00
|
|
|
atexit.Run()
|
2017-11-15 06:32:00 +01:00
|
|
|
if err == nil {
|
2020-11-05 12:33:32 +01:00
|
|
|
if ci.ErrorOnNoTransfer {
|
2019-12-18 12:52:20 +01:00
|
|
|
if accounting.GlobalStats().GetTransfers() == 0 {
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.NoFilesTransferred)
|
2019-12-18 12:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.Success)
|
2017-11-15 06:32:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2021-11-04 11:12:57 +01:00
|
|
|
case errors.Is(err, fs.ErrorDirNotFound):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.DirNotFound)
|
2021-11-04 11:12:57 +01:00
|
|
|
case errors.Is(err, fs.ErrorObjectNotFound):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.FileNotFound)
|
2021-11-04 11:12:57 +01:00
|
|
|
case errors.Is(err, errorUncategorized):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.UncategorizedError)
|
2021-11-04 11:12:57 +01:00
|
|
|
case errors.Is(err, accounting.ErrorMaxTransferLimitReached):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.TransferExceeded)
|
2023-07-15 18:41:13 +02:00
|
|
|
case errors.Is(err, fssync.ErrorMaxDurationReached):
|
|
|
|
os.Exit(exitcode.DurationExceeded)
|
2018-01-12 17:30:54 +01:00
|
|
|
case fserrors.ShouldRetry(err):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.RetryError)
|
2021-11-06 15:55:37 +01:00
|
|
|
case fserrors.IsNoRetryError(err), fserrors.IsNoLowLevelRetryError(err):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.NoRetryError)
|
2018-01-12 17:30:54 +01:00
|
|
|
case fserrors.IsFatalError(err):
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.FatalError)
|
2017-11-15 06:32:00 +01:00
|
|
|
default:
|
2021-07-05 23:48:57 +02:00
|
|
|
os.Exit(exitcode.UsageError)
|
2017-11-15 06:32:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 19:06:57 +02:00
|
|
|
|
2018-09-13 09:30:16 +02:00
|
|
|
var backendFlags map[string]struct{}
|
|
|
|
|
2018-05-14 19:06:57 +02:00
|
|
|
// AddBackendFlags creates flags for all the backend options
|
|
|
|
func AddBackendFlags() {
|
2018-09-13 09:30:16 +02:00
|
|
|
backendFlags = map[string]struct{}{}
|
2018-05-14 19:06:57 +02:00
|
|
|
for _, fsInfo := range fs.Registry {
|
2024-07-01 19:09:58 +02:00
|
|
|
flags.AddFlagsFromOptions(pflag.CommandLine, fsInfo.Prefix, fsInfo.Options)
|
|
|
|
// Store the backend flag names for the help generator
|
2018-05-14 19:06:57 +02:00
|
|
|
for i := range fsInfo.Options {
|
|
|
|
opt := &fsInfo.Options[i]
|
2018-09-24 22:27:30 +02:00
|
|
|
name := opt.FlagName(fsInfo.Prefix)
|
2024-07-01 19:09:58 +02:00
|
|
|
backendFlags[name] = struct{}{}
|
2018-05-14 19:06:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main runs rclone interpreting flags and commands out of os.Args
|
|
|
|
func Main() {
|
2018-09-13 09:29:26 +02:00
|
|
|
setupRootCommand(Root)
|
2018-05-14 19:06:57 +02:00
|
|
|
AddBackendFlags()
|
|
|
|
if err := Root.Execute(); err != nil {
|
2021-04-03 13:39:04 +02:00
|
|
|
if strings.HasPrefix(err.Error(), "unknown command") && selfupdateEnabled {
|
2021-03-11 20:39:30 +01:00
|
|
|
Root.PrintErrf("You could use '%s selfupdate' to get latest features.\n\n", Root.CommandPath())
|
|
|
|
}
|
2018-05-14 19:06:57 +02:00
|
|
|
log.Fatalf("Fatal error: %v", err)
|
|
|
|
}
|
|
|
|
}
|