fileutil

package
v0.0.0-...-b5ed7c0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMemoryAccess = errors.New("invalid memory access")

Functions

func CopyRegularFileByPath

func CopyRegularFileByPath(src, dst string) error

CopyRegularFileByPath copies a file from src to dst

func CopyRegularFileByRelativePath

func CopyRegularFileByRelativePath(srcName string, dstRoot *os.Root, dstName string) error

CopyRegularFileByRelativePath copies a file from src to dst, where destination is relative to a given root directory. This function needs capabilities to be set before it is called.

func CreateAt

func CreateAt(root *os.Root, relativePath string) (*os.File, error)

CreateAt creates or truncates a file relative to root.

func Dup

func Dup(file *os.File) (*os.File, error)

Dup is a wrapper function to the dup syscall using golang types.

func IsDirEmpty

func IsDirEmpty(pathname string) (bool, error)

IsDirEmpty returns true if directory contains no files

func IsRegularFile

func IsRegularFile(name string) (bool, error)

IsRegularFile checks if the given file name points to a regular file

func MkdirAllAtExist

func MkdirAllAtExist(root *os.Root, relativePath string, perm fs.FileMode) error

MkdirAllAtExist recursively creates a directory and all necessary parents relative to root, ignoring "already exists" errors.

func MkdirAtExist

func MkdirAtExist(root *os.Root, relativePath string, perm fs.FileMode) error

MkdirAtExist creates a directory relative to root, ignoring "already exists" errors.

func NewProtectedReader

func NewProtectedReader(data []byte) io.ReaderAt

NewProtectedReader returns an io.ReaderAt that wraps around a byte slice. Unlike the reader returned from bytes.NewReader(), this reader protects the reading function from fatal errors caused by invalid memory accesses to the underlying byte slice. If a fatal error occurs (e.g. SIGBUS or SIGSEGV), the read simply fails with an error that indicates a bad memory access. This is intended for reading from mmap'ed files, where a change to the file on disk can cause areas of the mapped memory to become invalid, resulting in a SIGBUS when accessing them.

func OpenAt

func OpenAt(root *os.Root, relativePath string, flags int, perm fs.FileMode) (*os.File, error)

OpenAt opens a file relative to root with the given flags and permissions. The operation is traversal-resistant: symlinks that escape root and ".." path components that would leave the tree are rejected by os.Root.

func OpenRootDir

func OpenRootDir(p string) (*os.Root, error)

OpenRootDir opens a directory as an os.Root, providing traversal-resistant file operations. All file access through the returned Root is confined to the directory tree: symlinks that escape the root and ".." traversals are rejected. O_NOFOLLOW is applied to the initial open so that if p itself is a symlink, the call fails with ELOOP. The Root is constructed from the already-open fd via /proc/self/fd to avoid a TOCTOU window between the symlink check and the Root creation. Requires /proc mounted (standard on Linux, where Tracee exclusively runs).

func RemoveAt

func RemoveAt(root *os.Root, relativePath string) error

RemoveAt removes a file or (empty) directory relative to root.

func RenameAt

func RenameAt(root *os.Root, oldpath string, newpath string) error

RenameAt renames a file within a root directory.

func SafeOpenFile

func SafeOpenFile(path string, flag int, perm os.FileMode) (*os.File, error)

SafeOpenFile opens a file with O_NOFOLLOW, refusing to follow a symlink at the final path component. Use this for absolute or user-supplied paths that are not under an os.Root context.

func SafeRemoveAll

func SafeRemoveAll(path string) error

SafeRemoveAll verifies with Lstat that path is not a symlink before calling os.RemoveAll. This prevents a top-level symlink from redirecting the recursive delete into an attacker-chosen tree.

Types

This section is empty.

Jump to

Keyboard shortcuts

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