-
Notifications
You must be signed in to change notification settings - Fork 814
downloader: CacheKey: include decompression flag #4067
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
Merged
+28
−20
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -230,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result, | |
| return res, nil | ||
| } | ||
|
|
||
| shad := cacheDirectoryPath(o.cacheDir, remote) | ||
| shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress) | ||
| if err := os.MkdirAll(shad, 0o700); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -255,7 +255,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result, | |
| // nil if the file was copied, nil, nil if the file is not in the cache or the | ||
| // cache needs update, or nil, error on fatal error. | ||
| func getCached(ctx context.Context, localPath, remote string, o options) (*Result, error) { | ||
| shad := cacheDirectoryPath(o.cacheDir, remote) | ||
| shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress) | ||
| shadData := filepath.Join(shad, "data") | ||
| shadTime := filepath.Join(shad, "time") | ||
| shadType := filepath.Join(shad, "type") | ||
|
|
@@ -301,7 +301,7 @@ func getCached(ctx context.Context, localPath, remote string, o options) (*Resul | |
|
|
||
| // fetch downloads remote to the cache and copy the cached file to local path. | ||
| func fetch(ctx context.Context, localPath, remote string, o options) (*Result, error) { | ||
| shad := cacheDirectoryPath(o.cacheDir, remote) | ||
| shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress) | ||
| shadData := filepath.Join(shad, "data") | ||
| shadTime := filepath.Join(shad, "time") | ||
| shadType := filepath.Join(shad, "type") | ||
|
|
@@ -354,7 +354,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) { | |
| return nil, errors.New("local files are not cached") | ||
| } | ||
|
|
||
| shad := cacheDirectoryPath(o.cacheDir, remote) | ||
| shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress) | ||
| shadData := filepath.Join(shad, "data") | ||
| shadTime := filepath.Join(shad, "time") | ||
| shadType := filepath.Join(shad, "type") | ||
|
|
@@ -404,8 +404,8 @@ func Cached(remote string, opts ...Opt) (*Result, error) { | |
| // - "data" file contains the data | ||
| // - "time" file contains the time (Last-Modified header) | ||
| // - "type" file contains the type (Content-Type header) | ||
| func cacheDirectoryPath(cacheDir, remote string) string { | ||
| return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote)) | ||
| func cacheDirectoryPath(cacheDir, remote string, decompress bool) string { | ||
| return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote, decompress)) | ||
| } | ||
|
|
||
| // cacheDigestPath returns the cache digest file path. | ||
|
|
@@ -775,8 +775,12 @@ func CacheEntries(opts ...Opt) (map[string]string, error) { | |
| } | ||
|
|
||
| // CacheKey returns the key for a cache entry of the remote URL. | ||
| func CacheKey(remote string) string { | ||
| return fmt.Sprintf("%x", sha256.Sum256([]byte(remote))) | ||
| func CacheKey(remote string, decompress bool) string { | ||
| k := fmt.Sprintf("%x", sha256.Sum256([]byte(remote))) | ||
| if decompress && decompressor(remote) != "" { | ||
| k += "+decomp" | ||
| } | ||
| return k | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (On second thought this might be rather confusing when the original data is not compressed) |
||
|
|
||
| // RemoveAllCacheDir removes the cache directory. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I never got around to reviewing this before it got merged, but I don't see how this can work:
decompressor()expects a file extension, but you pass the whole URL, sodecompressor(remote)will always return""and you will never append+decompto the cache key.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally there should be BATS tests for the downloader functionality, as it has been getting quite complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even a unit test comparing
CacheKey("https://example.com/image.qcow2.gz", true)withCacheKey("https://example.com/image.qcow2.gz", false)would detect this specific failure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened a PR to fix it: