walk

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 11 Imported by: 0 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 walk lists the files of a target safely. It is the only way Spackle enumerates target source, so the safety rules live in one place: never follow a symlink out of the root, never read huge or binary files, and skip dependency and VCS directories.

Index

Constants

View Source
const MaxFileSize = 1 << 20

MaxFileSize is the largest file Spackle reads as source. Larger files are reported as skipped, not silently ignored. The same holds for directories that are pruned by default.

View Source
const MaxHeldText = 64 << 20

MaxHeldText is the most text, in bytes, that a Texts holds at one time.

View Source
const SkippedDirReason = "dependency, cache or build output directory, not scanned by default; name it in [scan] include to read it"

SkippedDirReason is the skip reason of a directory pruned by default.

Variables

This section is empty.

Functions

func CheckPattern added in v0.2.0

func CheckPattern(p string) error

CheckPattern reports whether p is a usable Options pattern.

func Inside

func Inside(root, path string) bool

Inside reports whether path is root or lies beneath it. Both must be absolute and already cleaned of symlinks by the caller.

func IsTestPath added in v0.2.0

func IsTestPath(rel string) bool

IsTestPath reports whether a forward-slash relative path is test code: a file in a test directory, or a file named the way a test runner expects. It is the one definition every stage uses, so the seeders (test code is not attack surface), the mapper (a test is not an entry point) and the fixer (fix the code, not the check) agree on what a test is.

func LangOf

func LangOf(rel string) string

LangOf returns the language of a path by name and extension. It recognizes Dockerfiles ("dockerfile") and dotenv files ("dotenv") by name.

func Match added in v0.2.0

func Match(patterns []string, rel string) bool

Match reports whether the forward-slash relative path rel matches one of the patterns, either itself or through one of its parent directories.

func ReadText

func ReadText(abs string) (string, error)

ReadText reads a listed file and normalizes CRLF to LF so that line numbers and quoted lines are the same on every platform.

func Resolve

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

Resolve turns a path supplied by an untrusted party (a model, a scanner) into an absolute path inside root, or returns an error. It rejects absolute paths, parent traversal, and symlinks that leave the root.

Types

type File

type File struct {
	Rel  string // forward-slash path relative to the root
	Abs  string // absolute path on this machine
	Size int64
	Lang string // see LangOf; empty when not recognized
}

File is one readable text file in the target.

type Options added in v0.2.0

type Options struct {
	// Exclude lists paths that are not read. A pattern that matches a
	// directory excludes everything beneath it.
	Exclude []string
	// Include lists directories that are read although they are pruned by
	// default (node_modules, a root build or dist, a vendor tree).
	Include []string
}

Options narrow or widen what List reads. Patterns are forward-slash globs relative to the root: "*" and "?" match within one path segment and "**" matches any number of segments. A pattern with no slash is also matched against the file or directory name alone, so "*.min.js" works anywhere.

type Result

type Result struct {
	Files   []File
	Skipped []Skip
}

Result is the outcome of listing a target.

func List

func List(root string) (Result, error)

List returns every readable text file under root, sorted by relative path. Problems with individual files are recorded in Skipped; only a problem with root itself is an error.

func ListWith added in v0.2.0

func ListWith(root string, opts Options) (Result, error)

ListWith is List with configured exclusions and inclusions. Every excluded path is recorded in Skipped, so a report can say what was not covered.

type Skip

type Skip struct {
	Rel    string
	Reason string
}

Skip records a path that was not listed, and why.

type Texts added in v0.2.0

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

Texts hands the text of files from one stage of a scan to the next, so a file the mapper read is not read again by the seeders. A text is held until it is taken, and at most MaxHeldText bytes are held: past that a text is not kept, and the next stage reads that file itself. Memory use is therefore bounded however large the target is.

A nil *Texts holds nothing, so a stage can be run without one. A Texts is safe for concurrent use.

func NewTexts added in v0.2.0

func NewTexts() *Texts

NewTexts returns an empty Texts.

func (*Texts) Get added in v0.2.0

func (t *Texts) Get(abs string) (text string, ok bool)

Get returns the text kept for abs and keeps it.

func (*Texts) Put added in v0.2.0

func (t *Texts) Put(abs, text string)

Put keeps the text of the file at abs, as ReadText returned it, when there is room for it.

func (*Texts) Take added in v0.2.0

func (t *Texts) Take(abs string) (text string, ok bool)

Take returns the text kept for abs and forgets it. ok is false when none was kept.

Jump to

Keyboard shortcuts

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