Skip to content

Commit 0a0bf4b

Browse files
committed
Download: Add sha256sum parameter to function signature
This can used to verify the downloaded file with given hash and if this doesn't match then delete the corrupt file. This will be useful for larger file where there is network disconnection.
1 parent 1bacdb8 commit 0a0bf4b

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

cmd/crc-embedder/cmd/embed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func downloadDataFiles(goos string, destDir string) ([]string, error) {
111111
downloadedFiles := []string{}
112112
downloads := dataFileUrls[goos]
113113
for _, dl := range downloads {
114-
filename, err := download.Download(dl.url, destDir, dl.permissions)
114+
filename, err := download.Download(dl.url, destDir, dl.permissions, nil)
115115
if err != nil {
116116
return nil, err
117117
}

pkg/crc/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func (c *Cache) getExecutable(destDir string) (string, error) {
158158
destPath := filepath.Join(destDir, archiveName)
159159
err := embed.Extract(archiveName, destPath)
160160
if err != nil {
161-
return download.Download(c.archiveURL, destDir, 0600)
161+
return download.Download(c.archiveURL, destDir, 0600, nil)
162162
}
163163

164164
return destPath, err

pkg/download/download.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package download
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"net/http"
67
"os"
78

@@ -13,7 +14,9 @@ import (
1314
"github.com/pkg/errors"
1415
)
1516

16-
func Download(uri, destination string, mode os.FileMode) (string, error) {
17+
// Download function takes sha256sum as hex decoded byte
18+
// something like hex.DecodeString("33daf4c03f86120fdfdc66bddf6bfff4661c7ca11c5d")
19+
func Download(uri, destination string, mode os.FileMode, sha256sum []byte) (string, error) {
1720
logging.Debugf("Downloading %s to %s", uri, destination)
1821

1922
client := grab.NewClient()
@@ -23,6 +26,9 @@ func Download(uri, destination string, mode os.FileMode) (string, error) {
2326
if err != nil {
2427
return "", errors.Wrapf(err, "unable to get response from %s", uri)
2528
}
29+
if sha256sum != nil {
30+
req.SetChecksum(sha256.New(), sha256sum, true)
31+
}
2632

2733
respCh := consoleClient.Do(context.Background(), 3, req)
2834
resp := <-respCh

test/extended/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func DownloadBundle(bundleLocation string, bundleDestination string, bundleName
117117
return bundleDestination, err
118118
}
119119

120-
filename, err := download.Download(bundleLocation, bundleDestination, 0644)
120+
filename, err := download.Download(bundleLocation, bundleDestination, 0644, nil)
121121
fmt.Printf("Downloading bundle from %s to %s.\n", bundleLocation, bundleDestination)
122122
if err != nil {
123123
return "", err

0 commit comments

Comments
 (0)