safeio

package
v0.4.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Unrestricted allows any action on any file.
	Unrestricted = &FSAccess{unrestricted: true}

	// NoAccess forbids any action on any file.
	NoAccess = &FSAccess{}
)
View Source
var ErrNotAllowed = errors.New("safeio: operation not allowed")

ErrNotAllowed is returned when an action is not allowed by the FSAccess.

Functions

func OpenFile

func OpenFile(a FSAccessor, name string, flag int, perm fs.FileMode) (*os.File, error)

OpenFile is like os.OpenFile but only succeeds if a's FSAccess allows it. Opening with os.O_CREATE an existing file does not require CreateAction.

func RemoveFile

func RemoveFile(a FSAccessor, name string) error

RemoveFile is like os.Remove but only succeeds if a's FSAccess allows it.

func RenameFile

func RenameFile(a FSAccessor, oldName, newName string) error

RenameFile is like os.Rename but only succeeds if a's FSAccess allows it. When access is restricted, both files must be in the same granted directory, which must allow deleting and creating files (renaming can replace an existing file).

func TempFile

func TempFile(a FSAccessor, dir string, pattern string) (*os.File, error)

TempFile is like os.CreateTemp but only succeeds if a's FSAccess allows creating files in dir (os.TempDir() if dir is empty).

Types

type FSAccess added in v0.4.0

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

FSAccess describes which files can be accessed and what actions can be performed on them. It is either unrestricted (the Unrestricted value) or a set of directories, each opened as an *os.Root so that accessing a file can never escape the directory (e.g. via ".." or symbolic links).

An action on a file is allowed if any directory containing the file is granted all the actions required. This means that grants cannot be used to restrict access to a subdirectory of a granted directory: if a file can be reached via several grants, the most permissive one applies.

An FSAccess value is immutable apart from being closed.

func (*FSAccess) Close added in v0.4.0

func (a *FSAccess) Close() error

Close releases the directories opened by a (not those of its parent). Files opened via a remain open.

func (*FSAccess) IsUnrestricted added in v0.4.0

func (a *FSAccess) IsUnrestricted() bool

IsUnrestricted returns true if a allows any action on any file.

func (*FSAccess) Parent added in v0.4.0

func (a *FSAccess) Parent() *FSAccess

Parent returns the FSAccess that a was derived from via Restrict.

func (*FSAccess) Restrict added in v0.4.0

func (a *FSAccess) Restrict(grants []FSGrant) (*FSAccess, error)

Restrict returns a new FSAccess allowing only the given grants. The returned value can never allow more than a does:

  • if a is unrestricted, each directory is opened as an *os.Root;
  • otherwise each directory must be inside a directory granted by a, and it is opened through that directory's *os.Root, with the actions narrowed to those allowed by a.

The caller is responsible for calling Close on the returned value when it is no longer needed.

type FSAccessor added in v0.4.0

type FSAccessor interface {
	FSAccess() *FSAccess
}

FSAccessor is implemented by types that can tell what file system access is currently allowed (e.g. *runtime.Runtime).

type FSAction added in v0.4.0

type FSAction uint8

FSAction is a set of actions that can be performed on files.

const (
	ReadAction   FSAction = 1 << iota // Read the contents of a file
	WriteAction                       // Write to an existing file
	CreateAction                      // Create a new file
	DeleteAction                      // Delete or rename a file

	NoActions  FSAction = 0
	AllActions          = ReadAction | WriteAction | CreateAction | DeleteAction
)

func ParseFSActions added in v0.4.0

func ParseFSActions(s string) (FSAction, error)

ParseFSActions turns a string such as "rw" into an FSAction. Valid characters are 'r' (read), 'w' (write), 'c' (create), 'd' (delete) and '*' (all actions).

type FSGrant added in v0.4.0

type FSGrant struct {
	Dir     string
	Actions FSAction
}

FSGrant gives permission to perform some actions on files in a directory (recursively).

Jump to

Keyboard shortcuts

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