osfs

package
v6.0.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 31

Documentation

Overview

Package osfs provides a billy filesystem for the OS.

Index

Constants

This section is empty.

Variables

View Source
var Default = newBoundOS(string(os.PathSeparator))

Default Filesystem representing the root of the os filesystem.

View Source
var ErrPathEscapesParent = errors.New("path escapes from parent")

ErrPathEscapesParent represents when an action leads to escaping from the dir the filesystem is bound to.

On non-js builds this aligns with os.Root's containment guarantees; the upstream error returned by os.Root is not exported, so this sentinel is exposed instead. On js/wasm the symbol is defined for API parity but is not returned by the in-memory implementation.

Functions

func New

func New(baseDir string, opts ...Option) billy.Filesystem

New returns a new OS filesystem rooted at baseDir.

The returned filesystem is always a BoundOS: containment is enforced via os.Root, opened and closed per operation. For better performance with caller-managed lifecycle, use FromRoot instead.

All Option values are accepted for API compatibility but have no effect on the returned implementation.

Types

type BoundOS

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

BoundOS is a fs implementation based on the OS filesystem which relies on Go's os.Root. A new os.Root is opened and closed for each filesystem operation to avoid holding a directory handle open.

For better performance, prefer RootOS via FromRoot with a caller-managed os.Root.

Behaviours of note:

  1. Read and write operations can only be directed to files which descend from the base dir.
  2. Symlink targets are stored verbatim and not rewritten, so they may point outside the base dir or to non-existent paths. BoundOS.Readlink returns the stored target with path separators normalised to forward slashes (see filepath.ToSlash).
  3. Operations leading to escapes to outside the os.Root location result in ErrPathEscapesParent.

func (*BoundOS) Capabilities

func (fs *BoundOS) Capabilities() billy.Capability

func (*BoundOS) Chmod

func (fs *BoundOS) Chmod(path string, mode gofs.FileMode) error

func (*BoundOS) Chroot

func (fs *BoundOS) Chroot(path string) (billy.Filesystem, error)

Chroot returns a new BoundOS filesystem, with the base dir set to the result of joining the provided path with the underlying base dir.

func (*BoundOS) Create

func (fs *BoundOS) Create(name string) (billy.File, error)

func (*BoundOS) Join

func (fs *BoundOS) Join(elem ...string) string

func (*BoundOS) Lstat

func (fs *BoundOS) Lstat(name string) (os.FileInfo, error)

func (*BoundOS) MkdirAll

func (fs *BoundOS) MkdirAll(name string, perm gofs.FileMode) error

func (*BoundOS) Open

func (fs *BoundOS) Open(name string) (billy.File, error)

func (*BoundOS) OpenFile

func (fs *BoundOS) OpenFile(name string, flag int, perm gofs.FileMode) (billy.File, error)

func (*BoundOS) ReadDir

func (fs *BoundOS) ReadDir(name string) ([]gofs.DirEntry, error)
func (fs *BoundOS) Readlink(name string) (string, error)

func (*BoundOS) Remove

func (fs *BoundOS) Remove(name string) error

func (*BoundOS) RemoveAll

func (fs *BoundOS) RemoveAll(name string) error

func (*BoundOS) Rename

func (fs *BoundOS) Rename(from, to string) error

func (*BoundOS) Root

func (fs *BoundOS) Root() string

Root returns the current base dir of the billy.Filesystem.

func (*BoundOS) Stat

func (fs *BoundOS) Stat(name string) (os.FileInfo, error)
func (fs *BoundOS) Symlink(oldname, newname string) error

func (*BoundOS) TempFile

func (fs *BoundOS) TempFile(dir, prefix string) (billy.File, error)

TempFile creates a temporary file. If dir is empty, the file will be created within a .tmp dir.

type Option

type Option func(*options)

func WithBoundOS

func WithBoundOS() Option

WithBoundOS selects the BoundOS implementation.

BoundOS is the only OS-backed implementation returned by New, so this option is the default and is kept for API compatibility.

type RootOS

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

RootOS is a high-performance fs implementation based on a caller-managed os.Root. Since the root is reused across all operations, this avoids the overhead of opening and closing it on every call. The caller is responsible for the root's lifecycle.

For automatic lifecycle management at the cost of per-operation overhead, use BoundOS via New instead.

Behaviours of note:

  1. Read and write operations can only be directed to files which descend from the root dir.
  2. Symlink targets are stored verbatim and not rewritten, so they may point outside the root dir or to non-existent paths. RootOS.Readlink returns the stored target with path separators normalised to forward slashes (see filepath.ToSlash).
  3. Operations leading to escapes to outside the os.Root location result in ErrPathEscapesParent.

func FromRoot

func FromRoot(root *os.Root) (*RootOS, error)

FromRoot creates a new RootOS from an os.Root. The provided root is used directly for all operations and the caller is responsible for its lifecycle. Root must not be nil.

func (*RootOS) Capabilities

func (fs *RootOS) Capabilities() billy.Capability

func (*RootOS) Chmod

func (fs *RootOS) Chmod(path string, mode gofs.FileMode) error

func (*RootOS) Chroot

func (fs *RootOS) Chroot(path string) (billy.Filesystem, error)

Chroot returns a logical sub-filesystem rooted at the result of joining the provided path with the underlying root dir. The returned filesystem reuses this RootOS's os.Root (no new file descriptor is opened), so its lifecycle is tied to the parent root managed by the caller.

If path does not yet exist under the parent root it is created (with [defaultDirectoryMode]). If path exists but is not a directory an error is returned.

Containment is enforced by the parent os.Root, not by the chroot path: operations cannot escape the parent root, but a symlink within the chroot may resolve to a sibling location elsewhere under the parent root. For a tighter boundary at the chroot path, open a new os.Root via os.OpenRoot and wrap it with FromRoot.

func (*RootOS) Create

func (fs *RootOS) Create(name string) (billy.File, error)

func (*RootOS) Join

func (fs *RootOS) Join(elem ...string) string

func (*RootOS) Lstat

func (fs *RootOS) Lstat(name string) (os.FileInfo, error)

func (*RootOS) MkdirAll

func (fs *RootOS) MkdirAll(name string, perm gofs.FileMode) error

func (*RootOS) Open

func (fs *RootOS) Open(name string) (billy.File, error)

func (*RootOS) OpenFile

func (fs *RootOS) OpenFile(name string, flag int, perm gofs.FileMode) (billy.File, error)

func (*RootOS) ReadDir

func (fs *RootOS) ReadDir(name string) ([]gofs.DirEntry, error)
func (fs *RootOS) Readlink(name string) (string, error)

func (*RootOS) Remove

func (fs *RootOS) Remove(name string) error

func (*RootOS) RemoveAll

func (fs *RootOS) RemoveAll(name string) error

func (*RootOS) Rename

func (fs *RootOS) Rename(from, to string) error

func (*RootOS) Root

func (fs *RootOS) Root() string

Root returns the current root dir of the filesystem.

func (*RootOS) Stat

func (fs *RootOS) Stat(name string) (os.FileInfo, error)
func (fs *RootOS) Symlink(oldname, newname string) error

func (*RootOS) TempFile

func (fs *RootOS) TempFile(dir, prefix string) (billy.File, error)

TempFile creates a temporary file. If dir is empty, the file will be created within a .tmp dir.

type Type

type Type int

Type identifies an osfs implementation.

const (
	// BoundOSFS selects the [BoundOS] implementation.
	BoundOSFS Type = iota
)

Jump to

Keyboard shortcuts

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