Check disk space before trying a write from the downloader; don't make empty directories if we can't even write the file

This commit is contained in:
Dane Everitt
2020-12-20 11:17:53 -08:00
parent 9c53436470
commit c718da20e3
4 changed files with 33 additions and 22 deletions

View File

@@ -66,10 +66,19 @@ func (dl *Download) Execute() error {
return errors.New("downloader: failed opening request to download file")
}
defer res.Body.Close()
if res.StatusCode >= 300 || res.StatusCode < 200 {
if res.StatusCode != http.StatusOK {
return errors.New("downloader: got bad response status from endpoint: " + res.Status)
}
// If there is a Content-Length header on this request go ahead and check that we can
// even write the whole file before beginning this process. If there is no header present
// we'll just have to give it a spin and see how it goes.
if res.ContentLength > 0 {
if err := dl.server.Filesystem().HasSpaceFor(res.ContentLength); err != nil {
return errors.WrapIf(err, "downloader: failed to write file: not enough space")
}
}
fnameparts := strings.Split(dl.req.URL.Path, "/")
p := filepath.Join(dl.req.Directory, fnameparts[len(fnameparts)-1])
dl.server.Log().WithField("path", p).Debug("writing remote file to disk")