]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: replace uses of ioutil.ReadFile with renameio.ReadFile
authorBryan C. Mills <bcmills@google.com>
Tue, 4 Jun 2019 20:50:59 +0000 (16:50 -0400)
committerBryan C. Mills <bcmills@google.com>
Wed, 5 Jun 2019 15:58:13 +0000 (15:58 +0000)
Windows does not have atomic renames; instead, it produces one of a
handful of errors in case a read races with a rename.

CL 180219 added a utility function that retries those errors in most
cases; this change updates the locations that use renameio for writes
to also use the new renameio.ReadFile function for reads.

It remains possible for a renameio.ReadFile to fail with a spurious
ERROR_FILE_NOT_FOUND, but with retries in place for the other errors
(and practical limits on write concurrency) such failures are unlikely
in practice.

Fixes #32188

Change-Id: I78c81051cc871325c1e3229e696b921b0fcd865a
Reviewed-on: https://go-review.googlesource.com/c/go/+/180517
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/cache/cache.go
src/cmd/go/internal/modfetch/cache.go
src/cmd/go/internal/modfetch/fetch.go
src/cmd/go/internal/modload/init.go

index c1d073806e31fb58b0290a7ca2f32f5efba5f149..116279c977d466041b4da1014d23e64e359b7e40 100644 (file)
@@ -261,7 +261,7 @@ func (c *Cache) Trim() {
        // We maintain in dir/trim.txt the time of the last completed cache trim.
        // If the cache has been trimmed recently enough, do nothing.
        // This is the common case.
-       data, _ := ioutil.ReadFile(filepath.Join(c.dir, "trim.txt"))
+       data, _ := renameio.ReadFile(filepath.Join(c.dir, "trim.txt"))
        t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
        if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval {
                return
index 2b2f86d96ae026727e16582f88a04c8402a96332..98d4806b61cd75091db292f8b7b3850d72b3ea56 100644 (file)
@@ -482,7 +482,7 @@ func readDiskCache(path, rev, suffix string) (file string, data []byte, err erro
        if err != nil {
                return "", nil, errNotCached
        }
-       data, err = ioutil.ReadFile(file)
+       data, err = renameio.ReadFile(file)
        if err != nil {
                return file, nil, errNotCached
        }
@@ -576,7 +576,7 @@ func rewriteVersionList(dir string) {
                buf.WriteString(v)
                buf.WriteString("\n")
        }
-       old, _ := ioutil.ReadFile(listFile)
+       old, _ := renameio.ReadFile(listFile)
        if bytes.Equal(buf.Bytes(), old) {
                return
        }
index 3b2c68b281e937afba2082cccaa3c96b8a929188..94cb0d3a199ba410b81a848ac90ced6153f3e041 100644 (file)
@@ -293,7 +293,7 @@ func initGoSum() bool {
 
        goSum.m = make(map[module.Version][]string)
        goSum.checked = make(map[modSum]bool)
-       data, err := ioutil.ReadFile(GoSumFile)
+       data, err := renameio.ReadFile(GoSumFile)
        if err != nil && !os.IsNotExist(err) {
                base.Fatalf("go: %v", err)
        }
@@ -303,7 +303,7 @@ func initGoSum() bool {
        // Add old go.modverify file.
        // We'll delete go.modverify in WriteGoSum.
        alt := strings.TrimSuffix(GoSumFile, ".sum") + ".modverify"
-       if data, err := ioutil.ReadFile(alt); err == nil {
+       if data, err := renameio.ReadFile(alt); err == nil {
                migrate := make(map[module.Version][]string)
                readGoSum(migrate, alt, data)
                for mod, sums := range migrate {
@@ -363,7 +363,7 @@ func checkMod(mod module.Version) {
        if err != nil {
                base.Fatalf("verifying %s@%s: %v", mod.Path, mod.Version, err)
        }
-       data, err := ioutil.ReadFile(ziphash)
+       data, err := renameio.ReadFile(ziphash)
        if err != nil {
                if os.IsNotExist(err) {
                        // This can happen if someone does rm -rf GOPATH/src/cache/download. So it goes.
@@ -490,7 +490,7 @@ func Sum(mod module.Version) string {
        if err != nil {
                return ""
        }
-       data, err := ioutil.ReadFile(ziphash)
+       data, err := renameio.ReadFile(ziphash)
        if err != nil {
                return ""
        }
@@ -538,7 +538,7 @@ func WriteGoSum() {
        if !goSum.overwrite {
                // Re-read the go.sum file to incorporate any sums added by other processes
                // in the meantime.
-               data, err := ioutil.ReadFile(GoSumFile)
+               data, err := renameio.ReadFile(GoSumFile)
                if err != nil && !os.IsNotExist(err) {
                        base.Fatalf("go: re-reading go.sum: %v", err)
                }
index 14fadbf74e2574a7a0e9cd835fa5359231be41fd..6f1d2cee494e35802a8c7e0735d77231df073415 100644 (file)
@@ -316,7 +316,7 @@ func InitMod() {
        }
 
        gomod := filepath.Join(modRoot, "go.mod")
-       data, err := ioutil.ReadFile(gomod)
+       data, err := renameio.ReadFile(gomod)
        if err != nil {
                base.Fatalf("go: %v", err)
        }
@@ -696,7 +696,7 @@ func WriteGoMod() {
        defer unlock()
 
        file := filepath.Join(modRoot, "go.mod")
-       old, err := ioutil.ReadFile(file)
+       old, err := renameio.ReadFile(file)
        if !bytes.Equal(old, modFileData) {
                if bytes.Equal(old, new) {
                        // Some other process wrote the same go.mod file that we were about to write.