alist/internal/op/fs.go

350 lines
10 KiB
Go
Raw Normal View History

package op
2022-06-10 12:20:45 +00:00
import (
"context"
2022-07-01 07:04:02 +00:00
"os"
2022-06-13 13:14:01 +00:00
stdpath "path"
2022-06-27 12:56:17 +00:00
"strings"
2022-06-13 13:14:01 +00:00
"time"
2022-06-13 11:56:33 +00:00
"github.com/Xhofe/go-cache"
2022-06-10 12:20:45 +00:00
"github.com/alist-org/alist/v3/internal/driver"
"github.com/alist-org/alist/v3/internal/errs"
2022-06-11 06:43:03 +00:00
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/singleflight"
2022-06-11 06:43:03 +00:00
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
2022-06-10 12:20:45 +00:00
)
// In order to facilitate adding some other things before and after file op
2022-06-10 12:20:45 +00:00
2022-08-31 12:58:57 +00:00
var listCache = cache.NewMemCache(cache.WithShards[[]model.Obj](64))
var listG singleflight.Group[[]model.Obj]
2022-06-13 13:14:01 +00:00
2022-07-10 06:45:39 +00:00
func ClearCache(storage driver.Driver, path string) {
key := stdpath.Join(storage.GetStorage().MountPath, path)
2022-08-31 12:58:57 +00:00
listCache.Del(key)
2022-06-30 14:51:49 +00:00
}
func Key(storage driver.Driver, path string) string {
return stdpath.Join(storage.GetStorage().MountPath, utils.StandardizePath(path))
}
2022-06-10 12:20:45 +00:00
// List files in storage, not contains virtual file
2022-08-11 12:32:17 +00:00
func List(ctx context.Context, storage driver.Driver, path string, args model.ListArgs, refresh ...bool) ([]model.Obj, error) {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-06-27 12:56:17 +00:00
path = utils.StandardizePath(path)
log.Debugf("op.List %s", path)
2022-07-10 06:45:39 +00:00
dir, err := Get(ctx, storage, path)
2022-06-15 12:31:23 +00:00
if err != nil {
return nil, errors.WithMessage(err, "failed get dir")
}
2022-09-02 14:46:31 +00:00
log.Debugf("list dir: %+v", dir)
2022-06-23 08:09:22 +00:00
if !dir.IsDir() {
return nil, errors.WithStack(errs.NotFolder)
}
2022-07-10 06:45:39 +00:00
if storage.Config().NoCache {
2022-08-31 12:58:57 +00:00
objs, err := storage.List(ctx, dir, args)
return objs, errors.WithStack(err)
2022-06-13 13:15:58 +00:00
}
key := Key(storage, path)
2022-06-20 14:29:52 +00:00
if len(refresh) == 0 || !refresh[0] {
2022-08-31 12:58:57 +00:00
if files, ok := listCache.Get(key); ok {
2022-06-20 14:29:52 +00:00
return files, nil
}
2022-06-13 13:14:01 +00:00
}
2022-08-31 12:58:57 +00:00
objs, err, _ := listG.Do(key, func() ([]model.Obj, error) {
2022-08-11 12:32:17 +00:00
files, err := storage.List(ctx, dir, args)
2022-06-13 13:14:01 +00:00
if err != nil {
2022-08-31 12:58:57 +00:00
return nil, errors.Wrapf(err, "failed to list objs")
2022-06-13 13:14:01 +00:00
}
2022-08-31 12:58:57 +00:00
listCache.Set(key, files, cache.WithEx[[]model.Obj](time.Minute*time.Duration(storage.GetStorage().CacheExpiration)))
2022-06-13 13:14:01 +00:00
return files, nil
})
2022-08-31 12:58:57 +00:00
return objs, err
2022-06-10 12:20:45 +00:00
}
2022-06-27 11:51:23 +00:00
func isRoot(path, rootFolderPath string) bool {
if utils.PathEqual(path, rootFolderPath) {
return true
}
2022-06-27 12:56:17 +00:00
rootFolderPath = strings.TrimSuffix(rootFolderPath, "/")
rootFolderPath = strings.TrimPrefix(rootFolderPath, "\\")
// relative path, this shouldn't happen, because root folder path is absolute
2022-06-27 11:51:23 +00:00
if utils.PathEqual(path, "/") && rootFolderPath == "." {
return true
}
return false
}
2022-06-16 12:25:33 +00:00
// Get object from list of files
2022-07-10 06:45:39 +00:00
func Get(ctx context.Context, storage driver.Driver, path string) (model.Obj, error) {
2022-06-27 12:56:17 +00:00
path = utils.StandardizePath(path)
log.Debugf("op.Get %s", path)
2022-07-10 06:45:39 +00:00
if g, ok := storage.(driver.Getter); ok {
2022-08-31 08:32:51 +00:00
obj, err := g.Get(ctx, path)
if err == nil {
return obj, nil
}
2022-06-28 14:13:47 +00:00
}
2022-06-15 12:31:23 +00:00
// is root folder
2022-09-04 05:07:53 +00:00
if r, ok := storage.GetAddition().(driver.IRootId); ok && utils.PathEqual(path, "/") {
2022-09-02 10:24:14 +00:00
return &model.Object{
2022-09-04 05:07:53 +00:00
ID: r.GetRootId(),
2022-06-15 12:31:23 +00:00
Name: "root",
Size: 0,
2022-07-10 06:45:39 +00:00
Modified: storage.GetStorage().Modified,
2022-06-15 12:31:23 +00:00
IsFolder: true,
2022-06-11 06:43:03 +00:00
}, nil
}
2022-09-04 05:07:53 +00:00
if r, ok := storage.GetAddition().(driver.IRootPath); ok && isRoot(path, r.GetRootPath()) {
2022-09-02 10:24:14 +00:00
return &model.Object{
2022-09-04 05:07:53 +00:00
Path: r.GetRootPath(),
2022-06-11 06:43:03 +00:00
Name: "root",
Size: 0,
2022-07-10 06:45:39 +00:00
Modified: storage.GetStorage().Modified,
2022-06-11 06:43:03 +00:00
IsFolder: true,
}, nil
}
2022-06-15 12:31:23 +00:00
// not root folder
2022-06-11 06:43:03 +00:00
dir, name := stdpath.Split(path)
2022-08-11 12:32:17 +00:00
files, err := List(ctx, storage, dir, model.ListArgs{})
2022-06-11 06:43:03 +00:00
if err != nil {
return nil, errors.WithMessage(err, "failed get parent list")
}
for _, f := range files {
2022-09-02 14:46:31 +00:00
// TODO maybe copy obj here
2022-06-11 06:43:03 +00:00
if f.GetName() == name {
2022-06-16 12:25:33 +00:00
// use path as id, why don't set id in List function?
// because files maybe cache, set id here can reduce memory usage
2022-09-02 14:46:31 +00:00
if f.GetPath() == "" {
if s, ok := f.(model.SetPath); ok {
s.SetPath(path)
2022-06-16 12:25:33 +00:00
}
}
2022-06-11 06:43:03 +00:00
return f, nil
}
}
2022-06-23 08:03:27 +00:00
return nil, errors.WithStack(errs.ObjectNotFound)
2022-06-10 12:20:45 +00:00
}
2022-06-10 13:00:51 +00:00
2022-06-15 10:06:42 +00:00
var linkCache = cache.NewMemCache(cache.WithShards[*model.Link](16))
var linkG singleflight.Group[*model.Link]
2022-06-13 11:56:33 +00:00
// Link get link, if is an url. should have an expiry time
2022-07-10 06:45:39 +00:00
func Link(ctx context.Context, storage driver.Driver, path string, args model.LinkArgs) (*model.Link, model.Obj, error) {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return nil, nil, errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-10 06:45:39 +00:00
file, err := Get(ctx, storage, path)
2022-06-28 13:58:46 +00:00
if err != nil {
return nil, nil, errors.WithMessage(err, "failed to get file")
}
if file.IsDir() {
return nil, nil, errors.WithStack(errs.NotFile)
}
key := stdpath.Join(storage.GetStorage().MountPath, path) + ":" + args.IP
2022-06-13 11:56:33 +00:00
if link, ok := linkCache.Get(key); ok {
2022-06-28 13:58:46 +00:00
return link, file, nil
2022-06-13 11:56:33 +00:00
}
2022-06-15 10:06:42 +00:00
fn := func() (*model.Link, error) {
2022-07-10 06:45:39 +00:00
link, err := storage.Link(ctx, file, args)
if err != nil {
2022-08-31 12:58:57 +00:00
return nil, errors.Wrapf(err, "failed get link")
}
if link.Expiration != nil {
2022-06-15 10:06:42 +00:00
linkCache.Set(key, link, cache.WithEx[*model.Link](*link.Expiration))
}
return link, nil
2022-06-13 11:56:33 +00:00
}
link, err, _ := linkG.Do(key, fn)
2022-06-28 13:58:46 +00:00
return link, file, err
2022-06-10 13:00:51 +00:00
}
2022-08-11 12:32:17 +00:00
// Other api
2022-08-03 06:14:37 +00:00
func Other(ctx context.Context, storage driver.Driver, args model.FsOtherArgs) (interface{}, error) {
obj, err := Get(ctx, storage, args.Path)
if err != nil {
return nil, errors.WithMessagef(err, "failed to get obj")
}
2022-09-11 10:40:19 +00:00
if o, ok := storage.(driver.Other); ok {
return o.Other(ctx, model.OtherArgs{
Obj: obj,
Method: args.Method,
Data: args.Data,
})
} else {
return nil, errs.NotImplement
}
2022-08-03 06:14:37 +00:00
}
2022-07-10 06:45:39 +00:00
func MakeDir(ctx context.Context, storage driver.Driver, path string) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
path = utils.StandardizePath(path)
2022-06-15 11:10:11 +00:00
// check if dir exists
2022-07-10 06:45:39 +00:00
f, err := Get(ctx, storage, path)
2022-06-15 11:10:11 +00:00
if err != nil {
2022-06-23 08:03:27 +00:00
if errs.IsObjectNotFound(err) {
2022-06-15 12:31:23 +00:00
parentPath, dirName := stdpath.Split(path)
2022-07-10 06:45:39 +00:00
err = MakeDir(ctx, storage, parentPath)
2022-06-15 12:31:23 +00:00
if err != nil {
return errors.WithMessagef(err, "failed to make parent dir [%s]", parentPath)
}
2022-07-10 06:45:39 +00:00
parentDir, err := Get(ctx, storage, parentPath)
2022-06-15 12:31:23 +00:00
// this should not happen
if err != nil {
return errors.WithMessagef(err, "failed to get parent dir [%s]", parentPath)
}
err = storage.MakeDir(ctx, parentDir, dirName)
if err == nil {
ClearCache(storage, parentPath)
}
return errors.WithStack(err)
2022-06-15 12:31:23 +00:00
} else {
return errors.WithMessage(err, "failed to check if dir exists")
}
} else {
// dir exists
if f.IsDir() {
return nil
} else {
// dir to make is a file
return errors.New("file exists")
}
2022-06-15 11:10:11 +00:00
}
2022-06-10 13:00:51 +00:00
}
2022-07-10 06:45:39 +00:00
func Move(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-10 06:45:39 +00:00
srcObj, err := Get(ctx, storage, srcPath)
2022-06-15 12:31:23 +00:00
if err != nil {
return errors.WithMessage(err, "failed to get src object")
}
2022-07-10 06:45:39 +00:00
dstDir, err := Get(ctx, storage, dstDirPath)
2022-06-21 08:25:45 +00:00
if err != nil {
return errors.WithMessage(err, "failed to get dst dir")
}
2022-08-31 12:58:57 +00:00
return errors.WithStack(storage.Move(ctx, srcObj, dstDir))
2022-06-10 13:00:51 +00:00
}
2022-07-10 06:45:39 +00:00
func Rename(ctx context.Context, storage driver.Driver, srcPath, dstName string) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-10 06:45:39 +00:00
srcObj, err := Get(ctx, storage, srcPath)
2022-06-15 12:31:23 +00:00
if err != nil {
return errors.WithMessage(err, "failed to get src object")
}
2022-08-31 12:58:57 +00:00
return errors.WithStack(storage.Rename(ctx, srcObj, dstName))
2022-06-10 13:00:51 +00:00
}
2022-07-10 06:45:39 +00:00
// Copy Just copy file[s] in a storage
func Copy(ctx context.Context, storage driver.Driver, srcPath, dstDirPath string) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-10 06:45:39 +00:00
srcObj, err := Get(ctx, storage, srcPath)
2022-06-15 12:31:23 +00:00
if err != nil {
return errors.WithMessage(err, "failed to get src object")
}
2022-07-10 06:45:39 +00:00
dstDir, err := Get(ctx, storage, dstDirPath)
2022-08-31 12:58:57 +00:00
return errors.WithStack(storage.Copy(ctx, srcObj, dstDir))
2022-06-10 13:00:51 +00:00
}
2022-07-10 06:45:39 +00:00
func Remove(ctx context.Context, storage driver.Driver, path string) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-10 06:45:39 +00:00
obj, err := Get(ctx, storage, path)
2022-06-15 12:31:23 +00:00
if err != nil {
// if object not found, it's ok
2022-06-23 08:03:27 +00:00
if errs.IsObjectNotFound(err) {
2022-06-15 12:31:23 +00:00
return nil
}
return errors.WithMessage(err, "failed to get object")
}
err = storage.Remove(ctx, obj)
if err == nil {
key := Key(storage, stdpath.Dir(path))
if objs, ok := listCache.Get(key); ok {
j := -1
for i, m := range objs {
if m.GetName() == obj.GetName() {
j = i
break
}
}
if j >= 0 && j < len(objs) {
objs = append(objs[:j], objs[j+1:]...)
listCache.Set(key, objs)
} else {
log.Debugf("not found obj")
}
} else {
log.Debugf("not found parent cache")
}
}
return errors.WithStack(err)
2022-06-10 13:00:51 +00:00
}
2022-07-10 06:45:39 +00:00
func Put(ctx context.Context, storage driver.Driver, dstDirPath string, file model.FileStreamer, up driver.UpdateProgress) error {
2022-09-04 04:43:52 +00:00
if storage.Config().CheckStatus && storage.GetStorage().Status != WORK {
2022-09-03 14:32:09 +00:00
return errors.Errorf("storage not init: %s", storage.GetStorage().Status)
}
2022-07-01 07:04:02 +00:00
defer func() {
if f, ok := file.GetReadCloser().(*os.File); ok {
err := os.RemoveAll(f.Name())
if err != nil {
log.Errorf("failed to remove file [%s]", f.Name())
}
}
}()
2022-06-23 09:05:03 +00:00
defer func() {
if err := file.Close(); err != nil {
log.Errorf("failed to close file streamer, %v", err)
}
}()
// if file exist and size = 0, delete it
dstPath := stdpath.Join(dstDirPath, file.GetName())
fi, err := Get(ctx, storage, dstPath)
if err == nil {
if fi.GetSize() == 0 {
err = Remove(ctx, storage, dstPath)
if err != nil {
return errors.WithMessagef(err, "failed remove file that exist and have size 0")
}
}
}
err = MakeDir(ctx, storage, dstDirPath)
2022-06-15 11:20:36 +00:00
if err != nil {
return errors.WithMessagef(err, "failed to make dir [%s]", dstDirPath)
2022-06-15 12:31:23 +00:00
}
2022-07-10 06:45:39 +00:00
parentDir, err := Get(ctx, storage, dstDirPath)
2022-06-15 12:31:23 +00:00
// this should not happen
if err != nil {
return errors.WithMessagef(err, "failed to get dir [%s]", dstDirPath)
2022-06-15 11:20:36 +00:00
}
2022-06-17 13:23:44 +00:00
// if up is nil, set a default to prevent panic
if up == nil {
2022-06-20 12:34:58 +00:00
up = func(p int) {}
2022-06-17 13:23:44 +00:00
}
2022-07-10 06:45:39 +00:00
err = storage.Put(ctx, parentDir, file, up)
2022-07-01 07:04:02 +00:00
log.Debugf("put file [%s] done", file.GetName())
if err == nil {
2022-08-31 08:32:51 +00:00
// set as complete
up(100)
// clear cache
//key := stdpath.Join(storage.GetStorage().MountPath, dstDirPath)
//listCache.Del(key)
}
2022-08-31 12:58:57 +00:00
return errors.WithStack(err)
2022-06-10 13:00:51 +00:00
}