forked from sakishum/grsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
305 lines (252 loc) · 6.5 KB
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package grsync
import (
"bufio"
"bytes"
"io"
"math"
"os/exec"
"strconv"
"strings"
"sync"
)
// Task is high-level API under rsync
type Task struct {
rsync *Rsync
state *State
log *Log
}
// State contains information about rsync process
type State struct {
Remain int `json:"remain"`
Total int `json:"total"`
Speed string `json:"speed"`
Progress float64 `json:"progress"`
Filename string `json:"filename"`
TransferedBytes int64 `json:"transfered_bytes"`
TransferedPercent int `json:"transfered_percent"` // 0 ~ 100
}
// Log contains raw stderr and stdout outputs
type Log struct {
Stderr string `json:"stderr"`
Stdout string `json:"stdout"`
}
// State returns inforation about rsync processing task
func (t Task) State() State {
return *t.state
}
// Log return structure which contains raw stderr and stdout outputs
func (t Task) Log() Log {
return Log{
Stderr: t.log.Stderr,
Stdout: t.log.Stdout,
}
}
// String return the actual exec cmd string of the task
func (t Task) String() string {
return t.rsync.cmd.String()
}
// Run starts rsync process with options
func (t *Task) Run() error {
stderr, err := t.rsync.StderrPipe()
if err != nil {
return err
}
defer stderr.Close()
stdout, err := t.rsync.StdoutPipe()
if err != nil {
return err
}
defer stdout.Close()
var wg sync.WaitGroup
go processStdout(&wg, t, stdout)
go processStderr(&wg, t, stderr)
wg.Add(2)
err = t.rsync.Run()
wg.Wait()
return err
}
// GetCmd return the underlying exec.Cmd.
func (r Task) GetCmd() *exec.Cmd {
return r.rsync.cmd
}
// NewTask returns new rsync task
func NewTask(source, destination string, rsyncOptions RsyncOptions) *Task {
// Force set required options
rsyncOptions.HumanReadable = true
rsyncOptions.Partial = true
rsyncOptions.Archive = true
return &Task{
rsync: NewRsync(source, destination, rsyncOptions),
state: &State{},
log: &Log{},
}
}
func scanProgressLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexAny(data, "\r\n"); i >= 0 {
if data[i] == '\n' {
// We have a line terminated by single newline.
return i + 1, data[0:i], nil
}
advance = i + 1
if len(data) > i+1 && data[i+1] == '\n' {
advance += 1
}
return advance, data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
}
func processStdout(wg *sync.WaitGroup, task *Task, stdout io.Reader) {
const maxPercents = float64(100)
const minDivider = 1
defer wg.Done()
progressMatcher := newMatcher(`\(.+-chk=(\d+.\d+)`)
speedMatcher := newMatcher(`(\d+\.\d+.{2}\/s)`)
transferedMatcher := newMatcher(`(\S+.*)%`)
// Extract data from strings:
// 999,999 99% 999.99kB/s 0:00:59 (xfr#9, to-chk=999/9999)
// 2.39G 68% 659.73MB/s 0:00:03 (xfr#7217, to-chk=1113/10003)
scanner := bufio.NewScanner(stdout)
scanner.Split(scanProgressLines)
for scanner.Scan() {
logStr := scanner.Text()
if progressMatcher.Match(logStr) {
task.state.Remain, task.state.Total = getTaskProgress(progressMatcher.Extract(logStr))
copiedCount := float64(task.state.Total - task.state.Remain)
task.state.Progress = copiedCount / math.Max(float64(task.state.Total), float64(minDivider)) * maxPercents
}
if speedMatcher.Match(logStr) {
task.state.Speed = getTaskSpeed(speedMatcher.ExtractAllStringSubmatch(logStr, 2))
}
if transferedMatcher.Match(logStr) {
task.state.TransferedBytes, task.state.TransferedPercent = getTaskTransfered(transferedMatcher.Extract(logStr))
}
if isFilename(logStr) {
task.state.Filename = logStr
}
task.log.Stdout += logStr + "\n"
}
}
func processStderr(wg *sync.WaitGroup, task *Task, stderr io.Reader) {
defer wg.Done()
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
task.log.Stderr += scanner.Text() + "\n"
}
}
func getTaskProgress(remTotalString string) (int, int) {
const remTotalSeparator = "/"
const numbersCount = 2
const (
indexRem = iota
indexTotal
)
info := strings.Split(remTotalString, remTotalSeparator)
if len(info) < numbersCount {
return 0, 0
}
remain, _ := strconv.Atoi(info[indexRem])
total, _ := strconv.Atoi(info[indexTotal])
return remain, total
}
func getTaskSpeed(data [][]string) string {
if len(data) < 1 || len(data[0]) < 1 {
return ""
}
return data[0][0]
}
func getTaskTransfered(transfered string) (transferedBytes int64, transferedPercent int) {
info := strings.Split(transfered, " ")
if len(info) < 2 {
return 0, 0
}
numberStr := info[0]
percentStr := info[len(info)-1]
transferedPercent, _ = strconv.Atoi(percentStr)
var unit string
var number float64
units := []string{"KB", "K", "MB", "M", "GB", "G", "TB", "T"}
for _, u := range units {
s := info[0]
if strings.HasSuffix(s, u) {
unit = u
numberStr = s[:len(s)-len(u)]
break
}
}
numberStr = strings.ReplaceAll(numberStr, ",", "")
numberStr = strings.TrimSpace(numberStr)
number, _ = strconv.ParseFloat(numberStr, 64)
switch unit {
case "KB", "K":
transferedBytes = int64(number * 1024)
case "MB", "M":
transferedBytes = int64(number * 1024 * 1024)
case "GB", "G":
transferedBytes = int64(number * 1024 * 1024 * 1024)
case "TB", "T":
transferedBytes = int64(number * 1024 * 1024 * 1024 * 1024)
default:
transferedBytes = int64(number)
}
return
}
// # Call this if you want to filter out verbose messages (-v or -vv) from
// # the output of an rsync run (whittling the output down to just the file
// # messages). This isn't needed if you use -i without -v.
// filter_outfile() {
// sed -e '/^building file list /d' \
// -e '/^sending incremental file list/d' \
// -e '/^created directory /d' \
// -e '/^done$/d' \
// -e '/ --whole-file$/d' \
// -e '/^total: /d' \
// -e '/^client charset: /d' \
// -e '/^server charset: /d' \
// -e '/^$/,$d' \
// <"$outfile" >"$outfile.new"
// mv "$outfile.new" "$outfile"
// }
func isFilename(str string) bool {
if str == "" {
return false
}
if strings.HasPrefix(str, " ") {
return false
}
if strings.HasPrefix(str, " ") {
return false
}
verbose := []string{
"building file list ",
"sending ",
"created ",
"done",
"total ",
"total: ",
"client ",
"server ",
"to consider",
"to-chk=",
"to-check=",
}
for _, v := range verbose {
if strings.Contains(str, v) {
return false
}
}
if strings.HasPrefix(str, "sent") {
return false
}
if strings.HasSuffix(str, "/") {
return false
}
return true
}