Fix race condition writing soong.config am: d8f2014d3b

am: a0411a2cf6

Change-Id: Ic477b3640d32d3706e0adee785164be6d3c545d0
This commit is contained in:
Colin Cross 2016-11-03 19:30:37 +00:00 committed by android-build-merger
commit 22c3dc826b

View file

@ -17,6 +17,7 @@ package android
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@ -127,28 +128,34 @@ func loadFromConfigFile(configurable jsonConfigurable, filename string) error {
return nil
}
// atomically writes the config file in case two copies of soong_build are running simultaneously
// (for example, docs generation and ninja manifest generation)
func saveToConfigFile(config jsonConfigurable, filename string) error {
data, err := json.MarshalIndent(&config, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal config data: %s", err.Error())
}
configFileWriter, err := os.Create(filename)
f, err := ioutil.TempFile(filepath.Dir(filename), "config")
if err != nil {
return fmt.Errorf("cannot create empty config file %s: %s\n", filename, err.Error())
}
defer configFileWriter.Close()
defer os.Remove(f.Name())
defer f.Close()
_, err = configFileWriter.Write(data)
_, err = f.Write(data)
if err != nil {
return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
}
_, err = configFileWriter.WriteString("\n")
_, err = f.WriteString("\n")
if err != nil {
return fmt.Errorf("default config file: %s could not be written: %s", filename, err.Error())
}
f.Close()
os.Rename(f.Name(), filename)
return nil
}