-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (153 loc) · 3.53 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
package main
import (
"flag"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
var recursive bool
type Progress struct {
BytesCopied int64
TotalBytes int64
FilesCopied int
TotalFiles int
}
func main() {
flag.BoolVar(&recursive, "r", false, "copy directories recursively")
flag.Parse()
if flag.NArg() < 2 {
fmt.Println("Error: Too few arguments.")
fmt.Println("Usage: copy -r /path/to/source /path/to/destination")
os.Exit(1)
}
if flag.NArg() > 2 {
fmt.Println("Error: Too many arguments.")
fmt.Println("Usage: copy -r /path/to/source /path/to/destination")
os.Exit(1)
}
source := flag.Arg(0)
destination := flag.Arg(1)
fmt.Printf("copying: %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
if recursive {
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 := copyFile(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)
}
} else {
info, err := os.Stat(source)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
wg.Add(1)
go func() {
defer wg.Done()
err = copyFile(source, destination, info.Mode(), progressChan, &progress)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}()
}
wg.Wait()
close(progressChan)
duration := time.Since(startTime)
fmt.Printf("\nCopying took %s... done!\n", duration)
}
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.BytesCopied += int64(n)
progressChan <- *progress
if err == io.EOF {
break
}
}
progress.FilesCopied++
progressChan <- *progress
return nil
}
func displayProgress(progressChan <-chan Progress) {
for progress := range progressChan {
fmt.Printf("\r%d files - %.1fGB [%s]",
progress.FilesCopied,
float64(progress.BytesCopied)/(1<<30),
progressBar(progress.BytesCopied, 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
}