Skip to content

Commit

Permalink
Merge pull request #109 from WillAbides/cacheerr
Browse files Browse the repository at this point in the history
No error from hexHash
  • Loading branch information
WillAbides authored Mar 12, 2023
2 parents 0a66f04 + 34c32bc commit 3eaa111
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 32 deletions.
27 changes: 9 additions & 18 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,21 +340,15 @@ type ConfigDownloadDependencyOpts struct {
}

// extractsCacheDir returns the cache directory for an extraction based on the download's checksum and dependency name
func (c *Config) extractsCacheDir(dependencyName, checksum string) (string, error) {
hsh, err := hexHash(fnv.New64a(), []byte(checksum))
if err != nil {
return "", err
}
return filepath.Join(c.Cache, "extracts", hsh), nil
func (c *Config) extractsCacheDir(hashMaterial string) string {
hsh := hexHash(fnv.New64a(), []byte(hashMaterial))
return filepath.Join(c.Cache, "extracts", hsh)
}

// downloadCacheDir returns the cache directory for a file based on its checksum
func (c *Config) downloadCacheDir(checksum string) (string, error) {
hsh, err := hexHash(fnv.New64a(), []byte(checksum))
if err != nil {
return "", err
}
return filepath.Join(c.Cache, "downloads", hsh), nil
func (c *Config) downloadCacheDir(hashMaterial string) string {
hsh := hexHash(fnv.New64a(), []byte(hashMaterial))
return filepath.Join(c.Cache, "downloads", hsh)
}

// DownloadDependency downloads a dependency
Expand Down Expand Up @@ -397,15 +391,12 @@ func (c *Config) DownloadDependency(dependencyName string, sysInfo SystemInfo, o
}

if targetFile == "" {
var dlFile, cacheDir string
var dlFile string
dlFile, err = urlFilename(depURL)
if err != nil {
return "", err
}
cacheDir, err = c.downloadCacheDir(checksum)
if err != nil {
return "", err
}
cacheDir := c.downloadCacheDir(checksum)
targetFile = filepath.Join(cacheDir, dlFile)
}

Expand Down Expand Up @@ -508,7 +499,7 @@ func (c *Config) ExtractDependency(dependencyName string, sysInfo SystemInfo, op
return "", err
}
}
targetDir, err = c.extractsCacheDir(dependencyName, checksum)
targetDir = c.extractsCacheDir(checksum)
if err != nil {
return "", err
}
Expand Down
11 changes: 6 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,17 @@ func directoryChecksum(inputDir string) (string, error) {
}

// hexHash returns a hex representation of data's hash
// This will only return non-nil error if given a hasher that can return a non-nil error from Write()
func hexHash(hasher hash.Hash, data ...[]byte) (string, error) {
func hexHash(hasher hash.Hash, data ...[]byte) string {
hasher.Reset()
for _, datum := range data {
_, err := hasher.Write(datum)
if err != nil {
return "", err
// hash.Hash.Write() never returns an error
// https://github.com/golang/go/blob/go1.17/src/hash/hash.go#L27-L29
panic(err)
}
}
return hex.EncodeToString(hasher.Sum(nil)), nil
return hex.EncodeToString(hasher.Sum(nil))
}

// fileChecksum returns the hex checksum of a file
Expand All @@ -120,7 +121,7 @@ func fileChecksum(filename string) (string, error) {
if err != nil {
return "", err
}
return hexHash(sha256.New(), fileBytes)
return hexHash(sha256.New(), fileBytes), nil
}

// fileExists asserts that a file exists
Expand Down
12 changes: 3 additions & 9 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,11 @@ func Test_fileChecksum(t *testing.T) {
}

func Test_hexHash(t *testing.T) {
got, err := hexHash(fnv.New64a(), []byte("foo"))
require.NoError(t, err)
require.Equal(t, "dcb27518fed9d577", got)
got, err = hexHash(fnv.New64a(), []byte("foo"), []byte("bar"))
require.NoError(t, err)
require.Equal(t, "85944171f73967e8", got)
require.Equal(t, "dcb27518fed9d577", hexHash(fnv.New64a(), []byte("foo")))
require.Equal(t, "85944171f73967e8", hexHash(fnv.New64a(), []byte("foo"), []byte("bar")))
content, err := os.ReadFile(filepath.Join("testdata", "downloadables", "foo.tar.gz"))
require.NoError(t, err)
got, err = hexHash(sha256.New(), content)
require.NoError(t, err)
require.Equal(t, fooChecksum, got)
require.Equal(t, fooChecksum, hexHash(sha256.New(), content))
}

func Test_copyFile(t *testing.T) {
Expand Down

0 comments on commit 3eaa111

Please sign in to comment.