"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
Implement --max-transfer flag to quit transferring at a limit #1655
Nick Craig-Wood5c128272fd98
diff --git a/docs/content/docs.md b/docs/content/docs.mdindex 27c96a700..2489c9cda 100644--- a/docs/content/docs.md+++ b/docs/content/docs.md@@ -536,6 +536,13 @@ files not recursed through are considered excluded and will be deletedon the destination. Test first with `--dry-run` if you are not surewhat will happen.+### --max-transfer=SIZE ###++Rclone will stop transferring when it has reached the size specified.+Defaults to off.++When the limit is reached all transfers will stop immediately.+### --modify-window=TIME ###When checking whether a file has been modified, this is the maximumdiff --git a/fs/accounting/accounting.go b/fs/accounting/accounting.goindex cf0340f93..5d51d85aa 100644--- a/fs/accounting/accounting.go+++ b/fs/accounting/accounting.go@@ -10,8 +10,14 @@ import ("github.com/VividCortex/ewma""github.com/ncw/rclone/fs""github.com/ncw/rclone/fs/asyncreader"+ "github.com/ncw/rclone/fs/fserrors"+ "github.com/pkg/errors")+// ErrorMaxTransferLimitReached is returned from Read when the max+// transfer limit is reached.+var ErrorMaxTransferLimitReached = fserrors.FatalError(errors.New("Max transfer limit reached as set by --max-transfer"))+// Account limits and accounts for one transfertype Account struct {// The mutex is to make sure Read() and Close() aren't called@@ -27,6 +33,7 @@ type Account struct {name stringstatmu sync.Mutex // Separate mutex for stat values.bytes int64 // Total number of bytes read+ max int64 // if >=0 the max number of bytes to transferstart time.Time // Start time of first readlpTime time.Time // Time of last average measurementlpBytes int // Number of bytes read since last measurement@@ -48,6 +55,7 @@ func NewAccountSizeName(in io.ReadCloser, size int64, name string) *Account {exit: make(chan struct{}),avg: ewma.NewMovingAverage(),lpTime: time.Now(),+ max: int64(fs.Config.MaxTransfer),}go acc.averageLoop()Stats.inProgress.set(acc.name, acc)@@ -131,8 +139,12 @@ func (acc *Account) averageLoop() {// read bytes from the io.Reader passed in and account themfunc (acc *Account) read(in io.Reader, p []byte) (n int, err error) {- // Set start time.acc.statmu.Lock()+ if acc.max >= 0 && Stats.GetBytes() >= acc.max {+ acc.statmu.Unlock()+ return 0, ErrorMaxTransferLimitReached+ }+ // Set start time.if acc.start.IsZero() {acc.start = time.Now()}diff --git a/fs/accounting/accounting_test.go b/fs/accounting/accounting_test.goindex 554a49623..b197a4a7d 100644--- a/fs/accounting/accounting_test.go+++ b/fs/accounting/accounting_test.go@@ -7,7 +7,9 @@ import ("strings""testing"+ "github.com/ncw/rclone/fs""github.com/ncw/rclone/fs/asyncreader"+ "github.com/ncw/rclone/fs/fserrors""github.com/ncw/rclone/fstest/mockobject""github.com/stretchr/testify/assert""github.com/stretchr/testify/require"@@ -181,3 +183,28 @@ func TestAccountAccounter(t *testing.T) {assert.True(t, wrap(in3) == in3)}++func TestAccountMaxTransfer(t *testing.T) {+ old := fs.Config.MaxTransfer+ fs.Config.MaxTransfer = 15+ defer func() {+ fs.Config.MaxTransfer = old+ }()+ Stats.ResetCounters()++ in := ioutil.NopCloser(bytes.NewBuffer(make([]byte, 100)))+ acc := NewAccountSizeName(in, 1, "test")++ var b = make([]byte, 10)++ n, err := acc.Read(b)+ assert.Equal(t, 10, n)+ assert.NoError(t, err)+ n, err = acc.Read(b)+ assert.Equal(t, 10, n)+ assert.NoError(t, err)+ n, err = acc.Read(b)+ assert.Equal(t, 0, n)+ assert.Equal(t, ErrorMaxTransferLimitReached, err)+ assert.True(t, fserrors.IsFatalError(err))+}diff --git a/fs/accounting/stats.go b/fs/accounting/stats.goindex cd2557247..c666e39ff 100644--- a/fs/accounting/stats.go+++ b/fs/accounting/stats.go@@ -95,6 +95,13 @@ func (s *StatsInfo) Bytes(bytes int64) {s.bytes += bytes}+// GetBytes returns the number of bytes transferred so far+func (s *StatsInfo) GetBytes() int64 {+ s.lock.Lock()+ defer s.lock.Unlock()+ return s.bytes+}+// Errors updates the stats for errorsfunc (s *StatsInfo) Errors(errors int64) {s.lock.Lock()diff --git a/fs/config.go b/fs/config.goindex fb948cdf2..543923981 100644--- a/fs/config.go+++ b/fs/config.go@@ -71,6 +71,7 @@ type ConfigInfo struct {StatsFileNameLength intAskPassword boolUseServerModTime bool+ MaxTransfer SizeSuffix}// NewConfig creates a new config with everything set to the default@@ -98,6 +99,7 @@ func NewConfig() *ConfigInfo {c.StatsFileNameLength = 40c.AskPassword = truec.TPSLimitBurst = 1+ c.MaxTransfer = -1return c}diff --git a/fs/config/configflags/configflags.go b/fs/config/configflags/configflags.goindex 9d22dea85..c3a1531a7 100644--- a/fs/config/configflags/configflags.go+++ b/fs/config/configflags/configflags.go@@ -82,7 +82,7 @@ func AddFlags(flagSet *pflag.FlagSet) {flags.FVarP(flagSet, &fs.Config.BufferSize, "buffer-size", "", "Buffer size when copying files.")flags.FVarP(flagSet, &fs.Config.StreamingUploadCutoff, "streaming-upload-cutoff", "", "Cutoff for switching to chunked upload if file size is unknown. Upload starts after reaching cutoff or when file ends.")flags.FVarP(flagSet, &fs.Config.Dump, "dump", "", "List of items to dump from: "+fs.DumpFlagsList)-+ flags.FVarP(flagSet, &fs.Config.MaxTransfer, "max-transfer", "", "Maximum size of data to transfer.")}// SetFlags converts any flags into config which weren't straight foward