Files
core/paths/darwin.go
T

192 lines
6.5 KiB
Go
Raw Normal View History

package paths
import "yetano.dev/yetano/core"
// macOS is deliberately split by ApplicationKind (spec section 12): CLI
// and Service applications use the same XDG-flavored layout as Linux
// (unix.go, via flavor()'s default case) for consistent cross-platform
// administration; only Desktop (native GUI) applications use these
// ~/Library-based conventions.
//
// Config and Data intentionally share one canonical directory (a native
// macOS app's Application Support directory commonly holds both its
// preferences-adjacent files and its bulk data side by side); State gets
// its own subdirectory so it doesn't collide with either.
// darwinFlavor is the Linux layout CLI and Service applications use on
// macOS, except the system runtime base: macOS has no /run, its
// equivalent is /var/run.
var darwinFlavor = func() unixFlavor {
f := linuxFlavor
f.systemRuntimeBase = "/var/run"
return f
}()
func darwinAppSupportBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Application Support"
}
return unixJoin(home, "Library", "Application Support")
}
func darwinCachesBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Caches"
}
return unixJoin(home, "Library", "Caches")
}
func darwinLogsBase(scope core.Scope, home string) string {
if scope == core.SystemScope {
return "/Library/Logs"
}
return unixJoin(home, "Library", "Logs")
}
// darwinHomeIfNeeded returns "" (unused) for SystemScope, else the real
// home directory -- avoiding a spurious ErrNoHomeDirectory when
// SystemScope is resolved without HOME set.
func (r *Resolver) darwinHomeIfNeeded() (string, error) {
if r.app.Scope == core.SystemScope {
return "", nil
}
return r.homeDir()
}
func (r *Resolver) darwinDesktopAppDir() (value, error) {
if v, ok := r.overrideValue(ConfigPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinAppSupportBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopConfigDir() (value, error) {
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopDataDir() (value, error) {
// Config overrides (WithConfigDir) must not leak into DataDir, so
// DataDir consults its own override tier first.
if v, ok := r.overrideValue(DataPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
return r.darwinDesktopAppDir()
}
func (r *Resolver) darwinDesktopStateDir() (value, error) {
if v, ok := r.overrideValue(StatePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
// app.path already applied the root (see applyRootRebase), which can
// be a different address family than this simulated platform -- use
// JoinDirFile, not unixJoin, so the "State" suffix lands in whichever
// family app.path actually ended up in.
return value{path: JoinDirFile(app.path, "State"), source: app.source}, nil
}
func (r *Resolver) darwinDesktopCacheDir() (value, error) {
if v, ok := r.overrideValue(CachePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinCachesBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
func (r *Resolver) darwinDesktopLogDir() (value, error) {
if v, ok := r.overrideValue(LogPath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
home, err := r.darwinHomeIfNeeded()
if err != nil {
return value{}, err
}
source := MacOSNative
if r.app.Scope == core.SystemScope {
source = SystemDirectory
}
return value{path: r.applyRoot(unixJoin(darwinLogsBase(r.app.Scope, home), r.app.Name)), source: source}, nil
}
// darwinDesktopRuntimeDir prefers $TMPDIR -- the per-user, securely
// permissioned, OS-managed ephemeral directory macOS itself hands every
// process (confstr(_CS_DARWIN_USER_TEMP_DIR), what NSTemporaryDirectory()
// returns), the closest real native equivalent to XDG_RUNTIME_DIR's
// role. It falls back to the previous Application Support-based location
// only when TMPDIR is unset in the resolved environment (e.g. a
// simulated/test environment that never set it) -- see AGENTS.md TODO
// history: this was a placeholder before TMPDIR was wired in here.
func (r *Resolver) darwinDesktopRuntimeDir() (value, error) {
if v, ok := r.overrideValue(RuntimePath); ok {
return value{path: r.applyRoot(v.path), source: v.source}, nil
}
if tmpdir, ok := lookup(r.env, "TMPDIR"); ok {
return value{path: r.applyRoot(unixJoin(tmpdir, r.app.Name)), source: MacOSNative}, nil
}
app, err := r.darwinDesktopAppDir()
if err != nil {
return value{}, err
}
// See darwinDesktopStateDir: app.path is already rooted and may be in
// either address family, so JoinDirFile picks the right one.
return value{path: JoinDirFile(app.path, "Runtime"), source: app.source}, nil
}
// darwinDesktopBinDir has no native GUI-app convention; it reuses the
// shared Unix bin directory so a Desktop app that also ships a CLI shim
// still has a sensible, documented answer.
func (r *Resolver) darwinDesktopBinDir() (value, error) {
return r.binDirUnix()
}
func (r *Resolver) darwinDesktopConfigLayers() ([]layer, error) {
return r.darwinDesktopLayers(ConfigPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopDataLayers() ([]layer, error) {
return r.darwinDesktopLayers(DataPath, darwinAppSupportBase)
}
func (r *Resolver) darwinDesktopLayers(kind PathKind, base func(core.Scope, string) string) ([]layer, error) {
if v, ok := r.overrideValue(kind); ok {
return []layer{{path: r.applyRoot(v.path), source: v.source, priority: PriorityUser, canonical: true}}, nil
}
var user []layer
home, err := r.homeDir()
if err := skipMissingHome(err); err != nil {
return nil, err
}
if err == nil {
user = []layer{{
path: r.applyRoot(unixJoin(base(core.UserScope, home), r.app.Name)),
source: MacOSNative,
priority: PriorityUser,
canonical: true,
}}
}
system := []layer{{
path: r.applyRoot(unixJoin(base(core.SystemScope, ""), r.app.Name)),
source: SystemDirectory,
priority: PrioritySystem,
canonical: r.app.Scope == core.SystemScope,
}}
return composeSearch(user, system, r.app.Scope), nil
}