diff options
Diffstat (limited to 'rhimport')
-rw-r--r-- | rhimport/fetcher.go | 54 |
1 files changed, 30 insertions, 24 deletions
diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 6060e98..77f00e2 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -41,15 +41,17 @@ import ( ) type FetcherCurlCBData struct { + ctx *Context + res *Result basepath string filename string remotename string - *os.File + file *os.File } func (self *FetcherCurlCBData) Cleanup() { - if self.File != nil { - self.File.Close() + if self.file != nil { + self.file.Close() } } @@ -69,7 +71,7 @@ func curlHeaderCallback(ptr []byte, userdata interface{}) bool { func curlWriteCallback(ptr []byte, userdata interface{}) bool { data := userdata.(*FetcherCurlCBData) - if data.File == nil { + if data.file == nil { if data.filename == "" { data.filename = data.basepath + "/" + data.remotename } @@ -78,15 +80,33 @@ func curlWriteCallback(ptr []byte, userdata interface{}) bool { rhl.Printf("Unable to create file %s: %s", data.filename, err) return false } - data.File = fp + data.file = fp } - if _, err := data.File.Write(ptr); err != nil { + if _, err := data.file.Write(ptr); err != nil { rhl.Printf("Unable to write file %s: %s", data.filename, err) return false } return true } +func curlProgressCallback(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool { + data := userdata.(*FetcherCurlCBData) + + if data.ctx.Cancel != nil && len(data.ctx.Cancel) > 0 { + rhl.Printf("downloading '%s' got canceled", data.ctx.SourceUri) + data.res.ResponseCode = http.StatusNoContent + data.res.ErrorString = "canceled" + return false + } + + if data.ctx.ProgressCallBack != nil { + if keep := data.ctx.ProgressCallBack(1, "downloading", dlnow/dltotal, data.ctx.ProgressCallBackData); !keep { + data.ctx.ProgressCallBack = nil + } + } + return true +} + func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { rhl.Printf("curl-based fetcher called for '%s'", ctx.SourceUri) @@ -97,7 +117,7 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { easy.Setopt(curl.OPT_FOLLOWLOCATION, true) easy.Setopt(curl.OPT_URL, ctx.SourceUri) - cbdata := &FetcherCurlCBData{remotename: path.Base(uri.Path)} + cbdata := &FetcherCurlCBData{ctx: ctx, res: res, remotename: path.Base(uri.Path)} defer cbdata.Cleanup() if cbdata.basepath, err = ioutil.TempDir(ctx.conf.TempDir, "rhimportd-"); err != nil { return @@ -110,25 +130,11 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { easy.Setopt(curl.OPT_WRITEDATA, cbdata) easy.Setopt(curl.OPT_NOPROGRESS, false) - easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool { - if ctx.Cancel != nil && len(ctx.Cancel) > 0 { - rhl.Printf("downloading '%s' got canceled", ctx.SourceUri) - res.ResponseCode = http.StatusNoContent - res.ErrorString = "canceled" - return false - } - - if ctx.ProgressCallBack != nil { - if keep := ctx.ProgressCallBack(1, "downloading", dlnow/dltotal, ctx.ProgressCallBackData); !keep { - ctx.ProgressCallBack = nil - } - } - return true - }) - easy.Setopt(curl.OPT_PROGRESSDATA, ctx) + easy.Setopt(curl.OPT_PROGRESSFUNCTION, curlProgressCallback) + easy.Setopt(curl.OPT_PROGRESSDATA, cbdata) if err = easy.Perform(); err != nil { - if cbdata.File != nil { + if cbdata.file != nil { rhdl.Printf("Removing stale file: %s", cbdata.filename) os.Remove(cbdata.filename) os.Remove(path.Dir(cbdata.filename)) |