@@ -18,6 +18,7 @@ import (
1818 "crypto/sha256"
1919 "fmt"
2020 "io"
21+ "log/slog"
2122 "os"
2223 "path/filepath"
2324)
@@ -66,23 +67,29 @@ func RepoDir(repo, commit, expectedSHA256 string) (string, error) {
6667 }
6768
6869 tgz := tarballPath (cacheDir , repo , commit )
69- outdir := filepath .Join (cacheDir , fmt .Sprintf ("%s@%s" , repo , commit ))
70+ outDir := filepath .Join (cacheDir , fmt .Sprintf ("%s@%s" , repo , commit ))
7071
7172 // Step 1: Check if extracted directory exists and contains files.
7273 if cached , err := extractedDir (cacheDir , repo , commit ); err == nil {
7374 return cached , nil
7475 }
7576
7677 // Step 2: Check if tarball exists. Verify its SHA256 matches expectedSHA256.
77- // If hash doesn't match, fall through to re-download.
78+ // If hash doesn't match or any error happens during the extraction, delete
79+ // the tarball and fall through to re-download.
7880 if _ , err := os .Stat (tgz ); err == nil {
7981 sha , err := computeSHA256 (tgz )
80- if err == nil && sha == expectedSHA256 {
81- if err := os .MkdirAll (outdir , 0755 ); err != nil {
82- return "" , fmt .Errorf ("failed creating %q: %w" , outdir , err )
82+ if err == nil {
83+ if sha == expectedSHA256 {
84+ if err := os .MkdirAll (outDir , 0755 ); err != nil {
85+ return "" , fmt .Errorf ("failed creating %q: %w" , outDir , err )
86+ }
87+ if err := ExtractTarball (tgz , outDir ); err == nil {
88+ return outDir , nil
89+ }
8390 }
84- if err := ExtractTarball (tgz , outdir ); err = = nil {
85- return outdir , nil
91+ if err := os . Remove (tgz ); err ! = nil {
92+ slog . Debug ( "failed to remove tarball" , "path" , tgz , "err" , err )
8693 }
8794 }
8895 }
@@ -92,16 +99,16 @@ func RepoDir(repo, commit, expectedSHA256 string) (string, error) {
9299 if err := os .MkdirAll (filepath .Dir (tgz ), 0755 ); err != nil {
93100 return "" , fmt .Errorf ("failed creating %q: %w" , filepath .Dir (tgz ), err )
94101 }
95- if err := os .MkdirAll (outdir , 0755 ); err != nil {
96- return "" , fmt .Errorf ("failed creating %q: %w" , outdir , err )
102+ if err := os .MkdirAll (outDir , 0755 ); err != nil {
103+ return "" , fmt .Errorf ("failed creating %q: %w" , outDir , err )
97104 }
98105 if err := DownloadTarball (tgz , sourceURL , expectedSHA256 ); err != nil {
99106 return "" , err
100107 }
101- if err := ExtractTarball (tgz , outdir ); err != nil {
108+ if err := ExtractTarball (tgz , outDir ); err != nil {
102109 return "" , fmt .Errorf ("failed to extract tarball: %w" , err )
103110 }
104- return outdir , nil
111+ return outDir , nil
105112}
106113
107114// cacheDir returns the root cache directory for librarian operations. It
0 commit comments