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.
See the testing/fstest package for support with testing implementations of file systems.
Index ¶
- Variables
- func FormatDirEntry(dir DirEntry) string
- func FormatFileInfo(info FileInfo) string
- 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 SubFS
- type WalkDirFunc
Examples ¶
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 SkipAll = errors.New("skip everything and stop the walk")
SkipAll is used as a return value from WalkDirFunc to indicate that all remaining files and directories are to be skipped. It is not returned as an error by any function.
var SkipDir = errors.New("skip this directory")
SkipDir is used as a return value from WalkDirFunc 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 FormatDirEntry ¶ added in go1.21.0
FormatDirEntry returns a formatted version of dir for human readability. Implementations of DirEntry can call this from a String method. The outputs for a directory named subdir and a file named hello.go are:
d subdir/ - hello.go
func FormatFileInfo ¶ added in go1.21.0
FormatFileInfo returns a formatted version of info for human readability. Implementations of FileInfo can call this from a String method. The output for a file named "hello.go", 100 bytes, mode 0o644, created January 1, 1970 at noon is
-rw-r--r-- 100 1970-01-01 12:00:00 hello.go
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.
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 UTF-8-encoded, unrooted, slash-separated sequences of path elements, like “x/y/z”. Path names must not contain an element that is “.” or “..” or the empty string, except for the special case that the root directory is named “.”. Paths must not start or end with a slash: “/x” and “x/” are invalid.
Note that paths are slash-separated on all systems, even Windows. Paths containing other characters such as backslash and colon are accepted as valid, but those characters must never be interpreted by an FS implementation as path element separators.