From 8ee6034b232379efcf4530ec3517d451668fb4ca Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 23 May 2019 13:12:09 +0100 Subject: [PATCH] Look for Fs in the cache rather than calling NewFs directly This will save operations when rclone is used in remote control mode or with the same remote multiple times in the command line. --- backend/cache/cache.go | 3 ++- backend/union/union.go | 3 ++- cmd/cmd.go | 7 ++++--- fs/sync/sync.go | 3 ++- vfs/cache.go | 3 ++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/backend/cache/cache.go b/backend/cache/cache.go index 708b41fc8..4bd27deed 100644 --- a/backend/cache/cache.go +++ b/backend/cache/cache.go @@ -20,6 +20,7 @@ import ( "github.com/ncw/rclone/backend/crypt" "github.com/ncw/rclone/fs" + "github.com/ncw/rclone/fs/cache" "github.com/ncw/rclone/fs/config" "github.com/ncw/rclone/fs/config/configmap" "github.com/ncw/rclone/fs/config/configstruct" @@ -481,7 +482,7 @@ func NewFs(name, rootPath string, m configmap.Mapper) (fs.Fs, error) { return nil, errors.Wrapf(err, "failed to create cache directory %v", f.opt.TempWritePath) } f.opt.TempWritePath = filepath.ToSlash(f.opt.TempWritePath) - f.tempFs, err = fs.NewFs(f.opt.TempWritePath) + f.tempFs, err = cache.Get(f.opt.TempWritePath) if err != nil { return nil, errors.Wrapf(err, "failed to create temp fs: %v", err) } diff --git a/backend/union/union.go b/backend/union/union.go index bca6a0b78..8fcd3361d 100644 --- a/backend/union/union.go +++ b/backend/union/union.go @@ -9,6 +9,7 @@ import ( "time" "github.com/ncw/rclone/fs" + "github.com/ncw/rclone/fs/cache" "github.com/ncw/rclone/fs/config/configmap" "github.com/ncw/rclone/fs/config/configstruct" "github.com/ncw/rclone/fs/hash" @@ -342,7 +343,7 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) { if configName != "local" { rootString = configName + ":" + rootString } - myFs, err := fs.NewFs(rootString) + myFs, err := cache.Get(rootString) if err != nil { if err == fs.ErrorIsFile { return myFs, err diff --git a/cmd/cmd.go b/cmd/cmd.go index 439c68ef3..ab8ecc5b2 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -22,6 +22,7 @@ import ( "github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs/accounting" + "github.com/ncw/rclone/fs/cache" "github.com/ncw/rclone/fs/config/configflags" "github.com/ncw/rclone/fs/config/flags" "github.com/ncw/rclone/fs/filter" @@ -83,7 +84,7 @@ func NewFsFile(remote string) (fs.Fs, string) { fs.CountError(err) log.Fatalf("Failed to create file system for %q: %v", remote, err) } - f, err := fs.NewFs(remote) + f, err := cache.Get(remote) switch err { case fs.ErrorIsFile: return f, path.Base(fsPath) @@ -131,7 +132,7 @@ func NewFsSrc(args []string) fs.Fs { // // This must point to a directory func newFsDir(remote string) fs.Fs { - f, err := fs.NewFs(remote) + f, err := cache.Get(remote) if err != nil { fs.CountError(err) log.Fatalf("Failed to create file system for %q: %v", remote, err) @@ -180,7 +181,7 @@ func NewFsSrcDstFiles(args []string) (fsrc fs.Fs, srcFileName string, fdst fs.Fs log.Fatalf("%q is a directory", args[1]) } } - fdst, err := fs.NewFs(dstRemote) + fdst, err := cache.Get(dstRemote) switch err { case fs.ErrorIsFile: fs.CountError(err) diff --git a/fs/sync/sync.go b/fs/sync/sync.go index 122a03335..2970efa37 100644 --- a/fs/sync/sync.go +++ b/fs/sync/sync.go @@ -10,6 +10,7 @@ import ( "github.com/ncw/rclone/fs" "github.com/ncw/rclone/fs/accounting" + "github.com/ncw/rclone/fs/cache" "github.com/ncw/rclone/fs/filter" "github.com/ncw/rclone/fs/fserrors" "github.com/ncw/rclone/fs/hash" @@ -123,7 +124,7 @@ func newSyncCopyMove(fdst, fsrc fs.Fs, deleteMode fs.DeleteMode, DoMove bool, de // Make Fs for --backup-dir if required if fs.Config.BackupDir != "" { var err error - s.backupDir, err = fs.NewFs(fs.Config.BackupDir) + s.backupDir, err = cache.Get(fs.Config.BackupDir) if err != nil { return nil, fserrors.FatalError(errors.Errorf("Failed to make fs for --backup-dir %q: %v", fs.Config.BackupDir, err)) } diff --git a/vfs/cache.go b/vfs/cache.go index 1bab888fb..d4bdffd4f 100644 --- a/vfs/cache.go +++ b/vfs/cache.go @@ -16,6 +16,7 @@ import ( "github.com/djherbis/times" "github.com/ncw/rclone/fs" + fscache "github.com/ncw/rclone/fs/cache" "github.com/ncw/rclone/fs/config" "github.com/pkg/errors" ) @@ -100,7 +101,7 @@ func newCache(ctx context.Context, f fs.Fs, opt *Options) (*cache, error) { root := filepath.Join(config.CacheDir, "vfs", f.Name(), fRoot) fs.Debugf(nil, "vfs cache root is %q", root) - f, err := fs.NewFs(root) + f, err := fscache.Get(root) if err != nil { return nil, errors.Wrap(err, "failed to create cache remote") }