Documentation ¶
Overview ¶
This file contains interfaces, wrappers, and imports from Golang versions prior to v1.16. It also exposes interfaces that can be used on previous versions analogously to those introduced in Go 1.16+. These interfaces are compatible with later versions and may be used anywhere they're required in code targeting Go 1.16 (where appropriate).
Index ¶
- Variables
- func Clean(path string) string
- func DefaultTempFileGenerator(length int) (string, error)
- func DefaultTempFilePatternParser(pattern string, length int, fn RandomNameFunc) string
- func Glob(pattern string, fs FS) (matches []string, err error)
- func HTTPDir(fs FS) *httpdir
- func IsNotDir(err error) bool
- func IsNotExists(err error) bool
- func NewLayeredFS() *layeredFS
- func NewVFS(basepath string) (*persistentFS, error)
- func NewZipFS(path string) (*zipfs, error)
- func NewZipFromVFS(path string, vfs FS) (*zipfs, error)
- func NumDirs(fs FS, path string) (i int, err error)
- func ReadFile(fs FS, path string) (buf []byte, err error)
- func RenameFile(fs FS, file File, to string) error
- func SafeClean(path string) string
- func Walk(efs VFS, path string, fn WalkDirFunc) error
- func WrapFS(fs FS) *fsWrapper
- func WriteFile(efs VFS, path string, data []byte, perm os.FileMode) error
- type DirEntry
- type FS
- type FSFileInfo
- type File
- type FileInfo
- type FileMode
- type GoFS
- type GoFile
- type LayeredFS
- type MutableFS
- type MutableFile
- type NotifierFS
- type NullableFS
- type PersistableFileSystem
- type RandomNameFunc
- type Raw
- type ReadOnlyFile
- type ReadSeeker
- type Reader
- type ReleasingFS
- type SortableDirEntry
- type SubFS
- type VFS
- type VFSFile
- type WalkDirFunc
Constants ¶
This section is empty.
Variables ¶
var ErrCreate = errors.NewError("error creating pathspec")
ErrCreate is returned whenever the underlying VFS was unable to create the object requested per the pathspec. This may be a file or directory depending on the function that returned it.
Not all VFS layers are capable of returning this error.
var ErrCwd = errors.NewError("error reading current working directory")
ErrCwd is returned whenever the current working directory cannot be read.
var ErrFileExceedsMaxRead = errors.NewError("maximum read exceeded")
var ErrInvalidPath = errors.NewError("not a valid path")
ErrInvalidPath is returned whenever the requested path is not valid as per the context of the current VFS.
The specific meaning of this error depends on the underlying VFS that returned it.
var ErrModify = errors.NewError("error modifying path")
ErrModify is returned if an attempt to modify an attribute of the specified path has failed.
var ErrNoInfo = errors.NewError("no FileInfo available")
ErrNoInfo should be returned by Stat() methods for which no FileInfo can be obtained for the underlying type.
Care should be taken to return this *only* if the underlying type cannot be persuaded to yield a FileInfo-compatible interface, such as if the entry does actually exist, but for whatever reason no further information may be divined.
In most cases, ErrNotExists should be returned instead.
var ErrNotADirectory = errors.NewError("not a directory")
ErrNotADirectory is returned when a directory is expected but a different file system object was received instead.
var ErrNotExists = errors.NewError("path does not exist")
ErrNotExists is returned when an attempt is made to open or stat a file or directory that does not exist.
var ErrOpeningFile = errors.NewError("error opening file")
ErrOpeningFile is returned whenever an attempt to open a file, either for reading or writing, has failed.
var ErrRandom = errors.NewError("error in underlying random number implementation")
ErrRandom may be returned by the random file name generator if the OS random entropy source has failed.
var ErrReadOnly = errors.NewError("read only file system")
ErrReadOnly is returned by file system manipulation methods that were called on a VFS instance flagged as read only.
var ErrReadingPath = errors.NewError("error reading path")
ErrReadingPath is returned if the underlying file system primitives are unable to read the path. What this means and what triggers this condition is determined by the VFS implementation. e.g., VFSZip returning this error may indicate an unreadable .zip file.
var ErrRemove = errors.NewError("error removing pathspec")
ErrRemove is returned whenever the underlying VFS was unable to remove the object as requested per the pathspec. This may be a file or directory depending on the nature of the object and a Stat() would be required to determine the type of the object.
var ErrRename = errors.NewError("error renaming pathspec")
ErrRename is returned whenever the underlying VFS was unable to rename the object as requested per its pathspec. The nature of the target depends on its underlying implementation, but generally the error can be drilled down for further information as to the cause.
var ErrSeekNotSupported = errors.NewError("seek is not supported for this type")
ErrSeekNotSupported is returned if the current VFS or file type does not support seeking.
var ErrWriteFailed = errors.NewError("write failed")
ErrWriteFailed is returned by helper functions or other methods that write to a file handle if either the write itself failed or the write was incomplete.
var MaxPlaceholderReplacements = 10
var MaxReadFileSize = 1024 * 1024 * 8
MaxReadFilesSize of 8 megabytes. This may be changed by client code.
Functions ¶
func Clean ¶
Clean operates similarly to filepath.Clean with the exception that it also trims leading "../" components.
func DefaultTempFileGenerator ¶
DefaultTempFileGenerator is the default generator for creating randomize file name components.
func DefaultTempFilePatternParser ¶
func DefaultTempFilePatternParser(pattern string, length int, fn RandomNameFunc) string
DefaultTempFilePatternParser is the default pattern parser for TempFile.
This parser replaces all instances of "*" with a `length` character random string using `fn` as the random character generator. Up to MaxPlaceholderReplacements will be performed.
func Glob ¶
Glob match the specified pattern against the contents of fs.
TODO: Consider implementing recursive globbing via "**" operators, e.g.: `**/*.txt` would match all .txt files in the current directory and below.
func HTTPDir ¶
func HTTPDir(fs FS) *httpdir
HTTPDir returns a VFS wrapper usable with net/http.FileServer.
Note: This implementation does NOT implement the ExtendedFS interface as it is intended strictly to support loading content from a VFS.
func IsNotDir ¶
IsNotDir returns true if the error matches ErrNotExists or ErrNotADirectory. If the error is nil or of any other value this will return false.
func IsNotExists ¶
IsNotExists returns true if the error indicates the path does not exist. For VFS errors, these will match ErrNotExists.
This function wraps os.IsNotExists to ensure compatibility.
The choice of "exists" rather than "exist" is deliberate.
func NewLayeredFS ¶
func NewLayeredFS() *layeredFS
func NewVFS ¶
NewVFS returns a new file system-based VFS instance.
If basepath does not exist it will be created with the FileMode 0755. To change this, call Chmod(".", mode).
func NewZipFromVFS ¶
func ReadFile ¶
ReadFile is a helper function that reads `path`'s content into a byte slice, returning the slice or an error, depending on which happens first.
ReadFile is analogous to its counterparts in the standard library with the exception that it accepts a FS as its first argument which is also a file system from which it will attempt to read. Unlike WriteFile, this accepts anything that implements the base FS interface, which is compatible with some parts of the Go standard library. This allows us to consume data from other sources including those that are outside VFS.
ReadFile returns primarily the ErrReadingPath error regardless of outcome. However, this error may wrap other errors (such as permission errors), and it may include additional metadata such as the number of bytes read ("read") versus the number of bytes that were expected ("expected"). These values can be obtained from the go/errors type method Get().
func RenameFile ¶
RenameFile renames the specified file to the target path using the specified VFS.
This differs from calling vfs.Rename in that the `file` entity is updated with its target name changed.
If `file` implements VFSFile, `file.Rename()` will be invoked instead. Otherwise, this will return a new VFSFile instance.
func SafeClean ¶
SafeClean is like clean except that it also trims leading "/" components.
This is mostly useful for sanitizing paths that will be ultimately interact with a physical file system and cannot escape from a given root directory.
func Walk ¶
func Walk(efs VFS, path string, fn WalkDirFunc) error
Walks the path specified calling `fn` for each iteration using the VFS `efs`.
This is ported from the Go 1.16 standard library and works identically to filepath.WalkDir. See CREDITS.md.
func WrapFS ¶
func WrapFS(fs FS) *fsWrapper
WrapFS wraps any type implementing the full FS wrapper and returns a simulated VFS instance with most methods performing analogously to their intended purpose. In circumstances where this isn't possible, a sensible error type will be returned instead.
func WriteFile ¶
WriteFile is a helper that writes the content `data` to the file located at `path`. If the file does not exist, it will be created.
WriteFile will return either ErrOpeningFile if the file cannot be opened (permissions, read only file system, etc) or ErrWriteFailed if the write could not be performed. Both error types can be unfurled and inspected for further information.
ErrOpeningFile may contain a further ErrReadOnly if an effort was made to write to a read-only file system.
If a write is incomplete, ErrWriteFailed will be returned with the attributes "written" and "expected" configured with the number of bytes written to the file system versus the number expected to be written, respectively.
Types ¶
type DirEntry ¶
func MakeDirEntry ¶
type FS ¶
type FS interface { // Open the file specified by name returning a file handle or error. // // Implements Go's io/fs.FS type. Open(name string) (ReadOnlyFile, error) // Glob returns a list of matching file names contained by the current FS // implementation. // // Implements Go's io/fs.GlobFS type. Glob(pattern string) (matches []string, err error) // ReadDir reads a path name and returns a collection of DirEntry(ies) // constrained by the specified path. // // Implements Go's io/fs.ReadDirFS type. ReadDir(name string) ([]DirEntry, error) // Stat returns a FileInfo instance for the specified path (or file.). // // Implements Go's io/fs.StatFS type. Stat(name string) (FileInfo, error) // Lstat returns a FileInfo instance for the specified path. This does not // follow links. If a symbolic link is requested via this function, FileInfo // will contain only information related to the symbolic link and not the // link target. Lstat(name string) (FileInfo, error) }
type FSFileInfo ¶
type FSFileInfo interface { os.FileInfo // FS returns the underlying file system instance type, if it was // set. FS() FS // MutableFS returns the underlying file system instance type cast // as an MutableFS interface. If casting is not possible or the // file system instance is not set, nil will be returned instead. MutableFS() MutableFS // VFS returns the underlying VFS implementation, if possible, or nil if it // cannot be converted. VFS() VFS }
type File ¶
type File interface { MutableFile ReadOnlyFile }
func NoopMutable ¶
func NoopMutable(f ReadOnlyFile, seterr bool) File
NoopMutable returns a File with all mutable functions (like Write) set to return an ErrReadOnly if `seterr` is true. If `seterr` is false, these methods will fail silently.
func TempFile ¶
TempFile generates a new temporary file in the specified directory and returns a File type capable of reading and writing. This is similar to the (now-deprecated) ioutil/TempFile function except that the specified directory *cannot* be the empty string (go/vfs has no concept currently of a system temporary directory, and even if it did, it would be unable to use it).
As with ioutil.TempFile, if pattern contains an asterisk ("*"), it will be replaced with the generated random pattern. Unlike ioutil.TempFile, any asterisk will be replaced with a generated random pattern, with each pattern unique to each asterisk.
efs must be a VFS implementation that can be converted to a MutableFS type, otherwise an error will be returned.
Random patterns will only contain alphanumeric patterns. Future implementation may allow alteration of the randomizer function and random character pools.
type MutableFS ¶
type MutableFS interface { VFS // Chmod changes the permissions for the object specified by path to mode. // Not all MutableFS implementations may support all FileMode values. Chmod(path string, mode FileMode) error // Mkdir creates a new directory within the confines of the current VFS // layer. For VFS-FS this will always be constrained to the base path even // if the path attempts to create itself under the root ("/") directory. // // This will fail for read only file systems, like .zip files, or for any // other VFS layer that was flagged as read only via SetReadOnly(true). // // For VFS collection types, this call will cascade until it succeeds or // ultimately fails. This allows for creating mutable file systems that can // be modified by a client while retaining the ability to merge a stacked // VFS collection with pre-existing images. // // See os.Mkdir. Mkdir(string, FileMode) error // MkdirAll creates a new directory and all of its intermediate path // components, as required, within the current VFS layer. For VFS-FS this // will always be constrained to the base path even if the path specified // attempts to create itself under the root ("/") directory. // // this will fail for read only file systems, like .zip files, or for any // other VFS layer that was flagged as read only via SetReadOnly(true). // // As with Mkdir, for VFS collection types, this call will cascade until it // either ultimately succeeds or fails. // // See os.MkdirAll. MkdirAll(string, FileMode) error // MkdirTemp creates a new temporary directory, returning the generated name // or any error state. This will be constrained to the current VFS layer. // // See Mkdir, MkdirAll, and os.MkdirTemp. MkdirTemp(string, string) (string, error) // OpenFile opens or creates a new File with the specified flags and file // mode. // // The semantics of what exactly "flags" and "mode" imply depend on the // underlying VFS. For the persistent FS, these are passed through to the // native os.OpenFile call. For read only VFS layers like VFSZip, this will // return a read only File handle as if os.O_RDONLY were set. OpenFile(path string, flags int, mode FileMode) (File, error) // Remove the file or directory. See os.Remove. // // The behavior of this function depends on the underlying VFS primitives. // For the persistent FS, this may return an *os.PathError when used on a // read only file system. Likewise, this will return an error if called on a // ZIP file or VFSCollection. Remove(string) error // RemoveAll path components. See os.RemoveAll. // // The behavior of this function depends on the underlying VFS primitives. // For the persistent FS, this may return an *os.PathError when used on a // read only file system. Likewise, this will return an error if called on a // ZIP file or VFSCollection. RemoveAll(string) error // Rename a given path from its current name to a new name. // // If `to` includes a new pathspec and is a file, if the target directory // exists, it will be moved. // // Returns an error for VFS types that are configured as ReadOnly(). Rename(from, to string) error }
func WrapAsMutable ¶
WrapAsMutable accepts a VFS argument and returns it as a MutableFS with all methods configured to return an ErrReadOnly.
type NotifierFS ¶
type NotifierFS interface { ReleasingFS Watch(func()) error }
NotifierFS exposes implementations that may utilize certain primitives to notify listeners of file system changes. At present, only the persistent FS implements this via fsnotify.
NotifierFS types must also implement ReleasingFS, which provides a mechanism for releasing the resources associated with the underlying notification primitives.
type NullableFS ¶
type NullableFS interface {
IsEmpty() bool
}
NullableFS should be implemented for those types that could potentially be returned in a null state wherein such circumstances may be disguised by the interface. Usually implementing this with
func (t *Type) IsEmpty() bool { return t == nil }
is considered sufficient.
Because interface types cannot be compared with nil without knowing the underlying type, this is the best option to protect against potential misuse.
type PersistableFileSystem ¶
type PersistableFileSystem interface {
Path() string
}
type RandomNameFunc ¶
type Raw ¶
Raw is a File-associated interface that unveils the raw *os.File underlying the given type. Not all returned Files will support this, depending on their implementation.
Presently supported only by file system-based VFS instances.
type ReadOnlyFile ¶
func NoopCloser ¶
func NoopCloser(f ReadSeeker, info FileInfo) ReadOnlyFile
NoopCloser returns a ReadOnlyFile with a Close method attached that does nothing.
This function will usually be used for readers returned by archives or other implementations where the byte stream generated doesn't supply a Close type but one is needed to fulfill other interfaces in this package.
type ReadSeeker ¶
type ReleasingFS ¶
type ReleasingFS interface { // Release attempts to remove, release, or nil out any references held by // this VFS instance. Not all VFS layers may support Release(). If they do, // calling this can provide hints to the Golang GC that the file system // instance may be collected when no longer in use. // // Not guaranteed to release or close any or all open file handles. Release() error }
ReleasingFS implementations expose a Release() method that closes, releases, or otherwise sets to nil any references that may be held open indefinitely even when the file system instance is no longer in use, thus preventing the GC from releasing resources associated with the file system object.
It is up to the caller to decide when it is appropriate to call Release().
type SortableDirEntry ¶
type SortableDirEntry []DirEntry
func (SortableDirEntry) Len ¶
func (s SortableDirEntry) Len() int
func (SortableDirEntry) Less ¶
func (s SortableDirEntry) Less(i, j int) bool
func (SortableDirEntry) Swap ¶
func (s SortableDirEntry) Swap(i, j int)
type VFS ¶
type VFS interface { FS // Abs returns the absolute path of the given component within the virtual // file system. If using the VFS-FS layer, this will only return the // absolute path if it is part of the configured base path when locked or an // error if it is not. When not using a locked VFS-FS layer, this will // always return the absolute path if it exists. Abs(string) (string, error) // Chdir changes the current in-memory directory of the virtual file system. // The semantics of this call limit file system views to the VFS' configured // root. Ergo, for VFS-FS, Chdir() cannot be used to view file system // entries above the configured base. Chdir("/") in all implementations will // reset the current working directory to the root of the VFS. Chdir(string) error // Chroot binds the VFS instance to the specified root directory. // // Implementations should, at minmum, Clone() or optionally replicate their // current instance when invoking this call. // // Implementations may call Clone() and Chdir() internally to accomplish the // same as above. Chroot(root string) (VFS, error) // SetRoot sets the immutable root path of the VFS layer to the string root, // if the path exists as a subpath of the current VFS and resolves to a // directory and is readable. // // SetRoot behave similarly to Chroot with the exception that it acts on the // current VFS instance. SetRoot(root string) error // Clone the file system. Clone() VFS // Cwd returns the current working directory for the VFS layer. // // This cannot return an error, because 1) VFS implementations should not // Chdir unless the target path is readable, and 2) VFS implementations // should fail if they are bound to a path that is not readable. Cwd() string // MaskedAbs returns a masked absolute path, if it exists within the // configured VFS layer. For VFS-FS, the base path will be masked; for // VFS-zip, the zip file name will be masked; and for VFS-HTTP, the URL will // be masked. Masked strings will be replaced with "vfs://". MaskedAbs(string) (string, error) // Mutable returns a MutableFS instance of the file system or ErrReadOnly if // the file system is read-only. // // Errors returned from this function are independent of whether or not the // underlying file system actually supports mutability; for example, if a // mutable file system implements the MutableFS interface, this will return // an ErrReadOnly if the ReadOnly() method returns true. Mutable() MutableFS // ReadOnly returns the read only state of the current VFS layer. As of this // writing, only the persistent FS may return false. ReadOnly() bool // SetReadOnly state for the current VFS. SetReadOnly(bool) }