-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.go
87 lines (75 loc) · 2.18 KB
/
lookup.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
// Copyright 2019 Vanessa Sochat. All rights reserved.
// Use of this source code is governed by the Polyform Strict license
// that can be found in the LICENSE file and available at
// https://polyformproject.org/licenses/noncommercial/1.0.0
package main
import (
"bytes"
"encoding/binary"
"fmt"
)
// GetGoArch returns the go runtime arch code from the SIF arch code.
// https://github.com/sylabs/sif/blob/master/pkg/sif/lookup.go#L48
func GetGoArch(sifarch string) (goarch string) {
var ok bool
archMap := map[string]string{
HdrArch386: "386",
HdrArchAMD64: "amd64",
HdrArchARM: "arm",
HdrArchARM64: "arm64",
HdrArchPPC64: "ppc64",
HdrArchPPC64le: "ppc64le",
HdrArchMIPS: "mips",
HdrArchMIPSle: "mipsle",
HdrArchMIPS64: "mips64",
HdrArchMIPS64le: "mips64le",
HdrArchS390x: "s390x",
}
if goarch, ok = archMap[sifarch]; !ok {
goarch = "unknown"
}
return goarch
}
// GetPartPrimSys returns the primary system partition if present. There should
// be only one primary system partition in a SIF file.
// https://github.com/sylabs/sif/blob/master/pkg/sif/lookup.go#L400
func (fimg *FileImage) GetPartPrimSys() (*Descriptor, int, error) {
var descr *Descriptor
index := -1
for i, v := range fimg.DescrArr {
if !v.Used {
continue
} else {
if v.Datatype == DataPartition {
ptype, err := v.GetPartType()
if err != nil {
return nil, -1, err
}
if ptype == PartPrimSys {
if index != -1 {
return nil, -1, ErrMultValues
}
index = i
descr = &fimg.DescrArr[i]
}
}
}
}
if index == -1 {
return nil, -1, ErrNotFound
}
return descr, index, nil
}
// GetPartType extracts the Parttype field from the Extra field of a Partition Descriptor.
// https://github.com/sylabs/sif/blob/master/pkg/sif/lookup.go#L300
func (d *Descriptor) GetPartType() (Parttype, error) {
if d.Datatype != DataPartition {
return -1, fmt.Errorf("expected DataPartition, got %v", d.Datatype)
}
var pinfo Partition
b := bytes.NewReader(d.Extra[:])
if err := binary.Read(b, binary.LittleEndian, &pinfo); err != nil {
return -1, fmt.Errorf("while extracting Partition extra info: %s", err)
}
return pinfo.Parttype, nil
}