Documentation
¶
Overview ¶
Package project resolves the project root selected by the current directory.
Index ¶
- Constants
- Variables
- func DeeperThanScan(root string, path string) bool
- func FindRoot(start string) (string, error)
- func FindRootBy(start string, holds func(directory string) (bool, error)) (string, error)
- func OwnedByCurrentUser(path string) bool
- func RefuseUnsafeConfig(info os.FileInfo, path string) error
- func SkipsScanning(name string) bool
- func Walk(root string, maxEntries int, visit func(path string, entry fs.DirEntry) error) (int, error)
- type ErrTooManyEntries
Constants ¶
const ConfigFileName = "grat.config"
ConfigFileName is the file that marks a directory as a grat project. It is declared once, here, because a scan that looks for one name and a writer that creates another would disagree about which directories are projects.
const MaxScanDepth = 6
MaxScanDepth is how far below a registered directory a scan looks for a project root.
A grat.config sits at the root of a project, and a registered directory is where projects are kept, so the marker is a small number of levels down. Going deeper than this only walks the contents of projects rather than finding more of them, and the contents are where the entry counts come from: an icon set alone measured 70,146 entries on one machine.
const MaxScanEntries = 250_000
MaxScanEntries is how many directory entries one scan may look at in total.
A development folder holds far more files than projects, so the bound is what keeps a scan from spending its time inside one of them: an icon set alone measured 70,146 entries on one machine, and an unpacked module tree measures more. It is one number rather than one per caller, because a scan that is bounded in one command and unbounded in another is unbounded.
Variables ¶
var CurrentUser = os.Getuid
CurrentUser reports the account grat is running as.
It is a variable so a test can drive the checks below without a second account on the machine, which is the one thing they are about and the one thing a test cannot arrange.
var ErrConfigNotFound = errors.New("grat.config not found")
ErrConfigNotFound means no grat.config exists between the start path and the filesystem root.
Functions ¶
func DeeperThanScan ¶
DeeperThanScan reports whether path lies further below root than a scan for project roots looks.
It is asked of a directory before descending into it, so a directory exactly at the limit is still read for its files and only its children are left out.
func FindRoot ¶
FindRoot walks from start toward the filesystem root and returns the nearest directory that contains a regular grat.config file.
func FindRootBy ¶
FindRootBy walks from start toward the filesystem root and returns the nearest directory that holds answers true for.
The walk is shared rather than repeated, because the rule about where it stops is the security-relevant part and a second copy of it would be a second answer to which directories may become a project.
func OwnedByCurrentUser ¶
OwnedByCurrentUser reports whether path belongs to the account grat runs as.
A path that cannot be inspected is not owned, rather than assumed to be: the answer decides whether a command from that file is executed.
func RefuseUnsafeConfig ¶
RefuseUnsafeConfig reports a grat.config that grat will not run.
Two things disqualify one. It belongs to another account, so somebody else decided what it says. Or it is writable by its group or by everybody, so somebody else can decide later. A configuration names commands that run through /bin/sh as whoever typed the grat command, so both are the same question: who chose this.
git closed the same shape in CVE-2022-24765 and this follows it, including the direction: refuse and say why, rather than silently looking elsewhere.
It takes the description of an already-open file rather than a path, so the answer is about the bytes that will actually be read and there is no moment between the two in which the file could be exchanged. path is for the message.
func SkipsScanning ¶
SkipsScanning reports whether a directory of this name is walked when looking for project roots.
func Walk ¶
func Walk(root string, maxEntries int, visit func(path string, entry fs.DirEntry) error) (int, error)
Walk visits every regular file below root that a scan for project roots is allowed to see.
It is the one traversal both the port registry and project discovery use, so that what counts as a project cannot differ between the command that finds one and the command that allocates its ports. The bounds are the same in both cases: no deeper than MaxScanDepth, never into a directory a tool wrote or a package manager unpacked, never through a symlink, and never past maxEntries so that a directory holding a whole development folder cannot make the scan run away.
visit sees every directory and every regular file that survives those bounds, so a caller decides for itself which of the two it cares about. Returning fs.SkipDir from it skips the rest of the current directory.
The number of entries visited comes back so that a caller walking several roots can spend one budget across all of them, which is what keeps the limit a statement about the whole scan rather than about each root separately.
Types ¶
type ErrTooManyEntries ¶
ErrTooManyEntries says a scan was stopped because the directory holds more than it was allowed to look at, and names the limit so a caller can say which one was reached.
func (ErrTooManyEntries) Error ¶
func (err ErrTooManyEntries) Error() string