Skip to content

Commit

Permalink
Rename File/Regular to Node/File
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Nov 29, 2018
1 parent 2b34e27 commit 960ec1e
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 70 deletions.
41 changes: 18 additions & 23 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ var (
ErrNotSupported = errors.New("operation not supported")
)

// File is an interface that provides functionality for handling
// files/directories as values that can be supplied to commands. For
// directories, child files are accessed serially by calling `NextFile()`
//
// Read/Seek methods are only valid for files
// NextFile method is only valid for directories
type File interface {
// Node is a common interface for files, directories and other special files
type Node interface {
io.Closer

// Size returns size of this file (if this file is a directory, total size of
Expand All @@ -28,28 +23,28 @@ type File interface {
Size() (int64, error)
}

// Regular represents a regular Unix file
type Regular interface {
File
// Node represents a regular Unix file
type File interface {
Node

io.Reader
io.Seeker
}

// DirEntry exposes information about a directory entry
type DirEntry interface {
// Name returns the base name of this entry, which is the base name of
// the referenced file
// Name returns base name of this entry, which is the base name of referenced
// file
Name() string

// File returns the file referenced by this DirEntry
File() File
// Node returns the file referenced by this DirEntry
Node() Node

// Regular is an alias for ent.File().(Regular). If the file isn't a regular
// File is an alias for ent.Node().(File). If the file isn't a regular
// file, nil value will be returned
Regular() Regular
File() File

// Dir is an alias for ent.File().(directory). If the file isn't a directory,
// Dir is an alias for ent.Node().(directory). If the file isn't a directory,
// nil value will be returned
Dir() Directory
}
Expand All @@ -63,18 +58,18 @@ type DirIterator interface {
// to Next() and after Next() returned false may result in undefined behavior
DirEntry

// Next advances the iterator to the next file.
// Next advances iterator to the next file.
Next() bool

// Err may return an error after the previous call to Next() returned `false`.
// If the previous call to Next() returned `true`, Err() is guaranteed to
// Err may return an error after previous call to Next() returned `false`.
// If previous call to Next() returned `true`, Err() is guaranteed to
// return nil
Err() error
}

// Directory is a special file which can link to any number of files.
type Directory interface {
File
Node

// Entries returns a stateful iterator over directory entries.
//
Expand All @@ -83,7 +78,7 @@ type Directory interface {
// it := dir.Entries()
// for it.Next() {
// name := it.Name()
// file := it.File()
// file := it.Node()
// [...]
// }
// if it.Err() != nil {
Expand All @@ -105,7 +100,7 @@ type Directory interface {

// FileInfo exposes information on files in local filesystem
type FileInfo interface {
File
Node

// AbsPath returns full real file path.
AbsPath() string
Expand Down
6 changes: 3 additions & 3 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSliceFiles(t *testing.T) {
if !it.Next() {
t.Fatal("Expected a file")
}
rf := it.Regular()
rf := it.File()
if rf == nil {
t.Fatal("Expected a regular file")
}
Expand Down Expand Up @@ -104,7 +104,7 @@ anotherfile
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
}
mf, ok := mpf.(Regular)
mf, ok := mpf.(File)
if !ok {
t.Fatal("Expected file to not be a directory")
}
Expand Down Expand Up @@ -147,7 +147,7 @@ anotherfile
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
}
mf, ok = mpf.(Regular)
mf, ok = mpf.(File)
if !ok {
t.Fatal("Expected file to not be a directory")
}
Expand Down
2 changes: 1 addition & 1 deletion is_hidden.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

func IsHidden(name string, f File) bool {
func IsHidden(name string, f Node) bool {
fName := filepath.Base(name)

if strings.HasPrefix(fName, ".") && len(fName) > 1 {
Expand Down
2 changes: 1 addition & 1 deletion is_hidden_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
windows "golang.org/x/sys/windows"
)

func IsHidden(name string, f File) bool {
func IsHidden(name string, f Node) bool {

fName := filepath.Base(name)

Expand Down
4 changes: 2 additions & 2 deletions linkfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Symlink struct {
reader io.Reader
}

func NewLinkFile(target string, stat os.FileInfo) Regular {
func NewLinkFile(target string, stat os.FileInfo) File {
return &Symlink{
Target: target,
stat: stat,
Expand Down Expand Up @@ -45,4 +45,4 @@ func (lf *Symlink) Size() (int64, error) {
return 0, ErrNotSupported
}

var _ Regular = &Symlink{}
var _ File = &Symlink{}
14 changes: 7 additions & 7 deletions multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sync"
)

// MultiFileReader reads from a `commands.File` (which can be a directory of files
// MultiFileReader reads from a `commands.Node` (which can be a directory of files
// or a regular file) as HTTP multipart encoded data.
type MultiFileReader struct {
io.Reader
Expand All @@ -20,7 +20,7 @@ type MultiFileReader struct {
files []DirIterator
path []string

currentFile File
currentFile Node
buf bytes.Buffer
mpWriter *multipart.Writer
closed bool
Expand Down Expand Up @@ -86,7 +86,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
// handle starting a new file part
if !mfr.closed {

mfr.currentFile = entry.File()
mfr.currentFile = entry.Node()

// write the boundary and headers
header := make(textproto.MIMEHeader)
Expand All @@ -95,7 +95,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {

var contentType string

switch f := entry.File().(type) {
switch f := entry.Node().(type) {
case *Symlink:
contentType = "application/symlink"
case Directory:
Expand All @@ -107,15 +107,15 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {
mfr.files = append(mfr.files, newIt)
mfr.path = append(mfr.path, entry.Name())
contentType = "application/x-directory"
case Regular:
case File:
// otherwise, use the file as a reader to read its contents
contentType = "application/octet-stream"
default:
return 0, ErrNotSupported
}

header.Set("Content-Type", contentType)
if rf, ok := entry.File().(FileInfo); ok {
if rf, ok := entry.Node().(FileInfo); ok {
header.Set("abspath", rf.AbsPath())
}

Expand All @@ -133,7 +133,7 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {

// otherwise, read from file data
switch f := mfr.currentFile.(type) {
case Regular:
case File:
written, err = f.Read(buf)
if err != io.EOF {
return written, err
Expand Down
6 changes: 3 additions & 3 deletions multifilereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestOutput(t *testing.T) {
if mpf == nil || err != nil {
t.Fatal("Expected non-nil MultipartFile, nil error")
}
mpr, ok := mpf.(Regular)
mpr, ok := mpf.(File)
if !ok {
t.Fatal("Expected file to be a regular file")
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestOutput(t *testing.T) {
if child == nil || err != nil {
t.Fatal("Expected to be able to read a child file")
}
if _, ok := child.(Regular); !ok {
if _, ok := child.(File); !ok {
t.Fatal("Expected file to not be a directory")
}
if cname != "a.txt" {
Expand All @@ -147,7 +147,7 @@ func TestOutput(t *testing.T) {
if child == nil || err != nil {
t.Fatal("Expected to be able to read a child file")
}
if _, ok := child.(Regular); !ok {
if _, ok := child.(File); !ok {
t.Fatal("Expected file to not be a directory")
}
if cname != "b.txt" {
Expand Down
14 changes: 7 additions & 7 deletions multipartfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ const (

var ErrPartOutsideParent = errors.New("file outside parent dir")

// MultipartFile implements File, and is created from a `multipart.Part`.
// MultipartFile implements Node, and is created from a `multipart.Part`.
// It can be either a directory or file (checked by calling `IsDirectory()`).
type MultipartFile struct {
File
Node

Part *multipart.Part
Reader PartReader
Mediatype string
}

func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (File, error) {
func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (Node, error) {
if !isDirectory(mediatype) {
return nil, ErrNotDirectory
}
Expand All @@ -45,7 +45,7 @@ func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (File, er
return f, nil
}

func newFileFromPart(parent string, part *multipart.Part, reader PartReader) (string, File, error) {
func newFileFromPart(parent string, part *multipart.Part, reader PartReader) (string, Node, error) {
f := &MultipartFile{
Part: part,
Reader: reader,
Expand Down Expand Up @@ -97,7 +97,7 @@ func isDirectory(mediatype string) bool {
type multipartIterator struct {
f *MultipartFile

curFile File
curFile Node
curName string
err error
}
Expand All @@ -106,11 +106,11 @@ func (it *multipartIterator) Name() string {
return it.curName
}

func (it *multipartIterator) File() File {
func (it *multipartIterator) File() Node {
return it.curFile
}

func (it *multipartIterator) Regular() Regular {
func (it *multipartIterator) Regular() File {
return castRegular(it.File())
}

Expand Down
4 changes: 2 additions & 2 deletions readerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type ReaderFile struct {
stat os.FileInfo
}

func NewReaderFile(reader io.ReadCloser, stat os.FileInfo) Regular {
func NewReaderFile(reader io.ReadCloser, stat os.FileInfo) File {
return &ReaderFile{"", reader, stat}
}

Expand Down Expand Up @@ -58,5 +58,5 @@ func (f *ReaderFile) Seek(offset int64, whence int) (int64, error) {
return 0, ErrNotSupported
}

var _ Regular = &ReaderFile{}
var _ File = &ReaderFile{}
var _ FileInfo = &ReaderFile{}
18 changes: 9 additions & 9 deletions serialfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
)

// serialFile implements File, and reads from a path on the OS filesystem.
// serialFile implements Node, and reads from a path on the OS filesystem.
// No more than one file will be opened at a time (directories will advance
// to the next file when NextFile() is called).
type serialFile struct {
Expand All @@ -26,13 +26,13 @@ type serialIterator struct {
path string

curName string
curFile File
curFile Node

err error
}

// TODO: test/document limitations
func NewSerialFile(path string, hidden bool, stat os.FileInfo) (File, error) {
func NewSerialFile(path string, hidden bool, stat os.FileInfo) (Node, error) {
switch mode := stat.Mode(); {
case mode.IsRegular():
file, err := os.Open(path)
Expand Down Expand Up @@ -63,11 +63,11 @@ func (it *serialIterator) Name() string {
return it.curName
}

func (it *serialIterator) File() File {
func (it *serialIterator) File() Node {
return it.curFile
}

func (it *serialIterator) Regular() Regular {
func (it *serialIterator) Regular() File {
return castRegular(it.File())
}

Expand Down Expand Up @@ -121,7 +121,7 @@ func (f *serialFile) Entries() (DirIterator, error) {
}, nil
}

func (f *serialFile) NextFile() (string, File, error) {
func (f *serialFile) NextFile() (string, Node, error) {
// if there aren't any files left in the root directory, we're done
if len(f.files) == 0 {
return "", nil, io.EOF
Expand Down Expand Up @@ -182,12 +182,12 @@ func (f *serialFile) Size() (int64, error) {
return du, err
}

func castRegular(f File) Regular {
r, _ := f.(Regular)
func castRegular(f Node) File {
r, _ := f.(File)
return r
}

func castDir(f File) Directory {
func castDir(f Node) Directory {
d, _ := f.(Directory)
return d
}
Expand Down
Loading

0 comments on commit 960ec1e

Please sign in to comment.