fix: always close file after hashing (#53)
This commit is contained in:
parent
e313efdfec
commit
da61008e32
1 changed files with 23 additions and 14 deletions
35
cli/cache/download.go
vendored
35
cli/cache/download.go
vendored
|
@ -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) {
|
func downloadInternal(cacheKey string, location string, hash string, url string, updates chan<- utils.GenericProgress, downloadSemaphore chan int) (int64, error) {
|
||||||
stat, err := os.Stat(location)
|
stat, err := os.Stat(location)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
existingHash := ""
|
matches, err := compareHash(hash, location)
|
||||||
|
|
||||||
if hash != "" {
|
|
||||||
f, err := os.Open(location)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, fmt.Errorf("failed to open file: %s: %w", location, err)
|
return 0, 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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if hash == existingHash {
|
if matches {
|
||||||
return stat.Size(), nil
|
return stat.Size(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,3 +212,22 @@ func downloadInternal(cacheKey string, location string, hash string, url string,
|
||||||
|
|
||||||
return resp.ContentLength, nil
|
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…
Reference in a new issue