diff options
-rw-r--r-- | rhimport/fetcher.go | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index f6e940c..061d0e1 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -76,14 +76,14 @@ func curlWriteCallback(ptr []byte, userdata interface{}) bool { } data.filename = filepath.Join(data.basepath, name) } - nc, newFilename, err := NewFetchConverter(data.ctx.FetchConverter, data.filename) + conv, newFilename, err := NewFetchConverter(data.ctx.FetchConverter, data.filename) if err != nil { rhl.Printf("Unable to create converter for file %s: %s", data.filename, err) data.writeError = err return false } data.filename = newFilename - data.conv = nc + data.conv = conv } if _, err := data.conv.Write(ptr); err != nil { rhl.Printf("Unable to write file %s: %s", data.filename, err) @@ -171,10 +171,8 @@ func fetchFileCurl(ctx *Context, res *Result, uri *url.URL) (err error) { err = cbdata.writeError } if convErr != nil { - err = convErr - if convOut != "" { - rhl.Printf("converter output: %s", convOut) - } + err = fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut) + rhl.Printf("%v", err) } err = fmt.Errorf("curl-fetcher('%s'): %s", ctx.SourceUri, err) rhl.Println(err) @@ -292,10 +290,8 @@ func fetchFileArchiv(ctx *Context, res *Result, uri *url.URL) (err error) { err = cbdata.writeError } if convErr != nil { - err = convErr - if convOut != "" { - rhl.Printf("converter output: %s", convOut) - } + err = fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut) + rhl.Printf("%v", err) } err = fmt.Errorf("archiv-fetcher('%s'): %s", ctx.SourceUri, err) rhl.Println(err) @@ -405,7 +401,7 @@ func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { return nil } -func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, src *os.File) error { +func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv FetchConverter) error { cancel := ctx.Cancel if cancel == nil { cancel = make(<-chan bool) @@ -429,11 +425,11 @@ func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, src *os.Fi left := sizeTotal - written if int(left) < len(data) { - rhl.Printf("attachment fetcher: truncating ") + rhl.Printf("attachment fetcher: truncating %d byes of extra data", len(data)-int(left)) data = data[0:left] } - w, err := src.Write(data) + w, err := conv.Write(data) if err != nil { rhl.Printf("Unable to write file %s: %s", ctx.SourceFile, err) return err @@ -479,12 +475,11 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { ctx.SourceFile = filepath.Join(basepath, path.Clean("/"+uri.Path)) - var src *os.File - if src, err = os.OpenFile(ctx.SourceFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600); err != nil { - rhl.Printf("Unable to create file %s: %s", ctx.SourceFile, err) + var conv FetchConverter + if conv, ctx.SourceFile, err = NewFetchConverter(ctx.FetchConverter, ctx.SourceFile); err != nil { + rhl.Printf("Unable to create converter for file %s: %s", ctx.SourceFile, err) return err } - defer src.Close() if ctx.ProgressCallBack != nil { if keep := ctx.ProgressCallBack(1, "receiving", 0.0, float64(sizeTotal), ctx.ProgressCallBackData); !keep { @@ -492,9 +487,19 @@ func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { } } - if err = writeAttachmentFile(ctx, res, sizeTotal, src); err != nil { + err = writeAttachmentFile(ctx, res, sizeTotal, conv) + conv.Close() + rhl.Printf("waiting for converter to finish...") + convOut, convErr := conv.GetResult() + if err != nil { return err } + if convErr != nil { + if convOut != "" { + rhl.Printf("converter error: %v; converter output: %s", convErr, convOut) + } + return fmt.Errorf("converter error: %v; converter output: %s", convErr, convOut) + } if ctx.SourceFilePolicy == Auto { ctx.DeleteSourceFile = true |