]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/cache: write shared mutable files atomically
authorBryan C. Mills <bcmills@google.com>
Tue, 23 Oct 2018 18:48:59 +0000 (14:48 -0400)
committerBryan C. Mills <bcmills@google.com>
Thu, 29 Nov 2018 15:46:07 +0000 (15:46 +0000)
Updates #26794

Change-Id: I2a50e3b756ff6a2bbaee4737ca7ed053b01c8d0e
Reviewed-on: https://go-review.googlesource.com/c/146378
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/internal/cache/cache.go

index 0cf01550ff90d0d2967a6f481982868bc20eebd5..ab84cf6302c0d1ec952db6dc0a53bfb7a84aad8e 100644 (file)
@@ -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
        }