package fd

import "github.com/cyphar/filepath-securejoin/pathrs-lite/internal/fd"

Package fd provides a drop-in interface-based replacement of *os.File that allows for things like noop-Close wrappers to be used.

Index

Variables

var Openat2 = func(dir Fd, path string, how *unix.OpenHow) (*os.File, error) {
	dirFd, fullPath := prepareAt(dir, path)

	how.Flags |= unix.O_CLOEXEC
	var tries int
	for {
		fd, err := unix.Openat2(dirFd, path, how)
		if err != nil {
			if scopedLookupShouldRetry(how, err) && tries < scopedLookupMaxRetries {

				tries++
				continue
			}
			return nil, &os.PathError{Op: "openat2", Path: fullPath, Err: err}
		}
		runtime.KeepAlive(dir)
		return os.NewFile(uintptr(fd), fullPath), nil
	}
}

Openat2 is an Fd-based wrapper around unix.Openat2, but with some retry logic in case of EAGAIN errors.

NOTE: This is a variable so that the lookup tests can force openat2 to fail.

Functions

func Dup

func Dup(fd Fd) (*os.File, error)

Dup creates a new file description referencing the same underlying file.

func DupWithName

func DupWithName(fd Fd, name string) (*os.File, error)

DupWithName creates a new file descriptor referencing the same underlying file, but with the provided name instead of fd.Name().

func Faccessat

func Faccessat(dir Fd, path string, mode uint32, flags int) error

Faccessat is an Fd-based wrapper around unix.Faccessat.

func Fsmount

func Fsmount(ctx Fd, flags, mountAttrs int) (*os.File, error)

Fsmount is an Fd-based wrapper around unix.Fsmount.

func Fsopen

func Fsopen(fsName string, flags int) (*os.File, error)

Fsopen is an Fd-based wrapper around unix.Fsopen.

func Fstat

func Fstat(fd Fd) (unix.Stat_t, error)

Fstat is an Fd-based wrapper around unix.Fstat.

func Fstatat

func Fstatat(dir Fd, path string, flags int) (unix.Stat_t, error)

Fstatat is an Fd-based wrapper around unix.Fstatat.

func Fstatfs

func Fstatfs(fd Fd) (unix.Statfs_t, error)

Fstatfs is an Fd-based wrapper around unix.Fstatfs.

func GetMountID

func GetMountID(dir Fd, path string) (uint64, error)

GetMountID gets the mount identifier associated with the fd and path combination. It is effectively a wrapper around fetching STATX_MNT_ID{,_UNIQUE} with unix.Statx, but with a fallback to 0 if the kernel doesn't support the feature.

func IsDeadInode

func IsDeadInode(file Fd) error

IsDeadInode detects whether the file has been unlinked from a filesystem and is thus a "dead inode" from the kernel's perspective.

func OpenTree

func OpenTree(dir Fd, path string, flags uint) (*os.File, error)

OpenTree is an Fd-based wrapper around unix.OpenTree.

func Openat

func Openat(dir Fd, path string, flags int, mode int) (*os.File, error)

Openat is an Fd-based wrapper around unix.Openat.

func Readlinkat

func Readlinkat(dir Fd, path string) (string, error)

Readlinkat is an Fd-based wrapper around unix.Readlinkat.

Types

type Fd

type Fd interface {
	io.Closer
	Name() string
	Fd() uintptr
}

Fd is an interface that mirrors most of the API of *os.File, allowing you to create wrappers that can be used in place of *os.File.

func NopCloser

func NopCloser(f Fd) Fd

NopCloser returns an *os.File-like object where the Close method is now a no-op.

Note that for *os.File and similar objects, the Go garbage collector will still call Close on the underlying file unless you use runtime.SetFinalizer to disable this behaviour. This is up to the caller to do (if necessary).

Source Files

at_linux.go fd.go fd_linux.go mount_linux.go openat2_linux.go

Version
v0.6.1 (latest)
Published
Nov 19, 2025
Platform
linux/amd64
Imports
9 packages
Last checked
48 minutes ago

Tools for package owners.