-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.go
207 lines (190 loc) · 4.01 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
package dpkg
import (
"bytes"
"compress/bzip2"
"compress/gzip"
"crypto/md5"
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path"
"sort"
"strings"
"time"
"unicode"
)
func WriteToFile(content []byte, target string, mode os.FileMode) error {
err := EnsureDirectory(path.Dir(target))
if err != nil {
return err
}
return ioutil.WriteFile(target, content, mode)
}
type readCloserWrap struct {
io.Reader
cs []io.Closer
}
func (w readCloserWrap) Close() error {
for _, c := range w.cs {
c.Close()
}
return nil
}
func ReadFile(fpath string) (io.ReadCloser, error) {
f, err := os.Open(fpath)
if err != nil {
return nil, err
}
switch strings.ToLower(path.Ext(fpath)) {
case ".gz":
r, err := gzip.NewReader(f)
if err != nil {
return nil, FormatError{"LoadControlFileGroup", fpath, err}
}
return readCloserWrap{r, []io.Closer{r, f}}, nil
case ".bz2":
r := bzip2.NewReader(f)
return readCloserWrap{r, []io.Closer{f}}, nil
default:
return f, nil
}
}
var httpClient = http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: func(_ string, addr string) (net.Conn, error) {
return net.Dial("tcp4", addr)
},
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
func DownloadTo(url string, w io.Writer) error {
reps, err := http.Get(url)
if err != nil {
return fmt.Errorf("can't download %q : %v", url, err)
}
defer reps.Body.Close()
if reps.StatusCode != 200 {
return fmt.Errorf("can't download %q : %v", url, reps.Status)
}
_, err = io.Copy(w, reps.Body)
return err
}
// download download the url content to "dest" file.
func DownloadToFile(url string, dest string) error {
DebugPrintf("Downloading %q to %q\n", url, dest)
if err := EnsureDirectory(path.Dir(dest)); err != nil {
return err
}
f, err := os.Create(dest)
if err != nil {
return fmt.Errorf("Can't create file %s", url)
}
defer f.Close()
return DownloadTo(url, f)
}
func IntersectionSet(s1, s2 []string) []string {
var ret []string
for _, i := range s1 {
for _, j := range s2 {
if i == j {
ret = append(ret, i)
break
}
}
}
return ret
}
func UnionSet(s1, s2 []string) []string {
var cache = make(map[string]struct{})
for _, i := range append(s1, s2...) {
cache[i] = struct{}{}
}
var ret []string
for i := range cache {
ret = append(ret, i)
}
return ret
}
func HashFiles(fpaths ...string) (string, error) {
var hashs []string
for _, f := range fpaths {
v, err := HashFile(f)
if err != nil {
return "", err
}
hashs = append(hashs, v)
}
return HashArrayString(hashs), nil
}
func HashArrayString(s []string) string {
sort.Strings(s)
var r string
for _, h := range s {
r += h
}
return HashBytes([]byte(r))
}
func HashBytes(bs []byte) string {
hash := md5.New()
hash.Write(bs)
var r [16]byte
copy(r[:], hash.Sum(nil))
return fmt.Sprintf("%x", r)
}
func HashFile(fpath string) (string, error) {
bs, err := ioutil.ReadFile(fpath)
if err != nil {
return "", err
}
return HashBytes(bs), nil
}
func TrimLeftSpace(d []byte) []byte {
return bytes.TrimFunc(d, unicode.IsSpace)
}
func EnsureDirectory(t string) error {
s, err := os.Stat(t)
if err != nil {
err := os.MkdirAll(t, 0755)
if err != nil {
DebugPrintf("EnsureDirectory failed: %v", err)
}
return err
} else {
if !s.IsDir() {
return fmt.Errorf("%q is a regular file", t)
}
}
return nil
}
func sortMapString(d map[string]struct{}) []string {
var r = make([]string, 0)
for k := range d {
r = append(r, k)
}
sort.Strings(r)
return r
}
func loadGOB(fpath string, obj interface{}) error {
f, err := os.Open(fpath)
if err != nil {
return fmt.Errorf("store %q failed --> %v.", fpath, err)
}
defer f.Close()
return gob.NewDecoder(f).Decode(obj)
}
func storeGOB(fpath string, obj interface{}) error {
f, err := os.Create(fpath)
if err != nil {
return fmt.Errorf("store %q failed --> %v.", fpath, err)
}
defer f.Close()
return gob.NewEncoder(f).Encode(obj)
}