-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patharch.go
242 lines (212 loc) · 5.22 KB
/
arch.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
package main
import (
"crypto/sha1"
"encoding/binary"
"errors"
"io"
"os"
"path/filepath"
"sort"
"strings"
"github.com/golang/glog"
)
// archIndex is a map of architecture identifiers (e.g., "x86_64") to
// parsed repodata.
type archIndex struct {
archs map[string]*RepoData
// Index listing
names []string
etag string
}
func (a *archIndex) Arch(name string) *RepoData {
if a == nil {
return nil
}
return a.archs[name]
}
func (a *archIndex) Index() []string {
if a == nil {
return nil
}
return a.names
}
func (a *archIndex) IndexETag() string {
if a == nil {
return ""
}
return a.etag
}
// loadArchIndices loads architecture-specific repodata into an
// archIndex and returns the resulting index.
//
// files is a list of files ending in -repodata or directories to be
// walked in search of -repodata files.
//
// A repodata file is of the form <arch>-repodata. So, x86_64-repodata
// is for the arch x86_64.
func loadArchIndices(files []string) (*archIndex, error) {
archs := &archIndex{
archs: map[string]*RepoData{},
names: []string{},
}
for _, rdpath := range files {
err := archs.loadPath(rdpath)
if err != nil {
return nil, err
}
}
return archs, archs.init()
}
func (a *archIndex) loadPath(path string) error {
fi, err := os.Stat(path)
if err != nil {
glog.Warningf("ignoring repodata path: stat failed: %v", err)
return nil
}
if fi.IsDir() {
glog.V(1).Infof("walking %s for repodata files", path)
return a.loadDir(path)
}
return a.loadFile(path, "")
}
func (a *archIndex) loadDir(searchRoot string) error {
searchRoot, err := filepath.Abs(searchRoot)
if err != nil {
return err
}
// Collect a list of -repodata files and load them.
var files []string
err = filepath.Walk(searchRoot, func(path string, wfi os.FileInfo, err error) error {
if wfi.IsDir() {
glog.V(2).Infof("walking %s for repodata files", path)
} else if strings.HasSuffix(path, "-repodata") {
files = append(files, path)
}
// TODO: Follow symlinks?
return nil
})
if err != nil {
return err
}
for _, path := range files {
repo := repositoryFromFileSearchRoot(searchRoot, path)
if err = a.loadFile(path, repo); err != nil {
return err
}
}
return nil
}
func (a *archIndex) loadFile(path, repo string) error {
glog.Infof("loading %s", path)
if !strings.HasSuffix(path, "-repodata") {
return &os.PathError{
Path: path,
Op: "read",
Err: errors.New("repodata files must end in -repodata"),
}
}
if repo == "" {
var ok bool
repo, ok = repositoryFromPath(path)
if !ok {
glog.V(1).Infof("unable to determine repository for %s; defaulting to %s",
path, defaultRepository)
repo = defaultRepository
}
}
arch := filepath.Base(strings.TrimSuffix(path, "-repodata"))
rd := a.archs[arch]
if rd == nil {
rd = NewRepoData()
a.archs[arch] = rd
}
packages := len(rd.Index())
err := rd.LoadRepo(path, repo)
if err != nil {
return &os.PathError{Path: path, Err: err, Op: "load"}
}
packages = len(rd.Index()) - packages
glog.V(1).Infof("loaded %s repo=%s new_packages=%d",
path, repo, packages)
return nil
}
func repositoryFromFileSearchRoot(searchRoot, path string) string {
// Add compatibility check when using /var/db/xbps repodata
if searchRoot == "/var/db/xbps" {
return ""
}
repo := filepath.ToSlash(filepath.Dir(path))
repo = strings.TrimPrefix(repo, searchRoot)
repo = strings.TrimPrefix(repo, "/")
repo = strings.TrimPrefix(repo, defaultRepository+"/")
if repo == "" || repo == "/" || repo == "." {
repo = defaultRepository
}
return repo
}
func repositoryFromPath(path string) (repo string, ok bool) {
abs, err := filepath.Abs(path)
if err != nil {
return "", false
}
dir := filepath.Dir(abs)
// Look for foo/bar/current/path-a/path-b in path and pull out
// everything from "current" and lower.
repo, ok = repositoryFromPathList(dir)
if !ok {
repo, ok = repositoryFromBase(filepath.Base(dir))
}
return repo, ok
}
func repositoryFromBase(path string) (repo string, ok bool) {
const currentSep = "_current_"
if sep := strings.LastIndex(path, currentSep); sep > -1 {
repo = path[sep+len(currentSep):]
if repo != "" {
return strings.Replace(repo, "_", "/", -1), true
}
}
sep := strings.LastIndexByte(path, '_')
if sep == -1 || sep >= len(path)-1 {
return "", false
}
return path[sep+1:], true
}
func repositoryFromPathList(path string) (repo string, ok bool) {
list := strings.Split(path, string(filepath.Separator))
nth := -1
for i := len(list) - 1; i >= 0; i-- {
if list[i] == "current" {
nth = i
break
}
}
if nth == -1 {
return "", false
}
list = list[nth:]
if len(list) > 1 { // drop "current" if there are more identifiers
list = list[1:]
}
return strings.Join(list, "/"), true
}
func (a *archIndex) computeETag() string {
h := sha1.New()
binary.Write(h, binary.LittleEndian, int64(len(a.names)))
for _, name := range a.names {
binary.Write(h, binary.LittleEndian, int64(len(name)))
io.WriteString(h, name)
}
sum := h.Sum(make([]byte, 0, h.Size()))
return `W/"` + etagEncoding.EncodeToString(sum) + `"`
}
func (a *archIndex) init() error {
names := make([]string, 0, len(a.archs))
for k := range a.archs {
names = append(names, k)
}
sort.Strings(names)
a.names = names
a.etag = a.computeETag()
return nil
}