From 956a5ae9062cee43b7cd19f431f5034d2d2d7721 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Thu, 23 Jun 2022 23:03:11 +0800 Subject: [PATCH] perf: extract fs func and add error log --- internal/aria2/monitor.go | 1 - internal/fs/copy.go | 14 +++---- internal/fs/fs.go | 81 +++++++++++++++++++++++++++++++++++++++ internal/fs/get.go | 38 ++++++++++++++++++ internal/fs/link.go | 16 ++++++++ internal/fs/list.go | 37 ++++++++++++++++++ internal/fs/put.go | 3 +- internal/fs/read.go | 76 ------------------------------------ internal/fs/write.go | 11 +++--- 9 files changed, 185 insertions(+), 92 deletions(-) create mode 100644 internal/fs/get.go create mode 100644 internal/fs/link.go create mode 100644 internal/fs/list.go delete mode 100644 internal/fs/read.go diff --git a/internal/aria2/monitor.go b/internal/aria2/monitor.go index 482c76d4..ba1b19d2 100644 --- a/internal/aria2/monitor.go +++ b/internal/aria2/monitor.go @@ -99,7 +99,6 @@ var TransferTaskManager = task.NewTaskManager[uint64](3, func(k *uint64) { func (m *Monitor) Complete() error { // check dstDir again account, dstDirActualPath, err := operations.GetAccountAndActualPath(m.dstDirPath) - println("dstDirActualPath:", dstDirActualPath) if err != nil { return errors.WithMessage(err, "failed get account") } diff --git a/internal/fs/copy.go b/internal/fs/copy.go index 413596e8..21dbd604 100644 --- a/internal/fs/copy.go +++ b/internal/fs/copy.go @@ -21,7 +21,7 @@ var CopyTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) { // Copy if in an account, call move method // if not, add copy task -func Copy(ctx context.Context, account driver.Driver, srcObjPath, dstDirPath string) (bool, error) { +func _copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) { srcAccount, srcObjActualPath, err := operations.GetAccountAndActualPath(srcObjPath) if err != nil { return false, errors.WithMessage(err, "failed get src account") @@ -32,19 +32,19 @@ func Copy(ctx context.Context, account driver.Driver, srcObjPath, dstDirPath str } // copy if in an account, just call driver.Copy if srcAccount.GetAccount() == dstAccount.GetAccount() { - return false, operations.Copy(ctx, account, srcObjActualPath, dstDirActualPath) + return false, operations.Copy(ctx, srcAccount, srcObjActualPath, dstDirActualPath) } // not in an account CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjActualPath, dstAccount.GetAccount().VirtualPath, dstDirActualPath), Func: func(task *task.Task[uint64]) error { - return CopyBetween2Accounts(task, srcAccount, dstAccount, srcObjActualPath, dstDirActualPath) + return copyBetween2Accounts(task, srcAccount, dstAccount, srcObjActualPath, dstDirActualPath) }, })) return true, nil } -func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcObjPath, dstDirPath string) error { +func copyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcObjPath, dstDirPath string) error { t.SetStatus("getting src object") srcObj, err := operations.Get(t.Ctx, srcAccount, srcObjPath) if err != nil { @@ -65,7 +65,7 @@ func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Dr CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstObjPath), Func: func(t *task.Task[uint64]) error { - return CopyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath) + return copyBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstObjPath) }, })) } @@ -73,14 +73,14 @@ func CopyBetween2Accounts(t *task.Task[uint64], srcAccount, dstAccount driver.Dr CopyTaskManager.Submit(task.WithCancelCtx(&task.Task[uint64]{ Name: fmt.Sprintf("copy [%s](%s) to [%s](%s)", srcAccount.GetAccount().VirtualPath, srcObjPath, dstAccount.GetAccount().VirtualPath, dstDirPath), Func: func(t *task.Task[uint64]) error { - return CopyFileBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstDirPath) + return copyFileBetween2Accounts(t, srcAccount, dstAccount, srcObjPath, dstDirPath) }, })) } return nil } -func CopyFileBetween2Accounts(tsk *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcFilePath, dstDirPath string) error { +func copyFileBetween2Accounts(tsk *task.Task[uint64], srcAccount, dstAccount driver.Driver, srcFilePath, dstDirPath string) error { srcFile, err := operations.Get(tsk.Ctx, srcAccount, srcFilePath) if err != nil { return errors.WithMessagef(err, "failed get src [%s] file", srcFilePath) diff --git a/internal/fs/fs.go b/internal/fs/fs.go index 0ae45e87..7d98115f 100644 --- a/internal/fs/fs.go +++ b/internal/fs/fs.go @@ -1,5 +1,86 @@ package fs +import ( + "context" + "github.com/alist-org/alist/v3/internal/model" + log "github.com/sirupsen/logrus" +) + // the param named path of functions in this package is a virtual path // So, the purpose of this package is to convert virtual path to actual path // then pass the actual path to the operations package + +func List(ctx context.Context, path string) ([]model.Obj, error) { + res, err := list(ctx, path) + if err != nil { + log.Errorf("failed list %s: %+v", path, err) + return nil, err + } + return res, nil +} + +func Get(ctx context.Context, path string) (model.Obj, error) { + res, err := get(ctx, path) + if err != nil { + log.Errorf("failed get %s: %+v", path, err) + return nil, err + } + return res, nil +} + +func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) { + res, err := link(ctx, path, args) + if err != nil { + log.Errorf("failed link %s: %+v", path, err) + return nil, err + } + return res, nil +} + +func MakeDir(ctx context.Context, path string) error { + err := makeDir(ctx, path) + if err != nil { + log.Errorf("failed make dir %s: %+v", path, err) + } + return err +} + +func Move(ctx context.Context, srcPath, dstDirPath string) error { + err := move(ctx, srcPath, dstDirPath) + if err != nil { + log.Errorf("failed move %s to %s: %+v", srcPath, dstDirPath, err) + } + return err +} + +func Copy(ctx context.Context, srcObjPath, dstDirPath string) (bool, error) { + res, err := _copy(ctx, srcObjPath, dstDirPath) + if err != nil { + log.Errorf("failed copy %s to %s: %+v", srcObjPath, dstDirPath, err) + } + return res, err +} + +func Rename(ctx context.Context, srcPath, dstName string) error { + err := rename(ctx, srcPath, dstName) + if err != nil { + log.Errorf("failed rename %s to %s: %+v", srcPath, dstName, err) + } + return err +} + +func Remove(ctx context.Context, path string) error { + err := remove(ctx, path) + if err != nil { + log.Errorf("failed remove %s: %+v", path, err) + } + return err +} + +func Put(ctx context.Context, dstDirPath string, file model.FileStreamer) error { + err := put(ctx, dstDirPath, file) + if err != nil { + log.Errorf("failed put %s: %+v", dstDirPath, err) + } + return err +} diff --git a/internal/fs/get.go b/internal/fs/get.go new file mode 100644 index 00000000..a41bb89c --- /dev/null +++ b/internal/fs/get.go @@ -0,0 +1,38 @@ +package fs + +import ( + "context" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/alist-org/alist/v3/pkg/utils" + "github.com/pkg/errors" + stdpath "path" + "time" +) + +func get(ctx context.Context, path string) (model.Obj, error) { + path = utils.StandardizePath(path) + // maybe a virtual file + if path != "/" { + virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path)) + for _, f := range virtualFiles { + if f.GetName() == stdpath.Base(path) { + return f, nil + } + } + } + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + // if there are no account prefix with path, maybe root folder + if path == "/" { + return model.Object{ + Name: "root", + Size: 0, + Modified: time.Time{}, + IsFolder: true, + }, nil + } + return nil, errors.WithMessage(err, "failed get account") + } + return operations.Get(ctx, account, actualPath) +} diff --git a/internal/fs/link.go b/internal/fs/link.go new file mode 100644 index 00000000..3b4cd680 --- /dev/null +++ b/internal/fs/link.go @@ -0,0 +1,16 @@ +package fs + +import ( + "context" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/pkg/errors" +) + +func link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) { + account, actualPath, err := operations.GetAccountAndActualPath(path) + if err != nil { + return nil, errors.WithMessage(err, "failed get account") + } + return operations.Link(ctx, account, actualPath, args) +} diff --git a/internal/fs/list.go b/internal/fs/list.go new file mode 100644 index 00000000..7439e9bc --- /dev/null +++ b/internal/fs/list.go @@ -0,0 +1,37 @@ +package fs + +import ( + "context" + "github.com/alist-org/alist/v3/internal/model" + "github.com/alist-org/alist/v3/internal/operations" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" +) + +// List files +// TODO: hide +// TODO: sort +func list(ctx context.Context, path string) ([]model.Obj, error) { + account, actualPath, err := operations.GetAccountAndActualPath(path) + virtualFiles := operations.GetAccountVirtualFilesByPath(path) + if err != nil { + if len(virtualFiles) != 0 { + return virtualFiles, nil + } + return nil, errors.WithMessage(err, "failed get account") + } + files, err := operations.List(ctx, account, actualPath) + if err != nil { + log.Errorf("%+v", err) + if len(virtualFiles) != 0 { + return virtualFiles, nil + } + return nil, errors.WithMessage(err, "failed get files") + } + for _, accountFile := range virtualFiles { + if !containsByName(files, accountFile) { + files = append(files, accountFile) + } + } + return files, nil +} diff --git a/internal/fs/put.go b/internal/fs/put.go index 86318e7e..5ffae600 100644 --- a/internal/fs/put.go +++ b/internal/fs/put.go @@ -3,7 +3,6 @@ package fs import ( "context" "fmt" - "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/model" "github.com/alist-org/alist/v3/internal/operations" @@ -17,7 +16,7 @@ var UploadTaskManager = task.NewTaskManager[uint64](3, func(tid *uint64) { }) // Put add as a put task -func Put(ctx context.Context, account driver.Driver, dstDirPath string, file model.FileStreamer) error { +func put(ctx context.Context, dstDirPath string, file model.FileStreamer) error { account, dstDirActualPath, err := operations.GetAccountAndActualPath(dstDirPath) if account.Config().NoUpload { return errors.WithStack(errs.UploadNotSupported) diff --git a/internal/fs/read.go b/internal/fs/read.go deleted file mode 100644 index 5daab5de..00000000 --- a/internal/fs/read.go +++ /dev/null @@ -1,76 +0,0 @@ -package fs - -import ( - "context" - stdpath "path" - "time" - - "github.com/alist-org/alist/v3/internal/model" - "github.com/alist-org/alist/v3/internal/operations" - "github.com/alist-org/alist/v3/pkg/utils" - "github.com/pkg/errors" - log "github.com/sirupsen/logrus" -) - -// List files -// TODO: hide -// TODO: sort -func List(ctx context.Context, path string) ([]model.Obj, error) { - account, actualPath, err := operations.GetAccountAndActualPath(path) - virtualFiles := operations.GetAccountVirtualFilesByPath(path) - if err != nil { - if len(virtualFiles) != 0 { - return virtualFiles, nil - } - return nil, errors.WithMessage(err, "failed get account") - } - files, err := operations.List(ctx, account, actualPath) - if err != nil { - log.Errorf("%+v", err) - if len(virtualFiles) != 0 { - return virtualFiles, nil - } - return nil, errors.WithMessage(err, "failed get files") - } - for _, accountFile := range virtualFiles { - if !containsByName(files, accountFile) { - files = append(files, accountFile) - } - } - return files, nil -} - -func Get(ctx context.Context, path string) (model.Obj, error) { - path = utils.StandardizePath(path) - // maybe a virtual file - if path != "/" { - virtualFiles := operations.GetAccountVirtualFilesByPath(stdpath.Dir(path)) - for _, f := range virtualFiles { - if f.GetName() == stdpath.Base(path) { - return f, nil - } - } - } - account, actualPath, err := operations.GetAccountAndActualPath(path) - if err != nil { - // if there are no account prefix with path, maybe root folder - if path == "/" { - return model.Object{ - Name: "root", - Size: 0, - Modified: time.Time{}, - IsFolder: true, - }, nil - } - return nil, errors.WithMessage(err, "failed get account") - } - return operations.Get(ctx, account, actualPath) -} - -func Link(ctx context.Context, path string, args model.LinkArgs) (*model.Link, error) { - account, actualPath, err := operations.GetAccountAndActualPath(path) - if err != nil { - return nil, errors.WithMessage(err, "failed get account") - } - return operations.Link(ctx, account, actualPath, args) -} diff --git a/internal/fs/write.go b/internal/fs/write.go index eb634a6c..531b17b4 100644 --- a/internal/fs/write.go +++ b/internal/fs/write.go @@ -2,13 +2,12 @@ package fs import ( "context" - "github.com/alist-org/alist/v3/internal/driver" "github.com/alist-org/alist/v3/internal/errs" "github.com/alist-org/alist/v3/internal/operations" "github.com/pkg/errors" ) -func MakeDir(ctx context.Context, account driver.Driver, path string) error { +func makeDir(ctx context.Context, path string) error { account, actualPath, err := operations.GetAccountAndActualPath(path) if err != nil { return errors.WithMessage(err, "failed get account") @@ -16,7 +15,7 @@ func MakeDir(ctx context.Context, account driver.Driver, path string) error { return operations.MakeDir(ctx, account, actualPath) } -func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string) error { +func move(ctx context.Context, srcPath, dstDirPath string) error { srcAccount, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) if err != nil { return errors.WithMessage(err, "failed get src account") @@ -28,10 +27,10 @@ func Move(ctx context.Context, account driver.Driver, srcPath, dstDirPath string if srcAccount.GetAccount() != dstAccount.GetAccount() { return errors.WithStack(errs.MoveBetweenTwoAccounts) } - return operations.Move(ctx, account, srcActualPath, dstDirActualPath) + return operations.Move(ctx, srcAccount, srcActualPath, dstDirActualPath) } -func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) error { +func rename(ctx context.Context, srcPath, dstName string) error { account, srcActualPath, err := operations.GetAccountAndActualPath(srcPath) if err != nil { return errors.WithMessage(err, "failed get account") @@ -39,7 +38,7 @@ func Rename(ctx context.Context, account driver.Driver, srcPath, dstName string) return operations.Rename(ctx, account, srcActualPath, dstName) } -func Remove(ctx context.Context, account driver.Driver, path string) error { +func remove(ctx context.Context, path string) error { account, actualPath, err := operations.GetAccountAndActualPath(path) if err != nil { return errors.WithMessage(err, "failed get account")