Commit a0787f7b authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go: replace uses of ioutil.ReadFile with renameio.ReadFile

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: default avatarJay Conrod <jayconrod@google.com>
parent bf1f4ec7
...@@ -261,7 +261,7 @@ func (c *Cache) Trim() { ...@@ -261,7 +261,7 @@ func (c *Cache) Trim() {
// We maintain in dir/trim.txt the time of the last completed 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. // If the cache has been trimmed recently enough, do nothing.
// This is the common case. // 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) t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval { if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval {
return return
......
...@@ -482,7 +482,7 @@ func readDiskCache(path, rev, suffix string) (file string, data []byte, err erro ...@@ -482,7 +482,7 @@ func readDiskCache(path, rev, suffix string) (file string, data []byte, err erro
if err != nil { if err != nil {
return "", nil, errNotCached return "", nil, errNotCached
} }
data, err = ioutil.ReadFile(file) data, err = renameio.ReadFile(file)
if err != nil { if err != nil {
return file, nil, errNotCached return file, nil, errNotCached
} }
...@@ -576,7 +576,7 @@ func rewriteVersionList(dir string) { ...@@ -576,7 +576,7 @@ func rewriteVersionList(dir string) {
buf.WriteString(v) buf.WriteString(v)
buf.WriteString("\n") buf.WriteString("\n")
} }
old, _ := ioutil.ReadFile(listFile) old, _ := renameio.ReadFile(listFile)
if bytes.Equal(buf.Bytes(), old) { if bytes.Equal(buf.Bytes(), old) {
return return
} }
......
...@@ -293,7 +293,7 @@ func initGoSum() bool { ...@@ -293,7 +293,7 @@ func initGoSum() bool {
goSum.m = make(map[module.Version][]string) goSum.m = make(map[module.Version][]string)
goSum.checked = make(map[modSum]bool) goSum.checked = make(map[modSum]bool)
data, err := ioutil.ReadFile(GoSumFile) data, err := renameio.ReadFile(GoSumFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }
...@@ -303,7 +303,7 @@ func initGoSum() bool { ...@@ -303,7 +303,7 @@ func initGoSum() bool {
// Add old go.modverify file. // Add old go.modverify file.
// We'll delete go.modverify in WriteGoSum. // We'll delete go.modverify in WriteGoSum.
alt := strings.TrimSuffix(GoSumFile, ".sum") + ".modverify" 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) migrate := make(map[module.Version][]string)
readGoSum(migrate, alt, data) readGoSum(migrate, alt, data)
for mod, sums := range migrate { for mod, sums := range migrate {
...@@ -363,7 +363,7 @@ func checkMod(mod module.Version) { ...@@ -363,7 +363,7 @@ func checkMod(mod module.Version) {
if err != nil { if err != nil {
base.Fatalf("verifying %s@%s: %v", mod.Path, mod.Version, err) 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 err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
// This can happen if someone does rm -rf GOPATH/src/cache/download. So it goes. // 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 { ...@@ -490,7 +490,7 @@ func Sum(mod module.Version) string {
if err != nil { if err != nil {
return "" return ""
} }
data, err := ioutil.ReadFile(ziphash) data, err := renameio.ReadFile(ziphash)
if err != nil { if err != nil {
return "" return ""
} }
...@@ -538,7 +538,7 @@ func WriteGoSum() { ...@@ -538,7 +538,7 @@ func WriteGoSum() {
if !goSum.overwrite { if !goSum.overwrite {
// Re-read the go.sum file to incorporate any sums added by other processes // Re-read the go.sum file to incorporate any sums added by other processes
// in the meantime. // in the meantime.
data, err := ioutil.ReadFile(GoSumFile) data, err := renameio.ReadFile(GoSumFile)
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
base.Fatalf("go: re-reading go.sum: %v", err) base.Fatalf("go: re-reading go.sum: %v", err)
} }
......
...@@ -316,7 +316,7 @@ func InitMod() { ...@@ -316,7 +316,7 @@ func InitMod() {
} }
gomod := filepath.Join(modRoot, "go.mod") gomod := filepath.Join(modRoot, "go.mod")
data, err := ioutil.ReadFile(gomod) data, err := renameio.ReadFile(gomod)
if err != nil { if err != nil {
base.Fatalf("go: %v", err) base.Fatalf("go: %v", err)
} }
...@@ -696,7 +696,7 @@ func WriteGoMod() { ...@@ -696,7 +696,7 @@ func WriteGoMod() {
defer unlock() defer unlock()
file := filepath.Join(modRoot, "go.mod") 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, modFileData) {
if bytes.Equal(old, new) { if bytes.Equal(old, new) {
// Some other process wrote the same go.mod file that we were about to write. // Some other process wrote the same go.mod file that we were about to write.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment