-
Notifications
You must be signed in to change notification settings - Fork 3
/
repo.go
266 lines (235 loc) · 6.19 KB
/
repo.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*******************************************************************************
*
* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/
package main
import (
"archive/tar"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
)
//Repository represents a directory containing package files.
type Repository struct {
Name string `toml:"name"`
Path string `toml:"path"`
}
//FileName returns the basename of the repository metadata archive.
func (r Repository) FileName() string {
return r.Name + ".db.tar.xz"
}
//RepositoryEntry represents an entry for a package in a repo metadata archive.
type RepositoryEntry struct {
PackageName string
FileName string
MD5Digest string
}
func (r Repository) readMetadata() ([]RepositoryEntry, error) {
metadataPath := filepath.Join(r.Path, r.FileName())
file, err := os.Open(metadataPath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
cmd := exec.Command("xz", "--decompress", "--stdout")
cmd.Stdin = file
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
return nil, err
}
tr := tar.NewReader(&buf)
var result []RepositoryEntry
for {
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
break
}
return nil, fmt.Errorf("error reading %s: %s", metadataPath, err.Error())
}
ok, entry, err := readMetadataEntry(hdr, tr)
if err != nil {
return nil, fmt.Errorf("error reading %s: %s", metadataPath, err.Error())
}
if ok {
result = append(result, entry)
}
}
return result, nil
}
func readMetadataEntry(h *tar.Header, r io.Reader) (ok bool, entry RepositoryEntry, err error) {
//entries are regular files like */desc
if !h.FileInfo().Mode().IsRegular() {
ok = false
return
}
if filepath.Base(h.Name) != "desc" {
ok = false
return
}
var buf []byte
buf, err = ioutil.ReadAll(r)
if err != nil {
ok = false
return
}
//read line by line
currentField := ""
for _, line := range strings.Split(string(buf), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
if strings.HasPrefix(line, "%") && strings.HasSuffix(line, "%") {
currentField = line
continue
}
switch currentField {
case "%NAME%":
entry.PackageName = line
case "%FILENAME%":
entry.FileName = line
case "%MD5SUM%":
entry.MD5Digest = line
}
}
ok = true
return
}
func (r Repository) addNewPackages(allOutputFiles []string, c *Cache, ui *UI) (ok bool) {
ui.SetCurrentTask("Adding new packages to repository", uint(len(allOutputFiles)))
defer ui.EndTask()
//get existing entries
entries, err := r.readMetadata()
if err != nil {
ui.ShowError(err)
return false
}
entryByFilename := make(map[string]RepositoryEntry, len(entries))
for _, entry := range entries {
entryByFilename[entry.FileName] = entry
}
//which files need to be added?
var newOutputFiles []string
for _, fileName := range allOutputFiles {
ui.StepTask()
entry, exists := entryByFilename[fileName]
if !exists {
newOutputFiles = append(newOutputFiles, fileName)
continue
}
cacheEntry, err := c.GetEntryForOutputFile(filepath.Join(r.Path, fileName))
if err != nil {
ui.ShowError(err)
return false
}
if entry.MD5Digest != cacheEntry.MD5Digest {
newOutputFiles = append(newOutputFiles, fileName)
}
}
if len(newOutputFiles) == 0 {
return true
}
cmd := exec.Command("repo-add", append([]string{"-R", r.FileName()}, newOutputFiles...)...)
cmd.Dir = r.Path
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
ui.ShowError(err)
return err == nil
}
func (r Repository) pruneMetadata(allOutputFiles []string, ui *UI) (ok bool) {
ui.SetCurrentTask("Removing old entries from repo metadata", 1)
defer ui.EndTask()
//get existing entries
entries, err := r.readMetadata()
if err != nil {
ui.ShowError(err)
return false
}
//collect all entries that do not match a current output file
isOutputFile := make(map[string]bool, len(allOutputFiles))
for _, fileName := range allOutputFiles {
isOutputFile[fileName] = true
}
var entriesToDelete []string
for _, entry := range entries {
if !isOutputFile[entry.FileName] {
entriesToDelete = append(entriesToDelete, entry.PackageName)
}
}
if len(entriesToDelete) == 0 {
return true
}
cmd := exec.Command("repo-remove", append([]string{r.FileName()}, entriesToDelete...)...)
cmd.Dir = r.Path
cmd.Stdin = nil
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
ui.ShowError(err)
return err == nil
}
func (r Repository) prunePackages(allOutputFiles []string, ui *UI) (ok bool) {
isOutputFile := make(map[string]bool, len(allOutputFiles))
for _, fileName := range allOutputFiles {
isOutputFile[fileName] = true
}
dir, err := os.Open(r.Path)
if err != nil {
ui.ShowError(err)
return false
}
names, err := dir.Readdirnames(-1)
if err != nil {
ui.ShowError(err)
return false
}
var filenamesToDelete []string
for _, fullFileName := range names {
fileName := strings.TrimSuffix(fullFileName, ".sig")
if strings.HasSuffix(fileName, ".pkg.tar.xz") && !isOutputFile[fileName] {
filenamesToDelete = append(filenamesToDelete, fullFileName)
continue
}
}
if len(filenamesToDelete) == 0 {
return true
}
ui.SetCurrentTask("Removing old files from target directory", uint(len(filenamesToDelete)))
defer ui.EndTask()
ok = true
for _, fileName := range filenamesToDelete {
ui.StepTask()
err := os.Remove(filepath.Join(r.Path, fileName))
if err != nil {
ui.ShowError(err)
ok = false
}
}
return
}