Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always close file after hashing #53

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions cli/cache/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,12 @@ func DownloadOrCache(cacheKey string, hash string, url string, updates chan<- ut
func downloadInternal(cacheKey string, location string, hash string, url string, updates chan<- utils.GenericProgress, downloadSemaphore chan int) (int64, error) {
stat, err := os.Stat(location)
if err == nil {
existingHash := ""

if hash != "" {
f, err := os.Open(location)
if err != nil {
return 0, fmt.Errorf("failed to open file: %s: %w", location, err)
}
defer f.Close()

existingHash, err = utils.SHA256Data(f)
if err != nil {
return 0, fmt.Errorf("could not compute hash for file: %s: %w", location, err)
}
matches, err := compareHash(hash, location)
if err != nil {
return 0, err
}

if hash == existingHash {
if matches {
return stat.Size(), nil
}

Expand Down Expand Up @@ -222,3 +212,22 @@ func downloadInternal(cacheKey string, location string, hash string, url string,

return resp.ContentLength, nil
}

func compareHash(hash string, location string) (bool, error) {
existingHash := ""

if hash != "" {
f, err := os.Open(location)
if err != nil {
return false, fmt.Errorf("failed to open file: %s: %w", location, err)
}
defer f.Close()

existingHash, err = utils.SHA256Data(f)
if err != nil {
return false, fmt.Errorf("could not compute hash for file: %s: %w", location, err)
}
}

return hash == existingHash, nil
}
Loading