Documentation
¶
Overview ¶
Package vfs is the interface through which hail reaches files (D23).
File systems ¶
A file system of hail is an fs.FS of the standard package io/fs. To read, hail uses io/fs as it is: fs.Stat, fs.ReadDir, fs.ReadFile, fs.ReadLink, fs.Lstat and fs.Sub. So any code that takes an fs.FS can read the files of hail.
io/fs has no interface to write, create, remove or rename. This package adds small interfaces for them, in the style of io/fs: OpenFileFS, MkdirFS, RemoveFS, RenameFS, SymlinkFS and AccessFS. A file system gives each one or not. A function of the same name, such as OpenFile or Mkdir, finds the interface with a type assertion, as fs.ReadDir finds fs.ReadDirFS. If the file system does not give it, the function fails with an *fs.PathError that wraps errors.ErrUnsupported. OpenFile can still open a file to read, and Access can still check the permission bits.
A File is an fs.File that also takes writes. A backend can give more methods on its files, such as Seek, ReadAt and Fd.
The mode that OpenFile and Mkdir take is the final mode. The umask is a field of the shell state (D7), and the interpreter applies it before it calls this package. No code in this package changes the umask of the process.
Paths and names ¶
The shell keeps each path as a string of the operating system: its working directory, $PWD, and the arguments of a command. An external command gets these strings as they are. This package turns a path into a file system and a name only when hail itself works on a file.
On Windows, the shell keeps a path in the form of Git Bash, as in /c/x, and reads C:\x and C:/x too (D36). OS turns the form of the shell into the path of Windows, and MSYS holds the rules. The interpreter converts the paths that a program of Windows gets.
A name of io/fs uses "/", and has no leading "/", no drive and no element "." or "..". The name of the root is ".". A path of the shell becomes a name in three steps:
- Abs makes the path absolute with the working directory of the shell. It does not clean the path.
- A Namespace takes the volume of the absolute path, such as C: or \\server\share on Windows, and selects the file system of that volume. On other platforms the volume is empty and one file system holds every path.
- Clean turns the rest of the path into a name. It resolves ".." as the kernel does, after each symbolic link is followed. A path without ".." costs no system call.
Clean drops a separator at the end of a path. A command that needs the path to name a directory checks that itself.
Backends ¶
OS returns the namespace of the files of the operating system. Its files are in the files os.go, os_access_*.go and os_mkfifo_*.go, which are the only files of hail that call the file functions of the os package. NewMem returns a file system in memory for tests. A later change adds a backend on the storage of the browser (W19).
Index ¶
- Constants
- Variables
- func Abs(cwd, p string) string
- func Access(fsys fs.FS, name string, mode AccessMode) error
- func Clean(fsys fs.FS, elems []string) (string, error)
- func HasDrive(p string) bool
- func IsAbs(p string) bool
- func Mkdir(fsys fs.FS, name string, perm fs.FileMode) error
- func Mkfifo(fsys fs.FS, name string, perm fs.FileMode) error
- func Remove(fsys fs.FS, name string) error
- func Rename(fsys fs.FS, oldname, newname string) error
- func ShellDrive(p string) byte
- func Symlink(fsys fs.FS, oldname, newname string) error
- func UserTemp(getenv func(string) string) string
- func ValidPath(name string) bool
- type AccessFS
- type AccessMode
- type File
- type MSYS
- type Mem
- func (m *Mem) Access(name string, mode AccessMode) error
- func (m *Mem) Lstat(name string) (fs.FileInfo, error)
- func (m *Mem) Mkdir(name string, perm fs.FileMode) error
- func (m *Mem) Open(name string) (fs.File, error)
- func (m *Mem) OpenFile(name string, flag int, perm fs.FileMode) (File, error)
- func (m *Mem) ReadDir(name string) ([]fs.DirEntry, error)
- func (m *Mem) ReadFile(name string) ([]byte, error)
- func (m *Mem) ReadLink(name string) (string, error)
- func (m *Mem) Remove(name string) error
- func (m *Mem) Rename(oldname, newname string) error
- func (m *Mem) Resolve(p string) (fs.FS, string, error)
- func (m *Mem) Stat(name string) (fs.FileInfo, error)
- func (m *Mem) Symlink(oldname, newname string) error
- type MkdirFS
- type MkfifoFS
- type Mount
- type Namespace
- type OSNamespace
- type OpenFileFS
- type RemoveFS
- type RenameFS
- type SymlinkFS
Constants ¶
const ( // O_RDONLY opens the file to read only. O_RDONLY = os.O_RDONLY // O_WRONLY opens the file to write only. O_WRONLY = os.O_WRONLY // O_RDWR opens the file to read and to write. O_RDWR = os.O_RDWR // O_APPEND makes each write go to the end of the file. O_APPEND = os.O_APPEND // O_CREATE creates the file if it does not exist. O_CREATE = os.O_CREATE // O_EXCL, with O_CREATE, fails if the file exists. O_EXCL = os.O_EXCL // O_SYNC makes each write wait for the storage. O_SYNC = os.O_SYNC // O_TRUNC sets the size of a regular file to zero when it opens. O_TRUNC = os.O_TRUNC )
The flags of OpenFile. Each has the value of the flag of the same name in the os package, so that the os backend passes a flag on as it is. Exactly one of O_RDONLY, O_WRONLY and O_RDWR gives the access mode. The others can be added with the operator |.
const MaxLinks = 40
MaxLinks is the number of symbolic links that one resolution follows before it fails with syscall.ELOOP. Linux uses the same limit.
Variables ¶
var ErrNotAbs = errors.New("path is not absolute")
ErrNotAbs is the error of a Namespace for a path that is not absolute.
var InstallRoot = sync.OnceValue(executableDir)
InstallRoot returns the folder of Windows that the root "/" of the shell names on Windows (D36). A path of the shell with no drive and no mount, such as /x/y, names a path under this folder.
Where the root is waits for open question 4 of docs/PLAN.md. Until a maintainer answers it, the root is the folder that holds the executable of hail. The answer changes this function only.
Functions ¶
func Abs ¶
Abs returns the absolute form of the path p, for the working directory cwd. cwd is an absolute path.
If p is absolute, Abs returns it as it is. Otherwise Abs returns cwd, a separator and p. Abs does not clean the result. An element ".." keeps the meaning that the kernel gives it, which is the parent of the directory after each symbolic link is followed. Clean gives it that meaning. The empty path stays empty, because the kernel does not find a file for it.
On Windows, cwd can be a path of the shell, as in /c/x, whose volume is /c (D36). A path that starts with "\" and has no volume, as in \x, takes the volume of cwd. A path with a drive and no separator, as in C:x, is relative to the working directory of that drive. hail keeps one working directory, and not one for each drive. So if C: is the drive of cwd, C:x is relative to cwd, and otherwise C:x becomes C:\x.
func Access ¶
func Access(fsys fs.FS, name string, mode AccessMode) error
Access checks that the user of hail has each permission in mode on the named file in fsys, as AccessFS.Access does.
If fsys is not an AccessFS, Access reads the mode of the file with fs.Stat and checks the permission bits of the owner. It does not know who the owner is.
func Clean ¶
Clean returns the name in fsys of the path made of the elements elems, which start at the root of fsys. The result is valid for ValidPath.
Clean drops each empty element and each element ".". It appends each other element as it is, with no system call. An element ".." removes the last element so far, but first Clean makes the elements so far physical. It walks them from the root with fs.Lstat. For each symbolic link, it reads the link with fs.ReadLink and puts the text of the link in its place. If the text is absolute, the walk starts again at the root of fsys. If the text is relative, it is relative to the parent of the link. ".." at the root stays at the root.
A name of io/fs cannot hold "..", so the shell cannot leave ".." for the file system to resolve. The kernel resolves ".." after it follows each symbolic link, and bash sees files as the kernel does. Clean gives the same result. The logical paths of cd -L are a matter for the interpreter, not for this package.
A path with no ".." costs no system call. If fsys is not an fs.ReadLinkFS, Clean removes the last element for ".." with no system call. Clean follows at most MaxLinks links, and then fails with an error that wraps syscall.ELOOP. If an element before ".." is not a directory, Clean fails with an error that wraps syscall.ENOTDIR.
On Windows, the text of a link that names a volume is taken as rooted in fsys. Clean cannot follow a link to another volume.
func IsAbs ¶
IsAbs reports whether p is an absolute path, by the rules of the platform. On Windows, an absolute path is a volume and then a separator, as in C:\x, a UNC path, as in \\server\share\x, or a path of the shell that starts with "/", as in /c/x (D36). On other platforms, an absolute path starts with "/".
func Mkdir ¶
Mkdir creates the named directory in fsys, as MkdirFS.Mkdir does. If fsys is not a MkdirFS, Mkdir returns an *fs.PathError that wraps errors.ErrUnsupported.
func Mkfifo ¶
Mkfifo creates the named pipe name in fsys, as MkfifoFS.Mkfifo does. If fsys is not a MkfifoFS, Mkfifo returns an *fs.PathError that wraps errors.ErrUnsupported.
func Remove ¶
Remove removes the named file or empty directory in fsys, as RemoveFS.Remove does. If fsys is not a RemoveFS, Remove returns an *fs.PathError that wraps errors.ErrUnsupported.
func Rename ¶
Rename moves the file oldname to newname in fsys, as RenameFS.Rename does. If fsys is not a RenameFS, Rename returns an *fs.PathError that wraps errors.ErrUnsupported.
func ShellDrive ¶
ShellDrive returns the letter of the drive of the path of the shell p, in lower case, when p is /c or starts with /c/, and 0 otherwise.
func Symlink ¶
Symlink creates newname in fsys as a symbolic link to the text oldname, as SymlinkFS.Symlink does. If fsys is not a SymlinkFS, Symlink returns an *fs.PathError that wraps errors.ErrUnsupported.
func UserTemp ¶
UserTemp returns the temporary folder of the user for the environment that getenv reads, as GetTempPath of Windows finds it and Git Bash mounts it on /tmp: TMP, then TEMP, then USERPROFILE, then the folder of Windows.
func ValidPath ¶
ValidPath reports whether name is a valid name for the file systems of this package, as fs.ValidPath does, but it allows bytes that are not UTF-8. A file name of Unix is any bytes but NUL and "/", and bash can reach each one, so the file systems of hail take such names too.
Types ¶
type AccessFS ¶
type AccessFS interface {
fs.FS
// Access checks that the user of hail has each permission in mode on the
// named file. It follows symbolic links. If a permission is missing,
// Access returns an error that wraps [fs.ErrPermission].
//
// If there is an error, it is of type [*fs.PathError].
Access(name string, mode AccessMode) error
}
AccessFS is a file system that knows the permission rules of its storage.
type AccessMode ¶
type AccessMode uint32
AccessMode is a set of the permissions that Access checks. The value 0 checks only that the file exists.
const ( // AccessExec checks that the user can run the file, or search the // directory. test -x checks it. AccessExec AccessMode = 1 // AccessWrite checks that the user can write the file. test -w checks // it. AccessWrite AccessMode = 2 // AccessRead checks that the user can read the file. test -r checks it. AccessRead AccessMode = 4 )
The bits of an AccessMode. Each has the value of the bit of the same use in the system call access of Unix, so that the os backend passes it on as it is.
type File ¶
File is a file that hail opened to read or to write. It is an fs.File that also takes writes.
A backend can give more methods on its files. The os backend gives an *os.File, and the memory backend gives io.Seeker, io.ReaderAt and fs.ReadDirFile. Find each method with a type assertion. The process layer asserts *os.File, or a method Fd() uintptr, to hand a real descriptor to a child. A file without either goes to a child through a pipe.
func OpenFile ¶
OpenFile opens the named file in fsys with flag and perm, as OpenFileFS.OpenFile does.
If fsys is not an OpenFileFS and flag only reads, OpenFile opens the file with fsys.Open. The returned file fails each write with fs.ErrPermission. If fsys is not an OpenFileFS and flag writes, OpenFile returns an *fs.PathError that wraps errors.ErrUnsupported.
type MSYS ¶
type MSYS struct {
// Root is the folder of Windows that "/" names, as in
// C:\Program Files\Git.
Root string
// Mounts are the paths of the shell that name another folder than
// the one under Root, as /tmp does.
Mounts []Mount
}
MSYS turns the paths of the shell on Windows into paths of Windows, and back, as Git Bash does (D36).
func HostMSYS ¶
func HostMSYS() MSYS
HostMSYS returns the rules of MSYS2 with the root of this install and the mounts for the environment of the process. A shell with an environment of its own makes its rules with MountsFor.
func (MSYS) Mounted ¶
Mounted returns the path of Windows, with "/", of the path of the shell p when p is a mount or a path under one, as in /tmp/x.
func (MSYS) Native ¶
Native returns the path of Windows of the path of the shell p, with "/" as the separator, as Git Bash gives it to a program of Windows:
- /c gives C:/, and /c/x gives C:/x.
- //server/share/x stays as it is.
- A mount, as in /tmp/x, gives its folder and the rest, as in C:/Users/me/AppData/Local/Temp/x.
- /x/y, where x has more than one letter, gives the root and /x/y, and / gives the root and "/".
Any other path, such as C:\x or a relative path, stays as it is.
func (MSYS) NativeBackslash ¶
NativeBackslash returns the path of Windows of the path of the shell p, as MSYS.Native does, with "\" as the separator, as Git Bash gives the variables HOME, TMP and TEMP to a program of Windows.
func (MSYS) NativeList ¶
NativeList returns the list of Windows, joined by ";", of the list of the shell v, joined by ":", as Git Bash gives PATH to a program of Windows. An empty element is ".", and each path has "\" as its separator. A value that holds a ";" or starts with a drive is a list of Windows already, and stays as it is.
func (MSYS) Shell ¶
Shell returns the path of the shell of the path of Windows p:
- C:\x or C:/x gives /c/x, and C:\ or C: gives /c.
- The folder of a mount, or a path under it, shows under the mount, as in /tmp/x. The names compare without case.
- \\server\share\x gives //server/share/x, and \\?\C:\x gives /c/x.
- A relative path takes "/" in place of each "\".
A path that starts with "/" is a path of the shell already, and stays as it is. A path under the root keeps its drive: hail does not show it as a path under "/", as Git Bash does.
type Mem ¶
type Mem struct {
// contains filtered or unexported fields
}
Mem is a file system in memory, and the Namespace of its files. Tests use it in place of the files of the operating system.
Mem holds directories, regular files and symbolic links, each with a mode and a time of modification. It is an fs.StatFS, fs.ReadDirFS, fs.ReadFileFS and fs.ReadLinkFS, and an OpenFileFS, MkdirFS, RemoveFS, RenameFS, SymlinkFS and AccessFS. It is safe for use by many goroutines at once.
Mem keeps the mode of a file as given and checks it only in Access. Its files are File, io.Seeker, io.ReaderAt and fs.ReadDirFile.
The errors are of type *fs.PathError, or *os.LinkError for Rename and Symlink. They wrap fs.ErrNotExist, fs.ErrExist, fs.ErrInvalid, fs.ErrClosed, or the error numbers syscall.ENOTDIR, syscall.EISDIR, syscall.ENOTEMPTY, syscall.ELOOP and syscall.EBADF, as the os backend does on Unix.
func NewMem ¶
func NewMem() *Mem
NewMem returns an empty file system in memory. Its root has the mode 0755.
func (*Mem) Access ¶
func (m *Mem) Access(name string, mode AccessMode) error
Access checks the permission bits of the owner of the named file against mode. It follows symbolic links.
func (*Mem) Lstat ¶
Lstat returns the information of the named file. It does not follow a symbolic link in the last element.
func (*Mem) OpenFile ¶
OpenFile opens the named file with flag and perm. See OpenFileFS.OpenFile. It follows symbolic links, except in the last element when flag holds both O_CREATE and O_EXCL.
func (*Mem) Remove ¶
Remove removes the named file or empty directory. It removes a symbolic link and not the file that the link names.
func (*Mem) Rename ¶
Rename moves the file oldname to newname. If newname exists, Rename replaces it. It replaces a directory only with a directory, and only an empty one.
func (*Mem) Resolve ¶
Resolve returns m and the name in m of the absolute path p. See Namespace. On Windows, m holds the files of every volume, and Resolve ignores the volume of p.
type MkdirFS ¶
type MkdirFS interface {
fs.FS
// Mkdir creates the named directory with the mode perm. The parent must
// exist. perm is the final mode, as for [OpenFileFS.OpenFile].
//
// If there is an error, it is of type [*fs.PathError].
Mkdir(name string, perm fs.FileMode) error
}
MkdirFS is a file system that creates directories.
type MkfifoFS ¶
type MkfifoFS interface {
fs.FS
// Mkfifo creates the named pipe name with the mode perm, before the
// umask of the process.
//
// If there is an error, it is of type [*fs.PathError].
Mkfifo(name string, perm fs.FileMode) error
}
MkfifoFS is a file system that creates named pipes.
type Mount ¶
type Mount struct {
// Path is the path of the shell, as in /tmp.
Path string
// Native is the folder of Windows, as in C:\Users\me\AppData\Local\Temp.
Native string
}
Mount is a path of the shell that names a folder of Windows, as Git Bash mounts /tmp on the temporary folder of the user.
type Namespace ¶
type Namespace interface {
// Resolve returns the file system that holds the absolute path p, and
// the name of p in that file system. The name is valid for
// [ValidPath]. Resolve resolves each element ".." as [Clean] does.
//
// Resolve fails with an [*fs.PathError] that wraps [ErrNotAbs] for a
// relative path, and one that wraps [fs.ErrNotExist] for the empty path.
Resolve(p string) (fs.FS, string, error)
}
Namespace maps the absolute paths of the operating system to file systems.
type OSNamespace ¶
type OSNamespace struct {
// contains filtered or unexported fields
}
OSNamespace is the Namespace of the files of the operating system. On Windows it has one file system for each volume, made when a path first names the volume. On other platforms it has one file system, with its root at "/".
Each file system of OSNamespace is an fs.StatFS, fs.ReadDirFS, fs.ReadFileFS and fs.ReadLinkFS, and an OpenFileFS, MkdirFS, RemoveFS, RenameFS, SymlinkFS, MkfifoFS and AccessFS. OpenFile returns an *os.File. An error keeps the error of the os package, which holds the error number of the system, but names the file with its name in the file system.
func OS ¶
func OS() *OSNamespace
OS returns the namespace of the files of the operating system. Each call returns the same namespace.
type OpenFileFS ¶
type OpenFileFS interface {
fs.FS
// OpenFile opens the named file with flag, a combination of the flags
// O_RDONLY and the others of this package. If the call creates the file,
// the file gets the mode perm.
//
// perm is the final mode. The interpreter applies the umask of the shell
// state before it calls OpenFile (D7). The files of the os backend also
// get the umask of the process from the kernel. W6 settles how hail sets
// that umask at start-up.
//
// If there is an error, it is of type [*fs.PathError].
OpenFile(name string, flag int, perm fs.FileMode) (File, error)
}
OpenFileFS is a file system that opens a file with flags, to read, to write or to create it.
type RemoveFS ¶
type RemoveFS interface {
fs.FS
// Remove removes the named file or empty directory. It does not follow
// a symbolic link in the last element of name, and so removes the link.
//
// If there is an error, it is of type [*fs.PathError].
Remove(name string) error
}
RemoveFS is a file system that removes files and empty directories.
type RenameFS ¶
type RenameFS interface {
fs.FS
// Rename moves the file oldname to newname. If newname exists and is not
// a directory, Rename replaces it. Both names are in the same file
// system.
//
// If there is an error, it is of type [*os.LinkError], which holds both
// names.
Rename(oldname, newname string) error
}
RenameFS is a file system that renames files.
type SymlinkFS ¶
type SymlinkFS interface {
fs.FS
// Symlink creates newname as a symbolic link to oldname. oldname is the
// text of the link, as the user wrote it, and not a name of the file
// system. newname is a name of the file system.
//
// If there is an error, it is of type [*os.LinkError], which holds both
// names.
Symlink(oldname, newname string) error
}
SymlinkFS is a file system that creates symbolic links.