Skip to content

Commit

Permalink
more FileSystem extension interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Aug 27, 2023
1 parent faeda51 commit 0e1d4a3
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 81 deletions.
39 changes: 35 additions & 4 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
29 changes: 24 additions & 5 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
go 1.21
go 1.21.0

use (
.
./dropboxfs
./sftpfs
./s3fs
)
4 changes: 2 additions & 2 deletions httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 0 additions & 16 deletions invalidfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 0 additions & 4 deletions readonlybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
11 changes: 0 additions & 11 deletions s3fs/s3fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions sftpfs/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
56 changes: 56 additions & 0 deletions sftpfs/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Loading

0 comments on commit 0e1d4a3

Please sign in to comment.