fs

package
v4.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(path string) (string, error)

Abs resolves a path to an absolute path. The supplied path can not be an empty string.

func CopyFile

func CopyFile(from, to string, mode os.FileMode) (err error)

CopyFile copies file to the provided location making sure the resulting file has permission bits set to the mode prior to umask. To honor umask correctly the resulting file must not exist.

func CopyFileAtomic

func CopyFileAtomic(from, to string, mode os.FileMode) (err error)

CopyFileAtomic copies file to a temporary file in the same destination directory and the renames to the final name. This is useful to avoid races where concurrent copies could happen to the same destination. It makes sure the resulting file has permission bits set to the mode prior to umask. To honor umask correctly the resulting file must not exist.

func EnsureFileWithPermission

func EnsureFileWithPermission(fn string, mode os.FileMode) error

EnsureFileWithPermission takes a file path, and 1. Creates it with the specified permission, or 2. ensures a file is the specified permission.

func EvalRelative

func EvalRelative(path string, root string) string

EvalRelative evaluates symlinks in path relative to root path, it returns a path as if it was evaluated from chroot. This function always returns an absolute path and is intended to be used to resolve mount points destinations, it helps the runtime to not bind mount directories/files outside of the container image provided by the root argument.

func FileHasOwner added in v4.5.0

func FileHasOwner(f *os.File, uid uint32) bool

FileHasOwner checks if passed os.File is owned by user identified with uid.

func FindSize

func FindSize(size int64) string

FindSize takes a size in bytes and converts it to a human-readable string representation expressing kB, MB, GB or TB (whatever is smaller, but still larger than one).

func FirstExistingParent

func FirstExistingParent(path string) (string, error)

FirstExistingParent walks up the supplied path and returns the first parent that exists. If the supplied path exists, it will just return that path. Assumes cwd and the root directory always exists

func ForceRemoveAll

func ForceRemoveAll(path string) error

ForceRemoveAll removes a directory like os.RemoveAll, except that it will chmod any directory who's permissions are preventing the removal of contents

func IsDir

func IsDir(name string) bool

IsDir check if name component is a directory.

func IsExec

func IsExec(name string) bool

IsExec check if name component has executable bit permission set.

func IsFile

func IsFile(name string) bool

IsFile check if name component is regular file.

func IsGroup

func IsGroup(name string, gid uint32) bool

IsGroup checks if named file is owned by group identified with gid.

func IsLink(name string) bool

IsLink check if name component is a symlink.

func IsOwner

func IsOwner(name string, uid uint32) bool

IsOwner checks if named file is owned by user identified with uid.

func IsReadable

func IsReadable(path string) bool

IsReadable returns true if the file that is passed in is readable by the user (note: uid is checked, not euid).

func IsSuid

func IsSuid(name string) bool

IsSuid check if name component has setuid bit permission set.

func IsWritable

func IsWritable(path string) bool

IsWritable returns true if the file that is passed in is writable by the user (note: uid is checked, not euid).

func MakeTmpDir

func MakeTmpDir(basedir, pattern string, mode os.FileMode) (string, error)

MakeTmpDir creates a temporary directory with provided mode in os.TempDir if basedir is "". This function assumes that basedir exists, so it's the caller's responsibility to create it before calling it.

func MakeTmpFile

func MakeTmpFile(basedir, pattern string, mode os.FileMode) (*os.File, error)

MakeTmpFile creates a temporary file with provided mode in os.TempDir if basedir is "". This function assumes that basedir exists, so it's the caller's responsibility to create it before calling it.

func Mkdir

func Mkdir(path string, mode os.FileMode) error

Mkdir creates a directory if it doesn't exist with mode after umask reset.

func MkdirAll

func MkdirAll(path string, mode os.FileMode) error

MkdirAll creates a directory and parents if it doesn't exist with mode after umask reset.

func MkdirAllAt added in v4.5.0

func MkdirAllAt(rootPath, relPath string, mode os.FileMode) error

MkdirAllAt creates a directory and parents below rootPath using os.Root, with mode after umask reset. relPath must be local, and may not escape rootPath.

func MkdirAt added in v4.5.0

func MkdirAt(rootPath, relPath string, mode os.FileMode) error

MkdirAt creates a directory below rootPath using os.Root, with mode after umask reset. relPath must be local, and may not escape rootPath.

func NewFileFromFd added in v4.5.0

func NewFileFromFd(fd int, name string) (*os.File, error)

NewFileFromFd wraps fd in an *os.File.

func OpenTrustedExecutable added in v4.5.0

func OpenTrustedExecutable(name string, uid uint32) (int, string, error)

OpenTrustedExecutable opens name without following a final symlink and returns a file descriptor path only if the opened file is owned by uid, is regular, is not writable by group or other, and has an executable bit set. The returned descriptor does not have close-on-exec set, so callers must close it when it is no longer needed or intentionally keep it open for a later exec.

func OpenTrustedFile added in v4.5.0

func OpenTrustedFile(name string, uid uint32) (*os.File, error)

OpenTrustedFile opens name without following a final symlink and returns the open file only if it is owned by user identified with uid, is regular, and is not writable by group or other.

func PathExists

func PathExists(path string) (bool, error)

PathExists simply checks if a path exists.

func PermWalk

func PermWalk(root string, walkFn filepath.WalkFunc) error

PermWalk is similar to filepath.Walk - but:

  1. The skipDir checks are removed (we never want to skip anything here)
  2. Our walk will call walkFn on a directory *before* attempting to look inside that directory.

func PermWalkRaiseError

func PermWalkRaiseError(root string, walkFn filepath.WalkFunc) error

PermWalkRaiseError is similar to filepath.Walk - but:

  1. The skipDir checks are removed (we never want to skip anything here)
  2. Our walk will call walkFn on a directory *before* attempting to look inside that directory.
  3. We back out of the recursion at the *first* error... we don't attempt to go through as much as we can.

func ProcFdPath added in v4.5.0

func ProcFdPath(fd uintptr) string

ProcFdPath returns a /proc/self/fd path for fd.

func RootDir

func RootDir(path string) string

RootDir returns the root directory of path (rootdir of /my/path is /my). Returns "." if path is empty.

func Touch

func Touch(path string) error

Touch behaves like touch command.

func WriteFileNoFollow added in v4.5.0

func WriteFileNoFollow(path string, data []byte, mode os.FileMode) error

WriteFileNoFollow mimics fs.WriteFileNoFollow but with O_NOFOLLOW to avoid following symlinks in the last part of the path. This prevents e.g. creating a file through a dangling symlink.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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