fs: add Type and FindFromFs to manage Fs and RegInfo

This commit is contained in:
Nick Craig-Wood 2022-07-04 09:25:10 +01:00
parent 9dbed02329
commit accf91742c
2 changed files with 36 additions and 1 deletions

View File

@ -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 // These need to work as filesystem names as the VFS cache will use them
configName += suffix 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. // ConfigFs makes the config for calling NewFs with.

View File

@ -10,6 +10,7 @@ import (
"reflect" "reflect"
"sort" "sort"
"strings" "strings"
"sync"
"github.com/rclone/rclone/fs/config/configmap" "github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct" "github.com/rclone/rclone/fs/config/configstruct"
@ -301,3 +302,33 @@ func MustFind(name string) *RegInfo {
} }
return fs 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)]
}