From 0e1d4a319e33b2942e2cbe516ca46d961e2d8a86 Mon Sep 17 00:00:00 2001 From: Erik Unger Date: Sun, 27 Aug 2023 17:05:55 +0200 Subject: [PATCH] more FileSystem extension interfaces --- file.go | 39 ++++++- filesystem.go | 29 +++++- go.work | 3 +- httpfs/httpfs.go | 4 +- invalidfilesystem.go | 16 --- readonlybase.go | 4 - s3fs/s3fs.go | 11 -- sftpfs/go.mod | 19 ++++ sftpfs/go.sum | 56 ++++++++++ sftpfs/sftpfs.go | 231 ++++++++++++++++++++++++++++++++++++++++++ sftpfs/sftpfs_test.go | 1 + utils.go | 17 ++++ zipfs/zipfs.go | 38 ------- 13 files changed, 387 insertions(+), 81 deletions(-) create mode 100644 sftpfs/go.mod create mode 100644 sftpfs/go.sum create mode 100644 sftpfs/sftpfs.go create mode 100644 sftpfs/sftpfs_test.go diff --git a/file.go b/file.go index 38045c3..6378968 100644 --- a/file.go +++ b/file.go @@ -608,7 +608,14 @@ func (file File) Touch(perm ...Permissions) error { return ErrEmptyPath } fileSystem, path := file.ParseRawURI() - return fileSystem.Touch(path, perm) + if fs, ok := fileSystem.(TouchFileSystem); ok { + return fs.Touch(path, perm) + } + w, err := file.OpenWriter(perm...) + if err != nil { + return err + } + return w.Close() } func (file File) MakeDir(perm ...Permissions) error { @@ -748,7 +755,15 @@ func (file File) ReadAllContext(ctx context.Context) (data []byte, err error) { return nil, ErrEmptyPath } fileSystem, path := file.ParseRawURI() - return fileSystem.ReadAll(ctx, path) + if fs, ok := fileSystem.(ReadAllFileSystem); ok { + return fs.ReadAll(ctx, path) + } + r, err := fileSystem.OpenReader(path) + if err != nil { + return nil, err + } + defer r.Close() + return ReadAllContext(ctx, r) } // ReadAllContentHash reads and returns all bytes of the file @@ -788,7 +803,15 @@ func (file File) WriteAllContext(ctx context.Context, data []byte, perm ...Permi return ErrEmptyPath } fileSystem, path := file.ParseRawURI() - return fileSystem.WriteAll(ctx, path, data, perm) + if fs, ok := fileSystem.(WriteAllFileSystem); ok { + return fs.WriteAll(ctx, path, data, perm) + } + w, err := fileSystem.OpenReadWriter(path, perm) + if err != nil { + return err + } + defer w.Close() + return WriteAllContext(ctx, w, data) } func (file File) WriteAllString(str string, perm ...Permissions) error { @@ -807,7 +830,15 @@ func (file File) Append(ctx context.Context, data []byte, perm ...Permissions) e return ErrEmptyPath } fileSystem, path := file.ParseRawURI() - return fileSystem.Append(ctx, path, data, perm) + if fs, ok := fileSystem.(AppendFileSystem); ok { + return fs.Append(ctx, path, data, perm) + } + w, err := fileSystem.OpenAppendWriter(path, perm) + if err != nil { + return err + } + defer w.Close() + return WriteAllContext(ctx, w, data) } func (file File) AppendString(ctx context.Context, str string, perm ...Permissions) error { diff --git a/filesystem.go b/filesystem.go index 148abbc..062084d 100644 --- a/filesystem.go +++ b/filesystem.go @@ -102,13 +102,8 @@ type FileSystem interface { Group(filePath string) string // TODO SetGroup(filePath string, group string) error // TODO - Touch(filePath string, perm []Permissions) error MakeDir(dirPath string, perm []Permissions) error - ReadAll(ctx context.Context, filePath string) ([]byte, error) - WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error - Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error - OpenReader(filePath string) (fs.File, error) OpenWriter(filePath string, perm []Permissions) (io.WriteCloser, error) OpenAppendWriter(filePath string, perm []Permissions) (io.WriteCloser, error) @@ -192,3 +187,27 @@ type WatchFileSystem interface { // will cancel a particular watch. Watch(filePath string, onEvent func(File, Event)) (cancel func() error, err error) } + +type TouchFileSystem interface { + FileSystem + + Touch(filePath string, perm []Permissions) error +} + +type ReadAllFileSystem interface { + FileSystem + + ReadAll(ctx context.Context, filePath string) ([]byte, error) +} + +type WriteAllFileSystem interface { + FileSystem + + WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error +} + +type AppendFileSystem interface { + FileSystem + + Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error +} diff --git a/go.work b/go.work index 4cb2582..d2d404d 100644 --- a/go.work +++ b/go.work @@ -1,7 +1,8 @@ -go 1.21 +go 1.21.0 use ( . ./dropboxfs + ./sftpfs ./s3fs ) diff --git a/httpfs/httpfs.go b/httpfs/httpfs.go index 893f3d7..d5000ec 100644 --- a/httpfs/httpfs.go +++ b/httpfs/httpfs.go @@ -32,8 +32,8 @@ const ( ) var ( - FileSystem fs.FileSystem = &HTTPFileSystem{prefix: Prefix} - FileSystemTLS fs.FileSystem = &HTTPFileSystem{prefix: PrefixTLS} + FileSystem = &HTTPFileSystem{prefix: Prefix} + FileSystemTLS = &HTTPFileSystem{prefix: PrefixTLS} ) type HTTPFileSystem struct { diff --git a/invalidfilesystem.go b/invalidfilesystem.go index bb70b52..21a3fc8 100644 --- a/invalidfilesystem.go +++ b/invalidfilesystem.go @@ -138,26 +138,10 @@ func (invalid InvalidFileSystem) SetGroup(filePath string, group string) error { return ErrInvalidFileSystem } -func (invalid InvalidFileSystem) Touch(filePath string, perm []Permissions) error { - return ErrInvalidFileSystem -} - func (invalid InvalidFileSystem) MakeDir(dirPath string, perm []Permissions) error { return ErrInvalidFileSystem } -func (invalid InvalidFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error) { - return nil, ErrInvalidFileSystem -} - -func (invalid InvalidFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []Permissions) error { - return ErrInvalidFileSystem -} - -func (invalid InvalidFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []Permissions) error { - return ErrInvalidFileSystem -} - func (invalid InvalidFileSystem) OpenReader(filePath string) (fs.File, error) { return nil, ErrInvalidFileSystem } diff --git a/readonlybase.go b/readonlybase.go index 81086fb..a665cde 100644 --- a/readonlybase.go +++ b/readonlybase.go @@ -44,10 +44,6 @@ func (*ReadOnlyBase) SetGroup(filePath string, group string) error { return ErrReadOnlyFileSystem } -func (*ReadOnlyBase) Touch(filePath string, perm []Permissions) error { - return ErrReadOnlyFileSystem -} - func (*ReadOnlyBase) MakeDir(dirPath string, perm []Permissions) error { return ErrReadOnlyFileSystem } diff --git a/s3fs/s3fs.go b/s3fs/s3fs.go index 2308bef..068ef01 100644 --- a/s3fs/s3fs.go +++ b/s3fs/s3fs.go @@ -380,17 +380,6 @@ func (s *S3FileSystem) WriteAll(ctx context.Context, filePath string, data []byt return err } -func (s *S3FileSystem) Append(ctx context.Context, filePath string, data []byte, perm []fs.Permissions) error { - if s.readOnly { - return fs.ErrReadOnlyFileSystem - } - current, err := s.ReadAll(ctx, filePath) - if err != nil { - return err - } - return s.WriteAll(ctx, filePath, append(current, data...), perm) -} - func (s *S3FileSystem) OpenReader(filePath string) (iofs.File, error) { if filePath == "" { return nil, fs.ErrEmptyPath diff --git a/sftpfs/go.mod b/sftpfs/go.mod new file mode 100644 index 0000000..a437e05 --- /dev/null +++ b/sftpfs/go.mod @@ -0,0 +1,19 @@ +module github.com/ungerik/go-fs/sftpfs + +go 1.21.0 + +require ( + github.com/stretchr/testify v1.8.4 + github.com/ungerik/go-fs v0.0.0-20230823102526-9529193e0ede +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/kr/fs v0.1.0 // indirect + github.com/pkg/sftp v1.13.6 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + golang.org/x/crypto v0.1.0 // indirect + golang.org/x/sys v0.11.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/sftpfs/go.sum b/sftpfs/go.sum new file mode 100644 index 0000000..6cef72a --- /dev/null +++ b/sftpfs/go.sum @@ -0,0 +1,56 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= +github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8= +github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= +github.com/pkg/sftp v1.13.6 h1:JFZT4XbOU7l77xGSpOdW+pwIMqP044IyjXX6FGyEKFo= +github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Qk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/ungerik/go-fs v0.0.0-20230823102526-9529193e0ede h1:grl7JvEXro/SMPysVOlI9ytg1KhxqfTjxJY2TqMOEXg= +github.com/ungerik/go-fs v0.0.0-20230823102526-9529193e0ede/go.mod h1:P8k1DG+Ox0KP4MFNTSPd8ojoDUwXjrWdGjsssF6vT/g= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU= +golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/sftpfs/sftpfs.go b/sftpfs/sftpfs.go new file mode 100644 index 0000000..5df890e --- /dev/null +++ b/sftpfs/sftpfs.go @@ -0,0 +1,231 @@ +// Package ftpfs implements a (S)FTP client file system. +package ftpfs + +import ( + "context" + "errors" + "fmt" + "io" + iofs "io/fs" + "strings" + + "github.com/pkg/sftp" + "github.com/ungerik/go-fs" + "github.com/ungerik/go-fs/fsimpl" + "golang.org/x/crypto/ssh" +) + +const ( + Prefix = "sftp://" + Separator = "/" +) + +var ( + _ fs.FileSystem = new(SFTPFileSystem) +) + +type SFTPFileSystem struct { + client *sftp.Client + prefix string +} + +func Dial(addr, user, password string) (*SFTPFileSystem, error) { + addr = strings.TrimSuffix(strings.TrimPrefix(addr, "sftp://"), "/") + + var hostKey ssh.PublicKey + config := &ssh.ClientConfig{ + User: user, + Auth: []ssh.AuthMethod{ + ssh.Password(password), + }, + HostKeyCallback: ssh.FixedHostKey(hostKey), + } + conn, err := ssh.Dial("tcp", addr, config) + if err != nil { + return nil, err + } + return New(addr, conn) +} + +func New(addr string, conn *ssh.Client) (*SFTPFileSystem, error) { + addr = strings.TrimSuffix(strings.TrimPrefix(addr, "sftp://"), "/") + + client, err := sftp.NewClient(conn) + if err != nil { + return nil, err + } + fileSystem := &SFTPFileSystem{ + client: client, + prefix: "sftp://" + addr, + } + fs.Register(fileSystem) + return fileSystem, nil +} + +func (f *SFTPFileSystem) Close() error { + fs.Unregister(f) + return f.client.Close() +} + +func (f *SFTPFileSystem) RootDir() fs.File { + return fs.File(f.prefix + Separator) +} + +func (f *SFTPFileSystem) ID() (string, error) { + return f.prefix, nil +} + +func (f *SFTPFileSystem) Prefix() string { + return f.prefix +} + +func (f *SFTPFileSystem) Name() string { + return "SFTP" +} + +func (f *SFTPFileSystem) String() string { + return f.prefix + " file system" +} + +func (f *SFTPFileSystem) URL(cleanPath string) string { + return Prefix + cleanPath +} + +func (f *SFTPFileSystem) JoinCleanFile(uriParts ...string) fs.File { + return fs.File(Prefix + f.JoinCleanPath(uriParts...)) +} + +func (f *SFTPFileSystem) JoinCleanPath(uriParts ...string) string { + return fsimpl.JoinCleanPath(uriParts, Prefix, Separator) +} + +func (f *SFTPFileSystem) SplitPath(filePath string) []string { + return strings.Split(strings.TrimPrefix(filePath, Prefix), Separator) +} + +func (f *SFTPFileSystem) Separator() string { return Separator } + +func (f *SFTPFileSystem) IsAbsPath(filePath string) bool { + return strings.HasPrefix(filePath, Prefix) +} + +func (f *SFTPFileSystem) AbsPath(filePath string) string { + if f.IsAbsPath(filePath) { + return filePath + } + return Prefix + strings.TrimPrefix(filePath, Separator) +} + +func (f *SFTPFileSystem) SplitDirAndName(filePath string) (dir, name string) { + return fsimpl.SplitDirAndName(filePath, 0, Separator) +} + +func (f *SFTPFileSystem) info(filePath string) fs.FileInfo { + + return fs.FileInfo{ + Exists: true, + // Name: path.Base(request.URL.Path), + // Size: size, + // Modified: modified, + } +} + +func (f *SFTPFileSystem) Stat(filePath string) (iofs.FileInfo, error) { + info := f.info(filePath) + if !info.Exists { + return nil, fs.NewErrDoesNotExist(fs.File(filePath)) + } + return info.StdFileInfo(), nil +} + +func (f *SFTPFileSystem) Exists(filePath string) bool { + return f.info(filePath).Exists +} + +func (f *SFTPFileSystem) IsHidden(filePath string) bool { return false } +func (f *SFTPFileSystem) IsSymbolicLink(filePath string) bool { return false } + +func (f *SFTPFileSystem) ListDirInfo(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error { + return fmt.Errorf("HTTPFileSystem.ListDirInfo: %w", errors.ErrUnsupported) +} + +func (f *SFTPFileSystem) ListDirInfoRecursive(ctx context.Context, dirPath string, callback func(fs.FileInfo) error, patterns []string) error { + return fmt.Errorf("HTTPFileSystem.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 + } + infos, err := f.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 +} + +func (f *SFTPFileSystem) OpenReader(filePath string) (reader iofs.File, err error) { + return f.client.Open(filePath) +} + +func (f *SFTPFileSystem) IsReadOnly() bool { + return false +} + +func (f *SFTPFileSystem) IsWriteOnly() bool { + return false +} + +func (f *SFTPFileSystem) MatchAnyPattern(name string, patterns []string) (bool, error) { + return fsimpl.MatchAnyPattern(name, patterns) +} + +func (f *SFTPFileSystem) SetPermissions(filePath string, perm fs.Permissions) error { + return fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) User(filePath string) string { return "" } + +func (f *SFTPFileSystem) SetUser(filePath string, user string) error { + return fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) Group(filePath string) string { return "" } + +func (f *SFTPFileSystem) SetGroup(filePath string, group string) error { + return fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) MakeDir(dirPath string, perm []fs.Permissions) error { + return fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) OpenWriter(filePath string, perm []fs.Permissions) (io.WriteCloser, error) { + return nil, fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) OpenAppendWriter(filePath string, perm []fs.Permissions) (io.WriteCloser, error) { + return nil, fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) OpenReadWriter(filePath string, perm []fs.Permissions) (fs.ReadWriteSeekCloser, error) { + return nil, fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) Truncate(filePath string, size int64) error { + return fs.ErrReadOnlyFileSystem +} + +func (f *SFTPFileSystem) Move(filePath string, destPath string) error { + return f.client.Rename(filePath, destPath) +} + +func (f *SFTPFileSystem) Remove(filePath string) error { + return f.client.Remove(filePath) +} diff --git a/sftpfs/sftpfs_test.go b/sftpfs/sftpfs_test.go new file mode 100644 index 0000000..f8bc70b --- /dev/null +++ b/sftpfs/sftpfs_test.go @@ -0,0 +1 @@ +package ftpfs diff --git a/utils.go b/utils.go index 8ff5348..e047aff 100644 --- a/utils.go +++ b/utils.go @@ -90,3 +90,20 @@ func ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error) { } return nil, ctx.Err() } + +// WriteAllContext writes all data wo the to w +// with a cancelable context. +func WriteAllContext(ctx context.Context, w io.Writer, data []byte) error { + const chunkSize = 4 * 1024 * 1024 // 4MB + for len(data) > 0 { + if ctx.Err() != nil { + return ctx.Err() + } + n, err := w.Write(data[:max(chunkSize, len(data))]) + if err != nil { + return err + } + data = data[n:] + } + return nil +} diff --git a/zipfs/zipfs.go b/zipfs/zipfs.go index 7861f88..cd83cc2 100644 --- a/zipfs/zipfs.go +++ b/zipfs/zipfs.go @@ -324,16 +324,6 @@ func (*ZipFileSystem) Group(filePath string) string { return "" } -func (zipfs *ZipFileSystem) ReadAll(ctx context.Context, filePath string) ([]byte, error) { - file, err := zipfs.OpenReader(filePath) - if err != nil { - return nil, err - } - defer file.Close() - - return fs.ReadAllContext(ctx, file) -} - func (zipfs *ZipFileSystem) OpenReader(filePath string) (iofs.File, error) { if zipfs.zipReader == nil { return nil, fs.ErrWriteOnlyFileSystem @@ -378,34 +368,6 @@ func (zipfs *ZipFileSystem) MakeDir(dirPath string, perm []fs.Permissions) error return nil } -func (zipfs *ZipFileSystem) WriteAll(ctx context.Context, filePath string, data []byte, perm []fs.Permissions) error { - if ctx.Err() != nil { - return ctx.Err() - } - writer, err := zipfs.OpenWriter(filePath, perm) - if err != nil { - return err - } - defer writer.Close() - - _, err = writer.Write(data) - return err -} - -func (zipfs *ZipFileSystem) Append(ctx context.Context, filePath string, data []byte, perm []fs.Permissions) error { - if ctx.Err() != nil { - return ctx.Err() - } - writer, err := zipfs.OpenAppendWriter(filePath, perm) - if err != nil { - return err - } - defer writer.Close() - - _, err = writer.Write(data) - return err -} - func (zipfs *ZipFileSystem) OpenWriter(filePath string, perm []fs.Permissions) (io.WriteCloser, error) { if zipfs.zipWriter == nil { return nil, fs.ErrReadOnlyFileSystem