docroot

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package docroot keeps a document's reads and writes inside the folder it is in, and out of hidden files: the document's own folder and the folders under it, less any file or folder whose name starts with a dot (.env, .ssh, .git, .zshrc), at any depth. Includes, report images, casts and out= assets go through it, so a document in ~ can't read ~/.ssh/id_ed25519 into its output or write over ~/.zshrc, and one in docs/ can't reach the rest of its repository. Being in a repository changes nothing: a document reaches its own folder only. Callers that trust the document skip it, as mdsh --allow-outside does.

Index

Constants

View Source
const MaxRead = 16 << 20

MaxRead is the most ReadFile reads by default: 16 MB.

Variables

View Source
var ErrHidden error = hiddenError{}

ErrHidden is returned for a path inside the root that is, or is in, a hidden file or folder: one whose name starts with a dot, at any depth below the root. Those hold settings and secrets (.env, .ssh, .git's config with a token in a remote's URL, .git/hooks that run later), so they count as outside. It matches ErrOutside with errors.Is.

View Source
var ErrNotRegular = errors.New("not a regular file")

ErrNotRegular is returned for a folder, device, FIFO or other file that isn't a regular file.

View Source
var ErrOutside = errors.New("outside the document's folder (mdsh --allow-outside allows it)")

ErrOutside is returned for a path outside the document's folder.

View Source
var ErrTooBig = errors.New("file too large")

ErrTooBig is returned for a file larger than ReadFile's limit.

Functions

func Hidden added in v1.6.0

func Hidden(rel string) bool

Hidden reports whether rel, a clean path relative to a root, names a hidden file or folder or something in one: any part of it starting with a dot. The root itself (".") isn't hidden, so a document kept in a hidden folder still reaches its own folder.

func Inside

func Inside(root, path string) bool

Inside reports whether path, absolute and with symlinks resolved, is root or under it, and not hidden: see ErrHidden.

func ReadFile

func ReadFile(root, path string, max int64) ([]byte, error)

ReadFile reads path, which must be a regular file of at most max bytes (MaxRead if max <= 0). With root set, it must also be inside root; with root empty, anywhere is allowed.

func Resolve

func Resolve(root, path string) (string, error)

Resolve returns path with symlinks resolved, or ErrOutside if that is outside root, or ErrHidden if it is hidden. A path that doesn't exist yet, such as a file about to be written, is resolved through its nearest existing folder.

func Root

func Root(dir string) string

Root returns the folder files for a document in dir must stay in: dir itself, absolute with symlinks resolved. A repository holding dir makes no difference.

func WriteFile

func WriteFile(root, path string, data []byte) error

WriteFile writes data to path, making its folder. With root set, the path must be inside root once symlinks in it are resolved, and a symlink at path itself is not followed: the file is written to a temporary file beside it and renamed over it.

Types

This section is empty.

Jump to

Keyboard shortcuts

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