Implements core/paths (Config/Data/State/Cache/Runtime/Log/Bin
directory resolution with canonical-vs-search-dir separation, XDG on
Unix, native APPDATA/LOCALAPPDATA/PROGRAMDATA layering on Windows,
~/Library on macOS Desktop, injectable environment/platform for
deterministic cross-platform tests) and core/config (file search,
first-match resolution, layered YAML merging, safe ${VAR} expansion,
Explain/doctor introspection) on top of the shared Application/
Kind/Scope model in the core package.
193 lines
5.5 KiB
Go
193 lines
5.5 KiB
Go
package paths
|
|
|
|
import "os"
|
|
|
|
// SearchResult describes one directory in a search/priority hierarchy,
|
|
// with enough metadata for Explain/doctor-style introspection (spec
|
|
// section 48).
|
|
type SearchResult struct {
|
|
Path string
|
|
Source Source
|
|
Priority int
|
|
Exists bool
|
|
Selected bool
|
|
}
|
|
|
|
// statExists reports whether path exists (any type), swallowing the
|
|
// distinction between "does not exist" and other stat errors (permission
|
|
// issues etc. are surfaced as "does not exist" here -- a resolver's job
|
|
// is to compute and search paths, not diagnose filesystem permissions).
|
|
func statExists(path string) bool {
|
|
_, err := os.Stat(path)
|
|
return err == nil
|
|
}
|
|
|
|
// resultsFromLayers converts layers (already in priority order) into
|
|
// SearchResults, checking each directory's existence and marking the
|
|
// first existing one Selected -- i.e. "the layer that would be used if
|
|
// resolving by first match over directories alone", consistent with
|
|
// FindConfig/FindData's file-level first-match semantics.
|
|
func resultsFromLayers(layers []layer) []SearchResult {
|
|
results := make([]SearchResult, len(layers))
|
|
selected := false
|
|
for i, l := range layers {
|
|
exists := statExists(l.path)
|
|
results[i] = SearchResult{
|
|
Path: l.path,
|
|
Source: l.source,
|
|
Priority: l.priority,
|
|
Exists: exists,
|
|
Selected: exists && !selected,
|
|
}
|
|
if exists {
|
|
selected = true
|
|
}
|
|
}
|
|
return results
|
|
}
|
|
|
|
// statInfo is a small, deliberately approximate writability check used
|
|
// only by Explain: it looks at the owner-write permission bit rather
|
|
// than performing a real access check (which would require platform-
|
|
// specific syscalls, and Explain must never create/delete a probe file
|
|
// to test writability -- see spec section 47's "no implicit mutation").
|
|
type writabilityProbe struct {
|
|
writable bool
|
|
}
|
|
|
|
func statInfo(path string) (writabilityProbe, error) {
|
|
info, err := os.Stat(path)
|
|
if err != nil {
|
|
return writabilityProbe{}, err
|
|
}
|
|
return writabilityProbe{writable: info.Mode().Perm()&0o200 != 0}, nil
|
|
}
|
|
|
|
// FindFile searches dirs (as returned by ConfigSearchDirs/DataSearchDirs/
|
|
// ExecutableSearchDirs, or any caller-built list in priority order) for
|
|
// name, returning the first directory that contains it. found is false
|
|
// if name exists in none of them; this is not an error.
|
|
func FindFile(dirs []SearchResult, name string) (result SearchResult, found bool, err error) {
|
|
for _, d := range dirs {
|
|
candidate := d
|
|
joined := JoinDirFile(d.Path, name)
|
|
if statExists(joined) {
|
|
candidate.Path = joined
|
|
candidate.Exists = true
|
|
candidate.Selected = true
|
|
return candidate, true, nil
|
|
}
|
|
}
|
|
return SearchResult{}, false, nil
|
|
}
|
|
|
|
// FindConfig searches ConfigSearchDirs for a file named name.
|
|
func (r *Resolver) FindConfig(name string) (SearchResult, bool, error) {
|
|
dirs, err := r.ConfigSearchDirs()
|
|
if err != nil {
|
|
return SearchResult{}, false, err
|
|
}
|
|
return FindFile(dirs, name)
|
|
}
|
|
|
|
// FindData searches DataSearchDirs for a file named name.
|
|
func (r *Resolver) FindData(name string) (SearchResult, bool, error) {
|
|
dirs, err := r.DataSearchDirs()
|
|
if err != nil {
|
|
return SearchResult{}, false, err
|
|
}
|
|
return FindFile(dirs, name)
|
|
}
|
|
|
|
// FindExecutable searches ExecutableSearchDirs for a file named name.
|
|
func (r *Resolver) FindExecutable(name string) (SearchResult, bool, error) {
|
|
dirs, err := r.ExecutableSearchDirs()
|
|
if err != nil {
|
|
return SearchResult{}, false, err
|
|
}
|
|
return FindFile(dirs, name)
|
|
}
|
|
|
|
// ensure creates dir (and any missing parents) with perm, matching the
|
|
// durable-write conventions used across the Yetano family (AGENTS.md).
|
|
// It is a no-op if dir already exists.
|
|
func ensure(dir string, perm os.FileMode) (string, error) {
|
|
if err := os.MkdirAll(dir, perm); err != nil {
|
|
return "", err
|
|
}
|
|
return dir, nil
|
|
}
|
|
|
|
// EnsureConfigDir creates ConfigDir() (mode 0700) if it does not exist
|
|
// and returns its path.
|
|
func (r *Resolver) EnsureConfigDir() (string, error) {
|
|
dir, err := r.ConfigDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureDataDir creates DataDir() (mode 0700) if it does not exist and
|
|
// returns its path.
|
|
func (r *Resolver) EnsureDataDir() (string, error) {
|
|
dir, err := r.DataDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureStateDir creates StateDir() (mode 0700) if it does not exist and
|
|
// returns its path.
|
|
func (r *Resolver) EnsureStateDir() (string, error) {
|
|
dir, err := r.StateDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureCacheDir creates CacheDir() (mode 0700) if it does not exist and
|
|
// returns its path.
|
|
func (r *Resolver) EnsureCacheDir() (string, error) {
|
|
dir, err := r.CacheDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureRuntimeDir creates RuntimeDir() (mode 0700) if it does not exist
|
|
// and returns its path. On Unix, this still fails with
|
|
// ErrRuntimeDirUnavailable when no runtime directory could be resolved --
|
|
// it does not itself fabricate one.
|
|
func (r *Resolver) EnsureRuntimeDir() (string, error) {
|
|
dir, err := r.RuntimeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureLogDir creates LogDir() (mode 0700) if it does not exist and
|
|
// returns its path.
|
|
func (r *Resolver) EnsureLogDir() (string, error) {
|
|
dir, err := r.LogDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o700)
|
|
}
|
|
|
|
// EnsureBinDir creates BinDir() (mode 0755, since it holds executables
|
|
// meant to be reached via PATH) if it does not exist and returns its
|
|
// path.
|
|
func (r *Resolver) EnsureBinDir() (string, error) {
|
|
dir, err := r.BinDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return ensure(dir, 0o755)
|
|
}
|