relpath

package module
v0.0.0-...-2a117d5 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package relpath implements a recursive relative path tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetGlobalParent

func SetGlobalParent(p *RelPath)

SetGlobalParent sets a global prefix to all the RelPath objects created afterwards.

Types

type RelPath

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

func FromPath

func FromPath(path string) *RelPath

FromPath creates a RelPath object from a file path.

func (*RelPath) Abs

func (p *RelPath) Abs() (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 (*RelPath) Add

func (p *RelPath) Add(p2 *RelPath) (ret *RelPath)

func (*RelPath) Append

func (p *RelPath) Append(path string)

Append adds a path segment after the current RelPath object.

func (*RelPath) Base

func (p *RelPath) Base() 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 (*RelPath) Chdir

func (p *RelPath) Chdir() error

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

func (*RelPath) Chmod

func (p *RelPath) Chmod(mode os.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 (*RelPath) Chown

func (p *RelPath) Chown(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 (*RelPath) Chtimes

func (p *RelPath) Chtimes(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 filesystem may truncate or round the values to a less precise time unit. If there is an error, it will be of type *PathError.

func (*RelPath) Clean

func (p *RelPath) Clean() string

Clean returns the shortest path name equivalent to path by purely lexical processing.

func (*RelPath) Copy

func (p *RelPath) Copy() *RelPath

Copy copies the current RelPath.

func (*RelPath) CopyDetached

func (p *RelPath) CopyDetached() *RelPath

CopyDetached copies the current RelPath while detaching it from its tree, creating a standalone new RelPath object.

func (*RelPath) Dir

func (p *RelPath) Dir() 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 (*RelPath) DirFS

func (p *RelPath) DirFS() fs.FS

DirFS returns a file system (an fs.FS) for the tree of files rooted at the directory dir.

Note that DirFS("/prefix") only guarantees that the Open calls it makes to the operating system will begin with "/prefix": DirFS("/prefix").Open("file") is the same as os.Open("/prefix/file"). So if /prefix/file is a symbolic link pointing outside the /prefix tree, then using DirFS does not stop the access any more than using os.Open does. DirFS is therefore not a general substitute for a chroot-style security mechanism when the directory tree contains arbitrary content.

func (*RelPath) Equal

func (p *RelPath) Equal(p2 *RelPath) bool

Equal compares if the 2 files are actually the same file. If any one of them does not exist, it returns false.

func (*RelPath) EqualAbsLexical

func (p *RelPath) EqualAbsLexical(p2 *RelPath) bool

EqualAbsLexical compares if the 2 RelPath objects have the same path when converted to absolute paths.

func (*RelPath) EqualLexical

func (p *RelPath) EqualLexical(p2 *RelPath) bool

EqualLexical compares if the 2 RelPath objects have the same path.

func (*RelPath) EqualObject

func (p *RelPath) EqualObject(p2 *RelPath) bool

EqualObject compares if the 2 RelPath objects have the same internal data fields.

func (*RelPath) EqualTree

func (p *RelPath) EqualTree(p2 *RelPath) bool

EqualTree compares if the 2 RelPath objects have exact tree components.

func (p *RelPath) EvalSymlinks() (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 (*RelPath) Exists

func (p *RelPath) Exists() bool

Exists return if the file exists, regardless of its type.

func (*RelPath) Ext

func (p *RelPath) Ext() string

Ext returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final element of path; it is empty if there is no dot.

func (*RelPath) FromSlash

func (p *RelPath) FromSlash() string

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

func (*RelPath) IsAbs

func (p *RelPath) IsAbs() bool

IsAbs reports whether the path is absolute.

func (*RelPath) Lchown

func (p *RelPath) Lchown(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 (p *RelPath) Link(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 (*RelPath) Lstat

func (p *RelPath) Lstat() (os.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 (*RelPath) MarshalJSON

func (p *RelPath) MarshalJSON() ([]byte, error)

func (*RelPath) Mkdir

func (p *RelPath) Mkdir(name string, perm os.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 (*RelPath) MkdirAll

func (p *RelPath) MkdirAll(name string, perm os.FileMode) error

MkdirAll creates a directory named path, 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 path is already a directory, MkdirAll does nothing and returns nil.

func (*RelPath) Open

func (p *RelPath) Open() (*os.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 (*RelPath) OpenFile

func (p *RelPath) OpenFile(flag int, perm os.FileMode) (*os.File, error)

func (*RelPath) Path

func (p *RelPath) Path() string

Path returns the full path from this RelPath to all its parents.

func (*RelPath) Prepend

func (p *RelPath) Prepend(path string)

Prepend prepends a path segment before the top of the RelPath tree. It affects all RelPath objects inherited from this tree.

func (*RelPath) ReadDir

func (p *RelPath) ReadDir() ([]os.DirEntry, error)

ReadDir reads the named directory, returning all its directory entries sorted by filename. If an error occurs reading the directory, ReadDir returns the entries it was able to read before the error, along with the error.

func (*RelPath) ReadFile

func (p *RelPath) ReadFile() ([]byte, error)

ReadFile reads the named file 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 (p *RelPath) Readlink() (string, error)

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

func (*RelPath) RelPath

func (p *RelPath) RelPath() string

RelPath returns the relative path from this RelPath object to its parent.

func (*RelPath) Relative

func (p *RelPath) Relative(path string) *RelPath

Relative creates a RelPath object that is relative to the current RelPath.

func (*RelPath) Remove

func (p *RelPath) Remove() error

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

func (*RelPath) RemoveAll

func (p *RelPath) RemoveAll() 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 (*RelPath) Rename

func (p *RelPath) Rename(newpath 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.

Note: Rename will detach the RelPath object from its inheritance tree.

func (*RelPath) Resolve

func (p *RelPath) Resolve() (path string, err error)

Resolve returns the absolute path of the actual file (with symlinks resolved).

func (*RelPath) Split

func (p *RelPath) Split() (string, string)

Split splits path immediately following the final Separator, separating it into a directory and file name component. If there is no Separator in path, Split returns an empty dir and file set to path. The returned values have the property that path = dir+file.

func (*RelPath) SplitList

func (p *RelPath) SplitList() []string

SplitList splits a list of paths joined by the OS-specific ListSeparator, usually found in PATH or GOPATH environment variables. Unlike strings.Split, SplitList returns an empty slice when passed an empty string.

func (*RelPath) Stat

func (p *RelPath) Stat() (os.FileInfo, error)

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

func (*RelPath) String

func (p *RelPath) String() string
func (p *RelPath) Symlink(newname string) error

Symlink creates newname as a symbolic link to oldname. On Windows, a symlink to a non-existent oldname creates a file symlink; if oldname is later created as a directory the symlink will not work. If there is an error, it will be of type *LinkError.

func (*RelPath) ToAbsolute

func (p *RelPath) ToAbsolute() error

func (*RelPath) ToSlash

func (p *RelPath) ToSlash() string

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

func (*RelPath) Touch

func (p *RelPath) Touch() (err error)

Touch creates the file if it does not exist, or update its access time.

func (*RelPath) UnmarshalJSON

func (p *RelPath) UnmarshalJSON(data []byte) error

func (*RelPath) VolumeName

func (p *RelPath) VolumeName() string

VolumeName returns leading volume name. Given "C:\foo\bar" it returns "C:" on Windows. Given "\\host\share\foo" it returns "\\host\share". On other platforms it returns "".

func (*RelPath) Walk

func (p *RelPath) Walk(fn filepath.WalkFunc) error

Walk 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 WalkFunc documentation for details.

The files are walked in lexical order, which makes the output deterministic but requires Walk to read an entire directory into memory before proceeding to walk that directory.

Walk does not follow symbolic links.

Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory.

func (*RelPath) WalkDir

func (p *RelPath) WalkDir(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.

Jump to

Keyboard shortcuts

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