Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

fs: add Type and FindFromFs to manage Fs and RegInfo

Nick Craig-Woodaccf91742c66
diff --git a/fs/newfs.go b/fs/newfs.go
index 1ac5e4807..f64cb2c8d 100644
--- a/fs/newfs.go
+++ b/fs/newfs.go
@@ -48,7 +48,11 @@ func NewFs(ctx context.Context, path string) (Fs, error) {
// These need to work as filesystem names as the VFS cache will use them
configName += suffix
}
- return fsInfo.NewFs(ctx, configName, fsPath, config)
+ f, err := fsInfo.NewFs(ctx, configName, fsPath, config)
+ if f != nil && (err == nil || err == ErrorIsFile) {
+ addReverse(f, fsInfo)
+ }
+ return f, err
}
// ConfigFs makes the config for calling NewFs with.
diff --git a/fs/registry.go b/fs/registry.go
index d380c7e0c..ca2c3d976 100644
--- a/fs/registry.go
+++ b/fs/registry.go
@@ -10,6 +10,7 @@ import (
"reflect"
"sort"
"strings"
+ "sync"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
@@ -301,3 +302,33 @@ func MustFind(name string) *RegInfo {
}
return fs
}
+
+// Type returns a textual string to identify the type of the remote
+func Type(f Fs) string {
+ typeName := fmt.Sprintf("%T", f)
+ typeName = strings.TrimPrefix(typeName, "*")
+ typeName = strings.TrimSuffix(typeName, ".Fs")
+ return typeName
+}
+
+var (
+ typeToRegInfoMu sync.Mutex
+ typeToRegInfo = map[string]*RegInfo{}
+)
+
+// Add the RegInfo to the reverse map
+func addReverse(f Fs, fsInfo *RegInfo) {
+ typeToRegInfoMu.Lock()
+ defer typeToRegInfoMu.Unlock()
+ typeToRegInfo[Type(f)] = fsInfo
+}
+
+// FindFromFs finds the *RegInfo used to create this Fs, provided
+// it was created by fs.NewFs or cache.Get
+//
+// It returns nil if not found
+func FindFromFs(f Fs) *RegInfo {
+ typeToRegInfoMu.Lock()
+ defer typeToRegInfoMu.Unlock()
+ return typeToRegInfo[Type(f)]
+}