-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
142 lines (123 loc) · 2.65 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
package main
import (
"fmt"
"log"
"os"
"path"
)
const ChunkSize = 64 * 1024 * 1024
var isVerbose = false
func getFileSize(filename string) (int64, error) {
fileInfo, err := os.Stat(filename)
if err != nil {
return 0, err
}
return fileInfo.Size(), nil
}
func readBlock(file *os.File, offset int64, size int64) (int, []byte, error) {
_, err := file.Seek(offset, 0)
if err != nil {
return 0, nil, err
}
block := make([]byte, size)
bytesRead, err := file.Read(block)
if err != nil {
return 0, nil, err
}
return bytesRead, block, nil
}
func appendBlock(file *os.File, sizeOfBlock int, block []byte) error {
_, err := file.Write(block[:sizeOfBlock])
if err != nil {
return err
}
// flush write
if isVerbose {
log.Printf("Flushing write to disk")
}
err = file.Sync()
if err != nil {
return err
}
return nil
}
func openFileRead(filename string) (*os.File, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return file, nil
}
func openFileWrite(filename string) (*os.File, error) {
file, err := os.Create(filename)
if err != nil {
return nil, err
}
return file, nil
}
func getPercentageRead(fileSize int64, fileByteCount int64) int {
return int((fileByteCount * 100) / fileSize)
}
func checkDestinationFileExists(filename string) bool {
_, err := os.Stat(filename)
if err != nil && !os.IsNotExist(err) {
fmt.Println(err)
os.Exit(1)
}
if os.IsNotExist(err) {
return false
}
return true
}
func main() {
args := os.Args[1:]
if len(args) != 2 {
fmt.Printf("Usage: %s <source> <destination>\n", os.Args[0])
os.Exit(1)
}
sourceFile := args[0]
destinationFilePath := args[1]
// check if file exists
if _, err := os.Stat(sourceFile); err != nil {
fmt.Println(err)
os.Exit(1)
}
// check if destination path exists
if _, err := os.Stat(destinationFilePath); err != nil {
fmt.Println(err)
os.Exit(1)
}
var sourceFilenName = path.Base(sourceFile)
var destinationFile = path.Join(destinationFilePath, sourceFilenName)
size, err := getFileSize(sourceFile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
f, e := openFileRead(sourceFile)
if e != nil {
fmt.Println(e)
os.Exit(1)
}
defer f.Close()
fdest, e := openFileWrite(destinationFile)
if e != nil {
fmt.Println(e)
os.Exit(1)
}
var fileByteCount = int64(0)
for {
count, b, e := readBlock(f, int64(fileByteCount), ChunkSize)
if e != nil {
fmt.Println(e)
os.Exit(1)
}
appendBlock(fdest, count, b)
if count == 0 || count < ChunkSize {
break
}
fileByteCount += int64(count)
fmt.Printf("Read %d bytes of %d bytes (%d%%)\n", fileByteCount, size, getPercentageRead(size, fileByteCount))
}
fmt.Println("Done")
}