Skip to content

Commit

Permalink
A release
Browse files Browse the repository at this point in the history
  • Loading branch information
mojatter committed Nov 18, 2021
1 parent 6d62059 commit 69e8fc4
Show file tree
Hide file tree
Showing 16 changed files with 2,899 additions and 2 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# iofs
Writable io/fs implentations
# fs2

[![PkgGoDev](https://pkg.go.dev/badge/github.com/jarxorg/fs2)](https://pkg.go.dev/github.com/jarxorg/fs2)
[![Report Card](https://goreportcard.com/badge/github.com/jarxorg/fs2)](https://goreportcard.com/report/github.com/jarxorg/fs2)

Package fs2 provides writable [io/fs](https://pkg.go.dev/io/fs).FS interfaces.

```go
// WriterFile is a file that provides an implementation fs.File and io.Writer.
type WriterFile interface {
fs.File
io.Writer
}

// WriteFileFS is the interface implemented by a filesystem that provides an
// optimized implementation of MkdirAll, CreateFile, WriteFile.
type WriteFileFS interface {
fs.FS
MkdirAll(dir string, mode fs.FileMode) error
CreateFile(name string, mode fs.FileMode) (WriterFile, error)
WriteFile(name string, p []byte, mode fs.FileMode) (n int, err error)
}

// RemoveFileFS is the interface implemented by a filesystem that provides an
// implementation of RemoveFile.
type RemoveFileFS interface {
fs.FS
RemoveFile(name string) error
RemoveAll(name string) error
}
```

This is one of the solutions to an [issue](https://github.com/golang/go/issues/45757) of github.com/golango/go.

The following packages are an implementation of fs2.

- [osfs](https://pkg.go.dev/badge/github.com/jarxorg/fs2/osfs)
- [memfs](https://pkg.go.dev/badge/github.com/jarxorg/fs2/memfs)
- [s3fs](https://pkg.go.dev/badge/github.com/jarxorg/s3fs)
105 changes: 105 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package fs2

import (
"errors"
"io"
"io/fs"
)

var (
// ErrNotImplemented "not implemented"
ErrNotImplemented = errors.New("not implemented")
)

// WriterFile is a file that provides an implementation fs.File and io.Writer.
type WriterFile interface {
fs.File
io.Writer
}

// WriteFileFS is the interface implemented by a filesystem that provides an
// optimized implementation of MkdirAll, CreateFile, WriteFile.
type WriteFileFS interface {
fs.FS
MkdirAll(dir string, mode fs.FileMode) error
CreateFile(name string, mode fs.FileMode) (WriterFile, error)
WriteFile(name string, p []byte, mode fs.FileMode) (n int, err error)
}

// MkdirAll creates the named directory. If the filesystem implements
// WriteFileFS calls fsys.MkdirAll otherwise returns a PathError.
func MkdirAll(fsys fs.FS, dir string, mode fs.FileMode) error {
if fsys, ok := fsys.(WriteFileFS); ok {
return fsys.MkdirAll(dir, mode)
}
return &fs.PathError{Op: "MkdirAll", Path: dir, Err: ErrNotImplemented}
}

// CreateFile creates the named file. If the filesystem implements
// WriteFileFS calls fsys.CreateFile otherwise returns a PathError.
func CreateFile(fsys fs.FS, name string, mode fs.FileMode) (WriterFile, error) {
if fsys, ok := fsys.(WriteFileFS); ok {
return fsys.CreateFile(name, mode)
}
return nil, &fs.PathError{Op: "CreateFile", Path: name, Err: ErrNotImplemented}
}

// WriteFile writes the specified bytes to the named file. If the filesystem implements
// WriteFileFS calls fsys.WriteFile otherwise returns a PathError.
func WriteFile(fsys fs.FS, name string, p []byte, mode fs.FileMode) (n int, err error) {
if fsys, ok := fsys.(WriteFileFS); ok {
return fsys.WriteFile(name, p, mode)
}
return 0, &fs.PathError{Op: "WriteFile", Path: name, Err: ErrNotImplemented}
}

// RemoveFileFS is the interface implemented by a filesystem that provides an
// implementation of RemoveFile.
type RemoveFileFS interface {
fs.FS
RemoveFile(name string) error
RemoveAll(name string) error
}

// RemoveFile removes the specified named file. If the filesystem implements
// RemoveFileFS calls fsys.RemoveFile otherwise return a PathError.
func RemoveFile(fsys fs.FS, name string) error {
if fsys, ok := fsys.(RemoveFileFS); ok {
return fsys.RemoveFile(name)
}
return &fs.PathError{Op: "RemoveFile", Path: name, Err: ErrNotImplemented}
}

// RemoveAll removes path and any children it contains. If the filesystem
// implements RemoveFileFS calls fsys.RemoveAll otherwise return a PathError.
func RemoveAll(fsys fs.FS, path string) error {
if fsys, ok := fsys.(RemoveFileFS); ok {
return fsys.RemoveAll(path)
}
return &fs.PathError{Op: "RemoveAll", Path: path, Err: ErrNotImplemented}
}

// CopyFS walks the specified root directory on src and copies directories and
// files to dest filesystem.
func CopyFS(dest, src fs.FS, root string) error {
return fs.WalkDir(src, root, func(path string, d fs.DirEntry, err error) error {
if err != nil || d == nil {
return err
}
if d.IsDir() {
return MkdirAll(dest, path, d.Type())
}
srcFile, err := src.Open(path)
if err != nil {
return err
}
destFile, err := CreateFile(dest, path, d.Type())
if err != nil {
return err
}
defer destFile.Close()

_, err = io.Copy(destFile, srcFile)
return err
})
}
Loading

0 comments on commit 69e8fc4

Please sign in to comment.