Skip to content

Commit edaed60

Browse files
committed
Fix some golangci-lint errors in cache/disk
1 parent 4ae989d commit edaed60

File tree

6 files changed

+100
-86
lines changed

6 files changed

+100
-86
lines changed

cache/disk/disk.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (c *diskCache) updateCacheAgeMetric() {
175175
func (c *diskCache) getElementPath(key Key, value lruItem) string {
176176
ks := key.(string)
177177
hash := ks[len(ks)-sha256.Size*2:]
178-
var kind cache.EntryKind = cache.AC
178+
var kind = cache.AC
179179
if strings.HasPrefix(ks, "cas") {
180180
kind = cache.CAS
181181
} else if strings.HasPrefix(ks, "ac") {
@@ -277,12 +277,15 @@ func (c *diskCache) Put(ctx context.Context, kind cache.EntryKind, hash string,
277277
defer func() {
278278
// No lock required to remove stray tempfiles.
279279
if removeTempfile {
280-
os.Remove(blobFile)
280+
err := os.Remove(blobFile)
281+
if err != nil {
282+
log.Printf("warning: failed to remove temp file: %q", blobFile)
283+
}
281284
} else if blobFile != "" {
282285
// Mark the file as "complete".
283286
err := os.Chmod(blobFile, tempfile.FinalMode)
284287
if err != nil {
285-
log.Println("Failed to mark", blobFile, "as complete:", err)
288+
log.Println("failed to mark", blobFile, "as complete:", err)
286289
}
287290
}
288291

@@ -358,7 +361,7 @@ func (c *diskCache) writeAndCloseFile(ctx context.Context, r io.Reader, kind cac
358361
closeFile := true
359362
defer func() {
360363
if closeFile {
361-
f.Close()
364+
_ = f.Close()
362365
}
363366
}()
364367

@@ -386,17 +389,17 @@ func (c *diskCache) writeAndCloseFile(ctx context.Context, r io.Reader, kind cac
386389

387390
if isSizeMismatch(sizeOnDisk, size) {
388391
return -1, fmt.Errorf(
389-
"Sizes don't match. Expected %d, found %d", size, sizeOnDisk)
392+
"sizes don't match. Expected %d, found %d", size, sizeOnDisk)
390393
}
391394

392395
err = f.Sync()
393396
if err != nil {
394-
return -1, fmt.Errorf("Failed to sync file to disk: %w", err)
397+
return -1, fmt.Errorf("failed to sync file to disk: %w", err)
395398
}
396399

397400
err = writeCloser.Close()
398401
if err != nil {
399-
return -1, fmt.Errorf("Failed to verify hash: %w", err)
402+
return -1, fmt.Errorf("failed to verify hash: %w", err)
400403
}
401404

402405
closeFile = false
@@ -499,15 +502,15 @@ func (c *diskCache) availableOrTryProxy(kind cache.EntryKind, hash string, size
499502

500503
if err != nil {
501504
log.Printf("Warning: expected item to be on disk, but something happened when retrieving %s (compressed: %v, legacy: %v): %v", blobPath, item.legacy, zstd, err)
502-
f.Close()
505+
_ = f.Close()
503506
} else {
504507
return rc, item.size, false, nil
505508
}
506509
} else {
507510
var fileInfo os.FileInfo
508511
fileInfo, err = f.Stat()
509512
if err != nil {
510-
f.Close()
513+
_ = f.Close()
511514
return nil, -1, true, err
512515
}
513516
foundSize := fileInfo.Size()
@@ -612,12 +615,15 @@ func (c *diskCache) get(ctx context.Context, kind cache.EntryKind, hash string,
612615
defer func() {
613616
// No lock required to remove stray tempfiles.
614617
if removeTempfile {
615-
os.Remove(blobFile)
618+
err := os.Remove(blobFile)
619+
if err != nil {
620+
log.Printf("warning: failed to remove temp file: %q", blobFile)
621+
}
616622
} else if blobFile != "" {
617623
// Mark the file as "complete".
618624
err := os.Chmod(blobFile, tempfile.FinalMode)
619625
if err != nil {
620-
log.Println("Failed to mark", blobFile, "as complete:", err)
626+
log.Println("failed to mark", blobFile, "as complete:", err)
621627
}
622628
}
623629

@@ -664,7 +670,7 @@ func (c *diskCache) get(ctx context.Context, kind cache.EntryKind, hash string,
664670

665671
r, foundSize, err := c.proxy.Get(ctx, kind, hash, size)
666672
if r != nil {
667-
defer r.Close()
673+
defer func() { _ = r.Close() }()
668674
}
669675
if err != nil {
670676
return nil, -1, internalErr(err)
@@ -673,7 +679,7 @@ func (c *diskCache) get(ctx context.Context, kind cache.EntryKind, hash string,
673679
return nil, -1, nil
674680
}
675681
if foundSize > c.maxProxyBlobSize {
676-
r.Close()
682+
_ = r.Close()
677683
return nil, -1, nil
678684
}
679685

@@ -694,7 +700,7 @@ func (c *diskCache) get(ctx context.Context, kind cache.EntryKind, hash string,
694700

695701
var sizeOnDisk int64
696702
sizeOnDisk, err = io.Copy(tf, r)
697-
tf.Close()
703+
_ = tf.Close()
698704
if err != nil {
699705
return nil, -1, internalErr(err)
700706
}
@@ -731,7 +737,7 @@ func (c *diskCache) get(ctx context.Context, kind cache.EntryKind, hash string,
731737

732738
unreserve, removeTempfile, err = c.commit(key, legacy, blobFile, size, foundSize, sizeOnDisk, random)
733739
if err != nil {
734-
rc.Close()
740+
_ = rc.Close()
735741
return nil, -1, internalErr(err)
736742
}
737743

@@ -806,7 +812,7 @@ func isSizeMismatch(requestedSize int64, foundSize int64) bool {
806812
func (c *diskCache) GetValidatedActionResult(ctx context.Context, hash string) (*pb.ActionResult, []byte, error) {
807813
rc, sizeBytes, err := c.Get(ctx, cache.AC, hash, -1, 0)
808814
if rc != nil {
809-
defer rc.Close()
815+
defer func() { _ = rc.Close() }()
810816
}
811817
if err != nil {
812818
return nil, nil, err
@@ -849,18 +855,18 @@ func (c *diskCache) GetValidatedActionResult(ctx context.Context, hash string) (
849855
return nil, nil, err // aka "not found", or an err if non-nil
850856
}
851857
if err != nil {
852-
r.Close()
858+
_ = r.Close()
853859
return nil, nil, err
854860
}
855861
if size != d.TreeDigest.SizeBytes {
856-
r.Close()
862+
_ = r.Close()
857863
return nil, nil, fmt.Errorf("expected %d bytes, found %d",
858864
d.TreeDigest.SizeBytes, size)
859865
}
860866

861867
var oddata []byte
862868
oddata, err = io.ReadAll(r)
863-
r.Close()
869+
_ = r.Close()
864870
if err != nil {
865871
return nil, nil, err
866872
}

0 commit comments

Comments
 (0)