From ffa0bc294a573efe80a15a2f6f0538301eb5d242 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 23 Jun 2022 17:04:37 +0800 Subject: [PATCH] chore: optimize standardize path --- internal/fs/read.go | 2 +- internal/operations/account.go | 8 ++++---- internal/operations/path.go | 4 ++-- internal/store/meta.go | 2 +- pkg/utils/path.go | 20 ++++++++++++++++---- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/internal/fs/read.go b/internal/fs/read.go index 5c472b22..5daab5de 100644 --- a/internal/fs/read.go +++ b/internal/fs/read.go @@ -41,7 +41,7 @@ func List(ctx context.Context, path string) ([]model.Obj, error) { } func Get(ctx context.Context, path string) (model.Obj, error) { - path = utils.StandardizationPath(path) + path = utils.StandardizePath(path) // maybe a virtual file if path != "/" { virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path)) diff --git a/internal/operations/account.go b/internal/operations/account.go index f73f998e..494c094f 100644 --- a/internal/operations/account.go +++ b/internal/operations/account.go @@ -32,7 +32,7 @@ func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) { // then instantiate corresponding driver and save it in memory func CreateAccount(ctx context.Context, account model.Account) error { account.Modified = time.Now() - account.VirtualPath = utils.StandardizationPath(account.VirtualPath) + account.VirtualPath = utils.StandardizePath(account.VirtualPath) err := store.CreateAccount(&account) if err != nil { return errors.WithMessage(err, "failed create account in database") @@ -61,7 +61,7 @@ func UpdateAccount(ctx context.Context, account model.Account) error { return errors.WithMessage(err, "failed get old account") } account.Modified = time.Now() - account.VirtualPath = utils.StandardizationPath(account.VirtualPath) + account.VirtualPath = utils.StandardizePath(account.VirtualPath) err = store.UpdateAccount(&account) if err != nil { return errors.WithMessage(err, "failed update account in database") @@ -155,7 +155,7 @@ func GetAccountVirtualFilesByPath(prefix string) []model.Obj { } return accounts[i].GetAccount().Index < accounts[j].GetAccount().Index }) - prefix = utils.StandardizationPath(prefix) + prefix = utils.StandardizePath(prefix) set := make(map[string]interface{}) for _, v := range accounts { // TODO should save a balanced account @@ -189,7 +189,7 @@ var balanceMap generic_sync.MapOf[string, int] // GetBalancedAccount get account by path func GetBalancedAccount(path string) driver.Driver { - path = utils.StandardizationPath(path) + path = utils.StandardizePath(path) accounts := getAccountsByPath(path) accountNum := len(accounts) switch accountNum { diff --git a/internal/operations/path.go b/internal/operations/path.go index 6b10c024..80775a73 100644 --- a/internal/operations/path.go +++ b/internal/operations/path.go @@ -15,13 +15,13 @@ func ActualPath(account driver.Additional, rawPath string) string { if i, ok := account.(driver.IRootFolderPath); ok { rawPath = stdpath.Join(i.GetRootFolderPath(), rawPath) } - return utils.StandardizationPath(rawPath) + return utils.StandardizePath(rawPath) } // GetAccountAndActualPath Get the corresponding account // for path: remove the virtual path prefix and join the actual root folder if exists func GetAccountAndActualPath(rawPath string) (driver.Driver, string, error) { - rawPath = utils.StandardizationPath(rawPath) + rawPath = utils.StandardizePath(rawPath) if strings.Contains(rawPath, "..") { return nil, "", errors.WithStack(errs.RelativePath) } diff --git a/internal/store/meta.go b/internal/store/meta.go index e44b6bc0..60bda4c0 100644 --- a/internal/store/meta.go +++ b/internal/store/meta.go @@ -17,7 +17,7 @@ var metaCache = cache.NewMemCache(cache.WithShards[*model.Meta](2)) var metaG singleflight.Group[*model.Meta] func GetNearestMeta(path string) (*model.Meta, error) { - path = utils.StandardizationPath(path) + path = utils.StandardizePath(path) meta, err := GetMetaByPath(path) if err == nil { return meta, nil diff --git a/pkg/utils/path.go b/pkg/utils/path.go index 7c225c81..2133d381 100644 --- a/pkg/utils/path.go +++ b/pkg/utils/path.go @@ -1,10 +1,22 @@ package utils -import "strings" +import ( + "path/filepath" + "runtime" + "strings" +) -// StandardizationPath convert path like '/' '/root' '/a/b' -func StandardizationPath(path string) string { +// StandardizePath convert path like '/' '/root' '/a/b' +func StandardizePath(path string) string { path = strings.TrimSuffix(path, "/") + // windows abs path + if filepath.IsAbs(path) && runtime.GOOS == "windows" { + return path + } + // relative path with prefix '..' + if strings.HasPrefix(path, "..") { + return path + } if !strings.HasPrefix(path, "/") { path = "/" + path } @@ -13,5 +25,5 @@ func StandardizationPath(path string) string { // PathEqual judge path is equal func PathEqual(path1, path2 string) bool { - return StandardizationPath(path1) == StandardizationPath(path2) + return StandardizePath(path1) == StandardizePath(path2) }