files

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2020 License: MIT Imports: 20 Imported by: 20

README

go-btfs-files

File interfaces and utils used in BTFS

Contribute

Feel free to join in. All welcome.

License

MIT

Documentation

Index

Constants

View Source
const (
	SmallestString = "\u0000"
)

Variables

View Source
var (
	ErrNotDirectory = errors.New("file isn't a directory")
	ErrNotReader    = errors.New("file isn't a regular file")

	ErrNotSupported = errors.New("operation not supported")
)

Functions

func IsMapDirectory added in v0.1.6

func IsMapDirectory(d Directory) bool

func IsMultiPartDirectory added in v0.1.6

func IsMultiPartDirectory(d Directory) bool

func IsSerialFileDirectory added in v0.1.6

func IsSerialFileDirectory(d Directory) bool

func MultiPartReader added in v0.1.6

func MultiPartReader(d Directory) *multipart.Reader

func Walk

func Walk(nd Node, cb func(fpath string, nd Node) error) error

Walk walks a file tree, like `os.Walk`.

func WriteTo

func WriteTo(nd Node, fpath string) error

WriteTo writes the given node to the local filesystem at fpath.

Types

type DirEntry

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

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

DirEntry exposes information about a directory entry

func FileEntry

func FileEntry(name string, file Node) DirEntry

type DirIterator

type DirIterator interface {
	// DirEntry holds information about current directory entry.
	// Note that after creating new iterator you MUST call Next() at least once
	// before accessing these methods. Calling these methods without prior calls
	// to Next() and after Next() returned false may result in undefined behavior
	DirEntry

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

	// 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

	// BreadthFirstTraversal indicates that the current DirIterator
	// instance will be used to traverse in breadth first walk9ng method
	BreadthFirstTraversal()
}

DirIterator is a iterator over directory entries. See Directory.Entries for more

type Directory

type Directory interface {
	Node

	// Entries returns a stateful iterator over directory entries.
	//
	// Example usage:
	//
	// it := dir.Entries()
	// for it.Next() {
	//   name := it.Name()
	//   file := it.Node()
	//   [...]
	// }
	// if it.Err() != nil {
	//   return err
	// }
	//
	// Note that you can't store the result of it.Node() and use it after
	// advancing the iterator
	Entries() DirIterator

	SetSize(int64) error

	IsReedSolomon() bool
}

Directory is a special file which can link to any number of files.

func DirFromEntry

func DirFromEntry(e DirEntry) Directory

DirFromEntry calls ToDir on Node in the given entry

func NewFileFromPartReader

func NewFileFromPartReader(reader *multipart.Reader, mediatype string) (Directory, error)

NewFileFromPartReader creates a Directory from a multipart reader.

func NewMapDirectory

func NewMapDirectory(f map[string]Node) Directory

func NewSliceDirectory

func NewSliceDirectory(files []DirEntry) Directory

func ToDir

func ToDir(n Node) Directory

ToDir is an alias for n.(Directory). If the file isn't directory, a nil value will be returned

type File

type File interface {
	Node

	io.Reader
	io.Seeker
}

Node represents a regular Unix file

func FileFromEntry

func FileFromEntry(e DirEntry) File

FileFromEntry calls ToFile on Node in the given entry

func NewBytesFile

func NewBytesFile(b []byte) File

func NewLinkFile

func NewLinkFile(target string, stat os.FileInfo) File

func NewReaderFile

func NewReaderFile(reader io.Reader) File

func NewReaderStatFile

func NewReaderStatFile(reader io.Reader, stat os.FileInfo) File

func ToFile

func ToFile(n Node) File

ToFile is an alias for n.(File). If the file isn't a regular file, nil value will be returned

type FileInfo

type FileInfo interface {
	Node

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

	// Stat returns os.Stat of this file, may be nil for some files
	Stat() os.FileInfo
}

FileInfo exposes information on files in local filesystem

type Filter added in v0.2.0

type Filter struct {
	// IncludeHidden - Include hidden files
	IncludeHidden bool
	// Rules - File filter rules
	Rules *ignore.GitIgnore
}

Filter represents a set of rules for determining if a file should be included or excluded. A rule follows the syntax for patterns used in .gitgnore files for specifying untracked files. Examples: foo.txt *.app bar/ **/baz fizz/**

func NewFilter added in v0.2.0

func NewFilter(ignoreFile string, rules []string, includeHidden bool) (*Filter, error)

NewFilter creates a new file filter from a .gitignore file and/or a list of ignore rules. An ignoreFile is a path to a file with .gitignore-style patterns to exclude, one per line rules is an array of strings representing .gitignore-style patterns For reference on ignore rule syntax, see https://git-scm.com/docs/gitignore

func (*Filter) ShouldExclude added in v0.2.0

func (filter *Filter) ShouldExclude(fileInfo os.FileInfo) (result bool)

ShouldExclude takes an os.FileInfo object and applies rules to determine if its target should be excluded.

type MultiFileReader

type MultiFileReader struct {
	io.Reader
	// contains filtered or unexported fields
}

MultiFileReader reads from a `commands.Node` (which can be a directory of files or a regular file) as HTTP multipart encoded data.

func NewMultiFileReader

func NewMultiFileReader(file Directory, form bool) *MultiFileReader

NewMultiFileReader constructs a MultiFileReader. `file` can be any `commands.Directory`. If `form` is set to true, the Content-Disposition will be "form-data". Otherwise, it will be "attachment".

func (*MultiFileReader) Boundary

func (mfr *MultiFileReader) Boundary() string

Boundary returns the boundary string to be used to separate files in the multipart data

func (*MultiFileReader) Read

func (mfr *MultiFileReader) Read(buf []byte) (written int, err error)

type Node

type Node interface {
	io.Closer

	// Size returns size of this file (if this file is a directory, total size of
	// all files stored in the tree should be returned). Some implementations may
	// choose not to implement this
	Size() (int64, error)
}

Node is a common interface for files, directories and other special files

func NewSerialFile

func NewSerialFile(path string, includeHidden bool, stat os.FileInfo) (Node, error)

NewSerialFile takes a filepath, a bool specifying if hidden files should be included, and a fileInfo and returns a Node representing file, directory or special file.

func NewSerialFileWithFilter added in v0.2.0

func NewSerialFileWithFilter(path string, filter *Filter, stat os.FileInfo) (Node, error)

NewSerialFileWith takes a filepath, a filter for determining which files should be operated upon if the filepath is a directory, and a fileInfo and returns a Node representing file, directory or special file.

type ReaderFile

type ReaderFile struct {
	// contains filtered or unexported fields
}

ReaderFile is a implementation of File created from an `io.Reader`. ReaderFiles are never directories, and can be read from and closed.

func NewReaderPathFile

func NewReaderPathFile(path string, reader io.ReadCloser, stat os.FileInfo) (*ReaderFile, error)

func (*ReaderFile) AbsPath

func (f *ReaderFile) AbsPath() string

func (*ReaderFile) Close

func (f *ReaderFile) Close() error

func (*ReaderFile) Read

func (f *ReaderFile) Read(p []byte) (int, error)

func (*ReaderFile) Reader added in v0.1.6

func (f *ReaderFile) Reader() io.Reader

func (*ReaderFile) Seek

func (f *ReaderFile) Seek(offset int64, whence int) (int64, error)

func (*ReaderFile) Size

func (f *ReaderFile) Size() (int64, error)

func (*ReaderFile) Stat

func (f *ReaderFile) Stat() os.FileInfo

type SliceFile

type SliceFile struct {
	// contains filtered or unexported fields
}

SliceFile implements Node, and provides simple directory handling. It contains children files, and is created from a `[]Node`. SliceFiles are always directories, and can't be read from or closed.

func (*SliceFile) Close

func (f *SliceFile) Close() error

func (*SliceFile) Entries

func (f *SliceFile) Entries() DirIterator

func (*SliceFile) IsReedSolomon added in v0.1.7

func (f *SliceFile) IsReedSolomon() bool

func (*SliceFile) Length

func (f *SliceFile) Length() int

func (*SliceFile) SetSize added in v0.1.6

func (f *SliceFile) SetSize(size int64) error

func (*SliceFile) Size

func (f *SliceFile) Size() (int64, error)
type Symlink struct {
	Target string
	// contains filtered or unexported fields
}
func ToSymlink(n Node) *Symlink

func (*Symlink) Close

func (lf *Symlink) Close() error

func (*Symlink) Read

func (lf *Symlink) Read(b []byte) (int, error)

func (*Symlink) Seek

func (lf *Symlink) Seek(offset int64, whence int) (int64, error)

func (*Symlink) Size

func (lf *Symlink) Size() (int64, error)

type TarWriter

type TarWriter struct {
	TarW *tar.Writer
}

func NewTarWriter

func NewTarWriter(w io.Writer) (*TarWriter, error)

NewTarWriter wraps given io.Writer into a new tar writer

func (*TarWriter) Close

func (w *TarWriter) Close() error

Close closes the tar writer.

func (*TarWriter) WriteFile

func (w *TarWriter) WriteFile(nd Node, fpath string) error

WriteNode adds a node to the archive.

type WebFile

type WebFile struct {
	// contains filtered or unexported fields
}

WebFile is an implementation of File which reads it from a Web URL (http). A GET request will be performed against the source when calling Read().

func NewWebFile

func NewWebFile(url *url.URL) *WebFile

NewWebFile creates a WebFile with the given URL, which will be used to perform the GET request on Read().

func (*WebFile) AbsPath

func (wf *WebFile) AbsPath() string

func (*WebFile) Close

func (wf *WebFile) Close() error

Close closes the WebFile (or the request body).

func (*WebFile) Read

func (wf *WebFile) Read(b []byte) (int, error)

Read reads the File from it's web location. On the first call to Read, a GET request will be performed against the WebFile's URL, using Go's default HTTP client. Any further reads will keep reading from the HTTP Request body.

func (*WebFile) Seek

func (wf *WebFile) Seek(offset int64, whence int) (int64, error)

TODO: implement

func (*WebFile) Size

func (wf *WebFile) Size() (int64, error)

func (*WebFile) Stat

func (wf *WebFile) Stat() os.FileInfo

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL