Skip to content

Commit a0787f7

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent bf1f4ec commit a0787f7

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

src/cmd/go/internal/cache/cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (c *Cache) Trim() {
261261
// We maintain in dir/trim.txt the time of the last completed cache trim.
262262
// If the cache has been trimmed recently enough, do nothing.
263263
// This is the common case.
264-
data, _ := ioutil.ReadFile(filepath.Join(c.dir, "trim.txt"))
264+
data, _ := renameio.ReadFile(filepath.Join(c.dir, "trim.txt"))
265265
t, err := strconv.ParseInt(strings.TrimSpace(string(data)), 10, 64)
266266
if err == nil && now.Sub(time.Unix(t, 0)) < trimInterval {
267267
return

src/cmd/go/internal/modfetch/cache.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ func readDiskCache(path, rev, suffix string) (file string, data []byte, err erro
482482
if err != nil {
483483
return "", nil, errNotCached
484484
}
485-
data, err = ioutil.ReadFile(file)
485+
data, err = renameio.ReadFile(file)
486486
if err != nil {
487487
return file, nil, errNotCached
488488
}
@@ -576,7 +576,7 @@ func rewriteVersionList(dir string) {
576576
buf.WriteString(v)
577577
buf.WriteString("\n")
578578
}
579-
old, _ := ioutil.ReadFile(listFile)
579+
old, _ := renameio.ReadFile(listFile)
580580
if bytes.Equal(buf.Bytes(), old) {
581581
return
582582
}

src/cmd/go/internal/modfetch/fetch.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ func initGoSum() bool {
293293

294294
goSum.m = make(map[module.Version][]string)
295295
goSum.checked = make(map[modSum]bool)
296-
data, err := ioutil.ReadFile(GoSumFile)
296+
data, err := renameio.ReadFile(GoSumFile)
297297
if err != nil && !os.IsNotExist(err) {
298298
base.Fatalf("go: %v", err)
299299
}
@@ -303,7 +303,7 @@ func initGoSum() bool {
303303
// Add old go.modverify file.
304304
// We'll delete go.modverify in WriteGoSum.
305305
alt := strings.TrimSuffix(GoSumFile, ".sum") + ".modverify"
306-
if data, err := ioutil.ReadFile(alt); err == nil {
306+
if data, err := renameio.ReadFile(alt); err == nil {
307307
migrate := make(map[module.Version][]string)
308308
readGoSum(migrate, alt, data)
309309
for mod, sums := range migrate {
@@ -363,7 +363,7 @@ func checkMod(mod module.Version) {
363363
if err != nil {
364364
base.Fatalf("verifying %s@%s: %v", mod.Path, mod.Version, err)
365365
}
366-
data, err := ioutil.ReadFile(ziphash)
366+
data, err := renameio.ReadFile(ziphash)
367367
if err != nil {
368368
if os.IsNotExist(err) {
369369
// 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 {
490490
if err != nil {
491491
return ""
492492
}
493-
data, err := ioutil.ReadFile(ziphash)
493+
data, err := renameio.ReadFile(ziphash)
494494
if err != nil {
495495
return ""
496496
}
@@ -538,7 +538,7 @@ func WriteGoSum() {
538538
if !goSum.overwrite {
539539
// Re-read the go.sum file to incorporate any sums added by other processes
540540
// in the meantime.
541-
data, err := ioutil.ReadFile(GoSumFile)
541+
data, err := renameio.ReadFile(GoSumFile)
542542
if err != nil && !os.IsNotExist(err) {
543543
base.Fatalf("go: re-reading go.sum: %v", err)
544544
}

src/cmd/go/internal/modload/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func InitMod() {
316316
}
317317

318318
gomod := filepath.Join(modRoot, "go.mod")
319-
data, err := ioutil.ReadFile(gomod)
319+
data, err := renameio.ReadFile(gomod)
320320
if err != nil {
321321
base.Fatalf("go: %v", err)
322322
}
@@ -696,7 +696,7 @@ func WriteGoMod() {
696696
defer unlock()
697697

698698
file := filepath.Join(modRoot, "go.mod")
699-
old, err := ioutil.ReadFile(file)
699+
old, err := renameio.ReadFile(file)
700700
if !bytes.Equal(old, modFileData) {
701701
if bytes.Equal(old, new) {
702702
// Some other process wrote the same go.mod file that we were about to write.

0 commit comments

Comments
 (0)