Skip to content

Commit

Permalink
Merge pull request #683 from stgraber/migrate
Browse files Browse the repository at this point in the history
Fix missing ioprogress when receiving btrfs migration
  • Loading branch information
brauner authored Mar 27, 2024
2 parents d33eeda + 00ab527 commit 71f9729
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
13 changes: 11 additions & 2 deletions internal/server/storage/drivers/driver_btrfs_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,22 @@ func (d *btrfs) loadOptimizedBackupHeader(r io.ReadSeeker, mountPath string) (*B
}

// receiveSubVolume receives a subvolume from an io.Reader into the receivePath and returns the path to the received subvolume.
func (d *btrfs) receiveSubVolume(r io.Reader, receivePath string) (string, error) {
func (d *btrfs) receiveSubVolume(r io.Reader, receivePath string, tracker *ioprogress.ProgressTracker) (string, error) {
files, err := os.ReadDir(receivePath)
if err != nil {
return "", fmt.Errorf("Failed listing contents of %q: %w", receivePath, err)
}

err = subprocess.RunCommandWithFds(context.TODO(), r, nil, "btrfs", "receive", "-e", receivePath)
// Setup progress tracker.
var stdin io.Reader = r

Check failure on line 613 in internal/server/storage/drivers/driver_btrfs_utils.go

View workflow job for this annotation

GitHub Actions / Code

var-declaration: should omit type io.Reader from declaration of var stdin; it will be inferred from the right-hand side (revive)
if tracker != nil {
stdin = &ioprogress.ProgressReader{
Reader: r,
Tracker: tracker,
}
}

err = subprocess.RunCommandWithFds(context.TODO(), stdin, nil, "btrfs", "receive", "-e", receivePath)
if err != nil {
return "", err
}
Expand Down
10 changes: 8 additions & 2 deletions internal/server/storage/drivers/driver_btrfs_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (d *btrfs) CreateVolumeFromBackup(vol Volume, srcBackup backup.Info, srcDat
}

if hdr.Name == srcFile {
subVolRecvPath, err := d.receiveSubVolume(tr, targetPath)
subVolRecvPath, err := d.receiveSubVolume(tr, targetPath, nil)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -628,6 +628,12 @@ func (d *btrfs) createVolumeFromMigrationOptimized(vol Volume, conn io.ReadWrite
receiveVolume := func(v Volume, receivePath string) error {
_, snapName, _ := api.GetParentAndSnapshotName(v.name)

// Setup progress tracking.
var wrapper *ioprogress.ProgressTracker
if volTargetArgs.TrackProgress {
wrapper = localMigration.ProgressTracker(op, "fs_progress", v.name)
}

for _, subVol := range subvolumes {
if subVol.Snapshot != snapName {
continue // Skip any subvolumes that dont belong to our volume (empty for main).
Expand All @@ -643,7 +649,7 @@ func (d *btrfs) createVolumeFromMigrationOptimized(vol Volume, conn io.ReadWrite
subVolTargetPath := filepath.Join(v.MountPath(), subVol.Path)
d.logger.Debug("Receiving volume", logger.Ctx{"name": v.name, "receivePath": receivePath, "path": subVolTargetPath})

subVolRecvPath, err := d.receiveSubVolume(conn, receivePath)
subVolRecvPath, err := d.receiveSubVolume(conn, receivePath, wrapper)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion shared/ioprogress/reader.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package ioprogress

import (
"fmt"
"io"
)

// ProgressReader is a wrapper around ReadCloser which allows for progress tracking.
type ProgressReader struct {
io.Reader
io.ReadCloser
Tracker *ProgressTracker
}

// Read in ProgressReader is the same as io.Read.
func (pt *ProgressReader) Read(p []byte) (int, error) {
var reader io.Reader
if pt.ReadCloser != nil {
reader = pt.ReadCloser
} else if pt.Reader != nil {
reader = pt.Reader
} else {
return -1, fmt.Errorf("ProgressReader is missing a reader")
}

// Do normal reader tasks
n, err := pt.ReadCloser.Read(p)
n, err := reader.Read(p)

// Do the actual progress tracking
if pt.Tracker != nil {
Expand Down

0 comments on commit 71f9729

Please sign in to comment.