Documentation
¶
Index ¶
- Variables
- func BuildBlockMatchDiagnosticJSON(lines []string, matchIdxs []int, matchWidth int, ...) string
- func CopyFileCtx(ctx context.Context, src, dst string, perm os.FileMode) (written int64, err error)
- func CopyFileToExistingCtx(ctx context.Context, src, dst string) (int64, error)
- func EnsureNonOverlappingFixedWidth(matchIdxs []int, width int) error
- func FindTrimmedAdjacentBlockMatches(lines, before, match, after []string) []int
- func FindTrimmedBlockMatches(lines, block []string) []int
- func GetBaseMIME(mt MIMEType) string
- func GetTrimmedLines(lines []string) []string
- func IsBlockEqualsAt(haystack, needle []string, start int) bool
- func MIMEForLocalFile(path string) (mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, err error)
- func MIMEForPath(p fspolicy.FSPolicy, path string) (abs string, mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, ...)
- func NormalizeLineBlockInput(in []string) []string
- func OneBasedLineNumbers(idxs []int) []int
- func ReadFile(path string, encoding ReadEncoding, maxBytes int64) (string, error)
- func RequireSingleMatch(idxs []int, name string) (int, error)
- func RequireSingleTrimmedBlockMatch(lines, block []string, name string) (int, error)
- func SniffFileMIME(path string) (MIMEType, ExtensionMode, error)
- func UniquePathInDir(dir, base string) (string, error)
- func WriteFileAtomicBytesResolved(p fspolicy.FSPolicy, dst string, data []byte, perm fs.FileMode, overwrite bool) error
- func WriteFileAtomicBytesWithParents(p fspolicy.FSPolicy, path string, data []byte, perm fs.FileMode, ...) (dst string, err error)
- type ExtensionMode
- type FileExt
- type ImageData
- type ImageInfo
- type ListDirectoryEntry
- type ListDirectoryKind
- type ListDirectoryOptions
- type MIMEDetectMethod
- type MIMEType
- type MaybeStartLineDiagnostic
- type NewlineKind
- type PathInfo
- type ReadEncoding
- type SearchFileMatch
- type SearchFileMatchKind
- type SearchFilesOptions
- type SearchFilesSearchIn
- type TextFile
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPath is shared with policy to keep behavior consistent. ErrInvalidPath = fspolicy.ErrInvalidPath ErrInvalidDir = errors.New("invalid dir") ErrUnknownExtension = errors.New("unknown extension") )
var ( // ErrNotUTF8Text indicates a file could be read but is not valid UTF‑8. ErrNotUTF8Text = errors.New("file is not valid UTF-8 text") ErrFileExceedsMaxSize = errors.New("file exceeds maximum allowed size") )
var AllExtensionModes = []ExtensionMode{ ExtensionModeText, ExtensionModeImage, ExtensionModeDocument, ExtensionModeDefault, }
var BaseMIMEToMode = map[string]ExtensionMode{ "": ExtensionModeDefault, "application/octet-stream": ExtensionModeDefault, "text/plain": ExtensionModeText, "text/markdown": ExtensionModeText, "text/html": ExtensionModeText, "text/css": ExtensionModeText, "application/json": ExtensionModeText, "application/xml": ExtensionModeText, "application/x-yaml": ExtensionModeText, "application/yaml": ExtensionModeText, "application/toml": ExtensionModeText, "application/sql": ExtensionModeText, "application/javascript": ExtensionModeText, "image/jpeg": ExtensionModeImage, "image/png": ExtensionModeImage, "image/gif": ExtensionModeImage, "image/webp": ExtensionModeImage, "image/bmp": ExtensionModeImage, "image/svg+xml": ExtensionModeImage, "application/pdf": ExtensionModeDocument, "application/msword": ExtensionModeDocument, "application/vnd.ms-powerpoint": ExtensionModeDocument, "application/vnd.ms-excel": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.wordprocessingml.document": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.presentationml.presentation": ExtensionModeDocument, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": ExtensionModeDocument, "application/vnd.oasis.opendocument.text": ExtensionModeDocument, "application/vnd.oasis.opendocument.spreadsheet": ExtensionModeDocument, }
BaseMIMEToMode maps base mime types (no parameters) to a coarse mode.
var ExtensionToMIMEType = map[FileExt]MIMEType{ ExtTxt: MIMETextPlain, ExtMd: MIMETextMarkdown, ExtMarkdown: MIMETextMarkdown, ExtLog: MIMETextPlain, ExtJSON: MIMEApplicationJSON, ExtYAML: MIMEApplicationYAML, ExtYML: MIMEApplicationYAML, ExtTOML: MIMEApplicationTOML, ExtJS: MIMEApplicationJS, ExtTS: MIMETextPlain, ExtTSX: MIMETextPlain, ExtJSX: MIMETextPlain, ExtPY: MIMETextPlain, ExtGO: MIMETextPlain, ExtRS: MIMETextPlain, ExtJAVA: MIMETextPlain, ExtC: MIMETextPlain, ExtCPP: MIMETextPlain, ExtH: MIMETextPlain, ExtHPP: MIMETextPlain, ExtCS: MIMETextPlain, ExtRB: MIMETextPlain, ExtPHP: MIMETextPlain, ExtHTML: MIMETextHTML, ExtHTM: MIMETextHTML, ExtCSS: MIMETextCSS, ExtSCSS: MIMETextPlain, ExtLESS: MIMETextPlain, ExtSQL: MIMEApplicationSQL, ExtMod: MIMETextPlain, ExtSum: MIMETextPlain, ExtJSONL: MIMETextPlain, ExtShell: MIMETextPlain, ExtSWIFT: MIMETextPlain, ExtM: MIMETextPlain, ExtKT: MIMETextPlain, ExtPL: MIMETextPlain, ExtSCALA: MIMETextPlain, ExtHS: MIMETextPlain, ExtLUA: MIMETextPlain, ExtDART: MIMETextPlain, ExtCmake: MIMETextPlain, ExtBazel: MIMETextPlain, ExtXML: MIMEApplicationXML, ExtJPG: MIMEImageJPEG, ExtJPEG: MIMEImageJPEG, ExtPNG: MIMEImagePNG, ExtGIF: MIMEImageGIF, ExtWEBP: MIMEImageWEBP, ExtBMP: MIMEImageBMP, ExtSVG: MIMEImageSVG, ExtPDF: MIMEApplicationPDF, ExtDOC: MIMEApplicationMSWord, ExtDOCX: MIMEApplicationOpenXMLDoc, ExtPPT: MIMEApplicationMSPowerPt, ExtPPTX: MIMEApplicationOpenXMLPPT, ExtXLS: MIMEApplicationMSExcel, ExtXLSX: MIMEApplicationOpenXMLXLS, ExtODT: MIMEApplicationODT, ExtODS: MIMEApplicationODS, }
ExtensionToMIMEType is an internal registry of common/explicitly-supported extensions.
var ModeToExtensions = func() map[ExtensionMode][]FileExt { m := make(map[ExtensionMode][]FileExt, len(AllExtensionModes)) for ext, mt := range ExtensionToMIMEType { mode := GetModeForMIME(mt) m[mode] = append(m[mode], ext) } return m }()
Functions ¶
func BuildBlockMatchDiagnosticJSON ¶ added in v0.13.0
func CopyFileCtx ¶
CopyFileCtx copies src->dst, creating dst with O_EXCL. It checks ctx between read iterations.
NOTE: This is a raw IO helper; callers should resolve/enforce policy before calling.
func CopyFileToExistingCtx ¶
CopyFileToExistingCtx copies src -> dst where dst is expected to already exist (typically a placeholder reserved with O_EXCL). "dst" is truncated and overwritten.
NOTE: This is a raw IO helper; callers should resolve/enforce policy before calling.
func FindTrimmedBlockMatches ¶
func GetBaseMIME ¶
func GetTrimmedLines ¶
func IsBlockEqualsAt ¶
func MIMEForLocalFile ¶
func MIMEForLocalFile(path string) (mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, err error)
func MIMEForPath ¶
func MIMEForPath( p fspolicy.FSPolicy, path string, ) (abs string, mimeType MIMEType, mode ExtensionMode, method MIMEDetectMethod, err error)
MIMEForPath returns a best-effort MIME type for a filesystem path with FSPolicy enforcement.
Behavior:
- Path is resolved via policy (base dir + allowed roots).
- If the extension maps to a non-generic MIME type and a non-default mode, it returns without IO (so it may succeed even if the file doesn't exist).
- Otherwise it requires an existing regular file via policy (enforcing BlockSymlinks rules) and sniffs bytes.
func NormalizeLineBlockInput ¶
func OneBasedLineNumbers ¶ added in v0.13.0
func ReadFile ¶
func ReadFile(path string, encoding ReadEncoding, maxBytes int64) (string, error)
ReadFile reads a file and returns its contents. If maxBytes > 0, it enforces a hard cap during reading.
NOTE: This is a raw IO helper; callers should resolve/enforce policy before calling.
func SniffFileMIME ¶
func SniffFileMIME(path string) (MIMEType, ExtensionMode, error)
func UniquePathInDir ¶
func WriteFileAtomicBytesResolved ¶
func WriteFileAtomicBytesResolved( p fspolicy.FSPolicy, dst string, data []byte, perm fs.FileMode, overwrite bool, ) error
WriteFileAtomicBytesResolved is like WriteFileAtomicBytes but assumes dst is already an absolute, policy-resolved path (i.e. returned from p.ResolvePath).
This avoids re-resolving (and re-checking allowed roots) at higher layers.
func WriteFileAtomicBytesWithParents ¶
func WriteFileAtomicBytesWithParents( p fspolicy.FSPolicy, path string, data []byte, perm fs.FileMode, overwrite bool, createParents bool, maxNewDirs int, ) (dst string, err error)
WriteFileAtomicBytesWithParents is a policy-aware convenience wrapper that:
- resolves path once via policy
- either verifies parent exists (createParents=false) or creates it (createParents=true)
- then performs the atomic write
It returns the resolved absolute destination path (even on many error paths) so callers can use it in messages/outputs.
Types ¶
type ExtensionMode ¶
type ExtensionMode string
const ( ExtensionModeText ExtensionMode = "text" ExtensionModeImage ExtensionMode = "image" ExtensionModeDocument ExtensionMode = "document" ExtensionModeDefault ExtensionMode = "default" )
func GetModeForMIME ¶
func GetModeForMIME(mt MIMEType) ExtensionMode
type FileExt ¶
type FileExt string
const ( ExtTxt FileExt = ".txt" ExtMd FileExt = ".md" ExtMarkdown FileExt = ".markdown" ExtLog FileExt = ".log" ExtJSON FileExt = ".json" ExtYAML FileExt = ".yaml" ExtYML FileExt = ".yml" ExtTOML FileExt = ".toml" ExtJS FileExt = ".js" ExtTS FileExt = ".ts" ExtTSX FileExt = ".tsx" ExtJSX FileExt = ".jsx" ExtPY FileExt = ".py" ExtGO FileExt = ".go" ExtRS FileExt = ".rs" ExtJAVA FileExt = ".java" ExtC FileExt = ".c" ExtCPP FileExt = ".cpp" ExtH FileExt = ".h" ExtHPP FileExt = ".hpp" ExtCS FileExt = ".cs" ExtRB FileExt = ".rb" ExtPHP FileExt = ".php" ExtHTML FileExt = ".html" ExtHTM FileExt = ".htm" ExtCSS FileExt = ".css" ExtSCSS FileExt = ".scss" ExtLESS FileExt = ".less" ExtSQL FileExt = ".sql" ExtMod FileExt = ".mod" ExtSum FileExt = ".sum" ExtJSONL FileExt = ".jsonl" ExtShell FileExt = ".sh" ExtSWIFT FileExt = ".swift" ExtM FileExt = ".m" ExtKT FileExt = ".kt" ExtPL FileExt = ".pl" ExtSCALA FileExt = ".scala" ExtHS FileExt = ".hs" ExtLUA FileExt = ".lua" ExtDART FileExt = ".dart" ExtCmake FileExt = ".cmake" ExtBazel FileExt = ".bazel" ExtXML FileExt = ".xml" ExtJPG FileExt = ".jpg" ExtJPEG FileExt = ".jpeg" ExtPNG FileExt = ".png" ExtGIF FileExt = ".gif" ExtWEBP FileExt = ".webp" ExtBMP FileExt = ".bmp" ExtSVG FileExt = ".svg" ExtPDF FileExt = ".pdf" ExtDOC FileExt = ".doc" ExtDOCX FileExt = ".docx" ExtPPT FileExt = ".ppt" ExtPPTX FileExt = ".pptx" ExtXLS FileExt = ".xls" ExtXLSX FileExt = ".xlsx" ExtODT FileExt = ".odt" ExtODS FileExt = ".ods" )
func GetNormalizedExt ¶
type ImageData ¶
type ImageData struct {
ImageInfo
Base64Data string `json:"base64Data,omitempty"` // optional, if requested
}
ImageData holds metadata (and optionally content) for an image file.
func ReadImage ¶
func ReadImage( p fspolicy.FSPolicy, path string, includeBase64Data bool, maxBytes int64, ) (*ImageData, error)
ReadImage inspects an image file and returns its intrinsic metadata. If includeBase64 is true, Base64Data will contain the base64-encoded file contents. If the file does not exist, Exists == false and err == nil.
FSPolicy enforcement:
- path is resolved via policy (base dir + allowed roots)
- if policy.BlockSymlinks == true: refuses symlink parent components and refuses symlink file.
- even if symlinks are allowed, symlink files are refused (strict).
type ListDirectoryEntry ¶ added in v0.13.0
type ListDirectoryEntry struct {
Name string `json:"name"`
Kind ListDirectoryKind `json:"kind"`
}
func ListDirectoryDetailedNormalized ¶ added in v0.13.0
func ListDirectoryDetailedNormalized(dir string, opts ListDirectoryOptions) ([]ListDirectoryEntry, bool, error)
ListDirectoryDetailedNormalized lists and filters immediate directory entries in a directory that is assumed to already be normalized.
type ListDirectoryKind ¶ added in v0.13.0
type ListDirectoryKind string
const ( ListDirectoryKindAll ListDirectoryKind = "all" ListDirectoryKindFile ListDirectoryKind = "file" ListDirectoryKindDirectory ListDirectoryKind = "directory" ListDirectoryKindOther ListDirectoryKind = "other" )
type ListDirectoryOptions ¶ added in v0.13.0
type ListDirectoryOptions struct {
NameGlob string
IncludeDotEntries bool
Kind ListDirectoryKind
MaxEntries int
}
type MIMEDetectMethod ¶
type MIMEDetectMethod string
const ( MIMEDetectMethodExtension MIMEDetectMethod = "extension" MIMEDetectMethodSniff MIMEDetectMethod = "sniff" )
type MIMEType ¶
type MIMEType string
const ( MIMEEmpty MIMEType = "" MIMEApplicationOctetStream MIMEType = "application/octet-stream" MIMETextPlain MIMEType = "text/plain; charset=utf-8" MIMETextMarkdown MIMEType = "text/markdown; charset=utf-8" MIMETextHTML MIMEType = "text/html; charset=utf-8" MIMETextCSS MIMEType = "text/css; charset=utf-8" MIMEApplicationJSON MIMEType = "application/json" MIMEApplicationXML MIMEType = "application/xml" MIMEApplicationYAML MIMEType = "application/x-yaml" MIMEApplicationTOML MIMEType = "application/toml" MIMEApplicationSQL MIMEType = "application/sql" MIMEApplicationJS MIMEType = "application/javascript" MIMEImageJPEG MIMEType = "image/jpeg" MIMEImagePNG MIMEType = "image/png" MIMEImageGIF MIMEType = "image/gif" MIMEImageWEBP MIMEType = "image/webp" MIMEImageBMP MIMEType = "image/bmp" MIMEImageSVG MIMEType = "image/svg+xml" MIMEApplicationPDF MIMEType = "application/pdf" MIMEApplicationMSWord MIMEType = "application/msword" MIMEApplicationMSPowerPt MIMEType = "application/vnd.ms-powerpoint" MIMEApplicationMSExcel MIMEType = "application/vnd.ms-excel" MIMEApplicationOpenXMLDoc MIMEType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" MIMEApplicationOpenXMLPPT MIMEType = "application/vnd.openxmlformats-officedocument.presentationml.presentation" MIMEApplicationOpenXMLXLS MIMEType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" MIMEApplicationODT MIMEType = "application/vnd.oasis.opendocument.text" MIMEApplicationODS MIMEType = "application/vnd.oasis.opendocument.spreadsheet" )
func MIMEFromExtensionString ¶
type MaybeStartLineDiagnostic ¶ added in v0.13.0
type MaybeStartLineDiagnostic struct {
RequestedLine int `json:"requestedLine"`
Tolerance int `json:"tolerance"`
ClosestDistance int `json:"closestDistance"`
ClosestStartLines []int `json:"closestStartLines,omitempty"`
Applied bool `json:"applied"`
}
func NarrowMatchIndicesByMaybeStartLine ¶ added in v0.13.0
func NarrowMatchIndicesByMaybeStartLine( idxs []int, maybeStartLine *int, tolerance int, ) ([]int, *MaybeStartLineDiagnostic)
NarrowMatchIndicesByMaybeStartLine applies a soft start-line hint.
Behavior:
- if maybeStartLine is nil or invalid, no narrowing is applied
- if there is a unique closest candidate within tolerance, returns only that candidate
- otherwise returns the original candidates and emits diagnostics explaining the closest set
type NewlineKind ¶
type NewlineKind string
NewlineKind describes the newline convention detected in a file.
const ( NewlineLF NewlineKind = "lf" NewlineCRLF NewlineKind = "crlf" )
type PathInfo ¶
type PathInfo struct {
Path string `json:"path"`
Name string `json:"name"`
Exists bool `json:"exists"`
IsDir bool `json:"isDir"`
Size int64 `json:"size,omitempty"`
ModTime *time.Time `json:"modTime,omitempty"`
}
func StatPath ¶
StatPath returns basic metadata for the supplied path without mutating the filesystem. If the path does not exist, exists == false and err == nil.
FSPolicy enforcement:
- path resolved via policy (base dir + allowed roots)
- if policy.BlockSymlinks == true: refuses symlink targets (Lstat + reject).
type ReadEncoding ¶
type ReadEncoding string
const ( ReadEncodingText ReadEncoding = "text" ReadEncodingBinary ReadEncoding = "binary" )
type SearchFileMatch ¶ added in v0.13.0
type SearchFileMatch struct {
Path string `json:"path"`
MatchKind SearchFileMatchKind `json:"matchKind"`
}
func SearchFilesDetailed ¶ added in v0.13.0
func SearchFilesDetailed( ctx context.Context, p fspolicy.FSPolicy, opts SearchFilesOptions, ) (matchedFiles []SearchFileMatch, reachedLimit bool, err error)
SearchFilesDetailed walks root recursively and returns up to MaxResults file matches.
type SearchFileMatchKind ¶ added in v0.13.0
type SearchFileMatchKind string
const ( SearchFileMatchKindPath SearchFileMatchKind = "path" SearchFileMatchKindContent SearchFileMatchKind = "content" SearchFileMatchKindPathAndContent SearchFileMatchKind = "pathAndContent" )
type SearchFilesOptions ¶ added in v0.13.0
type SearchFilesSearchIn ¶ added in v0.13.0
type SearchFilesSearchIn string
const ( SearchFilesSearchInPath SearchFilesSearchIn = "path" SearchFilesSearchInContent SearchFilesSearchIn = "content" SearchFilesSearchInPathOrContent SearchFilesSearchIn = "pathOrContent" )
type TextFile ¶
type TextFile struct {
Path string
Perm fs.FileMode
Newline NewlineKind
HasFinalNewline bool
Lines []string
SizeBytes int64
ModTimeUTC *time.Time
}
TextFile is a normalized in-memory view of a UTF‑8 text file. Lines never include trailing newline characters.
func ReadTextFileUTF8 ¶
ReadTextFileUTF8 reads a file as UTF‑8 text and returns a normalized TextFile view. It preserves newline kind (LF vs CRLF) and whether the file ended with a final newline.
Safety behavior (policy-driven):
- Enforces maxBytes if > 0.
- Uses policy.RequireExistingRegularFile (which enforces symlink rules if enabled).