Skip to content

Commit

Permalink
cmd/go: replace uses of ioutil.ReadFile with renameio.ReadFile
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Jay Conrod <[email protected]>
  • Loading branch information
Bryan C. Mills committed Jun 5, 2019
1 parent bf1f4ec commit a0787f7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/cmd/go/internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/modfetch/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions src/cmd/go/internal/modfetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ""
}
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a0787f7

Please sign in to comment.