Documentation
¶
Index ¶
- Variables
- func OpenFile(a FSAccessor, name string, flag int, perm fs.FileMode) (*os.File, error)
- func RemoveFile(a FSAccessor, name string) error
- func RenameFile(a FSAccessor, oldName, newName string) error
- func TempFile(a FSAccessor, dir string, pattern string) (*os.File, error)
- type FSAccess
- type FSAccessor
- type FSAction
- type FSGrant
Constants ¶
This section is empty.
Variables ¶
var ( // Unrestricted allows any action on any file. Unrestricted = &FSAccess{unrestricted: true} // NoAccess forbids any action on any file. NoAccess = &FSAccess{} )
var ErrNotAllowed = errors.New("safeio: operation not allowed")
ErrNotAllowed is returned when an action is not allowed by the FSAccess.
Functions ¶
func OpenFile ¶
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).
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
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
IsUnrestricted returns true if a allows any action on any file.
func (*FSAccess) Parent ¶ added in v0.4.0
Parent returns the FSAccess that a was derived from via Restrict.
func (*FSAccess) Restrict ¶ added in v0.4.0
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
ParseFSActions turns a string such as "rw" into an FSAction. Valid characters are 'r' (read), 'w' (write), 'c' (create), 'd' (delete) and '*' (all actions).