Repository navigation
Expand file tree
/
Copy pathutils.go
More file actions
405 lines (355 loc) · 11 KB
/
Copy pathutils.go
File metadata and controls
405 lines (355 loc) · 11 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/time/rate"
)
// FileStats represents statistics for a category of files
type FileStats struct {
Dirs int
Files int
Bytes int64
}
// RobocopyStats represents all statistics from a robocopy operation
type RobocopyStats struct {
// Categories of statistics
Total FileStats
Copied FileStats
Skipped FileStats
Mismatch FileStats
Failed FileStats
Extras FileStats
// Speed information
BytesPerSec int64
MegaBytesPerMin float64
// Duration
Duration time.Duration
// Exit code
ExitCode int
}
// improved regex patterns for file detection - to be used in main.go
var (
// File copying patterns with more specific matches for robocopy output
reFileCopying = regexp.MustCompile(`^\s*(?:New File|File)\s+(\d+)\s+(.+)`)
// reFileCopying2 = regexp.MustCompile(`^\s*(\d+)%\s+(.+)`)
reFileProgress = regexp.MustCompile(`(\d+\.\d+|\d+)\%`)
// Summary parsing patterns
reDirs = regexp.MustCompile(`^\s*Dirs\s*:\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)`)
reFiles = regexp.MustCompile(`^\s*Files\s*:\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)`)
reBytes = regexp.MustCompile(`^\s*Bytes\s*:\s*([0-9.]+\s*[kmgKMG]?)\s+([0-9.]+\s*[kmgKMG]?)\s+([0-9.]+\s*[kmgKMG]?)\s+([0-9.]+\s*[kmgKMG]?)\s+([0-9.]+\s*[kmgKMG]?)\s+([0-9.]+\s*[kmgKMG]?)`)
reSpeedBytes = regexp.MustCompile(`^\s*Speed\s*:\s*(\d+)\s*Bytes\/sec`)
reSpeedMB = regexp.MustCompile(`^\s*Speed\s*:\s*([0-9.]+)\s*MegaBytes\/min`)
reSummaryStart = regexp.MustCompile(`^\s*Total\s+Copied\s+Skipped\s+Mismatch\s+FAILED\s+Extras`)
)
// var progressMsgLimiter = rate.NewLimiter(1000 / 50, 3) // 1000 / N = 1 event per N ms
var progressMsgLimiter = rate.Sometimes{Interval: time.Millisecond*25} // 1 event per N ms
// var progressMsgLimiter = rate.Sometimes{Every: 20} // 1 event per N ms
func parseStreaming(stdout io.Reader, stats *RobocopyStats) error {
scanner := bufio.NewScanner(stdout)
split := func (data []byte, atEOF bool) (advance int, token []byte, err error) {
// similar to bufio.ScanLines but also splits on \r
if atEOF && len(data) == 0 {
return 0, nil, nil
} else if i := bytes.Index(data, []byte("\r\n")); i >= 0 {
return i + 2, data[0:i], nil
} else if i := bytes.IndexByte(data, '\n'); i >= 0 {
return i + 1, data[0:i], nil
} else if i := bytes.IndexByte(data, '\r'); i >= 0 {
return i + 1, 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
}
scanner.Split(split)
inSummary := false
// Replace the parsing section in copyWithProgress() function with:
var fileSize int64
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
// Debug line to see what's coming from robocopy (uncomment if needed)
// logger.Printf("DEBUG: %s\n", strconv.Quote(line))
// Check if we're in the summary section
if reSummaryStart.MatchString(line) {
// logger.Infof("IN SUMMARY : FOUND %v", line)
inSummary = true
if p != nil {
p.Send(tickMsg{})
}
continue
}
if inSummary {
// # Parse summary information
// Dirs
if matches := reDirs.FindStringSubmatch(line); len(matches) > 6 {
stats.Total.Dirs, _ = strconv.Atoi(matches[1])
stats.Copied.Dirs, _ = strconv.Atoi(matches[2])
stats.Skipped.Dirs, _ = strconv.Atoi(matches[3])
stats.Mismatch.Dirs, _ = strconv.Atoi(matches[4])
stats.Failed.Dirs, _ = strconv.Atoi(matches[5])
stats.Extras.Dirs, _ = strconv.Atoi(matches[6])
continue
}
// Files
if matches := reFiles.FindStringSubmatch(line); len(matches) > 6 {
stats.Total.Files, _ = strconv.Atoi(matches[1])
stats.Copied.Files, _ = strconv.Atoi(matches[2])
stats.Skipped.Files, _ = strconv.Atoi(matches[3])
stats.Mismatch.Files, _ = strconv.Atoi(matches[4])
stats.Failed.Files, _ = strconv.Atoi(matches[5])
stats.Extras.Files, _ = strconv.Atoi(matches[6])
continue
}
// Bytes
if matches := reBytes.FindStringSubmatch(line); len(matches) > 6 {
stats.Total.Bytes = parseByteValue(matches[1])
stats.Copied.Bytes = parseByteValue(matches[2])
stats.Skipped.Bytes = parseByteValue(matches[3])
stats.Mismatch.Bytes = parseByteValue(matches[4])
stats.Failed.Bytes = parseByteValue(matches[5])
stats.Extras.Bytes = parseByteValue(matches[6])
continue
}
// Speed (Bytes/sec)
if matches := reSpeedBytes.FindStringSubmatch(line); len(matches) > 1 {
stats.BytesPerSec, _ = strconv.ParseInt(matches[1], 10, 64)
continue
}
// Speed (MB/min)
if matches := reSpeedMB.FindStringSubmatch(line); len(matches) > 1 {
stats.MegaBytesPerMin, _ = strconv.ParseFloat(matches[1], 64)
continue
}
} else {
// # Try to detect which file is being processed
// Reset file size for this line
// fileSize = 0
// Look for a file size on the line first
// if sizeMatches := reFileSize.FindStringSubmatch(line); len(sizeMatches) > 1 {
// fileSize = parseByteValue(sizeMatches[1])
// }
// Try pattern 1 for file copying
if matches := reFileCopying.FindStringSubmatch(line); len(matches) > 2 {
fileSize = parseByteValue(matches[1])
p.Send(UpdateMsg{matches[2], fileSize, 0})
// m.UpdateProcessor(matches[2], fileSize)
continue
}
// if !progressMsgLimiter.Allow() {
// logger.Infof("trying to match %v: %v", line, reFileProgress.FindStringSubmatch(line))
// }
if matches := reFileProgress.FindStringSubmatch(line); len(matches) == 2 {
// logger.Infof("MATCH PROGRESS %v", line)
progress, err := strconv.ParseFloat(matches[1], 32)
if err != nil {
continue
}
if progress == 100 {
// always send completions
p.Send(ProgressMsg{fileProg: float32(progress)})
} else {
progressMsgLimiter.Do(func() {
p.Send(ProgressMsg{fileProg: float32(progress)})
// p.Printf("macthing %v -> %v", line, matches)
})
}
continue
}
// logger.Warnf("Could not match line %v", line)
// // Try pattern 2 for file copying (When /NC is used)
// if matches := reFileCopying2.FindStringSubmatch(line); len(matches) > 1 {
// p.Send(UpdateMsg{matches[1], true, fileSize})
// continue
// }
}
}
if err := scanner.Err(); err != nil {
logger.Errorf("error while reading stdout: %v", err)
return err
}
return nil
}
// parseByteValue converts a robocopy byte value string (like "10.5 m") to bytes
func parseByteValue(byteStr string) int64 {
byteStr = strings.TrimSpace(byteStr)
if byteStr == "0" || byteStr == "" {
return 0
}
// Extract the number part and the unit (if any)
parts := strings.Fields(byteStr)
if len(parts) == 0 {
return 0
}
value, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return 0
}
// Handle units if present
if len(parts) > 1 {
unit := strings.ToLower(parts[1])
switch unit {
case "k", "kb":
value *= 1024
case "m", "mb":
value *= 1024 * 1024
case "g", "gb":
value *= 1024 * 1024 * 1024
case "t", "tb":
value *= 1024 * 1024 * 1024 * 1024
}
}
return int64(value)
}
// formatByteValue formats a byte count to a human-readable string
func formatByteValue(bytes int64) string {
const (
KB = 1024
MB = KB * 1024
GB = MB * 1024
TB = GB * 1024
)
switch {
case bytes >= GB*100:
return fmt.Sprintf("%.2f TB", float64(bytes)/TB)
case bytes >= MB*100:
return fmt.Sprintf("%.2f GB", float64(bytes)/GB)
case bytes >= KB*100:
return fmt.Sprintf("%.2f MB", float64(bytes)/MB)
case bytes >= 100:
return fmt.Sprintf("%.2f KB", float64(bytes)/KB)
default:
return fmt.Sprintf("%d B", bytes)
}
}
// displaySummary outputs the final statistics in a formatted way
func displaySummary(stats RobocopyStats) {
// #EE6FF8
// : because skipped files are not errors
if stats.Skipped.Bytes == 0 {
fmt.Printf(
"Copied %s over %s\n",
impStyle.Render(strconv.Itoa(stats.Copied.Files) + " files"),
impStyle.Render(strconv.Itoa(stats.Copied.Dirs) + " directories"),
)
fmt.Printf(
" %s in %s [%s/s]\n",
impStyle.Render(formatByteValue(stats.Copied.Bytes)),
impStyle.Render(strconv.FormatFloat(stats.Duration.Seconds(), 'f', 2, 64) + " seconds"),
helpStyle.Render(formatByteValue(stats.BytesPerSec)),
)
} else {
fmt.Printf(
"Copied %s (%s skipped) over %s (%s skipped)\n",
impStyle.Render(strconv.Itoa(stats.Copied.Files) + " files"),
helpStyle.Render(strconv.Itoa(stats.Skipped.Files)),
impStyle.Render(strconv.Itoa(stats.Copied.Dirs) + " directories"),
helpStyle.Render(strconv.Itoa(stats.Skipped.Dirs)),
)
fmt.Printf(
" %s (%s skipped) in %s [%s/s]\n",
impStyle.Render(formatByteValue(stats.Copied.Bytes)),
helpStyle.Render(formatByteValue(stats.Skipped.Bytes)),
impStyle.Render(strconv.FormatFloat(stats.Duration.Seconds(), 'f', 2, 64) + " seconds"),
helpStyle.Render(formatByteValue(stats.BytesPerSec)),
)
}
if stats.Mismatch.Files > 0 {
fmt.Printf("Mismatched files: %d\n", stats.Mismatch.Files)
}
if stats.Failed.Files > 0 {
fmt.Printf("Failed files: %d\n", stats.Failed.Files)
}
if stats.Extras.Files > 0 {
fmt.Printf("Extra files: %d\n", stats.Extras.Files)
}
// Display exit code and meaning
ex := "Exit code: " + strconv.Itoa(stats.ExitCode)
if stats.ExitCode > 8 {
ex = errorStyle.Render(ex)
}
fmt.Println(ex)
if stats.ExitCode > 0 {
power := 5
rem := stats.ExitCode
for power >= 0 && rem > 0 {
r := rem >> power
logger.Debugf("exit code iteration power=%d, r=%d, rem=%d", power, r, rem)
if r > 0 {
p := PowInt(2, power)
logger.Debugf("printing for p=%d", p)
explainExitCode(p)
rem -= p
}
power -= 1
}
} else {
explainExitCode(stats.ExitCode)
}
}
// explainExitCode provides a description of what the robocopy exit code means
func explainExitCode(code int) {
switch code {
case 0:
fmt.Println("No files were copied. No failure was encountered.")
case 1:
fmt.Println("One or more files were copied successfully.")
case 2:
fmt.Println("Extra files or directories were detected.")
case 4:
fmt.Println("Some mismatched files or directories were detected.")
case 8:
fmt.Println(errorStyle.Render("Some files or directories could not be copied."))
case 16:
fmt.Println(errorStyle.Render("Serious error. Robocopy did not copy any files."))
default:
logger.Error("Unrecognized status", "exitcode", code)
}
}
func PowInt(base, exp int) int {
result := 1
for {
if exp&1 == 1 {
result *= base
}
exp >>= 1
if exp == 0 {
break
}
base *= base
}
return result
}
func getUserInputYN() (choice string) {
invalidChoice := func () (bool) {
return choice != "y" && choice != "n"
}
for range 3 {
_,err := fmt.Scan(&choice)
if err != nil {
logger.Error("Invalid input. Please type either y/Y or n/N.")
continue
}
if invalidChoice() {
logger.Errorf("Invalid input (%v). Please type either y/Y or n/N.", strconv.Quote(choice))
continue
}
return
}
if invalidChoice() {
logger.Fatal("Failed to answer.")
}
logger.Infof("You chose %v", choice)
return
}