Skip to content

Commit

Permalink
Update for new go-qcow2reader convert interface
Browse files Browse the repository at this point in the history
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 lima-vm/go-qcow2reader#47

Signed-off-by: Nir Soffer <[email protected]>
  • Loading branch information
nirs committed Nov 21, 2024
1 parent 2ef8669 commit 666ffef
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 1 addition & 6 deletions pkg/nativeimgutil/nativeimgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions pkg/progressbar/progressbar.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package progressbar

import (
"io"
"os"
"time"

Expand All @@ -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)

Expand Down

0 comments on commit 666ffef

Please sign in to comment.