-
Notifications
You must be signed in to change notification settings - Fork 1
/
archive.go
379 lines (326 loc) · 8.26 KB
/
archive.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package main
import (
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"time"
"compress/gzip"
)
// Archiver describes an interface for maintaining and generating
// the on disk repository
type Archiver interface {
PublicDir() string
Dists() map[string]StoreID
GetDist(name string) (*Release, error)
SetDist(name string, newrel StoreID) error
ReifyRelease(id StoreID) (err error)
DeleteDist(name string) error
AddUpload(session *UploadSession) error
ArchiveStorer
}
// An Archiver that uses a version historied blob store
type archiveStoreArchive struct {
base *string // The base directory of the repository
ArchiveStorer // The blob store to use
}
// NewAptBlobArchive creates a new Archiver that uses a version
// historied content addressable store
func NewAptBlobArchive(
storeDir *string,
tmpDir *string,
publicDir *string,
defConfig ReleaseConfig,
) Archiver {
archivestore := NewArchiveBlobStore(*storeDir, *tmpDir, defConfig)
return &archiveStoreArchive{
ArchiveStorer: archivestore,
base: publicDir,
}
}
func (a *archiveStoreArchive) Dists() map[string]StoreID {
tags := a.ReleaseTags()
dists := make(map[string]StoreID, 0)
for tag := range tags {
if !strings.HasPrefix(tag, "heads/") {
continue
}
tagSuffix := strings.TrimPrefix(tag, "heads/")
if strings.Index(tagSuffix, "/") != -1 {
continue
}
dists[tagSuffix] = tags[tag]
}
return dists
}
func (a *archiveStoreArchive) GetDist(name string) (*Release, error) {
if strings.Index(name, "/") != -1 {
return nil, errors.New("Distribution name cannot include /")
}
releaseID, err := a.GetReleaseTag("heads/" + name)
if err != nil {
return nil, err
}
release, err := a.GetRelease(releaseID)
if err != nil {
return nil, err
}
return release, nil
}
func (a *archiveStoreArchive) SetDist(name string, newrel StoreID) error {
if strings.Index(name, "/") != -1 {
return errors.New("Distribution name cannot include /")
}
return a.SetReleaseTag("heads/"+name, newrel)
}
func (a *archiveStoreArchive) DeleteDist(name string) error {
if strings.Index(name, "/") != -1 {
return errors.New("Distribution name cannot include /")
}
err := a.DeleteReleaseTag("heads/" + name)
if err != nil {
return fmt.Errorf("Distribution note deleted, %v", err.Error())
}
return os.RemoveAll(*a.base + "/dists/" + name)
}
func (a *archiveStoreArchive) ReifyRelease(id StoreID) (err error) {
release, err := a.GetRelease(id)
if err != nil {
return err
}
distBase := *a.base + "/dists/" + release.CodeName
distAlias := *a.base + "/dists/" + release.Suite
clearTime := time.Now()
clearDist := func() {
os.Remove(distAlias)
os.RemoveAll(distBase)
}
clearDist()
clearDuration := time.Since(clearTime)
log.Println("Cleared old distribution in ", clearDuration)
defer func() {
if err != nil {
clearDist()
}
}()
reifyTime := time.Now()
fileCount := 0
log.Printf("Reifying release %v", release.CodeName)
for _, component := range release.Components {
log.Printf("Reifying component %v", component.Name)
componentBase := distBase + "/" + component.Name
// Reify the compressed sources file
sourcesBase := componentBase + "/source"
err = a.Link(component.SourcesGz, sourcesBase+"/Sources.gz")
if err != nil {
return err
}
// Reify the uncompressed sources file
gzreader, err := os.Open(sourcesBase + "/Sources.gz")
defer gzreader.Close()
if err != nil {
return err
}
srcs, err := os.Create(sourcesBase + "/Sources")
defer srcs.Close()
if err != nil {
return err
}
gunzipper, err := gzip.NewReader(gzreader)
defer gunzipper.Close()
if err != nil {
return err
}
io.Copy(srcs, gunzipper)
for _, arch := range component.Architectures {
archBase := componentBase + "/binary-" + arch.Name
// Reify the compressed packages file
err = a.Link(arch.PackagesGz, archBase+"/Packages.gz")
if err != nil {
return err
}
// Reify the uncompressed packages file
gzreader, err := os.Open(archBase + "/Packages.gz")
defer gzreader.Close()
if err != nil {
return err
}
pkgs, err := os.Create(archBase + "/Packages")
defer pkgs.Close()
if err != nil {
return err
}
gunzipper, err := gzip.NewReader(gzreader)
defer gunzipper.Close()
if err != nil {
return err
}
io.Copy(pkgs, gunzipper)
}
}
err = a.Link(release.Release, distBase+"/Release")
if err != nil {
return err
}
if len(release.InRelease) != 0 {
err = a.Link(release.InRelease, distBase+"/InRelease")
if err != nil {
return err
}
err = a.Link(release.ReleaseGPG, distBase+"/Release.gpg")
if err != nil {
return err
}
key, _ := release.SignerKey()
if key != nil {
keyfile, err := os.Create(distBase + "/pubkey.gpg")
defer keyfile.Close()
if err == nil {
key.Serialize(keyfile)
}
}
}
err = a.updatePool(release)
if err != nil {
return err
}
reifyDuration := time.Since(reifyTime)
log.Printf("Reified %v files in %v ", fileCount, reifyDuration)
return
}
func (a *archiveStoreArchive) updatePool(release *Release) error {
index, err := a.OpenReleaseIndex(release.IndexID)
defer index.Close()
if err != nil {
return err
}
poolBase := a.PublicDir() + "/pool/" + release.CodeName
log.Printf("Clearing pool %v ", poolBase)
os.RemoveAll(poolBase)
for {
e, err := index.NextEntry()
if err != nil {
if err != io.EOF {
return err
}
break
}
srcName := e.SourceItem.Name
srcVersion := e.SourceItem.Version.String()
poolpath := fmt.Sprintf("%s/%s%s/%s/",
a.PublicDir(),
release.PoolFilePath(srcName),
srcName,
srcVersion,
)
err = a.Link(e.ChangesID, poolpath+fmt.Sprintf("%s_%s.changes", srcName, srcVersion))
if err != nil {
return err
}
for _, s := range e.SourceItem.Files {
filename := s.Name
path := poolpath + filename
err = a.Link(s.StoreID, path)
if err != nil {
return err
}
}
for _, b := range e.BinaryItems {
filename := b.Files[0].Name
path := poolpath + filename
err = a.Link(b.Files[0].StoreID, path)
if err != nil {
return err
}
}
}
log.Printf("Pool rebuild complete")
return nil
}
// Return the raw path to the base directory, used for directly
// serving content
func (a *archiveStoreArchive) PublicDir() string {
return *a.base
}
func (a *archiveStoreArchive) AddUpload(session *UploadSession) error {
entry, err := NewReleaseIndexEntry(session)
if err != nil {
return fmt.Errorf("Collating repository items failed, %v", err)
}
branchName := session.ReleaseName
heads := a.Dists()
head, ok := heads[branchName]
if !ok {
defCfgID, err := a.GetDefaultReleaseConfigID()
if err != nil {
return fmt.Errorf("Creating distro root failed getting default config ID for release, %v", err)
}
head, err = a.GetReleaseRoot(Release{
CodeName: branchName,
Suite: "stable",
ConfigID: defCfgID,
})
if err != nil {
return fmt.Errorf("Creating distro root failed, %v", err)
}
}
newidx, actions, err := a.mergeEntryIntoRelease(head, entry)
if err != nil {
return fmt.Errorf("Creating new index failed, %v", err)
}
realchange := false
for _, item := range actions {
switch item.Type {
case ActionADD:
{
log.Println("Added " + item.Description)
realchange = true
}
case ActionSKIPPRESENT:
{
log.Println("Item already present: " + item.Description)
}
case ActionSKIPPRUNE:
{
log.Println("Skipped due to prune policy: " + item.Description)
}
case ActionPRUNE:
{
log.Println("Pruned old item " + item.Description)
realchange = true
}
case ActionDELETE:
{
log.Println("Deleted " + item.Description)
realchange = true
}
case ActionTRIM:
{
log.Println("Trimmed " + item.Description)
}
default:
{
log.Println(item.Description)
}
}
}
if !realchange {
log.Println("No changes to index to cmmit")
return nil
}
newhead, err := NewRelease(a, head, newidx, actions)
if err != nil {
return fmt.Errorf("Creating updated commit failed, %v", err)
}
if err = a.SetDist(branchName, newhead); err != nil {
return fmt.Errorf("Setting dist ref failed, %v", err)
}
log.Printf("Branch %v set to %v", branchName, StoreID(newhead).String())
if err = a.ReifyRelease(newhead); err != nil {
return fmt.Errorf("Repopulating the archive directory failed,, %v", err)
}
a.GarbageCollect()
return nil
}