Documentation ¶
Overview ¶
Package fs defines basic interfaces to a file system. A file system can be provided by the host operating system but also by other packages.
Index ¶
- Variables
- func Glob(fsys FS, pattern string) (matches []string, err error)
- func ReadFile(fsys FS, name string) ([]byte, error)
- func ValidPath(name string) bool
- func WalkDir(fsys FS, root string, fn WalkDirFunc) error
- type DirEntry
- type FS
- type File
- type FileInfo
- type FileMode
- type GlobFS
- type PathError
- type ReadDirFS
- type ReadDirFile
- type ReadFileFS
- type StatFS
- type WalkDirFunc
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalid = errInvalid() // "invalid argument" ErrPermission = errPermission() // "permission denied" ErrExist = errExist() // "file already exists" ErrNotExist = errNotExist() // "file does not exist" ErrClosed = errClosed() // "file already closed" )
Generic file system errors. Errors returned by file systems can be tested against these errors using errors.Is.
var SkipDir = errors.New("skip this directory")
SkipDir is used as a return value from WalkDirFuncs to indicate that the directory named in the call is to be skipped. It is not returned as an error by any function.
Functions ¶
func Glob ¶
Glob returns the names of all files matching pattern or nil if there is no matching file. The syntax of patterns is the same as in path.Match. The pattern may describe hierarchical names such as /usr/*/bin/ed (assuming the Separator is '/').
Glob ignores file system errors such as I/O errors reading directories. The only possible returned error is path.ErrBadPattern, reporting that the pattern is malformed.
If fs implements GlobFS, Glob calls fs.Glob. Otherwise, Glob uses ReadDir to traverse the directory tree and look for matches for the pattern.
func ReadFile ¶
ReadFile reads the named file from the file system fs and returns its contents. A successful call returns a nil error, not io.EOF. (Because ReadFile reads the whole file, the expected EOF from the final Read is not treated as an error to be reported.)
If fs implements ReadFileFS, ReadFile calls fs.ReadFile. Otherwise ReadFile calls fs.Open and uses Read and Close on the returned file.
func ValidPath ¶
ValidPath reports whether the given path name is valid for use in a call to Open. Path names passed to open are unrooted, slash-separated sequences of path elements, like “x/y/z”. Path names must not contain a “.” or “..” or empty element, except for the special case that the root directory is named “.”.
Paths are slash-separated on all systems, even Windows. Backslashes must not appear in path names.
func WalkDir ¶
func WalkDir(fsys FS, root string, fn WalkDirFunc) error
WalkDir walks the file tree rooted at root, calling fn for each file or directory in the tree, including root.
All errors that arise visiting files and directories are filtered by fn: see the fs.WalkDirFunc documentation for details.
The files are walked in lexical order, which makes the output deterministic but requires WalkDir to read an entire directory into memory before proceeding to walk that directory.
WalkDir does not follow symbolic links found in directories, but if root itself is a symbolic link, its target will be walked.
Types ¶
type DirEntry ¶
type DirEntry interface { // Name returns the name of the file (or subdirectory) described by the entry. // This name is only the final element of the path (the base name), not the entire path. // For example, Name would return "hello.go" not "/home/gopher/hello.go". Name() string // IsDir reports whether the entry describes a directory. IsDir() bool // Type returns the type bits for the entry. // The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method. Type() FileMode // Info returns the FileInfo for the file or subdirectory described by the entry. // The returned FileInfo may be from the time of the original directory read // or from the time of the call to Info. If the file has been removed or renamed // since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist). // If the entry denotes a symbolic link, Info reports the information about the link itself, // not the link's target. Info() (FileInfo, error) }
A DirEntry is an entry read from a directory (using the ReadDir function or a ReadDirFile's ReadDir method).
type FS ¶
type FS interface { // Open opens the named file. // // When Open returns an error, it should be of type *PathError // with the Op field set to "open", the Path field set to name, // and the Err field describing the problem. // // Open should reject attempts to open names that do not satisfy // ValidPath(name), returning a *PathError with Err set to // ErrInvalid or ErrNotExist. Open(name string) (File, error) }
An FS provides access to a hierarchical file system.
The FS interface is the minimum implementation required of the file system. A file system may implement additional interfaces, such as fsutil.ReadFileFS, to provide additional or optimized functionality. See io/fsutil for details.
type File ¶
A File provides access to a single file. The File interface is the minimum implementation required of the file. A file may implement additional interfaces, such as ReadDirFile, ReaderAt, or Seeker, to provide additional or optimized functionality.
type FileInfo ¶
type FileInfo interface { Name() string // base name of the file Size() int64 // length in bytes for regular files; system-dependent for others Mode() FileMode // file mode bits ModTime() time.Time // modification time IsDir() bool // abbreviation for Mode().IsDir() Sys() interface{} // underlying data source (can return nil) }
A FileInfo describes a file and is returned by Stat.
type FileMode ¶
type FileMode uint32
A FileMode represents a file's mode and permission bits. The bits have the same definition on all systems, so that information about files can be moved from one system to another portably. Not all bits apply to all systems. The only required bit is ModeDir for directories.
const ( // The single letters are the abbreviations // used by the String method's formatting. ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory ModeAppend // a: append-only ModeExclusive // l: exclusive use ModeTemporary // T: temporary file; Plan 9 only ModeSymlink // L: symbolic link ModeDevice // D: device file ModeNamedPipe // p: named pipe (FIFO) ModeSocket // S: Unix domain socket ModeSetuid // u: setuid ModeSetgid // g: setgid ModeCharDevice // c: Unix character device, when ModeDevice is set ModeSticky // t: sticky ModeIrregular // ?: non-regular file; nothing else is known about this file // Mask for the type bits. For regular files, none will be set. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular ModePerm FileMode = 0777 // Unix permission bits )
The defined file mode bits are the most significant bits of the FileMode. The nine least-significant bits are the standard Unix rwxrwxrwx permissions. The values of these bits should be considered part of the public API and may be used in wire protocols or disk representations: they must not be changed, although new bits might be added.
func (FileMode) IsDir ¶
IsDir reports whether m describes a directory. That is, it tests for the ModeDir bit being set in m.
func (FileMode) IsRegular ¶
IsRegular reports whether m describes a regular file. That is, it tests that no mode type bits are set.
type GlobFS ¶
type GlobFS interface { FS // Glob returns the names of all files matching pattern, // providing an implementation of the top-level // Glob function. Glob(pattern string) ([]string, error) }
A GlobFS is a file system with a Glob method.
type PathError ¶
PathError records an error and the operation and file path that caused it.
type ReadDirFS ¶
type ReadDirFS interface { FS // ReadDir reads the named directory // and returns a list of directory entries sorted by filename. ReadDir(name string) ([]DirEntry, error) }
ReadDirFS is the interface implemented by a file system that provides an optimized implementation of ReadDir.
type ReadDirFile ¶
type ReadDirFile interface { File // ReadDir reads the contents of the directory and returns // a slice of up to n DirEntry values in directory order. // Subsequent calls on the same file will yield further DirEntry values. // // If n > 0, ReadDir returns at most n DirEntry structures. // In this case, if ReadDir returns an empty slice, it will return // a non-nil error explaining why. // At the end of a directory, the error is io.EOF. // // If n <= 0, ReadDir returns all the DirEntry values from the directory // in a single slice. In this case, if ReadDir succeeds (reads all the way // to the end of the directory), it returns the slice and a nil error. // If it encounters an error before the end of the directory, // ReadDir returns the DirEntry list read until that point and a non-nil error. ReadDir(n int) ([]DirEntry, error) }
A ReadDirFile is a directory file whose entries can be read with the ReadDir method. Every directory file should implement this interface. (It is permissible for any file to implement this interface, but if so ReadDir should return an error for non-directories.)
type ReadFileFS ¶
type ReadFileFS interface { FS // ReadFile reads the named file and returns its contents. // A successful call returns a nil error, not io.EOF. // (Because ReadFile reads the whole file, the expected EOF // from the final Read is not treated as an error to be reported.) ReadFile(name string) ([]byte, error) }
ReadFileFS is the interface implemented by a file system that provides an optimized implementation of ReadFile.
type StatFS ¶
type StatFS interface { FS // Stat returns a FileInfo describing the file. // If there is an error, it should be of type *PathError. Stat(name string) (FileInfo, error) }
A StatFS is a file system with a Stat method.
type WalkDirFunc ¶
WalkDirFunc is the type of the function called by WalkDir to visit each each file or directory.
The path argument contains the argument to Walk as a prefix. That is, if Walk is called with root argument "dir" and finds a file named "a" in that directory, the walk function will be called with argument "dir/a".
The directory and file are joined with Join, which may clean the directory name: if Walk is called with the root argument "x/../dir" and finds a file named "a" in that directory, the walk function will be called with argument "dir/a", not "x/../dir/a".
The d argument is the fs.DirEntry for the named path.
The error result returned by the function controls how WalkDir continues. If the function returns the special value SkipDir, WalkDir skips the current directory (path if d.IsDir() is true, otherwise path's parent directory). Otherwise, if the function returns a non-nil error, WalkDir stops entirely and returns that error.
The err argument reports an error related to path, signaling that WalkDir will not walk into that directory. The function can decide how to handle that error; as described earlier, returning the error will cause WalkDir to stop walking the entire tree.
WalkDir calls the function with a non-nil err argument in two cases.
First, if the initial os.Lstat on the root directory fails, WalkDir calls the function with path set to root, d set to nil, and err set to the error from os.Lstat.
Second, if a directory's ReadDir method fails, WalkDir calls the function with path set to the directory's path, d set to an fs.DirEntry describing the directory, and err set to the error from ReadDir. In this second case, the function is called twice with the path of the directory: the first call is before the directory read is attempted and has err set to nil, giving the function a chance to return SkipDir and avoid the ReadDir entirely. The second call is after a failed ReadDir and reports the error from ReadDir. (If ReadDir succeeds, there is no second call.)
The differences between WalkDirFunc compared to filepath.WalkFunc are:
- The second argument has type fs.DirEntry instead of fs.FileInfo.
- The function is called before reading a directory, to allow SkipDir to bypass the directory read entirely.
- If a directory read fails, the function is called a second time for that directory to report the error.