Skip to content
This repository has been archived by the owner on Mar 29, 2023. It is now read-only.

Commit

Permalink
Allow Close on dirs, make sure errors make sense
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Oct 24, 2018
1 parent dc3989c commit cfdeccb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
19 changes: 14 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ var (

// 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 `Files()`
// or `Walk()`.
// directories, child files are accessed serially by calling `NextFile()`
//
// Read/Seek/Close methods are only valid for files
// Files/Walk methods are only valid for directories
// Read/Seek methods are only valid for files
// NextFile method is only valid for directories
type File interface {
io.Reader
io.Closer
Expand All @@ -37,14 +36,24 @@ type File interface {
// directory). It will return io.EOF if no more files are
// available. If the file is a regular file (not a directory), NextFile
// will return a non-nil error.
//
// Note:
// - Some implementations may only allow reading in order - if a
// child directory is returned, you need to read all it's children
// first before calling NextFile on parent again. Before doing parallel
// reading or reading entire level at once, make sure the implementation
// you are using allows that
// - Returned files may not be sorted
// - Depending on implementation it may not be safe to iterate multiple
// children in parallel
NextFile() (string, File, error)
}

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

// AbsPath returns full/real file path.
// AbsPath returns full real file path.
AbsPath() string

// Stat returns os.Stat of this file
Expand Down
10 changes: 5 additions & 5 deletions file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func TestSliceFiles(t *testing.T) {
t.Fatal("SliceFile should always be a directory")
}

if n, err := sf.Read(buf); n > 0 || err != io.EOF {
if n, err := sf.Read(buf); n > 0 || err != ErrNotReader {
t.Fatal("Shouldn't be able to read data from a SliceFile")
}

if err := sf.Close(); err != ErrNotReader {
t.Fatal("Shouldn't be able to call `Close` on a SliceFile")
if err := sf.Close(); err != nil {
t.Fatal("Should be able to call `Close` on a SliceFile")
}

_, file, err := sf.NextFile()
Expand Down Expand Up @@ -151,8 +151,8 @@ anotherfile
if n, err := mpf.Read(buf); n > 0 || err != ErrNotReader {
t.Fatal("Shouldn't be able to call `Read` on a directory")
}
if err := mpf.Close(); err != ErrNotReader {
t.Fatal("Shouldn't be able to call `Close` on a directory")
if err := mpf.Close(); err != nil {
t.Fatal("Should be able to call `Close` on a directory")
}

// test properties of file created from third part (nested file)
Expand Down
8 changes: 6 additions & 2 deletions multifilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MultiFileReader struct {
files []File
path []string

currentFile io.Reader
currentFile File
buf bytes.Buffer
mpWriter *multipart.Writer
closed bool
Expand Down Expand Up @@ -121,7 +121,11 @@ func (mfr *MultiFileReader) Read(buf []byte) (written int, err error) {

// otherwise, read from file data
written, err = mfr.currentFile.Read(buf)
if err == io.EOF {
if err == io.EOF || err == ErrNotReader {
if err := mfr.currentFile.Close(); err != nil {
return written, err
}

mfr.currentFile = nil
return written, nil
}
Expand Down
7 changes: 2 additions & 5 deletions multipartfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,18 @@ func (f *MultipartFile) Read(p []byte) (int, error) {
}

func (f *MultipartFile) Close() error {
if f.IsDirectory() {
return ErrNotReader
}
return f.Part.Close()
}

func (f *MultipartFile) Seek(offset int64, whence int) (int64, error) {
if f.IsDirectory() {
return 0, ErrNotReader
}
return 0, ErrNotReader
return 0, ErrNotSupported
}

func (f *MultipartFile) Size() (int64, error) {
return 0, ErrNotReader
return 0, ErrNotSupported
}

type PartReader interface {
Expand Down
3 changes: 1 addition & 2 deletions readerfile.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package files

import (
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (f *ReaderFile) Stat() os.FileInfo {

func (f *ReaderFile) Size() (int64, error) {
if f.stat == nil {
return 0, errors.New("file size unknown")
return 0, ErrNotSupported
}
return f.stat.Size(), nil
}
Expand Down
2 changes: 1 addition & 1 deletion serialfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (f *serialFile) NextFile() (string, File, error) {
}

func (f *serialFile) Read(p []byte) (int, error) {
return 0, io.EOF
return 0, ErrNotReader
}

func (f *serialFile) Close() error {
Expand Down
4 changes: 2 additions & 2 deletions slicefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func (f *SliceFile) NextFile() (string, File, error) {
}

func (f *SliceFile) Read(p []byte) (int, error) {
return 0, io.EOF
return 0, ErrNotReader
}

func (f *SliceFile) Close() error {
return ErrNotReader
return nil
}

func (f *SliceFile) Seek(offset int64, whence int) (int64, error) {
Expand Down

0 comments on commit cfdeccb

Please sign in to comment.