@@ -97,8 +97,8 @@ func WithDecompress(decompress bool) Opt {
97
97
// - The digest was not specified.
98
98
// - The file already exists in the local target path.
99
99
//
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
102
102
// digest string. So, the actual digest of the `data` file is not computed.
103
103
func WithExpectedDigest (expectedDigest digest.Digest ) Opt {
104
104
return func (o * options ) error {
@@ -183,15 +183,11 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
183
183
return res , nil
184
184
}
185
185
186
- shad := filepath . Join (o .cacheDir , "download" , "by-url-sha256" , fmt . Sprintf ( "%x" , sha256 . Sum256 ([] byte ( remote ))) )
186
+ shad := cacheDirectoryPath (o .cacheDir , remote )
187
187
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
195
191
}
196
192
if _ , err := os .Stat (shadData ); err == nil {
197
193
logrus .Debugf ("file %q is cached as %q" , localPath , shadData )
@@ -247,6 +243,27 @@ func Download(local, remote string, opts ...Opt) (*Result, error) {
247
243
return res , nil
248
244
}
249
245
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
+
250
267
func IsLocal (s string ) bool {
251
268
return ! strings .Contains (s , "://" ) || strings .HasPrefix (s , "file://" )
252
269
}
0 commit comments