-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
234 lines (192 loc) · 3.9 KB
/
main.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
package main
import (
"github.com/cheggaaa/pb/v3"
"github.com/logrusorgru/aurora"
"github.com/urfave/cli/v2"
"log"
"math"
"os"
"strconv"
"strings"
)
type tMyFile struct {
*os.File
size int64
}
var mHideBar bool
var mNotation = []string{"", "k", "m", "g", "t", "p", "e", "z"}
func main() {
app := cli.NewApp()
app.Name = "gdd"
app.EnableBashCompletion = true
app.Usage = "A dd but in GOLANG !!"
app.CustomAppHelpTemplate =
`NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.HelpName}} -i <file> -o <file> [options]
OPTIONS:
{{range .VisibleFlags}}{{.}}
{{end}}
`
app.Flags = []cli.Flag{
&cli.PathFlag{
Name: "input",
Aliases: []string{"i"},
Usage: "Input File",
Required: true,
TakesFile: true,
},
&cli.PathFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "Output File",
Required: true,
TakesFile: true,
},
&cli.BoolFlag{
Name: "hide-progress",
Aliases: []string{"hp"},
Usage: "Remove Progress bar",
Destination: &mHideBar,
HasBeenSet: false,
},
&cli.StringFlag{
Name: "block-size",
Aliases: []string{"bs"},
Usage: "Size of blocks in bytes",
Value: "512",
},
&cli.StringFlag{
Name: "block-count",
Aliases: []string{"count", "c"},
Usage: "cont of blocks in bytes",
Value: "-1",
},
}
app.Action = func(context *cli.Context) error {
input := getFile(context.Path("input"), false)
output := getFile(context.Path("output"), true)
var bar *pb.ProgressBar
defer func() {
_ = input.Close()
}()
defer func() {
_ = output.Close()
}()
block := make([]byte, getInt64(context.String("block-size")))
bc := context.String("block-count")
var blockCount int64
if bc == "-1" {
blockCount = -1
} else {
blockCount = getInt64(bc)
}
var pos int64 = 0
var count int64 = 0
if !mHideBar {
if blockCount != -1 {
bar = pb.Full.Start64(blockCount)
} else {
bar = pb.Full.Start64(input.size)
}
bar.StartTime()
}
if blockCount != -1 {
for copyFile2File(input, output, &block, &pos) {
count++
if bar != nil {
bar.SetCurrent(count)
}
if count >= blockCount {
break
}
}
} else {
for copyFile2File(input, output, &block, &pos) {
if bar != nil {
bar.SetCurrent(pos)
}
}
}
if bar != nil {
bar.Finish()
}
return nil
}
err := app.Run(os.Args)
if err != nil {
logErrorFatal(err)
}
}
func copyFile2File(input, output *tMyFile, block *[]byte, pos *int64) bool {
c, _ := input.ReadAt(*block, *pos)
if c == 0 {
return false
}
_, err := output.WriteAt((*block)[0:c], *pos)
if err != nil {
logErrorFatal(err)
return false
}
*pos += int64(c)
return true
}
func getInt64(s string) int64 {
m := 0
n := strings.ToLower(s)
for i := 1; i < len(mNotation); i++ {
if strings.Contains(n, mNotation[i]) {
m = i * 3
n = strings.Replace(n, mNotation[i], "", -1)
break
}
}
coefficient, err := strconv.ParseInt(n, 10, 64)
if err != nil {
logErrorFatal("cant convert ", s)
}
result := coefficient * int64(math.Pow10(m))
if result < 0 {
logErrorFatal(s, "is negative")
}
return result
}
func getFile(path string, isOutput bool) (file *tMyFile) {
info, err := os.Stat(path)
if os.IsNotExist(err) && !isOutput {
logErrorFatal(err)
os.Exit(404)
}
var f *os.File
var size int64
if isOutput {
f, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if f == nil {
logErrorFatal("cant open or create the file", path)
os.Exit(1)
}
if info != nil && info.Size() != 0 {
errTruncate := f.Truncate(0)
if errTruncate != nil {
logErrorFatal(errTruncate)
os.Exit(1)
}
}
size = 0
} else {
f, err = os.OpenFile(path, os.O_RDWR, os.ModePerm)
size = info.Size()
}
if err != nil {
logErrorFatal(err)
os.Exit(1)
}
return &tMyFile{
f,
size,
}
}
func logErrorFatal(v ...interface{}) {
log.Fatal(aurora.Red(aurora.Bold(v)))
}