Skip to content

Commit 827d57d

Browse files
authored
Merge pull request #1 from signcl/fix-progress
fix bug of parsing rsync stdout
2 parents e0bbc36 + 550f228 commit 827d57d

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

task.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package grsync
22

33
import (
44
"bufio"
5+
"bytes"
56
"io"
67
"math"
78
"strconv"
@@ -45,6 +46,11 @@ func (t Task) Log() Log {
4546
}
4647
}
4748

49+
// String return the actual exec cmd string of the task
50+
func (t Task) String() string {
51+
return t.rsync.cmd.String()
52+
}
53+
4854
// Run starts rsync process with options
4955
func (t *Task) Run() error {
5056
stderr, err := t.rsync.StderrPipe()
@@ -84,6 +90,29 @@ func NewTask(source, destination string, rsyncOptions RsyncOptions) *Task {
8490
}
8591
}
8692

93+
func scanProgressLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
94+
if atEOF && len(data) == 0 {
95+
return 0, nil, nil
96+
}
97+
if i := bytes.IndexAny(data, "\r\n"); i >= 0 {
98+
if data[i] == '\n' {
99+
// We have a line terminated by single newline.
100+
return i + 1, data[0:i], nil
101+
}
102+
advance = i + 1
103+
if len(data) > i+1 && data[i+1] == '\n' {
104+
advance += 1
105+
}
106+
return advance, data[0:i], nil
107+
}
108+
// If we're at EOF, we have a final, non-terminated line. Return it.
109+
if atEOF {
110+
return len(data), data, nil
111+
}
112+
// Request more data.
113+
return 0, nil, nil
114+
}
115+
87116
func processStdout(wg *sync.WaitGroup, task *Task, stdout io.Reader) {
88117
const maxPercents = float64(100)
89118
const minDivider = 1
@@ -96,6 +125,7 @@ func processStdout(wg *sync.WaitGroup, task *Task, stdout io.Reader) {
96125
// Extract data from strings:
97126
// 999,999 99% 999.99kB/s 0:00:59 (xfr#9, to-chk=999/9999)
98127
scanner := bufio.NewScanner(stdout)
128+
scanner.Split(scanProgressLines)
99129
for scanner.Scan() {
100130
logStr := scanner.Text()
101131
if progressMatcher.Match(logStr) {
@@ -147,11 +177,11 @@ func getTaskProgress(remTotalString string) (int, int) {
147177
}
148178

149179
func getTaskSpeed(data [][]string) string {
150-
if len(data) < 2 || len(data[1]) < 2 {
180+
if len(data) < 1 || len(data[0]) < 1 {
151181
return ""
152182
}
153183

154-
return data[1][1]
184+
return data[0][0]
155185
}
156186

157187
// # Call this if you want to filter out verbose messages (-v or -vv) from

task_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func TestTaskProgressWithDifferentChkID(t *testing.T) {
3535

3636
func TestTaskSpeedParse(t *testing.T) {
3737
speedMatcher := newMatcher(`(\d+\.\d+.{2}\/s)`)
38-
const taskInfoString = `0.00kB/s \n 999,999 99% 999.99kB/s 0:00:59 (xfr#9, ir-chk=999/9999)`
38+
const taskInfoString = `999,999 99% 999.99kB/s 0:00:59 (xfr#9, ir-chk=999/9999)`
3939
speed := getTaskSpeed(speedMatcher.ExtractAllStringSubmatch(taskInfoString, 2))
4040
assert.Equal(t, "999.99kB/s", speed)
4141
}

0 commit comments

Comments
 (0)