filesystem

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: May 26, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrSubprocessNotSupported = errors.New("filesystem: subprocess execution not supported")

ErrSubprocessNotSupported is the sentinel returned by FileSystem implementations that cannot launch host-level subprocesses for the caller (e.g. an in-memory FS with no real host mapping). No production codepath returns it yet; it is exported for future consumers that need to distinguish "no host available" from other failure modes.

Functions

This section is empty.

Types

type FileSystem

type FileSystem interface {
	jail.Jailed

	// ReadFile reads the contents of the file at path.
	// Relative paths are resolved from the current working directory.
	ReadFile(path string) ([]byte, error)
	// WriteFile writes data to path with the provided permissions.
	// Relative paths are resolved from the current working directory.
	WriteFile(path string, data []byte, perm os.FileMode) error
	// Mkdir creates a directory at path, using MkdirAll when all is true.
	// Relative paths are resolved from the current working directory.
	Mkdir(path string, perm os.FileMode, all bool) error
	// Remove deletes path, using recursive removal when all is true.
	// Relative paths are resolved from the current working directory.
	Remove(path string, all bool) error
	// Rename moves or renames src to dst.
	// Relative src and dst paths are resolved from the current working directory.
	Rename(src, dst string) error
	// Stat returns file metadata for path, following symlinks when requested.
	// Relative paths are resolved from the current working directory.
	Stat(path string, followSymlinks bool) (os.FileInfo, error)
	// ReadDir reads and returns directory entries for path.
	// Relative paths are resolved from the current working directory.
	ReadDir(path string) ([]os.DirEntry, error)
	// Symlink creates newname as a symbolic link to oldname.
	// Relative oldname and newname paths are resolved from the current working directory.
	Symlink(oldname, newname string) error
	// Glob returns paths matching the provided pattern.
	// Relative patterns are evaluated from the current working directory.
	Glob(pattern string) ([]string, error)
	// AppendFile appends data to path, creating the file if it does not exist.
	// Relative paths are resolved from the current working directory.
	AppendFile(path string, data []byte, perm os.FileMode) error
	// OpenFile opens the file at path with the given flags and permissions,
	// returning an io.WriteCloser. Callers must close the returned writer.
	// Relative paths are resolved from the current working directory.
	OpenFile(path string, flag int, perm os.FileMode) (io.WriteCloser, error)
	// Chmod changes the mode of the file at path to mode.
	// Relative paths are resolved from the current working directory.
	// On Windows only the read-only bit is honored, matching the
	// behavior of WriteFile's perm argument.
	Chmod(path string, mode os.FileMode) error
	// Chown changes the numeric uid and gid of the file at path.
	// Relative paths are resolved from the current working directory.
	// On Windows this is a no-op.
	Chown(path string, uid, gid int) error
	// Lchown is like Chown but does not follow symlinks.
	// Relative paths are resolved from the current working directory.
	// On Windows this is a no-op.
	Lchown(path string, uid, gid int) error
	// Chtimes changes the access and modification times of the file at path.
	// Relative paths are resolved from the current working directory.
	// On Windows the resolution of access time is filesystem-dependent
	// (e.g. FAT32 truncates to 2-second granularity).
	Chtimes(path string, atime, mtime time.Time) error
	// AtomicWriteFile writes data to path atomically with the provided permissions.
	// Relative paths are resolved from the current working directory.
	AtomicWriteFile(path string, data []byte, perm os.FileMode) error
	// Rel returns a relative path from basePath to targetPath.
	// Relative paths are resolved from the current working directory.
	Rel(basePath, targetPath string) (string, error)
	// Getwd returns the current working directory used for relative path resolution.
	Getwd() (string, error)
	// Setwd sets the current working directory to path.
	// Relative paths are resolved from the current working directory.
	Setwd(path string) error
	// ResolvePath resolves path to an absolute normalized path, optionally following symlinks.
	// Relative paths are resolved from the current working directory.
	ResolvePath(path string, followSymlinks bool) (string, error)
	// HostPath translates a virtual (jail-relative) path into the
	// equivalent absolute host filesystem path that an external program
	// (subprocess, editor, etc.) needs to operate on.
	//
	// Implementations canonicalize the jail prefix via filepath.EvalSymlinks
	// before joining so platform-level symlinks (e.g. macOS /var ->
	// /private/var) do not produce phantom escapes when callers later
	// re-canonicalize the returned path. Implementations do NOT
	// canonicalize intermediate symlinks in the virtual path; callers
	// that need parent-traversal-symlink defense should resolve the
	// path through ResolvePath(_, true) first.
	//
	// HostPath is the public surface of the in-tree host-translation
	// helper used by the FileSystem implementation itself. It is
	// intended for callers that need to hand a host path to code outside
	// the FileSystem abstraction (e.g. exec.Command).
	//
	// The input may be absolute (treated as virtual when a jail is
	// configured) or relative (resolved against the current working
	// directory). Returns [jail.ErrEscapeAttempt] when the lexical jail
	// check rejects the resulting path. When no jail is configured, the
	// cleaned absolute host path is returned unchanged.
	HostPath(virtual string) (string, error)
}

FileSystem defines the contract for filesystem operations operating on resolved/host filesystem paths.

Path and pattern inputs may be absolute or relative. Relative inputs are resolved against the implementation's current working directory as reported by Getwd and changed by Setwd.

type OsFS

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

OsFS is the canonical FileSystem implementation for host and jailed access.

When jail is empty, paths resolve against the host filesystem. When jail is set, all virtual absolute paths are mapped under jail on the host.

func NewOsFS

func NewOsFS(jailPath, wd string) (*OsFS, error)

NewOsFS constructs an OsFS with optional jail and initial working directory.

If wd is empty and jail is set, wd defaults to "/". If wd is empty and jail is not set, wd defaults to the process working directory.

func (*OsFS) AppendFile added in v1.2.0

func (fs *OsFS) AppendFile(path string, data []byte, perm os.FileMode) error

func (*OsFS) AtomicWriteFile

func (fs *OsFS) AtomicWriteFile(path string, data []byte, perm os.FileMode) error

func (*OsFS) Chmod added in v1.5.1

func (fs *OsFS) Chmod(path string, mode os.FileMode) error

func (*OsFS) Chown added in v1.5.1

func (fs *OsFS) Chown(path string, uid, gid int) error

func (*OsFS) Chtimes added in v1.5.1

func (fs *OsFS) Chtimes(path string, atime, mtime time.Time) error

func (*OsFS) GetJail

func (fs *OsFS) GetJail() string

func (*OsFS) Getwd

func (fs *OsFS) Getwd() (string, error)

func (*OsFS) Glob

func (fs *OsFS) Glob(pattern string) ([]string, error)

func (*OsFS) HostPath added in v1.5.2

func (fs *OsFS) HostPath(virtual string) (string, error)

HostPath returns the absolute host filesystem path corresponding to a virtual (jail-relative) path. It is the public surface of the in-tree resolveHost translation used internally by FileSystem methods, intended for callers that need to hand a host path to code outside the FileSystem abstraction (e.g. exec.CommandContext).

Semantics:

  1. Resolve the input lexically through resolveVirtual with followSymlinks=false. This applies the working directory for relative inputs and re-checks IsInJail on the lexical jail join, returning jail.ErrEscapeAttempt for parent-traversal attempts.
  2. When a jail is configured, canonicalize the jail prefix via filepath.EvalSymlinks. On macOS, /var resolves to /private/var; on Linux, jail directories created under /tmp on certain distros may resolve through symlinks too. Without this canonicalization, downstream consumers that re-canonicalize the returned path would see a different prefix and falsely flag the path as outside the jail. Falls back to the raw jail when EvalSymlinks fails (e.g. the jail does not yet exist), matching resolveHostForCreate.
  3. Join the canonical jail with the cleaned virtual path and return the result. The final segment is intentionally NOT EvalSymlinks'd — callers may pass paths whose final component does not yet exist (e.g. files about to be created by an editor).

Returns jail.ErrEscapeAttempt when the virtual path would resolve outside the configured jail. When no jail is configured, returns the cleaned absolute host path unchanged.

func (*OsFS) Lchown added in v1.5.1

func (fs *OsFS) Lchown(path string, uid, gid int) error

func (*OsFS) Mkdir

func (fs *OsFS) Mkdir(path string, perm os.FileMode, all bool) error

func (*OsFS) OpenFile added in v1.3.0

func (fs *OsFS) OpenFile(path string, flag int, perm os.FileMode) (io.WriteCloser, error)

func (*OsFS) ReadDir

func (fs *OsFS) ReadDir(path string) ([]os.DirEntry, error)

func (*OsFS) ReadFile

func (fs *OsFS) ReadFile(path string) ([]byte, error)

func (*OsFS) Rel

func (fs *OsFS) Rel(basePath, targetPath string) (string, error)

func (*OsFS) Remove

func (fs *OsFS) Remove(path string, all bool) error

func (*OsFS) Rename

func (fs *OsFS) Rename(src, dst string) error

func (*OsFS) ResolvePath

func (fs *OsFS) ResolvePath(path string, followSymlinks bool) (string, error)

func (*OsFS) SetJail

func (fs *OsFS) SetJail(jailPath string) error

func (*OsFS) Setwd

func (fs *OsFS) Setwd(path string) error

func (*OsFS) Stat

func (fs *OsFS) Stat(path string, followSymlinks bool) (os.FileInfo, error)
func (fs *OsFS) Symlink(oldname, newname string) error

func (*OsFS) WriteFile

func (fs *OsFS) WriteFile(path string, data []byte, perm os.FileMode) error

Jump to

Keyboard shortcuts

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