Skip to content

Commit 65e3726

Browse files
committed
chore: revert minor changes
1 parent a79737e commit 65e3726

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

internal/cache/cache.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ type Cache struct {
5050
// to share a cache directory (for example, if the directory were stored
5151
// in a network file system). File locking is notoriously unreliable in
5252
// network file systems and may not suffice to protect the cache.
53+
//
5354
func Open(dir string) (*Cache, error) {
5455
info, err := os.Stat(dir)
5556
if err != nil {
5657
return nil, err
5758
}
5859
if !info.IsDir() {
59-
return nil, &os.PathError{Op: "open", Path: dir, Err: errors.New("not a directory")}
60+
return nil, &os.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")}
6061
}
6162
for i := 0; i < 256; i++ {
6263
name := filepath.Join(dir, fmt.Sprintf("%02x", i))
@@ -165,7 +166,7 @@ func (c *Cache) get(id ActionID) (Entry, error) {
165166
eid, entry := entry[3:3+hexSize], entry[3+hexSize:]
166167
eout, entry := entry[1:1+hexSize], entry[1+hexSize:]
167168
esize, entry := entry[1:1+20], entry[1+20:]
168-
etime := entry[1 : 1+20]
169+
etime, entry := entry[1:1+20], entry[1+20:]
169170
var buf [HashSize]byte
170171
if _, err = hex.Decode(buf[:], eid); err != nil || buf != id {
171172
return failed(fmt.Errorf("failed to hex decode eid data in %s: %w", fileName, err))
@@ -255,7 +256,7 @@ const (
255256
// and to reduce the amount of disk activity caused by using
256257
// cache entries, used only updates the mtime if the current
257258
// mtime is more than an hour old. This heuristic eliminates
258-
// nearly all the mtime updates that would otherwise happen,
259+
// nearly all of the mtime updates that would otherwise happen,
259260
// while still keeping the mtimes useful for cache trimming.
260261
func (c *Cache) used(file string) error {
261262
info, err := os.Stat(file)
@@ -301,15 +302,15 @@ func (c *Cache) Trim() {
301302

302303
// Ignore errors from here: if we don't write the complete timestamp, the
303304
// cache will appear older than it is, and we'll trim it again next time.
304-
_ = renameio.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix())), 0666)
305+
renameio.WriteFile(filepath.Join(c.dir, "trim.txt"), []byte(fmt.Sprintf("%d", now.Unix())), 0666)
305306
}
306307

307308
// trimSubdir trims a single cache subdirectory.
308309
func (c *Cache) trimSubdir(subdir string, cutoff time.Time) {
309310
// Read all directory entries from subdir before removing
310311
// any files, in case removing files invalidates the file offset
311312
// in the directory scan. Also, ignore error from f.Readdirnames,
312-
// because we don't care about reporting the error, and we still
313+
// because we don't care about reporting the error and we still
313314
// want to process any entries found before the error.
314315
f, err := os.Open(subdir)
315316
if err != nil {
@@ -346,7 +347,6 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
346347
// are entirely reproducible. As just noted, this may be unrealistic
347348
// in some cases but the check is also useful for shaking out real bugs.
348349
entry := fmt.Sprintf("v1 %x %x %20d %20d\n", id, out, size, time.Now().UnixNano())
349-
350350
if verify && allowVerify {
351351
old, err := c.get(id)
352352
if err == nil && (old.OutputID != out || old.Size != size) {
@@ -368,7 +368,7 @@ func (c *Cache) putIndexEntry(id ActionID, out OutputID, size int64, allowVerify
368368
// Truncate the file only *after* writing it.
369369
// (This should be a no-op, but truncate just in case of previous corruption.)
370370
//
371-
// This differs from os.WriteFile, which truncates to 0 *before* writing
371+
// This differs from ioutil.WriteFile, which truncates to 0 *before* writing
372372
// via os.O_TRUNC. Truncating only after writing ensures that a second write
373373
// of the same content to the same file is idempotent, and does not — even
374374
// temporarily! — undo the effect of the first write.
@@ -502,7 +502,7 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error {
502502
sum := h.Sum(nil)
503503
if !bytes.Equal(sum, out[:]) {
504504
_ = f.Truncate(0)
505-
return errors.New("file content changed underfoot")
505+
return fmt.Errorf("file content changed underfoot")
506506
}
507507

508508
// Commit cache file entry.

internal/cache/cache_test.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"encoding/binary"
1010
"fmt"
11+
"io/ioutil"
1112
"os"
1213
"path/filepath"
1314
"testing"
@@ -19,9 +20,7 @@ func init() {
1920
}
2021

2122
func TestBasic(t *testing.T) {
22-
t.Parallel()
23-
24-
dir, err := os.MkdirTemp("", "cachetest-")
23+
dir, err := ioutil.TempDir("", "cachetest-")
2524
if err != nil {
2625
t.Fatal(err)
2726
}
@@ -66,9 +65,7 @@ func TestBasic(t *testing.T) {
6665
}
6766

6867
func TestGrowth(t *testing.T) {
69-
t.Parallel()
70-
71-
dir, err := os.MkdirTemp("", "cachetest-")
68+
dir, err := ioutil.TempDir("", "cachetest-")
7269
if err != nil {
7370
t.Fatal(err)
7471
}
@@ -81,7 +78,7 @@ func TestGrowth(t *testing.T) {
8178

8279
n := 10000
8380
if testing.Short() {
84-
n = 1000
81+
n = 10
8582
}
8683

8784
for i := 0; i < n; i++ {
@@ -121,7 +118,7 @@ func TestVerifyPanic(t *testing.T) {
121118
t.Fatal("initEnv did not set verify")
122119
}
123120

124-
dir, err := os.MkdirTemp("", "cachetest-")
121+
dir, err := ioutil.TempDir("", "cachetest-")
125122
if err != nil {
126123
t.Fatal(err)
127124
}
@@ -154,9 +151,7 @@ func dummyID(x int) [HashSize]byte {
154151
}
155152

156153
func TestCacheTrim(t *testing.T) {
157-
t.Parallel()
158-
159-
dir, err := os.MkdirTemp("", "cachetest-")
154+
dir, err := ioutil.TempDir("", "cachetest-")
160155
if err != nil {
161156
t.Fatal(err)
162157
}
@@ -212,7 +207,7 @@ func TestCacheTrim(t *testing.T) {
212207
t.Fatal(err)
213208
}
214209
c.OutputFile(entry.OutputID)
215-
data, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
210+
data, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
216211
if err != nil {
217212
t.Fatal(err)
218213
}
@@ -225,15 +220,15 @@ func TestCacheTrim(t *testing.T) {
225220
t.Fatal(err)
226221
}
227222
c.OutputFile(entry.OutputID)
228-
data2, err := os.ReadFile(filepath.Join(dir, "trim.txt"))
223+
data2, err := ioutil.ReadFile(filepath.Join(dir, "trim.txt"))
229224
if err != nil {
230225
t.Fatal(err)
231226
}
232227
if !bytes.Equal(data, data2) {
233228
t.Fatalf("second trim did work: %q -> %q", data, data2)
234229
}
235230

236-
// Fast-forward and do another trim just before the 5-day cutoff.
231+
// Fast forward and do another trim just before the 5 day cutoff.
237232
// Note that because of usedQuantum the cutoff is actually 5 days + 1 hour.
238233
// We used c.Get(id) just now, so 5 days later it should still be kept.
239234
// On the other hand almost a full day has gone by since we wrote dummyID(2)

internal/cache/hash_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package cache
66

77
import (
88
"fmt"
9+
"io/ioutil"
910
"os"
1011
"testing"
1112
)
@@ -27,7 +28,7 @@ func TestHash(t *testing.T) {
2728
}
2829

2930
func TestHashFile(t *testing.T) {
30-
f, err := os.CreateTemp("", "cmd-go-test-")
31+
f, err := ioutil.TempFile("", "cmd-go-test-")
3132
if err != nil {
3233
t.Fatal(err)
3334
}

0 commit comments

Comments
 (0)