Skip to content

Commit 8288a76

Browse files
committed
refactor: extract methods for cache handling
Signed-off-by: Anders F Björklund <[email protected]>
1 parent d93772a commit 8288a76

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

pkg/downloader/downloader.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func WithDecompress(decompress bool) Opt {
9797
// - The digest was not specified.
9898
// - The file already exists in the local target path.
9999
//
100-
// When the `data` file exists in the cache dir with `digest.<ALGO>` file,
101-
// the digest is verified by comparing the content of `digest.<ALGO>` with the expected
100+
// When the `data` file exists in the cache dir with `<ALGO>.digest` file,
101+
// the digest is verified by comparing the content of `<ALGO>.digest` with the expected
102102
// digest string. So, the actual digest of the `data` file is not computed.
103103
func WithExpectedDigest(expectedDigest digest.Digest) Opt {
104104
return func(o *options) error {
@@ -183,15 +183,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
183183
return res, nil
184184
}
185185

186-
shad := filepath.Join(o.cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
186+
shad := cacheDirectoryPath(o.cacheDir, remote)
187187
shadData := filepath.Join(shad, "data")
188-
shadDigest := ""
189-
if o.expectedDigest != "" {
190-
algo := o.expectedDigest.Algorithm().String()
191-
if strings.Contains(algo, "/") || strings.Contains(algo, "\\") {
192-
return nil, fmt.Errorf("invalid digest algorithm %q", algo)
193-
}
194-
shadDigest = filepath.Join(shad, algo+".digest")
188+
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
189+
if err != nil {
190+
return nil, err
195191
}
196192
if _, err := os.Stat(shadData); err == nil {
197193
logrus.Debugf("file %q is cached as %q", localPath, shadData)
@@ -247,6 +243,27 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
247243
return res, nil
248244
}
249245

246+
// cacheDirectoryPath returns the cache subdirectory path.
247+
// - "url" file contains the url
248+
// - "data" file contains the data
249+
func cacheDirectoryPath(cacheDir string, remote string) string {
250+
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
251+
}
252+
253+
// cacheDigestPath returns the cache digest file path.
254+
// - "<ALGO>.digest" contains the digest
255+
func cacheDigestPath(shad string, expectedDigest digest.Digest) (string, error) {
256+
shadDigest := ""
257+
if expectedDigest != "" {
258+
algo := expectedDigest.Algorithm().String()
259+
if strings.Contains(algo, "/") || strings.Contains(algo, "\\") {
260+
return "", fmt.Errorf("invalid digest algorithm %q", algo)
261+
}
262+
shadDigest = filepath.Join(shad, algo+".digest")
263+
}
264+
return shadDigest, nil
265+
}
266+
250267
func IsLocal(s string) bool {
251268
return !strings.Contains(s, "://") || strings.HasPrefix(s, "file://")
252269
}

0 commit comments

Comments
 (0)