aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/app/env.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2025-10-03 16:59:29 +0900
committerOphestra <cat@gensokyo.uk>2025-10-03 16:59:29 +0900
commita5f0aa3f3077cb7bf3cbf7ac9e707ba165aceb4a (patch)
treef75910d2147ac7c02328d229a8cb8609d2355016 /internal/app/env.go
parentdd0bb0a39104a54c26015df807c1eb113331c242 (diff)
internal/app: declutter and merge small files
This should make internal/app easier to work with for the upcoming params to shim. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/app/env.go')
-rw-r--r--internal/app/env.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/internal/app/env.go b/internal/app/env.go
new file mode 100644
index 00000000..4305c665
--- /dev/null
+++ b/internal/app/env.go
@@ -0,0 +1,59 @@
+package app
+
+import (
+ "strconv"
+
+ "hakurei.app/container"
+ "hakurei.app/hst"
+)
+
+// EnvPaths holds paths copied from the environment and is used to create [hst.Paths].
+type EnvPaths struct {
+ // TempDir is returned by [os.TempDir].
+ TempDir *container.Absolute
+ // RuntimePath is copied from $XDG_RUNTIME_DIR.
+ RuntimePath *container.Absolute
+}
+
+// Copy expands [EnvPaths] into [hst.Paths].
+func (env *EnvPaths) Copy(v *hst.Paths, userid int) {
+ if env == nil || env.TempDir == nil || v == nil {
+ panic("attempting to use an invalid EnvPaths")
+ }
+
+ v.TempDir = env.TempDir
+ v.SharePath = env.TempDir.Append("hakurei." + strconv.Itoa(userid))
+
+ if env.RuntimePath == nil {
+ // fall back to path in share since hakurei has no hard XDG dependency
+ v.RunDirPath = v.SharePath.Append("run")
+ v.RuntimePath = v.RunDirPath.Append("compat")
+ } else {
+ v.RuntimePath = env.RuntimePath
+ v.RunDirPath = env.RuntimePath.Append("hakurei")
+ }
+}
+
+// CopyPaths returns a populated [EnvPaths].
+func CopyPaths() *EnvPaths { return copyPaths(direct{}) }
+
+// copyPaths returns a populated [EnvPaths].
+func copyPaths(k syscallDispatcher) *EnvPaths {
+ const xdgRuntimeDir = "XDG_RUNTIME_DIR"
+
+ var env EnvPaths
+
+ if tempDir, err := container.NewAbs(k.tempdir()); err != nil {
+ k.fatalf("invalid TMPDIR: %v", err)
+ panic("unreachable")
+ } else {
+ env.TempDir = tempDir
+ }
+
+ r, _ := k.lookupEnv(xdgRuntimeDir)
+ if a, err := container.NewAbs(r); err == nil {
+ env.RuntimePath = a
+ }
+
+ return &env
+}