Skip to content

Commit

Permalink
fix(sftp): read target obj of symlink file (close #4713)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Jul 9, 2023
1 parent 2b533e4 commit fa66358
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
7 changes: 5 additions & 2 deletions drivers/sftp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/alist-org/alist/v3/internal/model"
"github.com/alist-org/alist/v3/pkg/utils"
"github.com/pkg/sftp"
log "github.com/sirupsen/logrus"
)

type SFTP struct {
Expand Down Expand Up @@ -39,13 +40,15 @@ func (d *SFTP) Drop(ctx context.Context) error {
}

func (d *SFTP) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
log.Debugf("[sftp] list dir: %s", dir.GetPath())
files, err := d.client.ReadDir(dir.GetPath())
if err != nil {
return nil, err
}
return utils.SliceConvert(files, func(src os.FileInfo) (model.Obj, error) {
return fileToObj(src), nil
objs, err := utils.SliceConvert(files, func(src os.FileInfo) (model.Obj, error) {
return d.fileToObj(src, dir.GetPath())
})
return objs, err
}

func (d *SFTP) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
Expand Down
39 changes: 34 additions & 5 deletions drivers/sftp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,44 @@ package sftp

import (
"os"
stdpath "path"
"strings"

"github.com/alist-org/alist/v3/internal/model"
log "github.com/sirupsen/logrus"
)

func fileToObj(f os.FileInfo) model.Obj {
return &model.Object{
func (d *SFTP) fileToObj(f os.FileInfo, dir string) (model.Obj, error) {
symlink := f.Mode()&os.ModeSymlink != 0
if !symlink {
return &model.Object{
Name: f.Name(),
Size: f.Size(),
Modified: f.ModTime(),
IsFolder: f.IsDir(),
}, nil
}
path := stdpath.Join(dir, f.Name())
// set target path
target, err := d.client.ReadLink(path)
if err != nil {
return nil, err
}
if !strings.HasPrefix(target, "/") {
target = stdpath.Join(dir, target)
}
_f, err := d.client.Stat(target)
if err != nil {
return nil, err
}
// set basic info
obj := &model.Object{
Name: f.Name(),
Size: f.Size(),
Modified: f.ModTime(),
IsFolder: f.IsDir(),
Size: _f.Size(),
Modified: _f.ModTime(),
IsFolder: _f.IsDir(),
Path: target,
}
log.Debugf("[sftp] obj: %+v, is symlink: %v", obj, symlink)
return obj, nil
}

0 comments on commit fa66358

Please sign in to comment.