-
Notifications
You must be signed in to change notification settings - Fork 0
/
natives.go
104 lines (84 loc) · 2.07 KB
/
natives.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
package launcher
import (
"archive/zip"
"crypto/sha1"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/brawaru/marct/utils"
"github.com/brawaru/marct/utils/unzipper"
"github.com/brawaru/marct/validfile"
)
func (w *Instance) ExtractNatives(v Version) (string, error) {
np := filepath.Join(w.Path, "bin", utils.NewUUID())
if err := os.MkdirAll(np, 0777); err != nil {
return "", fmt.Errorf("cannot create bin directory: %w", err)
}
lp := w.LibrariesPath()
nativeValidator := func(e *unzipper.Unzipper, name string, dst string, f *os.File) error {
p := name + ".sha1"
if !e.Entries.HasKey(p) {
return nil
}
ce := e.Entries.Get(p)
rc, err := ce.Open()
if err != nil {
return &unzipper.AbortErr{
Err: fmt.Errorf("cannot open %q: %w", p, err),
}
}
defer utils.DClose(rc)
var s string
{
buf := new(strings.Builder)
if _, err := io.Copy(buf, rc); err != nil {
return &unzipper.AbortErr{
Err: fmt.Errorf("cannot read %q: %w", p, err),
}
}
s = buf.String()
}
s = strings.TrimRight(s, "\r\n")
err = validfile.ValidateFileHex(dst, sha1.New(), s)
var v *validfile.ValidateError
if errors.As(err, &v) && !v.Mismatch() {
return &unzipper.AbortErr{
Err: v.Err,
}
}
return err
}
metaSkipper := func(o *ExtractOptions) unzipper.EntryProcessor {
return func(e *unzipper.Unzipper, name string, f *zip.File, dest string) error {
if !f.FileInfo().IsDir() {
switch filepath.Ext(name) {
case ".sha1":
fallthrough
case ".git":
return unzipper.ErrSkip
default:
return nil
}
}
if o == nil || o.Include(name) {
return nil
}
return unzipper.ErrSkip
}
}
for _, l := range v.Libraries {
n := l.GetMatchingNatives()
if n == nil {
continue
}
ap := filepath.Join(lp, filepath.FromSlash(n.Path))
err := unzipper.Unzip(ap, np, unzipper.WithFileValidator(nativeValidator), unzipper.WithEntryProcessor(metaSkipper(l.Extract)))
if err != nil {
return np, fmt.Errorf("extract native %q: %w", l.Coordinates.String(), err)
}
}
return np, nil
}