-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
212 lines (172 loc) · 3.85 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
package main
import (
"flag"
"fmt"
"hash/crc32"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type Progress struct {
BytesMoved int64
TotalBytes int64
FilesMoved int
TotalFiles int
}
func main() {
flag.Parse()
if flag.NArg() < 2 {
fmt.Println("Error: Too few arguments.")
fmt.Println("Usage: move /path/to/source /path/to/destination")
os.Exit(1)
}
if flag.NArg() > 2 {
fmt.Println("Error: Too many arguments.")
fmt.Println("Usage: move /path/to/source /path/to/destination")
os.Exit(1)
}
source := flag.Arg(0)
destination := flag.Arg(1)
fmt.Printf("moving: %s -> %s\n", source, destination)
progressChan := make(chan Progress)
go displayProgress(progressChan)
startTime := time.Now()
var totalFiles int
var totalBytes int64
filepath.Walk(source, func(srcPath string, info fs.FileInfo, err error) error {
if !info.IsDir() {
totalFiles++
totalBytes += info.Size()
}
return nil
})
var progress Progress
progress.TotalFiles = totalFiles
progress.TotalBytes = totalBytes
var wg sync.WaitGroup
err := filepath.Walk(source, func(srcPath string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(source, srcPath)
if err != nil {
return err
}
dstPath := filepath.Join(destination, relPath)
if info.IsDir() {
return os.Mkdir(dstPath, info.Mode())
}
wg.Add(1)
go func() {
defer wg.Done()
err := moveFile(srcPath, dstPath, info.Mode(), progressChan, &progress)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}()
return nil
})
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
wg.Wait()
close(progressChan)
duration := time.Since(startTime)
fmt.Printf("\nMoving took %s... done!\n", duration)
}
func moveFile(src, dst string, mode fs.FileMode, progressChan chan<- Progress, progress *Progress) error {
err := copyFile(src, dst, mode, progressChan, progress)
if err != nil {
return err
}
if !compareFiles(src, dst) {
return fmt.Errorf("CRC check failed for: %s and %s", src, dst)
}
err = os.Remove(src)
if err != nil {
return err
}
return nil
}
func compareFiles(file1, file2 string) bool {
f1, err := os.Open(file1)
if err != nil {
return false
}
defer f1.Close()
f2, err := os.Open(file2)
if err != nil {
return false
}
defer f2.Close()
h1 := crc32.NewIEEE()
h2 := crc32.NewIEEE()
_, err = io.Copy(h1, f1)
if err != nil {
return false
}
_, err = io.Copy(h2, f2)
if err != nil {
return false
}
return h1.Sum32() == h2.Sum32()
}
func copyFile(src, dst string, mode fs.FileMode, progressChan chan<- Progress, progress *Progress) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return err
}
defer dstFile.Close()
buf := make([]byte, 32*1024)
for {
n, err := srcFile.Read(buf)
if err != nil && err != io.EOF {
return err
}
if n == 0 {
break
}
if _, err := dstFile.Write(buf[:n]); err != nil {
return err
}
progress.BytesMoved += int64(n)
progressChan <- *progress
if err == io.EOF {
break
}
}
progress.FilesMoved++
progressChan <- *progress
return nil
}
func displayProgress(progressChan <-chan Progress) {
for progress := range progressChan {
fmt.Printf("\r%d files - %.1fGB [%s]",
progress.FilesMoved,
float64(progress.BytesMoved)/(1<<30),
progressBar(progress.BytesMoved, progress.TotalBytes, 20),
)
}
}
func progressBar(current, total int64, width int) string {
ratio := float64(current) / float64(total)
completeChars := int(ratio * float64(width))
progressBar := strings.Repeat("=", completeChars)
if completeChars < width {
progressBar += ">"
remainingChars := width - completeChars - 1
progressBar += strings.Repeat(" ", remainingChars)
}
return progressBar
}