-
Notifications
You must be signed in to change notification settings - Fork 7
/
freeze.go
81 lines (78 loc) · 1.92 KB
/
freeze.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
package span
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"path/filepath"
json "github.com/segmentio/encoding/json"
)
// UnfreezeFilterConfig takes the name of a zipfile (from span-freeze) and
// returns of the path the thawed filterconfig (along with the temporary
// directory and error). When this function returns, all URLs in the
// filterconfig have then been replaced by absolute path on the file system.
// Cleanup of temporary directory is responsibility of caller.
func UnfreezeFilterConfig(frozenfile string) (dir, blob string, err error) {
var (
r *zip.ReadCloser
rc io.ReadCloser
ff *os.File
mappings = make(map[string]string)
b []byte
)
if dir, err = os.MkdirTemp("", "span-tag-unfreeze-"); err != nil {
return
}
if r, err = zip.OpenReader(frozenfile); err != nil {
return
}
defer r.Close()
if err = os.MkdirAll(filepath.Join(dir, "files"), 0777); err != nil {
return
}
for _, f := range r.File {
if rc, err = f.Open(); err != nil {
return
}
if ff, err = os.Create(filepath.Join(dir, f.Name)); err != nil {
return
}
if f.Name == "mapping.json" {
var (
buf bytes.Buffer
tr = io.TeeReader(rc, &buf)
)
if _, err = io.Copy(ff, tr); err != nil {
return
}
if err = json.NewDecoder(&buf).Decode(&mappings); err != nil {
return
}
} else {
if _, err = io.Copy(ff, rc); err != nil {
return
}
}
if err = rc.Close(); err != nil {
return
}
if err = ff.Close(); err != nil {
return
}
}
blob = filepath.Join(dir, "blob")
if b, err = os.ReadFile(blob); err != nil {
return
}
for url, file := range mappings {
value := []byte(fmt.Sprintf(`%q`, url))
// Debian is fine w/ file://, but fedora not?
replacement := []byte(fmt.Sprintf(`"file://%s"`, filepath.Join(dir, file)))
b = bytes.Replace(b, value, replacement, -1)
}
if err = os.WriteFile(blob, b, 0777); err != nil {
return
}
return dir, blob, nil
}