github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/commands/files/file.go (about)

     1  package files
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"os"
     7  )
     8  
     9  var (
    10  	ErrNotDirectory = errors.New("Couln't call NextFile(), this isn't a directory")
    11  	ErrNotReader    = errors.New("This file is a directory, can't use Reader functions")
    12  )
    13  
    14  // File is an interface that provides functionality for handling files/directories
    15  // as values that can be supplied to commands. For directories, child files are
    16  // accessed serially by calling `NextFile()`.
    17  type File interface {
    18  	// Files implement ReadCloser, but can only be read from or closed if they are not directories
    19  	io.ReadCloser
    20  
    21  	// FileName returns a filename path associated with this file
    22  	FileName() string
    23  
    24  	// FullPath returns the full path in the os associated with this file
    25  	FullPath() string
    26  
    27  	// IsDirectory returns true if the File is a directory (and therefore supports calling `NextFile`)
    28  	// and false if the File is a normal file (and therefor supports calling `Read` and `Close`)
    29  	IsDirectory() bool
    30  
    31  	// NextFile returns the next child file available (if the File is a directory).
    32  	// It will return (nil, io.EOF) if no more files are available.
    33  	// If the file is a regular file (not a directory), NextFile will return a non-nil error.
    34  	NextFile() (File, error)
    35  }
    36  
    37  type StatFile interface {
    38  	File
    39  
    40  	Stat() os.FileInfo
    41  }
    42  
    43  type PeekFile interface {
    44  	SizeFile
    45  
    46  	Peek(n int) File
    47  	Length() int
    48  }
    49  
    50  type SizeFile interface {
    51  	File
    52  
    53  	Size() (int64, error)
    54  }