Skip to content

Commit

Permalink
Annotate some more errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mostynb committed Apr 9, 2024
1 parent 0b3da3f commit 60f00fb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
19 changes: 12 additions & 7 deletions cache/disk/casblob/casblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func WriteAndClose(zstd zstdimpl.ZstdImpl, r io.Reader, f *os.File, t Compressio

_, err = io.ReadFull(r, uncompressedChunk[0:chunkEnd])
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to read %d bytes: %w", chunkEnd+1, err)
}

compressedChunk := zstd.EncodeAll(uncompressedChunk[0:chunkEnd])
Expand All @@ -587,7 +587,7 @@ func WriteAndClose(zstd zstdimpl.ZstdImpl, r io.Reader, f *os.File, t Compressio

written, err := f.Write(compressedChunk)
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to write compressed chunk to disk: %w", err)
}

fileOffset += int64(written)
Expand All @@ -599,7 +599,7 @@ func WriteAndClose(zstd zstdimpl.ZstdImpl, r io.Reader, f *os.File, t Compressio
if err == nil {
return -1, fmt.Errorf("expected %d bytes but got at least %d more", size, bytesAfter)
} else if err != io.EOF {
return -1, err
return -1, fmt.Errorf("Failed to read chunk of size %d: %w", len(uncompressedChunk), err)
}

actualHash := hex.EncodeToString(hasher.Sum(nil))
Expand All @@ -611,18 +611,23 @@ func WriteAndClose(zstd zstdimpl.ZstdImpl, r io.Reader, f *os.File, t Compressio
// We know all the chunk offsets now, go back and fill those in.
_, err = f.Seek(chunkTableOffset, io.SeekStart)
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to seek to offset %d: %w", chunkTableOffset, err)
}

err = binary.Write(f, binary.LittleEndian, h.chunkOffsets)
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to write chunk offsets: %w", err)
}

err = f.Sync()
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to sync file: %w", err)
}

err = f.Close()
if err != nil {
return -1, fmt.Errorf("Failed to close file: %w", err)
}

return fileOffset, f.Close()
return fileOffset, nil
}
10 changes: 5 additions & 5 deletions cache/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,27 +354,27 @@ func (c *diskCache) writeAndCloseFile(r io.Reader, kind cache.EntryKind, hash st
if kind == cache.CAS && c.storageMode != casblob.Identity {
sizeOnDisk, err = casblob.WriteAndClose(c.zstd, r, f, c.storageMode, hash, size)
if err != nil {
return -1, err
return -1, fmt.Errorf("Failed to write compressed CAS blob to disk: %w", err)
}
closeFile = false
return sizeOnDisk, nil
}

if sizeOnDisk, err = io.Copy(f, r); err != nil {
return -1, err
return -1, fmt.Errorf("Failed to copy data to disk: %w", err)
}

if isSizeMismatch(sizeOnDisk, size) {
return -1, fmt.Errorf(
"sizes don't match. Expected %d, found %d", size, sizeOnDisk)
"Sizes don't match. Expected %d, found %d", size, sizeOnDisk)
}

if err = f.Sync(); err != nil {
return -1, err
return -1, fmt.Errorf("Failed to sync file to disk: %w", err)
}

if err = f.Close(); err != nil {
return -1, err
return -1, fmt.Errorf("Failed to close file: %w", err)
}
closeFile = false

Expand Down
4 changes: 3 additions & 1 deletion utils/tempfile/tempfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tempfile

import (
"errors"
"fmt"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -78,7 +79,8 @@ func (c *Creator) Create(base string, legacy bool) (*os.File, string, error) {
}

// Unexpected error.
return nil, "", err
return nil, "", fmt.Errorf("Unexpected error opening temp file: %w", err)
}

return nil, "", errNoTempfile
}

0 comments on commit 60f00fb

Please sign in to comment.