From 92ce2c9e39e82985630ddfeff0a5a0e2c96fd535 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Wed, 9 Nov 2016 18:22:53 -0500 Subject: [PATCH] Use uuids in persist temp files to avoid race (#2494) --- command/agent/agent.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/command/agent/agent.go b/command/agent/agent.go index 23137f275e..54e2b0c9ee 100644 --- a/command/agent/agent.go +++ b/command/agent/agent.go @@ -20,6 +20,7 @@ import ( "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/types" + "github.com/hashicorp/go-uuid" "github.com/hashicorp/serf/coordinate" "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 // directory, does an fsync and then renames the file to its real path 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 { return err }