-
Notifications
You must be signed in to change notification settings - Fork 2
/
package_index.go
64 lines (55 loc) · 1.43 KB
/
package_index.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
// This file is part of *kellner*
//
// Copyright (C) 2015, Travelping GmbH <copyright@travelping.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"bytes"
"fmt"
"io"
"sort"
"sync"
)
type packageIndex struct {
sync.Mutex
Entries map[string]*ipkArchive
}
func (pi *packageIndex) Len() int { return len(pi.Entries) }
// StringTo writes all control data and checksums
// to write 'w'. Essentially it creates a
// a 'Packages' file
func (pi *packageIndex) StringTo(w io.Writer) {
for _, name := range pi.SortedNames() {
entry := pi.Entries[name]
entry.ControlAndChecksumTo(w)
fmt.Fprintln(w)
}
}
// StampsTo writes all timestamps to write 'w'
func (pi *packageIndex) StampsTo(w io.Writer) {
for _, name := range pi.SortedNames() {
entry := pi.Entries[name]
fmt.Fprintf(w, "%d %s\n", entry.FileInfo.ModTime().Unix(), name)
}
}
func (pi *packageIndex) String() string {
buf := bytes.NewBuffer(nil)
pi.StringTo(buf)
return buf.String()
}
func (pi *packageIndex) SortedNames() []string {
var names = make([]string, 0, len(pi.Entries))
for _, pkg := range pi.Entries {
names = append(names, pkg.Name)
}
sort.Strings(names)
return names
}
func (pi *packageIndex) Add(name string, ipk *ipkArchive) {
pi.Lock()
pi.Entries[name] = ipk
pi.Unlock()
}