Skip to content

Commit

Permalink
rename FileFS to StdFS, FileDirEntry to StdDirEntry, FileInfo.OSFileI…
Browse files Browse the repository at this point in the history
…nfo to StdFileInfo
  • Loading branch information
ungerik committed Aug 14, 2023
1 parent f7ff27f commit b51c86d
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 211 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ str, err := file.ReadAllString(ctx)
var w io.Writer
n, err := file.WriteTo(w)

f, err := file.OpenReader() // os/fs.File
f, err := file.OpenReader() // io/fs.File
r, err := file.OpenReadSeeker() // fs.ReadSeekCloser

```
Expand Down
2 changes: 1 addition & 1 deletion dropboxfs/dropboxfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (dbfs *DropboxFileSystem) Stat(filePath string) (os.FileInfo, error) {
if !info.Exists {
return nil, fs.NewErrDoesNotExist(fs.File(filePath))
}
return info.OSFileInfo(), nil
return info.StdFileInfo(), nil
}

func (dbfs *DropboxFileSystem) Exists(filePath string) bool {
Expand Down
42 changes: 23 additions & 19 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (file File) Joinf(format string, args ...interface{}) File {
return fileSystem.JoinCleanFile(path, fmt.Sprintf(format, args...))
}

// Stat returns a io/fs.FileInfo describing the File.
// Stat returns a standard library io/fs.FileInfo describing the file.
func (file File) Stat() (fs.FileInfo, error) {
if file == "" {
return nil, ErrEmptyPath
Expand All @@ -193,6 +193,8 @@ func (file File) Stat() (fs.FileInfo, error) {
}

// Info returns FileInfo. The FileInfo.ContentHash field is optional.
//
// Use File.Stat to get a standard library io/fs.FileInfo.
func (file File) Info() FileInfo {
fileSystem, path := file.ParseRawURI()
info, err := fileSystem.Stat(path)
Expand Down Expand Up @@ -677,7 +679,7 @@ func (file File) ReadFrom(reader io.Reader) (n int64, err error) {
return io.Copy(writer, reader)
}

// OpenReader opens the file and returns a os/fs.File that has to be closed after reading
// OpenReader opens the file and returns a io/fs.File that has to be closed after reading
func (file File) OpenReader() (fs.File, error) {
if file == "" {
return nil, ErrEmptyPath
Expand Down Expand Up @@ -1060,22 +1062,24 @@ func (file File) HTTPFileSystem() http.FileSystem {
return httpFileSystem{root: file}
}

// AsFS wraps the file as a FileFS that implements
// the FS interfaces of the os/fs package for a File.
//
// FileFS implements the following interfaces:
// StdFS wraps the file as a StdFS struct that
// implements the io/fs.FS interface
// of the standard library for a File.
//
// os/fs.FS
// os/fs.SubFS
// os/fs.StatFS
// os/fs.GlobFS
// os/fs.ReadDirFS
// os/fs.ReadFileFS
func (file File) AsFS() FileFS {
return FileFS{file}
}

// AsDirEntry wraps the file as os/fs.DirEntry.
func (file File) AsDirEntry() fs.DirEntry {
return FileDirEntry{file}
// StdFS implements the following interfaces:
// - io/fs.FS
// - io/fs.SubFS
// - io/fs.StatFS
// - io/fs.GlobFS
// - io/fs.ReadDirFS
// - io/fs.ReadFileFS
func (file File) StdFS() StdFS {
return StdFS{file}
}

// StdDirEntry wraps the file as a StdDirEntry struct
// that implements the io/fs.DirEntry interface
// from the standard library for a File.
func (file File) StdDirEntry() StdDirEntry {
return StdDirEntry{file}
}
173 changes: 0 additions & 173 deletions filefs.go

This file was deleted.

8 changes: 2 additions & 6 deletions fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ func NewNonExistingFileInfo(name string) FileInfo {
}
}

// OSFileInfo returns an os.FileInfo wrapper
// StdFileInfo returns an io/fs.FileInfo wrapper
// for the data stored in the FileInfo struct.
func (i *FileInfo) OSFileInfo() os.FileInfo { return fileInfo{i} }

// FSFileInfo returns an io/fs.FileInfo wrapper
// for the data stored in the FileInfo struct.
func (i *FileInfo) FSFileInfo() fs.FileInfo { return fileInfo{i} }
func (i *FileInfo) StdFileInfo() fs.FileInfo { return fileInfo{i} }

// fileInfo implements os.FileInfo and fs.FileInfo for a given FileInfo
type fileInfo struct{ i *FileInfo }
Expand Down
2 changes: 1 addition & 1 deletion filereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type FileReader interface {
// WriteTo implements the io.WriterTo interface
WriteTo(writer io.Writer) (n int64, err error)

// OpenReader opens the file and returns a io/fs.File that has be closed after reading
// OpenReader opens the file and returns a io/fs.File that has to be closed after reading
OpenReader() (fs.File, error)

// OpenReadSeeker opens the file and returns a ReadSeekCloser.
Expand Down
4 changes: 2 additions & 2 deletions httpfilesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (fs httpFileSystem) Open(name string) (http.File, error) {
if !info.Exists {
return nil, os.ErrNotExist
}
f := &httpFile{info: info.OSFileInfo()}
f := &httpFile{info: info.StdFileInfo()}
if info.IsDir {
f.dir = file
} else {
Expand All @@ -40,7 +40,7 @@ type httpFile struct {
func (f *httpFile) Readdir(count int) (files []os.FileInfo, err error) {
err = f.dir.ListDirInfo(func(_ File, info FileInfo) error {
if !info.IsHidden {
files = append(files, info.OSFileInfo())
files = append(files, info.StdFileInfo())
}
return nil
})
Expand Down
2 changes: 1 addition & 1 deletion httpfs/httpfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (f *HTTPFileSystem) Stat(filePath string) (os.FileInfo, error) {
if !info.Exists {
return nil, fs.NewErrDoesNotExist(fs.File(filePath))
}
return info.OSFileInfo(), nil
return info.StdFileInfo(), nil
}

func (f *HTTPFileSystem) Exists(filePath string) bool {
Expand Down
2 changes: 1 addition & 1 deletion memfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (f MemFile) WriteTo(writer io.Writer) (n int64, err error) {
return int64(i), err
}

// OpenReader opens the file and returns a os/fs.File that has be closed after reading
// OpenReader opens the file and returns a io/fs.File that has to be closed after reading
func (f MemFile) OpenReader() (fs.File, error) {
return fsimpl.NewReadonlyFileBuffer(f.FileData, memFileInfo{f}), nil
}
Expand Down
2 changes: 1 addition & 1 deletion multipartfs/multipartfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (mpfs *MultipartFileSystem) Stat(filePath string) (os.FileInfo, error) {
if !info.Exists {
return nil, fs.NewErrDoesNotExist(fs.File(filePath))
}
return info.OSFileInfo(), nil
return info.StdFileInfo(), nil
}

func (mpfs *MultipartFileSystem) Exists(filePath string) bool {
Expand Down
45 changes: 45 additions & 0 deletions stddirentry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package fs

import "io/fs"

var (
_ fs.DirEntry = StdDirEntry{File("")}
)

// StdDirEntry implements the io/fs.DirEntry interface
// from the standard library for a File.
type StdDirEntry struct {
File File
}

// Name returns the name of the file (or subdirectory) described by the entry.
// This name is only the final element of the path (the base name), not the entire path.
// For example, Name would return "hello.go" not "/home/gopher/hello.go".
func (de StdDirEntry) Name() string {
return de.File.Name()
}

// IsDir reports whether the entry describes a directory.
func (de StdDirEntry) IsDir() bool {
return de.File.IsDir()
}

// Type returns the type bits for the entry.
// The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method.
func (de StdDirEntry) Type() fs.FileMode {
stat, err := de.File.Stat()
if err != nil {
return 0
}
return stat.Mode().Type()
}

// Info returns the FileInfo for the file or subdirectory described by the entry.
// The returned FileInfo may be from the time of the original directory read
// or from the time of the call to Info. If the file has been removed or renamed
// since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist).
// If the entry denotes a symbolic link, Info reports the information about the link itself,
// not the link's target.
func (de StdDirEntry) Info() (fs.FileInfo, error) {
return de.File.Stat()
}
Loading

0 comments on commit b51c86d

Please sign in to comment.