fix(runtime): redirect child TEMP/TMP/TMPDIR into DataRoot/tmp
This commit is contained in:
1 parent
c4df14c785
commit
f601236f74
3 files changed
+63
No files matched your search
@@ -188,6 +188,7 @@ func (c *Config) XDGConfigDir() string { return filepath.Join(c.DataRoot, "confi
|
||||
func (c *Config) XDGDataDir() string { return filepath.Join(c.DataRoot, "data") }
|
||||
func (c *Config) XDGCacheDir() string { return filepath.Join(c.DataRoot, "cache") }
|
||||
func (c *Config) ImagesDir() string { return filepath.Join(c.DataRoot, "images") }
|
||||
func (c *Config) TempDir() string { return filepath.Join(c.DataRoot, "tmp") }
|
||||
|
||||
func (c *Config) EnsureDirs() error {
|
||||
for _, d := range []string{
|
||||
@@ -197,6 +198,7 @@ func (c *Config) EnsureDirs() error {
|
||||
c.XDGDataDir(),
|
||||
c.XDGCacheDir(),
|
||||
c.ImagesDir(),
|
||||
c.TempDir(),
|
||||
} {
|
||||
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Nwflower/reims-go/internal/config"
|
||||
)
|
||||
|
||||
func TestBuildChildEnvRedirectsTemp(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := &config.Config{DataRoot: root}
|
||||
rt := Installed{
|
||||
Dir: filepath.Join(root, "runtimes", "3731afd"),
|
||||
}
|
||||
|
||||
t.Setenv("TEMP", `C:\Users\中文用户\AppData\Local\Temp`)
|
||||
t.Setenv("TMP", `C:\Users\中文用户\AppData\Local\Temp`)
|
||||
t.Setenv("TMPDIR", `C:\Users\中文用户\AppData\Local\Temp`)
|
||||
|
||||
env := BuildChildEnv(cfg, rt)
|
||||
wantTmp := cfg.TempDir()
|
||||
|
||||
if info, err := os.Stat(wantTmp); err != nil || !info.IsDir() {
|
||||
t.Fatalf("expected TempDir %q to exist: err=%v", wantTmp, err)
|
||||
}
|
||||
|
||||
counts := map[string]int{}
|
||||
values := map[string]string{}
|
||||
for _, kv := range env {
|
||||
idx := strings.IndexByte(kv, '=')
|
||||
if idx <= 0 {
|
||||
continue
|
||||
}
|
||||
k := strings.ToUpper(kv[:idx])
|
||||
v := kv[idx+1:]
|
||||
if k == "TEMP" || k == "TMP" || k == "TMPDIR" {
|
||||
counts[k]++
|
||||
values[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
for _, key := range []string{"TEMP", "TMP", "TMPDIR"} {
|
||||
if counts[key] != 1 {
|
||||
t.Errorf("%s appeared %d times in child env, want 1", key, counts[key])
|
||||
}
|
||||
if values[key] != wantTmp {
|
||||
t.Errorf("%s = %q, want %q", key, values[key], wantTmp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,11 +158,19 @@ func ResolveActive(cfg *config.Config) (*Installed, error) {
|
||||
// BuildChildEnv returns a clean, D-drive-isolated environment for invoking reims-ctl.
|
||||
func BuildChildEnv(cfg *config.Config, rt Installed) []string {
|
||||
binDir := filepath.Join(rt.Dir, "bin")
|
||||
tmpDir := cfg.TempDir()
|
||||
_ = os.MkdirAll(tmpDir, 0o755)
|
||||
// Redirect TEMP/TMP/TMPDIR into DataRoot so QEMU's snapshot=on temporary
|
||||
// overlay does not write into C:\Users\<non-ASCII>\AppData\Local\Temp
|
||||
// (MinGW QEMU's open() fails with ENOENT on non-ASCII Windows usernames).
|
||||
overrides := map[string]string{
|
||||
"XDG_CONFIG_HOME": cfg.XDGConfigDir(),
|
||||
"XDG_DATA_HOME": cfg.XDGDataDir(),
|
||||
"XDG_CACHE_HOME": cfg.XDGCacheDir(),
|
||||
"REIMS_CTL_QEMU": rt.QemuPath(),
|
||||
"TEMP": tmpDir,
|
||||
"TMP": tmpDir,
|
||||
"TMPDIR": tmpDir,
|
||||
}
|
||||
env := os.Environ()
|
||||
out := make([]string, 0, len(env)+len(overrides))
|
||||
|
||||
Reference in new issue
Block a user