Use uuids in persist temp files to avoid race (#2494)

This commit is contained in:
Kyle Havlovitz 2016-11-09 18:22:53 -05:00 committed by James Phillips
parent d11500c8f9
commit 92ce2c9e39
1 changed files with 7 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import (
"github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/consul/structs"
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/types" "github.com/hashicorp/consul/types"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/serf/coordinate" "github.com/hashicorp/serf/coordinate"
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
) )
@ -794,7 +795,12 @@ func (a *Agent) purgeCheck(checkID types.CheckID) error {
// writeFileAtomic writes the given contents to a temporary file in the same // writeFileAtomic writes the given contents to a temporary file in the same
// directory, does an fsync and then renames the file to its real path // directory, does an fsync and then renames the file to its real path
func writeFileAtomic(path string, contents []byte) error { func writeFileAtomic(path string, contents []byte) error {
tempPath := path + ".tmp" uuid, err := uuid.GenerateUUID()
if err != nil {
return err
}
tempPath := fmt.Sprintf("%s-%s.tmp", path, uuid)
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil { if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return err return err
} }