Skip to content

Commit

Permalink
sftpfs and ftpsfs only implement ListDirInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Nov 9, 2023
1 parent 7b7e0bb commit fe0a202
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 54 deletions.
37 changes: 10 additions & 27 deletions ftpfs/ftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ func (f *FTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callbac
return err
}
for _, entry := range entries {
if ctx.Err() != nil {
return ctx.Err()
}
match, err := fsimpl.MatchAnyPattern(entry.Name, patterns)
if err != nil {
return err
}
if !match {
continue
}
err = callback(entryToFileInfo(entry, f.JoinCleanFile(dirPath, entry.Name)))
if err != nil {
return err
Expand All @@ -253,33 +263,6 @@ func (f *FTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callbac
return nil
}

func (f *FTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fmt.Errorf("FTPFileSystem.ListDirInfoRecursive: %w", errors.ErrUnsupported)
}

func (f *FTPFileSystem) ListDirMax(ctx context.Context, dirPath string, max int, patterns []string) (files []fs.File, err error) {
if max == 0 {
return nil, nil
}
conn, dirPath, release, err := f.getConn(dirPath)
if err != nil {
return nil, err
}
defer release()

names, err := conn.NameList(dirPath)
if err != nil {
return nil, err
}
for _, name := range names {
files = append(files, f.JoinCleanFile(dirPath, name))
if max > 0 && len(files) == max {
break
}
}
return files, nil
}

func (f *FTPFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error) {
return fsimpl.MatchAnyPattern(name, patterns)
}
Expand Down
37 changes: 10 additions & 27 deletions sftpfs/sftpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ func (f *SFTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callba
return err
}
for _, info := range infos {
if ctx.Err() != nil {
return ctx.Err()
}
match, err := fsimpl.MatchAnyPattern(info.Name(), patterns)
if err != nil {
return err
}
if !match {
continue
}
err = callback(fs.NewFileInfo(f.JoinCleanFile(dirPath, info.Name()), info, false))
if err != nil {
return err
Expand All @@ -229,33 +239,6 @@ func (f *SFTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callba
return nil
}

func (f *SFTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(*fs.FileInfo) error, patterns []string) error {
return fmt.Errorf("SFTPFileSystem.ListDirInfoRecursive: %w", errors.ErrUnsupported)
}

func (f *SFTPFileSystem) ListDirMax(ctx context.Context, dirPath string, max int, patterns []string) (files []fs.File, err error) {
if max == 0 {
return nil, nil
}
client, dirPath, release, err := f.getClient(dirPath)
if err != nil {
return nil, err
}
defer release()

infos, err := client.ReadDir(dirPath)
if err != nil {
return nil, err
}
for _, info := range infos {
files = append(files, f.JoinCleanFile(dirPath, info.Name()))
if max > 0 && len(files) == max {
break
}
}
return files, nil
}

type sftpFile struct {
*sftp.File
release func() error
Expand Down

0 comments on commit fe0a202

Please sign in to comment.