-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrarFolder.go
129 lines (114 loc) · 3.24 KB
/
rarFolder.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
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/schollz/progressbar/v3"
)
func rarFolder(path string, tempPath string, folderSize uint64) error {
Log.Info("Starting rar process")
var (
volumeSize uint64
parameters []string
cmdReader io.ReadCloser
scanner *bufio.Scanner
rarProgressBar *progressbar.ProgressBar
err error
)
if volumeSize, err = setVolumeSize(folderSize, conf.ArticleSize); err != nil {
return err
}
Log.Debug("Rar volume size: %v Bytes", volumeSize)
parameters = append(parameters, "a", "-o+", "-idcd", "-ep1", "-r")
parameters = append(parameters, fmt.Sprintf("-m%v", conf.Compression))
if conf.Encrypt || conf.Password != "" {
if conf.Password == "" {
conf.Password = randomString(conf.PasswordLength, 0, true)
}
parameters = append(parameters, fmt.Sprintf("-hp%v", conf.Password))
}
if volumeSize > 0 {
parameters = append(parameters, fmt.Sprintf("-v%vb", volumeSize))
}
rarName := ""
if conf.ObfuscateRar {
rarName = shortHeader
} else {
rarName = filepath.Base(path)
}
parameters = append(parameters, filepath.Join(tempPath, rarName+".rar"))
parameters = append(parameters, filepath.Join(path, "*"))
cmd := exec.Command(conf.RarExe, parameters...)
Log.Debug("Rar 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)
go func() {
// progress bar
if conf.Verbose > 0 {
rarProgressBar = progressbar.NewOptions(int(100),
progressbar.OptionSetDescription("INFO: Creating rar "),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionThrottle(time.Millisecond*100),
progressbar.OptionShowElapsedTimeOnFinish(),
progressbar.OptionOnCompletion(newline),
)
}
for scanner.Scan() {
output := strings.Trim(scanner.Text(), " \r\n")
if conf.Debug {
if output != "" && !strings.Contains(output, "%") {
Log.Debug("RAR: %v", output)
}
}
if conf.Verbose > 0 {
exp := regexp.MustCompile(`0*(\d+)%`)
if output != "" && exp.MatchString(output) {
percentStr := exp.FindStringSubmatch(output)
percentInt, _ := strconv.Atoi(percentStr[1])
rarProgressBar.Set(percentInt)
}
}
}
}()
}
if err = cmd.Run(); err != nil {
return err
}
if conf.Verbose > 0 {
rarProgressBar.Finish()
}
Log.Info("Creation of rar archives successful")
return nil
}
func setVolumeSize(folderSize uint64, blockSize uint64) (uint64, error) {
defaultVolumeSize := calculateVolumeSize(conf.VolumeSize, blockSize)
if conf.MakeVolumes && defaultVolumeSize < folderSize {
if (conf.MaxVolumes == 0) || (folderSize/conf.MaxVolumes <= defaultVolumeSize) {
return defaultVolumeSize, nil
} else {
volumeSize := (folderSize / conf.MaxVolumes) + 1
return calculateVolumeSize(volumeSize, blockSize), nil
}
}
return 0, nil
}
func calculateVolumeSize(volumeSize uint64, blockSize uint64) uint64 {
if volumeSize > 0 {
multiplier := volumeSize / blockSize
if volumeSize%blockSize != 0 {
multiplier++
}
return multiplier * blockSize
}
return 0
}