Clean up truncated and partial files on failed CAS downloads (#674)

When streaming a blob to a file in readBlobToFileUnder or ReadBlobToFile,
the target file is opened with os.O_WRONLY|os.O_CREATE|os.O_TRUNC before
streaming begins. Similarly, in downloadNonUnified, batch output files
were written before checking whether BatchDownloadBlobsWithStats returned
an error, and single-file downloads used the parent ctx instead of the
errgroup's eCtx. If a download fails (for example, after exhausting
retries with codes.Unavailable during a CAS outage or receiving a partial
stream that fails digest verification), an empty (0-length) or partially
written file was left behind on disk, corrupting incremental builds
(b/553667080).

This change fixes truncated/partial file handling in CAS downloads:
1. Explicitly close the destination file (checking closeErr) and remove
   the target file on error in ReadBlobToFile and readBlobToFileUnder so
   0-byte or partial files are never left on disk.
2. Check BatchDownloadBlobsWithStats errors immediately before writing
   files in downloadNonUnified, verify digest presence in bchMap before
   dereferencing batch results, and pass eCtx to readBlobToFileUnder so
   in-flight streams abort and clean up promptly on sibling errors.

UnifiedDownloads=true error handling and DownloadFiles sibling
cancellation are tracked separately in #676.
3 files changed
tree: 2b2414e4342ea278500dfc068882eb7b195a7683
  1. .bazelci/
  2. .githooks/
  3. .github/
  4. external/
  5. go/
  6. .bazelrc
  7. .bazelversion
  8. .gitignore
  9. .golangci.yml
  10. AUTHORS
  11. BUILD.bazel
  12. check-gofmt.sh
  13. check-golint.sh
  14. CONTRIBUTING.md
  15. CONTRIBUTORS
  16. go.mod
  17. go.sum
  18. LICENSE
  19. MODULE.bazel
  20. MODULE.bazel.lock
  21. README.md
  22. setup-githooks.sh
README.md

Remote Execution API SDKs

CI status: Build Status

PkgGoDev

This repository contains SDKs for the Remote Execution API.

See each language subdirectory's README.md for more specific instructions on using the SDK for that language.