Skip to content

Commit

Permalink
perf: extract fs func and add error log
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jun 23, 2022
1 parent 40b7ecc commit 956a5ae
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 92 deletions.
1 change: 0 additions & 1 deletion internal/aria2/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
14 changes: 7 additions & 7 deletions internal/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -65,22 +65,22 @@ 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)
},
}))
}
} else {
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)
Expand Down
81 changes: 81 additions & 0 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions internal/fs/get.go
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions internal/fs/link.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions internal/fs/list.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 1 addition & 2 deletions internal/fs/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
76 changes: 0 additions & 76 deletions internal/fs/read.go

This file was deleted.

11 changes: 5 additions & 6 deletions internal/fs/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ 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")
}
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")
Expand All @@ -28,18 +27,18 @@ 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")
}
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")
Expand Down

0 comments on commit 956a5ae

Please sign in to comment.