| 1 | package sandbox |
| 2 | |
| 3 | import "runtime" |
| 4 | |
| 5 | // SessionTempEnvKeys are the standard temporary-directory environment variables |
| 6 | // Reasonix overrides for session-private temporary directories. |
| 7 | var SessionTempEnvKeys = []string{"TMPDIR", "TMP", "TEMP"} |
| 8 | |
| 9 | // SessionTempEnv returns KEY=value overrides for the session-private temporary |
| 10 | // directory. When linuxSandboxed is true (Linux bwrap with SessionTemp bound at |
| 11 | // /tmp), the variables point at the virtual /tmp path so scripts using $TMPDIR |
| 12 | // and /tmp agree. Otherwise they point at the host private directory. |
| 13 | // |
| 14 | // An empty sessionTemp yields nil (no overrides). |
| 15 | func SessionTempEnv(sessionTemp string, linuxSandboxed bool) []string { |
| 16 | if sessionTemp == "" { |
| 17 | return nil |
| 18 | } |
| 19 | value := sessionTemp |
| 20 | if linuxSandboxed && runtime.GOOS == "linux" { |
| 21 | value = "/tmp" |
| 22 | } |
| 23 | out := make([]string, 0, len(SessionTempEnvKeys)) |
| 24 | for _, key := range SessionTempEnvKeys { |
| 25 | out = append(out, key+"="+value) |
| 26 | } |
| 27 | return out |
| 28 | } |
| 29 | |
| 30 | // SessionTempEnvMap is SessionTempEnv as a name→value map for ACP terminal/create. |
| 31 | func SessionTempEnvMap(sessionTemp string, linuxSandboxed bool) map[string]string { |
| 32 | pairs := SessionTempEnv(sessionTemp, linuxSandboxed) |
| 33 | if len(pairs) == 0 { |
| 34 | return nil |
| 35 | } |
| 36 | out := make(map[string]string, len(pairs)) |
| 37 | for _, kv := range pairs { |
| 38 | for i := 0; i < len(kv); i++ { |
| 39 | if kv[i] == '=' { |
| 40 | out[kv[:i]] = kv[i+1:] |
| 41 | break |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | return out |
| 46 | } |
| 47 |