Skip to content

Commit

Permalink
fix(volume): Add back transmit progress
Browse files Browse the repository at this point in the history
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed May 14, 2024
1 parent 589457c commit 99b9159
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 53 deletions.
25 changes: 24 additions & 1 deletion internal/cli/kraft/cloud/volume/import/cpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ func buildCPIO(ctx context.Context, source string) (path string, size int64, err
}

// copyCPIO copies the CPIO archive at the given path over the provided tls.Conn.
func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS uint64) error {
func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS, size uint64, callback progressCallbackFunc) error {
var resp okResponse
var currentSize uint64

// NOTE(antoineco): this call is critical as it allows writes to be later
// cancelled, because the deadline applies to all future and pending I/O and
Expand Down Expand Up @@ -201,6 +202,8 @@ func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS u
if err := resp.waitForOK(conn, "header copy failed"); err != nil {
return err
}
currentSize += uint64(len(raw.Bytes()))
updateProgress(float64(currentSize), float64(size), callback)

nameBytesToSend := []byte(hdr.Name)

Expand All @@ -222,6 +225,8 @@ func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS u
if err := resp.waitForOK(conn, "name copy failed"); err != nil {
return err
}
currentSize += uint64(len(nameBytesToSend))
updateProgress(float64(currentSize), float64(size), callback)

// 2'. Stop when `TRAILER!!!` met
if shouldStop {
Expand Down Expand Up @@ -263,6 +268,8 @@ func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS u
}

empty = false
currentSize += uint64(bread)
updateProgress(float64(currentSize), float64(size), callback)
}
} else {
bread := len(hdr.Linkname)
Expand All @@ -279,6 +286,8 @@ func copyCPIO(ctx context.Context, conn *tls.Conn, auth, path string, timeoutS u
}

empty = false
currentSize += uint64(bread)
updateProgress(float64(currentSize), float64(size), callback)
}

// Don't wait for ok if nothing was written
Expand Down Expand Up @@ -307,3 +316,17 @@ func isNetClosedError(err error) bool {
}
return false
}

type progressCallbackFunc func(progress float64)

// updateProgress updates the progress bar with the current progress.
// NOTE(craciunoiuc): Currently the entry pad nd the name pad are not taken
// into consideration so the progress will fall behind at times by some bytes.
func updateProgress(progress float64, size float64, callback progressCallbackFunc) {
pct := progress / size
// FIXME(antoineco): the TUI component does not turn green at the end of the
// copy if we call callback() with a value of 1.
if pct < 1.0 {
callback(pct)
}
}
51 changes: 0 additions & 51 deletions internal/cli/kraft/cloud/volume/import/fileread.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/cli/kraft/cloud/volume/import/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func importVolumeData(ctx context.Context, opts *ImportOptions) (retErr error) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()
err = copyCPIO(ctx, conn, authStr, cpioPath, opts.Timeout)
err = copyCPIO(ctx, conn, authStr, cpioPath, opts.Timeout, uint64(cpioSize), callback)
copyCPIOErr = err
return err
},
Expand Down

0 comments on commit 99b9159

Please sign in to comment.