-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpar2.go
121 lines (109 loc) · 2.78 KB
/
par2.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/schollz/progressbar/v3"
)
func par2() error {
Log.Info("Starting repair process")
var (
par2ExitCodes map[int]string
par2FileName string
parameters []string
cmdReader io.ReadCloser
scanner *bufio.Scanner
parProgressBar *progressbar.ProgressBar
err error
)
// par2 exit codes
par2ExitCodes = map[int]string{
0: "Success",
1: "Repair possible",
2: "Repair not possible",
3: "Invalid command line arguments",
4: "Insufficient critical data to verify",
5: "Repair failed",
6: "FileIO Error",
7: "Logic Error",
8: "Out of memory",
}
exp, _ := regexp.Compile(`^.+\.par2`)
if err = filepath.Walk(conf.TempPath, func(file string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && exp.MatchString(filepath.Base(info.Name())) {
par2FileName = info.Name()
}
return nil
}); err != nil {
checkForFatalErr(err)
}
// set parameters
parameters = append(parameters, "r", "-q")
if conf.DeletePar2 {
parameters = append(parameters, "-p")
}
parameters = append(parameters, filepath.Join(conf.TempPath, par2FileName))
cmd := exec.Command(conf.Par2Exe, parameters...)
Log.Debug("Par command: %s", cmd.String())
if conf.Debug || conf.Verbose > 0 {
// create a pipe for the output of the program
if cmdReader, err = cmd.StdoutPipe(); err != nil {
return err
}
scanner = bufio.NewScanner(cmdReader)
scanner.Split(scanLines)
go func() {
// progress bar
if conf.Verbose > 0 {
parProgressBar = progressbar.NewOptions(int(100),
progressbar.OptionSetDescription("INFO: Repairing files "),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(time.Millisecond*100),
progressbar.OptionShowElapsedTimeOnFinish(),
progressbar.OptionOnCompletion(newline),
)
}
for scanner.Scan() {
output := strings.Trim(scanner.Text(), " \r\n")
if output != "" && !strings.Contains(output, "%") {
Log.Debug("PAR: %v", output)
}
if conf.Verbose > 0 {
exp := regexp.MustCompile(`(\d+)\.?\d*%`)
if output != "" && exp.MatchString(output) {
percentStr := exp.FindStringSubmatch(output)
percentInt, _ := strconv.Atoi(percentStr[1])
parProgressBar.Set(percentInt)
}
}
}
}()
}
if err = cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
if parProgressBar != nil {
parProgressBar.Exit()
}
if errMsg, ok := par2ExitCodes[exitError.ExitCode()]; ok {
return fmt.Errorf(errMsg)
} else {
return fmt.Errorf("Unknown error")
}
}
}
if parProgressBar != nil {
parProgressBar.Finish()
}
Log.Info("Repair successful")
return nil
}