From 68f496949b9524c610cd25bffd109f27b78536dc Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Tue, 23 Oct 2018 14:48:59 -0400 Subject: [PATCH] cmd/go/internal/cache: write shared mutable files atomically Updates #26794 Change-Id: I2a50e3b756ff6a2bbaee4737ca7ed053b01c8d0e Reviewed-on: https://go-review.googlesource.com/c/146378 Reviewed-by: Russ Cox --- src/cmd/go/internal/cache/cache.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/go/internal/cache/cache.go b/src/cmd/go/internal/cache/cache.go index 0cf01550ff..ab84cf6302 100644 --- a/src/cmd/go/internal/cache/cache.go +++ b/src/cmd/go/internal/cache/cache.go @@ -18,6 +18,8 @@ import ( "strconv" "strings" "time" + + "cmd/go/internal/renameio" ) // An ActionID is a cache action key, the hash of a complete description of a @@ -283,7 +285,9 @@ func (c *Cache) Trim() { c.trimSubdir(subdir, cutoff) } - ioutil.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix())), 0666) + // Ignore errors from here: if we don't write the complete timestamp, the + // cache will appear older than it is, and we'll trim it again next time. + renameio.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix()))) } // trimSubdir trims a single cache subdirectory. @@ -338,6 +342,8 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify } file := c.fileName(id, "a") if err := ioutil.WriteFile(file, entry, 0666); err != nil { + // TODO(bcmills): This Remove potentially races with another go command writing to file. + // Can we eliminate it? os.Remove(file) return err } -- 2.50.0