diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-06-20 02:42:35 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-06-20 02:42:35 +0900 |
| commit | 58ce1347188b94743ea05a1492c53d53d0e4fa91 (patch) | |
| tree | 6b5e93b7620096dc6ae1a96bcbaf2a58ca66fb50 /internal/env | |
| parent | 2066093343c34a7b05d7fb0f1082ce513f2b4c0f (diff) | |
internal/outcome: attempt nscd path-hiding if present
This avoids creating the mount point on musl setups which accomplishes nothing and can run into permission problems.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/env')
| -rw-r--r-- | internal/env/env.go | 28 | ||||
| -rw-r--r-- | internal/env/env_test.go | 5 |
2 files changed, 28 insertions, 5 deletions
diff --git a/internal/env/env.go b/internal/env/env.go index 45fd5fcb..0c413733 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -1,21 +1,29 @@ -// Package env provides the [Paths] struct for efficiently building paths from the environment. +// Package env provides the [Paths] struct for efficiently building paths from +// the environment. package env import ( + "errors" + "io/fs" "log" "os" "strconv" "hakurei.app/check" + "hakurei.app/fhs" "hakurei.app/hst" ) +const VarRunNscd = fhs.Var + "run/nscd" + // Paths holds paths copied from the environment and is used to create [hst.Paths]. type Paths struct { // TempDir is returned by [os.TempDir]. TempDir *check.Absolute // RuntimePath is copied from $XDG_RUNTIME_DIR. RuntimePath *check.Absolute + // Whether [VarRunNscd] is a directory. + HasNscd bool } // Copy expands [Paths] into [hst.Paths]. @@ -37,14 +45,17 @@ func (env *Paths) Copy(v *hst.Paths, userid int) { } // CopyPaths returns a populated [Paths]. -func CopyPaths() *Paths { return CopyPathsFunc(log.Fatalf, os.TempDir, os.Getenv) } +func CopyPaths() *Paths { + return CopyPathsFunc(log.Fatalf, os.TempDir, os.Getenv, os.Stat) +} -// CopyPathsFunc returns a populated [Paths], -// using the provided [log.Fatalf], [os.TempDir], [os.Getenv] functions. +// CopyPathsFunc returns a populated [Paths], using the provided [log.Fatalf], +// [os.TempDir], [os.Getenv] functions. func CopyPathsFunc( fatalf func(format string, v ...any), tempdir func() string, getenv func(key string) string, + stat func(name string) (fs.FileInfo, error), ) *Paths { const xdgRuntimeDir = "XDG_RUNTIME_DIR" @@ -61,5 +72,14 @@ func CopyPathsFunc( env.RuntimePath = a } + if fi, err := stat(VarRunNscd); err != nil { + if !errors.Is(err, fs.ErrNotExist) { + fatalf("%v", err) + panic("unreachable") + } + } else { + env.HasNscd = fi.IsDir() + } + return &env } diff --git a/internal/env/env_test.go b/internal/env/env_test.go index 0e5db4a9..cce97da0 100644 --- a/internal/env/env_test.go +++ b/internal/env/env_test.go @@ -2,6 +2,7 @@ package env_test import ( "fmt" + "io/fs" "reflect" "testing" @@ -104,7 +105,9 @@ func TestCopyPaths(t *testing.T) { t.Fatalf("fatalf: %q, want %q", got, tc.fatal) } panic(stub.PanicExit) - }, func() string { return tc.tmp }, func(key string) string { return tc.env[key] }) + }, func() string { return tc.tmp }, func(key string) string { return tc.env[key] }, func(name string) (fs.FileInfo, error) { + return nil, fs.ErrNotExist + }) if tc.fatal != "" { t.Fatalf("copyPaths: expected fatal %q", tc.fatal) |
