basepathfs

package
v0.33.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package basepathfs restricts all operations to a given path within a file system.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BasePathFS

type BasePathFS struct {
	avfs.Utils[*BasePathFS] // utils regroups common functions used by emulated file systems.
	// contains filtered or unexported fields
}

BasePathFS implements a base path file system.

func New

func New(baseFS avfs.VFS, basePath string) *BasePathFS

New returns a new base path file system (BasePathFS).

func (*BasePathFS) Abs

func (vfs *BasePathFS) Abs(path string) (string, error)

Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The absolute path name for a given file is not guaranteed to be unique. Abs calls Clean on the result.

func (*BasePathFS) Base

func (vfs *BasePathFS) Base(path string) string

Base returns the last element of path. Trailing path separators are removed before extracting the last element. If the path is empty, Base returns ".". If the path consists entirely of separators, Base returns a single separator.

func (*BasePathFS) Chdir

func (vfs *BasePathFS) Chdir(dir string) error

Chdir changes the current working directory to the named directory. If there is an error, it will be of type *PathError.

func (*BasePathFS) Chmod

func (vfs *BasePathFS) Chmod(name string, mode fs.FileMode) error

Chmod changes the mode of the named file to mode. If the file is a symbolic link, it changes the mode of the link's target. If there is an error, it will be of type *PathError.

A different subset of the mode bits are used, depending on the operating system.

On Unix, the mode's permission bits, ModeSetuid, ModeSetgid, and ModeSticky are used.

On Windows, only the 0200 bit (owner writable) of mode is used; it controls whether the file's read-only attribute is set or cleared. The other bits are currently unused. For compatibility with Go 1.12 and earlier, use a non-zero mode. Use mode 0400 for a read-only file and 0600 for a readable+writable file.

On Plan 9, the mode's permission bits, ModeAppend, ModeExclusive, and ModeTemporary are used.

func (*BasePathFS) Chown

func (vfs *BasePathFS) Chown(name string, uid, gid int) error

Chown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link's target. A uid or gid of -1 means to not change that value. If there is an error, it will be of type *PathError.

On Windows or Plan 9, Chown always returns the syscall.EWINDOWS or EPLAN9 error, wrapped in *PathError.

func (*BasePathFS) Chtimes

func (vfs *BasePathFS) Chtimes(name string, atime, mtime time.Time) error

Chtimes changes the access and modification times of the named file, similar to the Unix utime() or utimes() functions.

The underlying file system may truncate or round the values to a less precise time unit. If there is an error, it will be of type *PathError.

func (*BasePathFS) Clean

func (vfs *BasePathFS) Clean(path string) string

Clean returns the shortest path name equivalent to path by purely lexical processing. It applies the following rules iteratively until no further processing can be done:

  1. Replace multiple Separator elements with a single one.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path, assuming Separator is '/'.

The returned path ends in a slash only if it represents a root directory, such as "/" on Unix or `C:\` on Windows.

Finally, any occurrences of slash are replaced by Separator.

If the result of this process is an empty string, Clean returns the string ".".

See also Rob Pike, “Lexical File Names in Plan 9 or Getting Dot-Dot Right,” https://9p.io/sys/doc/lexnames.html

func (*BasePathFS) Create

func (vfs *BasePathFS) Create(name string) (avfs.File, error)

Create creates or truncates the named file. If the file already exists, it is truncated. If the file does not exist, it is created with mode 0666 (before umask). If successful, methods on the returned File can be used for I/O; the associated file descriptor has mode O_RDWR. If there is an error, it will be of type *PathError.

func (*BasePathFS) CreateHomeDir

func (vfs *BasePathFS) CreateHomeDir(u avfs.UserReader) (string, error)

CreateHomeDir creates and returns the home directory of a user. If there is an error, it will be of type *PathError.

func (*BasePathFS) CreateSystemDirs

func (vfs *BasePathFS) CreateSystemDirs(basePath string) error

CreateSystemDirs creates the system directories of a file system.

func (*BasePathFS) CreateTemp

func (vfs *BasePathFS) CreateTemp(dir, pattern string) (avfs.File, error)

CreateTemp creates a new temporary file in the directory dir, opens the file for reading and writing, and returns the resulting file. The filename is generated by taking pattern and adding a random string to the end. If pattern includes a "*", the random string replaces the last "*". If dir is the empty string, CreateTemp uses the default directory for temporary files, as returned by TempDir. Multiple programs or goroutines calling CreateTemp simultaneously will not choose the same file. The caller can use the file's Name method to find the pathname of the file. It is the caller's responsibility to remove the file when it is no longer needed.

func (*BasePathFS) Dir

func (vfs *BasePathFS) Dir(path string) string

Dir returns all but the last element of path, typically the path's directory. After dropping the final element, Dir calls Clean on the path and trailing slashes are removed. If the path is empty, Dir returns ".". If the path consists entirely of separators, Dir returns a single separator. The returned path does not end in a separator unless it is the root directory.

func (vfs *BasePathFS) EvalSymlinks(path string) (string, error)

EvalSymlinks returns the path name after the evaluation of any symbolic links. If path is relative the result will be relative to the current directory, unless one of the components is an absolute symbolic link. EvalSymlinks calls Clean on the result.

func (*BasePathFS) Getwd

func (vfs *BasePathFS) Getwd() (dir string, err error)

Getwd returns a rooted name link corresponding to the current directory. If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them.

func (*BasePathFS) Glob

func (vfs *BasePathFS) Glob(pattern string) (matches []string, err error)

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 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 ErrBadPattern, when pattern is malformed.

func (*BasePathFS) Idm

func (vfs *BasePathFS) Idm() avfs.IdentityMgr

Idm returns the identity manager of the file system. If the file system does not have an identity manager, avfs.DummyIdm is returned.

func (*BasePathFS) Lchown

func (vfs *BasePathFS) Lchown(name string, uid, gid int) error

Lchown changes the numeric uid and gid of the named file. If the file is a symbolic link, it changes the uid and gid of the link itself. If there is an error, it will be of type *PathError.

On Windows, it always returns the syscall.EWINDOWS error, wrapped in *PathError.

func (vfs *BasePathFS) Link(oldname, newname string) error

Link creates newname as a hard link to the oldname file. If there is an error, it will be of type *LinkError.

func (*BasePathFS) Lstat

func (vfs *BasePathFS) Lstat(path string) (fs.FileInfo, error)

Lstat returns a FileInfo describing the named file. If the file is a symbolic link, the returned FileInfo describes the symbolic link. Lstat makes no attempt to follow the link. If there is an error, it will be of type *PathError.

func (*BasePathFS) Mkdir

func (vfs *BasePathFS) Mkdir(name string, perm fs.FileMode) error

Mkdir creates a new directory with the specified name and permission bits (before umask). If there is an error, it will be of type *PathError.

func (*BasePathFS) MkdirAll

func (vfs *BasePathFS) MkdirAll(path string, perm fs.FileMode) error

MkdirAll creates a directory named name, along with any necessary parents, and returns nil, or else returns an error. The permission bits perm (before umask) are used for all directories that MkdirAll creates. If name is already a directory, MkdirAll does nothing and returns nil.

func (*BasePathFS) MkdirTemp

func (vfs *BasePathFS) MkdirTemp(dir, prefix string) (name string, err error)

MkdirTemp creates a new temporary directory in the directory dir and returns the pathname of the new directory. The new directory's name is generated by adding a random string to the end of pattern. If pattern includes a "*", the random string replaces the last "*" instead. If dir is the empty string, MkdirTemp uses the default directory for temporary files, as returned by TempDir. Multiple programs or goroutines calling MkdirTemp simultaneously will not choose the same directory. It is the caller's responsibility to remove the directory when it is no longer needed.

func (*BasePathFS) Name

func (vfs *BasePathFS) Name() string

Name returns the name of the fileSystem.

func (*BasePathFS) OSType

func (vfs *BasePathFS) OSType() avfs.OSType

OSType returns the operating system type of the file system.

func (*BasePathFS) Open

func (vfs *BasePathFS) Open(path string) (avfs.File, error)

Open opens the named file for reading. If successful, methods on the returned file can be used for reading; the associated file descriptor has mode O_RDONLY. If there is an error, it will be of type *PathError.

func (*BasePathFS) OpenFile

func (vfs *BasePathFS) OpenFile(name string, flag int, perm fs.FileMode) (avfs.File, error)

OpenFile is the generalized open call; most users will use Open or Create instead. It opens the named file with specified flag (O_RDONLY etc.). If the file does not exist, and the O_CREATE flag is passed, it is created with mode perm (before umask). If successful, methods on the returned File can be used for I/O. If there is an error, it will be of type *PathError.

func (*BasePathFS) ReadDir

func (vfs *BasePathFS) ReadDir(dirname string) ([]fs.DirEntry, error)

ReadDir reads the directory named by dirname and returns a list of directory entries sorted by filename.

func (*BasePathFS) ReadFile

func (vfs *BasePathFS) ReadFile(filename string) ([]byte, error)

ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

func (vfs *BasePathFS) Readlink(name string) (string, error)

Readlink returns the destination of the named symbolic link. If there is an error, it will be of type *PathError.

func (*BasePathFS) Remove

func (vfs *BasePathFS) Remove(name string) error

Remove removes the named file or (empty) directory. If there is an error, it will be of type *PathError.

func (*BasePathFS) RemoveAll

func (vfs *BasePathFS) RemoveAll(path string) error

RemoveAll removes path and any children it contains. It removes everything it can but returns the first error it encounters. If the path does not exist, RemoveAll returns nil (no error). If there is an error, it will be of type *PathError.

func (*BasePathFS) Rename

func (vfs *BasePathFS) Rename(oldname, newname string) error

Rename renames (moves) oldpath to newpath. If newpath already exists and is not a directory, Rename replaces it. OS-specific restrictions may apply when oldpath and newpath are in different directories. If there is an error, it will be of type *LinkError.

func (*BasePathFS) SameFile

func (vfs *BasePathFS) SameFile(fi1, fi2 fs.FileInfo) bool

SameFile reports whether fi1 and fi2 describe the same file. For example, on Unix this means that the device and inode fields of the two underlying structures are identical; on other systems the decision may be based on the path names. SameFile only applies to results returned by this package's Stat. It returns false in other cases.

func (*BasePathFS) SetUMask

func (vfs *BasePathFS) SetUMask(mask fs.FileMode)

SetUMask sets the file mode creation mask.

func (*BasePathFS) SetUser

func (vfs *BasePathFS) SetUser(name string) (avfs.UserReader, error)

SetUser sets and returns the current user. If the user is not found, the returned error is of type UnknownUserError.

func (*BasePathFS) Stat

func (vfs *BasePathFS) Stat(path string) (fs.FileInfo, error)

Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError.

func (*BasePathFS) Sub

func (vfs *BasePathFS) Sub(dir string) (avfs.VFS, error)

Sub returns an FS corresponding to the subtree rooted at dir.

func (vfs *BasePathFS) Symlink(oldname, newname string) error

Symlink creates newname as a symbolic link to oldname. If there is an error, it will be of type *LinkError.

func (*BasePathFS) TempDir

func (vfs *BasePathFS) TempDir() string

TempDir returns the default directory to use for temporary files.

On Unix systems, it returns $TMPDIR if non-empty, else /tmp. On Windows, it uses GetTempPath, returning the first non-empty value from %TMP%, %TEMP%, %USERPROFILE%, or the Windows directory. On Plan 9, it returns /tmp.

The directory is neither guaranteed to exist nor have accessible permissions.

func (*BasePathFS) ToSlash

func (vfs *BasePathFS) ToSlash(path string) string

ToSlash returns the result of replacing each separator character in path with a slash ('/') character. Multiple separators are replaced by multiple slashes.

func (*BasePathFS) ToSysStat

func (vfs *BasePathFS) ToSysStat(info fs.FileInfo) avfs.SysStater

ToSysStat takes a value from fs.FileInfo.Sys() and returns a value that implements interface avfs.SysStater.

func (*BasePathFS) Truncate

func (vfs *BasePathFS) Truncate(name string, size int64) error

Truncate changes the size of the named file. If the file is a symbolic link, it changes the size of the link's target. If there is an error, it will be of type *PathError.

func (*BasePathFS) Type

func (*BasePathFS) Type() string

Type returns the type of the fileSystem or Identity manager.

func (*BasePathFS) UMask

func (vfs *BasePathFS) UMask() fs.FileMode

UMask returns the file mode creation mask.

func (*BasePathFS) User

func (vfs *BasePathFS) User() avfs.UserReader

User returns the current user.

func (*BasePathFS) WalkDir

func (vfs *BasePathFS) WalkDir(root string, fn fs.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.

func (*BasePathFS) WriteFile

func (vfs *BasePathFS) WriteFile(filename string, data []byte, perm fs.FileMode) error

WriteFile writes data to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm; otherwise WriteFile truncates it before writing.

type BasePathFile

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

BasePathFile represents an open file descriptor.

func (*BasePathFile) Chdir

func (f *BasePathFile) Chdir() error

Chdir changes the current working directory to the file, which must be a directory. If there is an error, it will be of type *PathError.

func (*BasePathFile) Chmod

func (f *BasePathFile) Chmod(mode fs.FileMode) error

Chmod changes the mode of the file to mode. If there is an error, it will be of type *PathError.

func (*BasePathFile) Chown

func (f *BasePathFile) Chown(uid, gid int) error

Chown changes the numeric uid and gid of the named file. If there is an error, it will be of type *PathError.

On Windows, it always returns the syscall.EWINDOWS error, wrapped in *PathError.

func (*BasePathFile) Close

func (f *BasePathFile) Close() error

Close closes the File, rendering it unusable for I/O. On files that support SetDeadline, any pending I/O operations will be canceled and return immediately with an error.

func (*BasePathFile) Fd

func (f *BasePathFile) Fd() uintptr

Fd returns the integer Unix file descriptor referencing the open file. The file descriptor is valid only until f.Close is called or f is garbage collected. On Unix systems this will cause the SetDeadline methods to stop working.

func (*BasePathFile) Name

func (f *BasePathFile) Name() string

Name returns the link of the file as presented to Open.

func (*BasePathFile) Read

func (f *BasePathFile) Read(b []byte) (n int, err error)

Read reads up to len(b) bytes from the MemFile. It returns the number of bytes read and any error encountered. At end of file, Read returns 0, io.EOF.

func (*BasePathFile) ReadAt

func (f *BasePathFile) ReadAt(b []byte, off int64) (n int, err error)

ReadAt reads len(b) bytes from the File starting at byte offset off. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(b). At end of file, that error is io.EOF.

func (*BasePathFile) ReadDir

func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error)

ReadDir reads the contents of the directory associated with the file f and returns a slice of DirEntry values in directory order. Subsequent calls on the same file will yield later DirEntry records in the directory.

If n > 0, ReadDir returns at most n DirEntry records. In this case, if ReadDir returns an empty slice, it will return an error explaining why. At the end of a directory, the error is io.EOF.

If n <= 0, ReadDir returns all the DirEntry records remaining in the directory. When it succeeds, it returns a nil error (not io.EOF).

func (*BasePathFile) Readdirnames

func (f *BasePathFile) Readdirnames(n int) (names []string, err error)

Readdirnames reads and returns a slice of names from the directory f.

If n > 0, Readdirnames returns at most n names. In this case, if Readdirnames 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, Readdirnames returns all the names from the directory in a single slice. In this case, if Readdirnames 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, Readdirnames returns the names read until that point and a non-nil error.

func (*BasePathFile) Seek

func (f *BasePathFile) Seek(offset int64, whence int) (ret int64, err error)

Seek sets the offset for the next Read or Write on file to offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error, if any. The behavior of Seek on a file opened with O_APPEND is not specified.

func (*BasePathFile) Stat

func (f *BasePathFile) Stat() (fs.FileInfo, error)

Stat returns the FileInfo structure describing file. If there is an error, it will be of type *PathError.

func (*BasePathFile) Sync

func (f *BasePathFile) Sync() error

Sync commits the current contents of the file to stable storage. Typically, this means flushing the file system's in-memory copy of recently written data to disk.

func (*BasePathFile) Truncate

func (f *BasePathFile) Truncate(size int64) error

Truncate changes the size of the file. It does not change the I/O offset. If there is an error, it will be of type *PathError.

func (*BasePathFile) Write

func (f *BasePathFile) Write(b []byte) (n int, err error)

Write writes len(b) bytes to the File. It returns the number of bytes written and an error, if any. Write returns a non-nil error when n != len(b).

func (*BasePathFile) WriteAt

func (f *BasePathFile) WriteAt(b []byte, off int64) (n int, err error)

WriteAt writes len(b) bytes to the File starting at byte offset off. It returns the number of bytes written and an error, if any. WriteAt returns a non-nil error when n != len(b).

func (*BasePathFile) WriteString

func (f *BasePathFile) WriteString(s string) (n int, err error)

WriteString is like Write, but writes the contents of string s rather than a slice of bytes.

Jump to

Keyboard shortcuts

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