From 5dc5331f477aa0c6d6f2038ec63f8783a0a37579 Mon Sep 17 00:00:00 2001 From: lixz Date: Thu, 27 May 2021 15:44:56 +0800 Subject: [PATCH 1/2] sftpfs Readdirnames,Readdir --- go.sum | 1 - sftpfs/file.go | 32 ++++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/go.sum b/go.sum index 89d9bfb..5cf2cd2 100644 --- a/go.sum +++ b/go.sum @@ -18,7 +18,6 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/sftpfs/file.go b/sftpfs/file.go index e4ccb55..eef14e3 100644 --- a/sftpfs/file.go +++ b/sftpfs/file.go @@ -14,12 +14,14 @@ package sftpfs import ( - "github.com/pkg/sftp" "os" + + "github.com/pkg/sftp" ) type File struct { - fd *sftp.File + client *sftp.Client + fd *sftp.File } func FileOpen(s *sftp.Client, name string) (*File, error) { @@ -27,7 +29,7 @@ func FileOpen(s *sftp.Client, name string) (*File, error) { if err != nil { return &File{}, err } - return &File{fd: fd}, nil + return &File{fd: fd, client: s}, nil } func FileCreate(s *sftp.Client, name string) (*File, error) { @@ -35,7 +37,7 @@ func FileCreate(s *sftp.Client, name string) (*File, error) { if err != nil { return &File{}, err } - return &File{fd: fd}, nil + return &File{fd: fd, client: s}, nil } func (f *File) Close() error { @@ -67,14 +69,28 @@ func (f *File) ReadAt(b []byte, off int64) (n int, err error) { return 0, nil } -// TODO func (f *File) Readdir(count int) (res []os.FileInfo, err error) { - return nil, nil + res, err = f.client.ReadDir(f.Name()) + if err != nil { + return + } + if count > 0 { + if len(res) > count { + res = res[:count] + } + } + return } -// TODO func (f *File) Readdirnames(n int) (names []string, err error) { - return nil, nil + data, err := f.Readdir(n) + if err != nil { + return nil, err + } + for _, v := range data { + names = append(names, v.Name()) + } + return } func (f *File) Seek(offset int64, whence int) (int64, error) { From 5b44be244d8aa56c595b14067a657e5e272f92d6 Mon Sep 17 00:00:00 2001 From: lixz Date: Tue, 8 Jun 2021 10:52:32 +0800 Subject: [PATCH 2/2] sftpfs OpenFile perm --- sftpfs/sftp.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sftpfs/sftp.go b/sftpfs/sftp.go index c66ab47..eadf1e0 100644 --- a/sftpfs/sftp.go +++ b/sftpfs/sftp.go @@ -101,7 +101,8 @@ func (s Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error if err != nil { return nil, err } - return &File{fd: sshfsFile}, nil + err = sshfsFile.Chmod(perm) + return &File{fd: sshfsFile}, err } func (s Fs) Remove(name string) error {