MCPcopy
hub / github.com/kopia/kopia / downloadInternal

Function downloadInternal

tools/gettool/autodownload/autodownload.go:253–312  ·  view source on GitHub ↗
(url, dir string, checksum map[string]string, stripPathComponents int)

Source from the content-addressed store, hash-verified

251var errNotFound = errors.New("not found")
252
253func downloadInternal(url, dir string, checksum map[string]string, stripPathComponents int) (err error) {
254 resp, err := http.Get(url) //nolint:gosec,noctx
255 if err != nil {
256 return errors.Wrapf(err, "unable to get %q", url)
257 }
258
259 defer resp.Body.Close() //nolint:errcheck
260
261 if resp.StatusCode != http.StatusOK {
262 if resp.StatusCode == http.StatusNotFound {
263 return errNotFound
264 }
265
266 return errors.Errorf("invalid server response for %q: %v", url, resp.Status)
267 }
268
269 var buf bytes.Buffer
270
271 h := sha256.New()
272
273 if _, cerr := io.Copy(io.MultiWriter(h, &buf), resp.Body); cerr != nil {
274 return errors.Wrap(cerr, "copy error")
275 }
276
277 actualChecksum := hex.EncodeToString(h.Sum(nil))
278
279 switch {
280 case checksum[url] == "":
281 checksum[url] = actualChecksum
282 return InvalidChecksumError{actualChecksum, ""}
283
284 case checksum[url] != actualChecksum:
285 return InvalidChecksumError{actualChecksum, checksum[url]}
286
287 default:
288 log.Printf("%v checksum ok", url)
289 }
290
291 var r io.Reader
292
293 if strings.HasSuffix(url, ".gz") {
294 gzr, err := gzip.NewReader(&buf)
295 if err != nil {
296 return errors.New("unable to gunzip response")
297 }
298
299 r = gzr
300 } else {
301 r = &buf
302 }
303
304 switch {
305 case strings.HasSuffix(url, ".tar.gz"):
306 return errors.Wrap(untar(dir, r, stripPathComponents), "untar error")
307 case strings.HasSuffix(url, ".zip"):
308 return errors.Wrap(unzip(dir, r, stripPathComponents), "unzip error")
309 default:
310 return errors.New("unsupported archive format")

Callers 1

DownloadFunction · 0.85

Calls 6

untarFunction · 0.85
unzipFunction · 0.85
ErrorfMethod · 0.80
PrintfMethod · 0.80
GetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected