Fix rsync space estimation with carriage-return progress; release 2026.10.15.r001

This commit is contained in:
Mikei386 committed 2026-10-15 17:01:43 +02:00
1 parent 62d765d629
commit a7dbd77492
8 files changed
+99 -13

No files matched your search

+14 -9
View File
@@ -91,9 +91,10 @@ func (r *Runner) Run(ctx context.Context, runID string, job model.Job, callback
defer unregister()
var summary Summary
var scanErr error
done := make(chan struct{})
go func() {
scanOutput(stdout, func(line string) {
scanErr = scanOutput(stdout, func(line string) {
summary.parseLine(line)
if callback != nil {
if progress, ok := parseProgress(line); ok {
@@ -121,6 +122,9 @@ func (r *Runner) Run(ctx context.Context, runID string, job model.Job, callback
}
return Summary{}, fmt.Errorf("rsync: %s", message)
}
if scanErr != nil {
return summary, fmt.Errorf("rsync output: %w", scanErr)
}
return summary, nil
}
@@ -176,16 +180,11 @@ func (r *Runner) Estimate(ctx context.Context, runID string, job model.Job) (Sum
space := newSpaceEstimate(job.Rsync.Target)
done := make(chan struct{})
go func() {
scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 64*1024), 2*1024*1024)
for scanner.Scan() {
line := scanner.Text()
if err := scanOutput(stdout, func(line string) {
summary.parseLine(line)
space.consume(line)
}
if err := scanner.Err(); err != nil {
}); err != nil {
space.problem = err.Error()
_, _ = io.Copy(io.Discard, stdout)
}
close(done)
}()
@@ -282,7 +281,7 @@ func (r *Runner) register(runID string, process *os.Process) func() {
}
}
func scanOutput(reader io.Reader, callback func(string)) {
func scanOutput(reader io.Reader, callback func(string)) error {
scanner := bufio.NewScanner(reader)
scanner.Split(splitLinesAndCarriageReturns)
scanner.Buffer(make([]byte, 64*1024), 2*1024*1024)
@@ -291,6 +290,12 @@ func scanOutput(reader io.Reader, callback func(string)) {
callback(line)
}
}
if err := scanner.Err(); err != nil {
// Keep draining so rsync cannot block on a full stdout pipe before Wait.
_, _ = io.Copy(io.Discard, reader)
return err
}
return nil
}
func drainLimited(reader io.Reader, limit int) []byte {
+41
View File
@@ -7,6 +7,7 @@ import (
"slices"
"strings"
"testing"
"time"
"git.casaderoll.de/michael/urbm/internal/model"
)
@@ -170,3 +171,43 @@ func TestRealRsyncDryRunAndCopy(t *testing.T) {
t.Fatalf("identical: %+v %v", identical, err)
}
}
// progress2 updates use CR without LF; a long comparison must not accumulate
// them into one multi-megabyte Scanner token or hide its final statistics.
func TestEstimateHandlesLongCarriageReturnProgress(t *testing.T) {
dir := t.TempDir()
output := filepath.Join(dir, "output")
progress := strings.Repeat(" 1,234,567 42% 12.34MB/s 0:00:05\r", 100000)
if err := os.WriteFile(output, []byte(progress+"\nNumber of regular files transferred: 0\nTotal transferred file size: 0 bytes\n"), 0600); err != nil {
t.Fatal(err)
}
binary := filepath.Join(dir, "rsync")
if err := os.WriteFile(binary, []byte("#!/bin/sh\ncat '"+output+"'\n"), 0700); err != nil {
t.Fatal(err)
}
runner := Runner{Binary: binary}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
got, err := runner.Estimate(ctx, "cr-progress", model.Job{Rsync: model.RsyncOptions{Target: dir}})
if err != nil || got.SpaceUnknown != "" || got.Files != 0 || got.Bytes != 0 || got.RequiredBytes != 0 {
t.Fatalf("summary=%+v err=%v", got, err)
}
}
func TestScanOutputMixedDelimitersAndFinalLine(t *testing.T) {
var got []string
err := scanOutput(strings.NewReader("one\rtwo\r\nthree\nfour"), func(s string) { got = append(got, s) })
if err != nil || !slices.Equal(got, []string{"one", "two", "three", "four"}) {
t.Fatalf("lines=%v err=%v", got, err)
}
}
func TestScanOutputReportsOversizedTokenAndDrains(t *testing.T) {
input := strings.NewReader(strings.Repeat("x", 3*1024*1024) + "\nremaining\n")
if err := scanOutput(input, func(string) {}); err == nil {
t.Fatal("oversized output silently accepted")
}
if input.Len() != 0 {
t.Fatalf("stdout not drained: %d bytes", input.Len())
}
}