All checks were successful
		
		
	
	Test / Create distribution (push) Successful in 34s
				
			Test / Hpkg (push) Successful in 4m3s
				
			Test / Sandbox (race detector) (push) Successful in 4m26s
				
			Test / Hakurei (race detector) (push) Successful in 5m19s
				
			Test / Sandbox (push) Successful in 1m28s
				
			Test / Hakurei (push) Successful in 2m16s
				
			Test / Flake checks (push) Successful in 1m37s
				
			This allows use of absolute pathname values without importing container. Signed-off-by: Ophestra <cat@gensokyo.uk>
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package app
 | |
| 
 | |
| import (
 | |
| 	"strconv"
 | |
| 
 | |
| 	"hakurei.app/container/check"
 | |
| 	"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 *check.Absolute
 | |
| 	// RuntimePath is copied from $XDG_RUNTIME_DIR.
 | |
| 	RuntimePath *check.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 := check.NewAbs(k.tempdir()); err != nil {
 | |
| 		k.fatalf("invalid TMPDIR: %v", err)
 | |
| 		panic("unreachable")
 | |
| 	} else {
 | |
| 		env.TempDir = tempDir
 | |
| 	}
 | |
| 
 | |
| 	r, _ := k.lookupEnv(xdgRuntimeDir)
 | |
| 	if a, err := check.NewAbs(r); err == nil {
 | |
| 		env.RuntimePath = a
 | |
| 	}
 | |
| 
 | |
| 	return &env
 | |
| }
 |