From 666ffef631a023d569aa1a04e4327e3954f56e09 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Thu, 21 Nov 2024 22:24:29 +0200 Subject: [PATCH] Update for new go-qcow2reader convert interface We cannot track conversion progress using the image, since convert reads only the allocated extents of the image. We need to pass the progress bar using convert.Options. pb.ProgressBar need to be adapted to convert.Updater interface, so progressbar returns now our own type. This can be used later for other improvement like hiding the progress bar. Testing https://github.com/lima-vm/go-qcow2reader/pull/47 Signed-off-by: Nir Soffer --- pkg/downloader/downloader.go | 2 +- pkg/nativeimgutil/nativeimgutil.go | 7 +------ pkg/progressbar/progressbar.go | 17 +++++++---------- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index ade98bd3b90..a8ea76117ae 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -30,7 +30,7 @@ import ( var HideProgress bool // hideBar is used only for testing. -func hideBar(bar *pb.ProgressBar) { +func hideBar(bar *progressbar.ProgressBar) { bar.Set(pb.Static, true) } diff --git a/pkg/nativeimgutil/nativeimgutil.go b/pkg/nativeimgutil/nativeimgutil.go index 070786ae2a8..3f2a368ab35 100644 --- a/pkg/nativeimgutil/nativeimgutil.go +++ b/pkg/nativeimgutil/nativeimgutil.go @@ -78,13 +78,8 @@ func ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile b if err != nil { return err } - conv, err := convert.New(convert.Options{}) - if err != nil { - return err - } bar.Start() - pra := progressbar.ProxyReaderAt{ReaderAt: srcImg, Bar: bar} - err = conv.Convert(destTmpF, &pra, srcImg.Size()) + err = convert.Convert(destTmpF, srcImg, convert.Options{Progress: bar}) bar.Finish() if err != nil { return fmt.Errorf("failed to convert image: %w", err) diff --git a/pkg/progressbar/progressbar.go b/pkg/progressbar/progressbar.go index 1e90954af6d..eea971dfbae 100644 --- a/pkg/progressbar/progressbar.go +++ b/pkg/progressbar/progressbar.go @@ -1,7 +1,6 @@ package progressbar import ( - "io" "os" "time" @@ -10,19 +9,17 @@ import ( "github.com/sirupsen/logrus" ) -type ProxyReaderAt struct { - io.ReaderAt - Bar *pb.ProgressBar +// ProgressBar adapts pb.ProgressBar to go-qcow2reader.convert.Updater interface. +type ProgressBar struct { + *pb.ProgressBar } -func (r *ProxyReaderAt) ReadAt(p []byte, off int64) (int, error) { - n, err := r.ReaderAt.ReadAt(p, off) - r.Bar.Add(n) - return n, err +func (b *ProgressBar) Update(n int64) { + b.Add64(n) } -func New(size int64) (*pb.ProgressBar, error) { - bar := pb.New64(size) +func New(size int64) (*ProgressBar, error) { + bar := &ProgressBar{pb.New64(size)} bar.Set(pb.Bytes, true)