]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: Plan 9: keep a consistent environment array
authorAkshat Kumar <seed@mail.nanosouffle.net>
Thu, 28 Feb 2013 05:39:02 +0000 (06:39 +0100)
committerRon Minnich <rminnich@gmail.com>
Thu, 28 Feb 2013 05:39:02 +0000 (06:39 +0100)
Map order is non-deterministic. Introduce a new
environment string array that tracks the env map.
This allows us to produce identical results for
Environ() upon successive calls, as expected by the
TestConsistentEnviron test in package os.

R=rsc, ality, rminnich, bradfitz, r
CC=golang-dev
https://golang.org/cl/7411047

src/pkg/syscall/env_plan9.go

index 0f89aa9ee3847d3c8b6997a4609b4d3cc3cc2a22..9587ab5af9d21796cb7d73dba70bce3eebdd9b01 100644 (file)
@@ -15,12 +15,15 @@ var (
        // envOnce guards copyenv, which populates env.
        envOnce sync.Once
 
-       // envLock guards env.
+       // envLock guards env and envs.
        envLock sync.RWMutex
 
        // env maps from an environment variable to its value.
        env = make(map[string]string)
 
+       // envs contains elements of env in the form "key=value".
+       envs []string
+
        errZeroLengthKey = errors.New("zero length key")
        errShortWrite    = errors.New("i/o count too small")
 )
@@ -71,12 +74,16 @@ func copyenv() {
        if err != nil {
                return
        }
+       envs = make([]string, len(files))
+       i := 0
        for _, key := range files {
                v, err := readenv(key)
                if err != nil {
                        continue
                }
                env[key] = v
+               envs[i] = key + "=" + v
+               i++
        }
 }
 
@@ -96,6 +103,7 @@ func Getenv(key string) (value string, found bool) {
                return "", false
        }
        env[key] = v
+       envs = append(envs, key+"="+v)
        return v, true
 }
 
@@ -112,6 +120,7 @@ func Setenv(key, value string) error {
                return err
        }
        env[key] = value
+       envs = append(envs, key+"="+value)
        return nil
 }
 
@@ -120,6 +129,7 @@ func Clearenv() {
        defer envLock.Unlock()
 
        env = make(map[string]string)
+       envs = []string{}
        RawSyscall(SYS_RFORK, RFCENVG, 0, 0)
 }
 
@@ -128,11 +138,5 @@ func Environ() []string {
        defer envLock.RUnlock()
 
        envOnce.Do(copyenv)
-       a := make([]string, len(env))
-       i := 0
-       for k, v := range env {
-               a[i] = k + "=" + v
-               i++
-       }
-       return a
+       return append([]string(nil), envs...)
 }