2016-08-04 21:18:56 +00:00
|
|
|
// Package cmd implemnts the rclone command
|
|
|
|
//
|
|
|
|
// It is in a sub package so it's internals can be re-used elsewhere
|
|
|
|
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 (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2016-11-22 04:04:05 +00:00
|
|
|
"regexp"
|
2016-08-04 21:18:56 +00:00
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
|
|
|
|
2017-11-15 05:32:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-08-04 21:18:56 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 16:30:54 +00:00
|
|
|
"github.com/ncw/rclone/fs/accounting"
|
|
|
|
"github.com/ncw/rclone/fs/config/configflags"
|
|
|
|
"github.com/ncw/rclone/fs/config/flags"
|
|
|
|
"github.com/ncw/rclone/fs/filter"
|
|
|
|
"github.com/ncw/rclone/fs/filter/filterflags"
|
|
|
|
"github.com/ncw/rclone/fs/fserrors"
|
|
|
|
"github.com/ncw/rclone/fs/fspath"
|
|
|
|
fslog "github.com/ncw/rclone/fs/log"
|
2018-03-05 11:44:16 +00:00
|
|
|
"github.com/ncw/rclone/fs/rc"
|
|
|
|
"github.com/ncw/rclone/fs/rc/rcflags"
|
2018-01-25 10:10:21 +00:00
|
|
|
"github.com/ncw/rclone/lib/atexit"
|
2016-08-04 21:18:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
var (
|
|
|
|
// Flags
|
2018-01-12 16:30:54 +00:00
|
|
|
cpuProfile = flags.StringP("cpuprofile", "", "", "Write cpu profile to file")
|
|
|
|
memProfile = flags.StringP("memprofile", "", "", "Write memory profile to file")
|
|
|
|
statsInterval = flags.DurationP("stats", "", time.Minute*1, "Interval between printing stats, e.g 500ms, 60s, 5m. (0 to disable)")
|
|
|
|
dataRateUnit = flags.StringP("stats-unit", "", "bytes", "Show data rate in stats as either 'bits' or 'bytes'/s")
|
2016-08-04 21:18:56 +00:00
|
|
|
version bool
|
2018-01-12 16:30:54 +00:00
|
|
|
retries = flags.IntP("retries", "", 3, "Retry operations this many times if they fail")
|
2017-11-15 05:32:00 +00:00
|
|
|
// Errors
|
|
|
|
errorCommandNotFound = errors.New("command not found")
|
|
|
|
errorUncategorized = errors.New("uncategorized error")
|
|
|
|
errorNotEnoughArguments = errors.New("not enough arguments")
|
|
|
|
errorTooManyArguents = errors.New("too many arguments")
|
|
|
|
errorUsageError = errors.New("usage error")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
exitCodeSuccess = iota
|
|
|
|
exitCodeUsageError
|
|
|
|
exitCodeUncategorizedError
|
|
|
|
exitCodeDirNotFound
|
|
|
|
exitCodeFileNotFound
|
|
|
|
exitCodeRetryError
|
|
|
|
exitCodeNoRetryError
|
|
|
|
exitCodeFatalError
|
2016-08-04 21:18:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Root is the main rclone command
|
|
|
|
var Root = &cobra.Command{
|
|
|
|
Use: "rclone",
|
|
|
|
Short: "Sync files and directories to and from local and remote object stores - " + fs.Version,
|
|
|
|
Long: `
|
|
|
|
Rclone is a command line program to sync files and directories to and
|
2017-05-24 22:24:46 +00:00
|
|
|
from various cloud storage systems and using file transfer services, such as:
|
2016-08-04 21:18:56 +00:00
|
|
|
|
2017-07-08 22:31:58 +00:00
|
|
|
* Amazon Drive
|
2016-08-04 21:18:56 +00:00
|
|
|
* Amazon S3
|
2017-07-08 22:31:58 +00:00
|
|
|
* Backblaze B2
|
|
|
|
* Box
|
2016-08-04 21:18:56 +00:00
|
|
|
* Dropbox
|
2017-07-08 22:31:58 +00:00
|
|
|
* FTP
|
2016-08-04 21:18:56 +00:00
|
|
|
* Google Cloud Storage
|
2017-07-08 22:31:58 +00:00
|
|
|
* Google Drive
|
|
|
|
* HTTP
|
2016-08-04 21:18:56 +00:00
|
|
|
* Hubic
|
2017-07-25 14:18:13 +00:00
|
|
|
* Microsoft Azure Blob Storage
|
2017-07-08 22:31:58 +00:00
|
|
|
* Microsoft OneDrive
|
|
|
|
* Openstack Swift / Rackspace cloud files / Memset Memstore
|
2017-09-19 15:09:43 +00:00
|
|
|
* pCloud
|
2017-06-25 23:47:54 +00:00
|
|
|
* QingStor
|
2017-05-24 22:24:46 +00:00
|
|
|
* SFTP
|
2017-10-02 19:29:23 +00:00
|
|
|
* Webdav / Owncloud / Nextcloud
|
2017-07-08 22:31:58 +00:00
|
|
|
* Yandex Disk
|
2016-08-04 21:18:56 +00:00
|
|
|
* The local filesystem
|
|
|
|
|
|
|
|
Features
|
|
|
|
|
|
|
|
* MD5/SHA1 hashes checked at all times for file integrity
|
|
|
|
* Timestamps preserved on files
|
|
|
|
* Partial syncs supported on a whole file basis
|
|
|
|
* Copy mode to just copy new/changed files
|
|
|
|
* Sync (one way) mode to make a directory identical
|
|
|
|
* Check mode to check for file hash equality
|
|
|
|
* Can sync to and from network, eg two different cloud accounts
|
|
|
|
|
|
|
|
See the home page for installation, usage, documentation, changelog
|
|
|
|
and configuration walkthroughs.
|
|
|
|
|
2017-03-29 12:38:34 +00:00
|
|
|
* https://rclone.org/
|
2016-08-04 21:18:56 +00:00
|
|
|
`,
|
2017-02-20 16:33:45 +00:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
2017-02-20 16:36:25 +00:00
|
|
|
fs.Debugf("rclone", "Version %q finishing with parameters %q", fs.Version, os.Args)
|
2018-01-25 10:10:21 +00:00
|
|
|
atexit.Run()
|
2017-02-20 16:33:45 +00:00
|
|
|
},
|
2016-08-05 16:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runRoot implements the main rclone command with no subcommands
|
|
|
|
func runRoot(cmd *cobra.Command, args []string) {
|
|
|
|
if version {
|
|
|
|
ShowVersion()
|
2017-11-15 05:32:00 +00:00
|
|
|
resolveExitCode(nil)
|
2016-08-05 16:12:27 +00:00
|
|
|
} else {
|
|
|
|
_ = Root.Usage()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command not found.\n")
|
2017-11-15 05:32:00 +00:00
|
|
|
resolveExitCode(errorCommandNotFound)
|
2016-08-05 16:12:27 +00:00
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2018-01-12 16:30:54 +00:00
|
|
|
// Add global flags
|
|
|
|
configflags.AddFlags(pflag.CommandLine)
|
|
|
|
filterflags.AddFlags(pflag.CommandLine)
|
2018-03-05 11:44:16 +00:00
|
|
|
rcflags.AddFlags(pflag.CommandLine)
|
2018-01-12 16:30:54 +00:00
|
|
|
|
2016-08-05 16:12:27 +00:00
|
|
|
Root.Run = runRoot
|
2016-08-04 21:18:56 +00:00
|
|
|
Root.Flags().BoolVarP(&version, "version", "V", false, "Print the version number")
|
|
|
|
cobra.OnInitialize(initConfig)
|
|
|
|
}
|
|
|
|
|
2016-08-05 16:12:27 +00:00
|
|
|
// ShowVersion prints the version to stdout
|
|
|
|
func ShowVersion() {
|
2016-08-04 21:18:56 +00:00
|
|
|
fmt.Printf("rclone %s\n", fs.Version)
|
2017-08-04 13:25:20 +00:00
|
|
|
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
|
|
|
fmt.Printf("- go version: %s\n", runtime.Version())
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:34:17 +00:00
|
|
|
// newFsFile creates a dst Fs from a name but may point to a file.
|
2016-08-04 21:18:56 +00:00
|
|
|
//
|
2016-10-23 16:34:17 +00:00
|
|
|
// It returns a string with the file name if points to a file
|
|
|
|
func newFsFile(remote string) (fs.Fs, string) {
|
2016-08-04 21:18:56 +00:00
|
|
|
fsInfo, configName, fsPath, err := fs.ParseRemote(remote)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
f, err := fsInfo.NewFs(configName, fsPath)
|
2016-10-23 16:34:17 +00:00
|
|
|
switch err {
|
|
|
|
case fs.ErrorIsFile:
|
|
|
|
return f, path.Base(fsPath)
|
|
|
|
case nil:
|
|
|
|
return f, ""
|
|
|
|
default:
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-10-23 16:34:17 +00:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
return nil, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFsSrc creates a src Fs from a name
|
|
|
|
//
|
|
|
|
// It returns a string with the file name if limiting to one file
|
|
|
|
//
|
|
|
|
// This can point to a file
|
|
|
|
func newFsSrc(remote string) (fs.Fs, string) {
|
|
|
|
f, fileName := newFsFile(remote)
|
|
|
|
if fileName != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
if !filter.Active.InActive() {
|
2017-11-15 05:32:00 +00:00
|
|
|
err := errors.Errorf("Can't limit to single files when using filters: %v", remote)
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2017-11-15 05:32:00 +00:00
|
|
|
log.Fatalf(err.Error())
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
// Limit transfers to this file
|
2018-01-12 16:30:54 +00:00
|
|
|
err := filter.Active.AddFile(fileName)
|
2016-10-23 16:34:17 +00:00
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-10-23 16:34:17 +00:00
|
|
|
log.Fatalf("Failed to limit to single file %q: %v", remote, err)
|
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
2016-10-23 16:34:17 +00:00
|
|
|
return f, fileName
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFsDst creates a dst Fs from a name
|
|
|
|
//
|
|
|
|
// This must point to a directory
|
|
|
|
func newFsDst(remote string) fs.Fs {
|
|
|
|
f, err := fs.NewFs(remote)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatalf("Failed to create file system for %q: %v", remote, err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFsSrcDst creates a new src and dst fs from the arguments
|
|
|
|
func NewFsSrcDst(args []string) (fs.Fs, fs.Fs) {
|
2016-10-23 16:34:17 +00:00
|
|
|
fsrc, _ := newFsSrc(args[0])
|
|
|
|
fdst := newFsDst(args[1])
|
2016-08-04 21:18:56 +00:00
|
|
|
fs.CalculateModifyWindow(fdst, fsrc)
|
2016-08-05 23:07:36 +00:00
|
|
|
return fsrc, fdst
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 16:34:17 +00: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) {
|
|
|
|
fsrc, srcFileName = newFsSrc(args[0])
|
|
|
|
// If copying a file...
|
|
|
|
dstRemote := args[1]
|
2017-12-15 11:37:31 +00:00
|
|
|
// If file exists then srcFileName != "", however if the file
|
|
|
|
// doesn't exist then we assume it is a directory...
|
2016-10-23 16:34:17 +00:00
|
|
|
if srcFileName != "" {
|
2018-01-12 16:30:54 +00:00
|
|
|
dstRemote, dstFileName = fspath.RemoteSplit(dstRemote)
|
2016-10-23 16:34:17 +00:00
|
|
|
if dstRemote == "" {
|
2017-02-22 21:24:04 +00:00
|
|
|
dstRemote = "."
|
|
|
|
}
|
|
|
|
if dstFileName == "" {
|
|
|
|
log.Fatalf("%q is a directory", args[1])
|
2016-10-23 16:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-15 11:37:31 +00:00
|
|
|
fdst, err := fs.NewFs(dstRemote)
|
|
|
|
switch err {
|
|
|
|
case fs.ErrorIsFile:
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2017-12-15 11:37:31 +00:00
|
|
|
log.Fatalf("Source doesn't exist or is a directory and destination is a file")
|
|
|
|
case nil:
|
|
|
|
default:
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2017-12-15 11:37:31 +00:00
|
|
|
log.Fatalf("Failed to create file system for destination %q: %v", dstRemote, err)
|
|
|
|
}
|
2016-10-23 16:34:17 +00:00
|
|
|
fs.CalculateModifyWindow(fdst, fsrc)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:18:56 +00:00
|
|
|
// NewFsSrc creates a new src fs from the arguments
|
|
|
|
func NewFsSrc(args []string) fs.Fs {
|
2016-10-23 16:34:17 +00:00
|
|
|
fsrc, _ := newFsSrc(args[0])
|
2016-08-04 21:18:56 +00:00
|
|
|
fs.CalculateModifyWindow(fsrc)
|
|
|
|
return fsrc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFsDst creates a new dst fs from the arguments
|
|
|
|
//
|
|
|
|
// Dst fs-es can't point to single files
|
|
|
|
func NewFsDst(args []string) fs.Fs {
|
|
|
|
fdst := newFsDst(args[0])
|
|
|
|
fs.CalculateModifyWindow(fdst)
|
|
|
|
return fdst
|
|
|
|
}
|
|
|
|
|
2017-08-03 19:42:35 +00:00
|
|
|
// NewFsDstFile creates a new dst fs with a destination file name from the arguments
|
|
|
|
func NewFsDstFile(args []string) (fdst fs.Fs, dstFileName string) {
|
2018-01-12 16:30:54 +00:00
|
|
|
dstRemote, dstFileName := fspath.RemoteSplit(args[0])
|
2017-08-03 19:42:35 +00:00
|
|
|
if dstRemote == "" {
|
|
|
|
dstRemote = "."
|
|
|
|
}
|
|
|
|
if dstFileName == "" {
|
|
|
|
log.Fatalf("%q is a directory", args[0])
|
|
|
|
}
|
|
|
|
fdst = newFsDst(dstRemote)
|
|
|
|
fs.CalculateModifyWindow(fdst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-15 17:40:17 +00: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 21:18:56 +00:00
|
|
|
// Run the function with stats and retries if required
|
2016-12-04 16:52:24 +00:00
|
|
|
func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) {
|
2016-08-04 21:18:56 +00:00
|
|
|
var err error
|
2016-12-04 16:52:24 +00:00
|
|
|
var stopStats chan struct{}
|
2016-12-15 17:40:17 +00:00
|
|
|
if !showStats && ShowStats() {
|
|
|
|
showStats = true
|
|
|
|
}
|
2016-12-04 16:52:24 +00:00
|
|
|
if showStats {
|
2016-12-01 08:49:47 +00:00
|
|
|
stopStats = StartStats()
|
2016-12-04 16:52:24 +00:00
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
for try := 1; try <= *retries; try++ {
|
|
|
|
err = f()
|
2018-01-12 16:30:54 +00:00
|
|
|
if !Retry || (err == nil && !accounting.Stats.Errored()) {
|
2016-09-12 14:42:57 +00:00
|
|
|
if try > 1 {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d succeeded", try, *retries)
|
2016-09-12 14:42:57 +00:00
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
break
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if fserrors.IsFatalError(err) {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Errorf(nil, "Fatal error received - not attempting retries")
|
2016-08-04 21:18:56 +00:00
|
|
|
break
|
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if fserrors.IsNoRetryError(err) {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Errorf(nil, "Can't retry this error - not attempting retries")
|
2016-08-04 21:18:56 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d failed with %d errors and: %v", try, *retries, accounting.Stats.GetErrors(), err)
|
2016-08-04 21:18:56 +00:00
|
|
|
} else {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.Errorf(nil, "Attempt %d/%d failed with %d errors", try, *retries, accounting.Stats.GetErrors())
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
if try < *retries {
|
2018-01-12 16:30:54 +00:00
|
|
|
accounting.Stats.ResetErrors()
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-04 16:52:24 +00:00
|
|
|
if showStats {
|
|
|
|
close(stopStats)
|
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
if err != nil {
|
2017-11-15 05:32:00 +00:00
|
|
|
log.Printf("Failed to %s: %v", cmd.Name(), err)
|
|
|
|
resolveExitCode(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
2018-01-12 16:30:54 +00:00
|
|
|
if showStats && (accounting.Stats.Errored() || *statsInterval > 0) {
|
|
|
|
accounting.Stats.Log()
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
2018-03-09 17:15:48 +00:00
|
|
|
fs.Debugf(nil, "%d go routines active\n", runtime.NumGoroutine())
|
2018-01-12 16:30:54 +00:00
|
|
|
if accounting.Stats.Errored() {
|
|
|
|
resolveExitCode(accounting.Stats.GetLastError())
|
2016-08-04 21:18:56 +00: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()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments mininum\n", cmd.Name(), MinArgs)
|
2017-11-15 05:32:00 +00:00
|
|
|
// os.Exit(1)
|
|
|
|
resolveExitCode(errorNotEnoughArguments)
|
2016-08-04 21:18:56 +00:00
|
|
|
} else if len(args) > MaxArgs {
|
|
|
|
_ = cmd.Usage()
|
|
|
|
fmt.Fprintf(os.Stderr, "Command %s needs %d arguments maximum\n", cmd.Name(), MaxArgs)
|
2017-11-15 05:32:00 +00:00
|
|
|
// os.Exit(1)
|
|
|
|
resolveExitCode(errorTooManyArguents)
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 08:49:47 +00:00
|
|
|
// StartStats prints the stats every statsInterval
|
2016-08-04 21:18:56 +00:00
|
|
|
//
|
|
|
|
// It returns a channel which should be closed to stop the stats.
|
2016-12-01 08:49:47 +00:00
|
|
|
func StartStats() chan struct{} {
|
2016-08-04 21:18:56 +00:00
|
|
|
stopStats := make(chan struct{})
|
|
|
|
if *statsInterval > 0 {
|
|
|
|
go func() {
|
|
|
|
ticker := time.NewTicker(*statsInterval)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2018-01-12 16:30:54 +00:00
|
|
|
accounting.Stats.Log()
|
2016-08-04 21:18:56 +00:00
|
|
|
case <-stopStats:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return stopStats
|
|
|
|
}
|
|
|
|
|
|
|
|
// initConfig is run by cobra after initialising the flags
|
|
|
|
func initConfig() {
|
2017-02-10 13:28:06 +00:00
|
|
|
// Start the logger
|
2018-01-12 16:30:54 +00:00
|
|
|
fslog.InitLogging()
|
|
|
|
|
|
|
|
// Finish parsing any command line flags
|
|
|
|
configflags.SetFlags()
|
2016-08-04 21:18:56 +00:00
|
|
|
|
2018-01-12 16:30:54 +00:00
|
|
|
// Load filters
|
|
|
|
var err error
|
|
|
|
filter.Active, err = filter.NewFilter(&filterflags.Opt)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to load filters: %v", err)
|
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
|
|
|
|
// Write the args for debug purposes
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Debugf("rclone", "Version %q starting with parameters %q", fs.Version, os.Args)
|
2016-08-04 21:18:56 +00:00
|
|
|
|
2018-03-05 11:44:16 +00:00
|
|
|
// Start the remote control if configured
|
|
|
|
rc.Start(&rcflags.Opt)
|
|
|
|
|
2016-08-04 21:18:56 +00:00
|
|
|
// Setup CPU profiling if desired
|
|
|
|
if *cpuProfile != "" {
|
2017-02-09 17:08:51 +00:00
|
|
|
fs.Infof(nil, "Creating CPU profile %q\n", *cpuProfile)
|
2016-08-04 21:18:56 +00:00
|
|
|
f, err := os.Create(*cpuProfile)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-01-25 10:10:21 +00:00
|
|
|
atexit.Register(func() {
|
2017-02-20 16:33:45 +00:00
|
|
|
pprof.StopCPUProfile()
|
|
|
|
})
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup memory profiling if desired
|
|
|
|
if *memProfile != "" {
|
2018-01-25 10:10:21 +00:00
|
|
|
atexit.Register(func() {
|
2017-02-09 17:08:51 +00:00
|
|
|
fs.Infof(nil, "Saving Memory profile %q\n", *memProfile)
|
2016-08-04 21:18:56 +00:00
|
|
|
f, err := os.Create(*memProfile)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = pprof.WriteHeapProfile(f)
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2018-01-12 16:30:54 +00:00
|
|
|
fs.CountError(err)
|
2016-08-04 21:18:56 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-02-20 16:33:45 +00:00
|
|
|
})
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
2016-11-22 04:04:05 +00:00
|
|
|
|
|
|
|
if m, _ := regexp.MatchString("^(bits|bytes)$", *dataRateUnit); m == false {
|
2017-02-09 11:01:20 +00:00
|
|
|
fs.Errorf(nil, "Invalid unit passed to --stats-unit. Defaulting to bytes.")
|
2016-11-22 04:04:05 +00:00
|
|
|
fs.Config.DataRateUnit = "bytes"
|
|
|
|
} else {
|
|
|
|
fs.Config.DataRateUnit = *dataRateUnit
|
|
|
|
}
|
2016-08-04 21:18:56 +00:00
|
|
|
}
|
2017-11-15 05:32:00 +00:00
|
|
|
|
|
|
|
func resolveExitCode(err error) {
|
|
|
|
if err == nil {
|
|
|
|
os.Exit(exitCodeSuccess)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = errors.Cause(err)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case err == fs.ErrorDirNotFound:
|
|
|
|
os.Exit(exitCodeDirNotFound)
|
|
|
|
case err == fs.ErrorObjectNotFound:
|
|
|
|
os.Exit(exitCodeFileNotFound)
|
|
|
|
case err == errorUncategorized:
|
|
|
|
os.Exit(exitCodeUncategorizedError)
|
2018-01-12 16:30:54 +00:00
|
|
|
case fserrors.ShouldRetry(err):
|
2017-11-15 05:32:00 +00:00
|
|
|
os.Exit(exitCodeRetryError)
|
2018-01-12 16:30:54 +00:00
|
|
|
case fserrors.IsNoRetryError(err):
|
2017-11-15 05:32:00 +00:00
|
|
|
os.Exit(exitCodeNoRetryError)
|
2018-01-12 16:30:54 +00:00
|
|
|
case fserrors.IsFatalError(err):
|
2017-11-15 05:32:00 +00:00
|
|
|
os.Exit(exitCodeFatalError)
|
|
|
|
default:
|
|
|
|
os.Exit(exitCodeUsageError)
|
|
|
|
}
|
|
|
|
}
|