forked from lhcb-org/lbpkr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
251 lines (223 loc) · 4.58 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"archive/tar"
"compress/gzip"
"fmt"
"io"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
)
// newCommand is like os/exec.Command but ensures the subprocess is part of a process-group
func newCommand(name string, args ...string) *exec.Cmd {
cmd := exec.Command(name, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return cmd
}
// killProcess kills a process by way of its process-group.
func killProcess(p *os.Process) error {
pgid, err := syscall.Getpgid(p.Pid)
if err != nil {
return err
}
err = syscall.Kill(-pgid, syscall.SIGKILL) // note the minus sign
return err
}
func path_exists(name string) bool {
_, err := os.Stat(name)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func handle_err(err error) {
if err != nil {
if g_ctx != nil {
g_ctx.msg.Errorf("%v\n", err.Error())
} else {
fmt.Fprintf(os.Stderr, "**error** %v\n", err)
}
os.Exit(1)
}
}
func bincp(dst, src string) error {
fsrc, err := os.Open(src)
if err != nil {
return err
}
defer fsrc.Close()
fdst, err := os.Create(dst)
if err != nil {
return err
}
defer fdst.Close()
fisrc, err := os.Stat(src)
if err != nil {
return err
}
err = fdst.Chmod(fisrc.Mode())
if err != nil {
return err
}
_, err = io.Copy(fdst, fsrc)
return err
}
func _tar_gz(targ, workdir string) error {
f, err := os.Create(targ)
if err != nil {
return err
}
zout := gzip.NewWriter(f)
tw := tar.NewWriter(zout)
err = filepath.Walk(workdir, func(path string, fi os.FileInfo, err error) error {
//fmt.Printf("::> [%s]...\n", path)
if !strings.HasPrefix(path, workdir) {
err = fmt.Errorf("walked filename %q doesn't begin with workdir %q", path, workdir)
return err
}
name := path[len(workdir):] //path
// make name "relative"
if strings.HasPrefix(name, "/") {
name = name[1:]
}
target, _ := os.Readlink(path)
if err != nil {
return err
}
hdr, err := tar.FileInfoHeader(fi, target)
if err != nil {
return err
}
hdr.Name = name
hdr.Uname = "root"
hdr.Gname = "root"
hdr.Uid = 0
hdr.Gid = 0
// Force permissions to 0755 for executables, 0644 for everything else.
if fi.Mode().Perm()&0111 != 0 {
hdr.Mode = hdr.Mode&^0777 | 0755
} else {
hdr.Mode = hdr.Mode&^0777 | 0644
}
err = tw.WriteHeader(hdr)
if err != nil {
return fmt.Errorf("Error writing file %q: %v", name, err)
}
// handle directories and symlinks
if hdr.Size <= 0 {
return nil
}
r, err := os.Open(path)
if err != nil {
return err
}
defer r.Close()
_, err = io.Copy(tw, r)
return err
})
if err != nil {
return err
}
if err := tw.Close(); err != nil {
return err
}
if err := zout.Close(); err != nil {
return err
}
return f.Close()
}
func sanitizePathOrURL(path string) (string, error) {
switch {
case strings.Contains(path, "://"):
// a url. hopefully a correctly formed one.
return path, nil
case strings.Contains(path, ":/"):
// maybe a url. hopefully a correctly formed one.
return path, nil
}
p, err := filepath.Abs(path)
if err != nil {
return path, err
}
// p, err = filepath.EvalSymlinks(p)
// if err != nil {
// return path, err
// }
p = filepath.Clean(p)
return p, nil
}
func getRemoteData(rpath string) (io.ReadCloser, error) {
url, err := url.Parse(rpath)
if err != nil {
return nil, err
}
switch url.Scheme {
case "file":
f, err := os.Open(url.Path)
if err != nil {
return nil, err
}
return f, nil
default:
resp, err := http.Get(rpath)
if err != nil {
return nil, err
}
return resp.Body, nil
}
}
var (
rpmRe1 = regexp.MustCompile(`(.*?)-([\d\.]+)$`)
rpmRe2 = regexp.MustCompile(`(.*?)-([\d\.]+)-(\d*)$`)
)
// splitRPM splits a RPM package name into name-version-release
func splitRPM(rpm string) [3]string {
switch strings.Count(rpm, "-") {
case 0:
return [3]string{rpm, "", ""}
case 1:
re := rpmRe1.FindAllStringSubmatch(rpm, -1)
if len(re) == 1 {
m := re[0]
switch len(m) {
case 2:
return [3]string{m[1], "", ""}
case 3:
return [3]string{m[1], m[2], ""}
}
}
default:
re := rpmRe2.FindAllStringSubmatch(rpm, -1)
if len(re) == 1 {
m := re[0]
switch len(m) {
case 2:
return [3]string{m[1], "", ""}
case 3:
return [3]string{m[1], m[2], ""}
case 4:
return [3]string{m[1], m[2], m[3]}
}
}
re = rpmRe1.FindAllStringSubmatch(rpm, -1)
if len(re) == 1 {
m := re[0]
switch len(m) {
case 2:
return [3]string{m[1], m[2], ""}
case 3:
return [3]string{m[1], m[2], ""}
}
}
}
return [3]string{rpm, "", ""}
}
// EOF