package paths import ( "errors" "yetano.dev/yetano/core" ) // layer is one directory in a search/priority hierarchy (see // AGENTS.md-style docs in paths.go: "canonical path vs. search paths"). // Search order is descending priority; merge order (used by core/config) // is the reverse. type layer struct { path string source Source priority int canonical bool } // Priority constants shared with core/config, which adds the working // directory layer (always PriorityWorkingDirectory) on top of whatever // paths.Resolver reports for the user/system layers. const ( PriorityWorkingDirectory = 100 PriorityUserLocal = 90 // Windows-only machine-local override layer PriorityUser = 80 PrioritySystem = 10 ) // composeSearch applies the one Scope rule used across every search-dir // category (Config, Data, Executable): SystemScope resolves only the // system tier (a system-scope process, e.g. a system service, has no // meaningful per-user home directory); UserScope resolves the user // tier(s) plus the system tier as a lower-priority fallback, so a // system-wide default can still be found -- and, per spec section 36, // a user installation overrides a system one. func composeSearch(user, system []layer, scope core.Scope) []layer { if scope == core.SystemScope { return system } out := make([]layer, 0, len(user)+len(system)) out = append(out, user...) out = append(out, system...) return out } // skipMissingHome filters a user-tier lookup error for the search-dir // builders (Config/Data/Executable): a missing home directory only means // there is no user tier to search -- the working-directory and system // tiers are still meaningful (e.g. a system service started without // HOME whose config lives in /etc/), so ErrNoHomeDirectory is // dropped here and the caller simply omits its user layer(s). Any other // error is returned unchanged. The canonical Dir() methods are // unaffected and still report ErrNoHomeDirectory: a write location is // never silently swapped for a different tier. func skipMissingHome(err error) error { if errors.Is(err, ErrNoHomeDirectory) { return nil } return err } // envSuffix maps each category to the "_HOME" suffix used to build its // YETANO_*/_* environment variable names (e.g. ConfigPath -> // "YETANO_CONFIG_HOME" / "YET_CONFIG_HOME"). var envSuffix = map[PathKind]string{ ConfigPath: "CONFIG_HOME", DataPath: "DATA_HOME", StatePath: "STATE_HOME", CachePath: "CACHE_HOME", RuntimePath: "RUNTIME_HOME", LogPath: "LOG_HOME", BinPath: "BIN_HOME", } // appendsAppName reports whether the YetanoEnvironment/platform-native/ // PlatformDefault tiers for kind are treated as a *base* directory that // the application name is joined onto (true, e.g. XDG_CONFIG_HOME) or as // an already-final, shared directory used as-is (false; only BinPath: a // bin directory is conventionally shared across every application on the // machine, like ~/.local/bin, not namespaced per app). // // APIOverride and AppEnvironment are always used as-is for every // category, regardless of appendsAppName -- an explicit or app-specific // override already names an exact directory; it would be surprising for // "YET_CONFIG_HOME=/foo" to actually mean "/foo/yet". func appendsAppName(kind PathKind) bool { return kind != BinPath } // overrideValue resolves tiers 1-3 of the override chain (API override, // app-specific environment, shared YETANO_* environment) for kind. ok is // false if none of these tiers is set, meaning the caller must fall back // to its own platform-native tier 4 / default tier 5. func (r *Resolver) overrideValue(kind PathKind) (v value, ok bool) { if explicit, set := r.overrides[kind]; set { return value{path: explicit, source: APIOverride}, true } suffix := envSuffix[kind] if r.envPrefix != "" { if v, set := lookup(r.env, r.envPrefix+"_"+suffix); set { return value{path: v, source: AppEnvironment}, true } } if v, set := lookup(r.env, "YETANO_"+suffix); set { base := v if appendsAppName(kind) { base = r.join(v, r.app.Name) } return value{path: base, source: YetanoEnvironment}, true } return value{}, false } // join joins path elements using the address family of r.platform // (windows: backslash; everything else: forward slash) -- see join.go. func (r *Resolver) join(elems ...string) string { if r.isWindows() { return windowsJoin(elems...) } return unixJoin(elems...) } func (r *Resolver) isWindows() bool { return r.platform == "windows" } func (r *Resolver) isDarwin() bool { return r.platform == "darwin" } // applyRoot rebases an absolute path (in r.platform's simulated address // family) under the portable root (WithRoot), if one is set -- see // applyRootRebase for why root's own family, not r.platform's, decides // the result's family. func (r *Resolver) applyRoot(p string) string { return applyRootRebase(r.root, p, r.isWindows()) }