-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
212 lines (197 loc) · 4.43 KB
/
utils.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 installer
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"os"
"os/exec"
"path/filepath"
"time"
)
var TargetAlreadyExistErr = errors.New("Target file already exists, please clean the install directory and retry")
var EmptyLinkArrayErr = errors.New("Link array is empty")
type StringMap = map[string]string
func osCopy(src, dst string, mode os.FileMode) (err error) {
srcFd, err := os.Open(src)
if err != nil {
return
}
defer srcFd.Close()
dstFd, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return
}
_, err = io.Copy(dstFd, srcFd)
if er := dstFd.Close(); err == nil && er != nil {
err = er
}
if err != nil {
os.Remove(dst)
return
}
return
}
func renameIfNotExist(src, dst string, mode os.FileMode) (err error) {
if _, e := os.Stat(dst); os.IsNotExist(e) {
if err = os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
return
}
} else {
return &os.LinkError{
Op: "rename",
Old: src,
New: dst,
Err: TargetAlreadyExistErr,
}
}
if err = os.Rename(src, dst); err != nil {
if crossDevice(err) {
if err = osCopy(src, dst, mode); err != nil {
return
}
os.Remove(src)
return
}
return
}
os.Chmod(dst, mode)
return
}
func safeDownload(reader io.Reader, path string) (err error) {
var fd *os.File
if fd, err = os.OpenFile(path+".downloading", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644); err != nil {
return
}
defer os.Remove(fd.Name())
_, err = io.Copy(fd, reader)
fd.Close()
if err != nil {
return
}
if err = renameIfNotExist(fd.Name(), path, 0644); err != nil {
return
}
return nil
}
func lookJavaPath() (string, error) {
javahome := os.Getenv("JAVA_HOME")
if len(javahome) > 0 {
if path, err := exec.LookPath(filepath.Join(javahome, "bin", "java")); err == nil {
return path, nil
}
}
return exec.LookPath("java")
}
var hashesNewer = map[string]func() hash.Hash{
"md5": md5.New,
"sha1": sha1.New,
"sha224": sha256.New224,
"sha256": sha256.New,
"sha384": sha512.New384,
"sha512": sha512.New,
}
func checkHashStream(r io.Reader, hashes StringMap, w io.Writer) (n int64, err error) {
hashers := make([]hash.Hash, 0, len(hashes))
expects := make([][2]string, 0, len(hashes))
for h, sum := range hashes {
n, ok := hashesNewer[h]
if ok {
hashers = append(hashers, n())
expects = append(expects, [2]string{h, sum})
}
}
writers := make([]io.Writer, len(hashers), len(hashers)+1)
for i, h := range hashers {
writers[i] = h
}
if w != nil {
writers = append(writers, w)
}
if n, err = io.Copy(io.MultiWriter(writers...), r); err != nil {
fmt.Printf("err is not nil: %T", err)
return
}
for i, h := range hashers {
sum := hex.EncodeToString(h.Sum(nil))
if expect := expects[i]; expect[1] != sum {
err = &HashErr{
Hash: expect[0],
Sum: sum,
Expect: expect[1],
}
return
}
}
return
}
func matchHashes(path string, hashes StringMap) (ok bool) {
fd, err := os.Open(path)
if err != nil {
return false
}
defer fd.Close()
_, err = checkHashStream(fd, hashes, nil)
return err == nil
}
func downloadAnyAndCheckHashes(links []string, path string, hashes StringMap, size int64) (err error) {
if matchHashes(path, hashes) {
return
}
if len(links) == 0 {
err = EmptyLinkArrayErr
return
}
for _, l := range links {
var tmp string
if tmp, err = DefaultHTTPClient.DownloadTmp(l, "*.downloading", 0644, hashes, size,
downloadingCallback(l)); err != nil {
continue
}
defer os.Remove(tmp)
if err = renameIfNotExist(tmp, path, 0644); err != nil {
return
}
break
}
return
}
var sizeUnits = []string{"B", "KB", "MB", "GB", "TB", "PB"}
func formatSize(bytes int64, format string) string {
b := (float32)(bytes)
var unit string
for _, u := range sizeUnits {
unit = u
if b <= 1000 {
break
}
b /= 1024
}
return fmt.Sprintf(format, b) + unit
}
type DlCallback = func(n int64, size int64)
func downloadingCallback(url string) DlCallback {
var last time.Time
var start time.Time
return func(n int64, size int64) {
if n == 0 {
start = time.Now()
last = start
loger.Infof("Downloading %q...", url)
return
}
if n == size {
loger.Infof("Downloaded %q (%v)", url, time.Since(start))
return
}
if time.Since(last) > time.Second {
last = time.Now()
loger.Infof("Downloading %q [%s/%s %.2f%%]\n", url, formatSize(n, "%.2f"), formatSize(size, "%.2f"), (float32)(n)/(float32)(size)*100)
}
}
}