Skip to content

Commit 666ffef

Browse files
committed
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 lima-vm/go-qcow2reader#47 Signed-off-by: Nir Soffer <[email protected]>
1 parent 2ef8669 commit 666ffef

File tree

3 files changed

+9
-17
lines changed

3 files changed

+9
-17
lines changed

pkg/downloader/downloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
var HideProgress bool
3131

3232
// hideBar is used only for testing.
33-
func hideBar(bar *pb.ProgressBar) {
33+
func hideBar(bar *progressbar.ProgressBar) {
3434
bar.Set(pb.Static, true)
3535
}
3636

pkg/nativeimgutil/nativeimgutil.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,8 @@ func ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile b
7878
if err != nil {
7979
return err
8080
}
81-
conv, err := convert.New(convert.Options{})
82-
if err != nil {
83-
return err
84-
}
8581
bar.Start()
86-
pra := progressbar.ProxyReaderAt{ReaderAt: srcImg, Bar: bar}
87-
err = conv.Convert(destTmpF, &pra, srcImg.Size())
82+
err = convert.Convert(destTmpF, srcImg, convert.Options{Progress: bar})
8883
bar.Finish()
8984
if err != nil {
9085
return fmt.Errorf("failed to convert image: %w", err)

pkg/progressbar/progressbar.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package progressbar
22

33
import (
4-
"io"
54
"os"
65
"time"
76

@@ -10,19 +9,17 @@ import (
109
"github.com/sirupsen/logrus"
1110
)
1211

13-
type ProxyReaderAt struct {
14-
io.ReaderAt
15-
Bar *pb.ProgressBar
12+
// ProgressBar adapts pb.ProgressBar to go-qcow2reader.convert.Updater interface.
13+
type ProgressBar struct {
14+
*pb.ProgressBar
1615
}
1716

18-
func (r *ProxyReaderAt) ReadAt(p []byte, off int64) (int, error) {
19-
n, err := r.ReaderAt.ReadAt(p, off)
20-
r.Bar.Add(n)
21-
return n, err
17+
func (b *ProgressBar) Update(n int64) {
18+
b.Add64(n)
2219
}
2320

24-
func New(size int64) (*pb.ProgressBar, error) {
25-
bar := pb.New64(size)
21+
func New(size int64) (*ProgressBar, error) {
22+
bar := &ProgressBar{pb.New64(size)}
2623

2724
bar.Set(pb.Bytes, true)
2825

0 commit comments

Comments
 (0)