-
Notifications
You must be signed in to change notification settings - Fork 5
/
sink.go
195 lines (160 loc) · 4.3 KB
/
sink.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
package checker
import (
"fmt"
"io"
"log"
"math"
"github.com/itchio/savior"
"github.com/pkg/errors"
)
// TODO: check that everything has been extracted once properly
// Sink is a special kind of savior.Sink that records statistics
// about what's written to assist in unit testing.
type Sink struct {
Items map[string]*Item
DoneItems map[string]*DoneItem
}
var _ savior.Sink = (*Sink)(nil)
// Item represents a savior.Entry + bytes pair
type Item struct {
Entry *savior.Entry
Data []byte
}
// DoneItem represents the result of an item having been written to
type DoneItem struct {
MinWrite int64
MaxWrite int64
Linkname string
}
// NewSink returns a new checker sink
func NewSink() *Sink {
cs := &Sink{
Items: make(map[string]*Item),
}
cs.Reset()
return cs
}
func (cs *Sink) Reset() {
cs.DoneItems = make(map[string]*DoneItem)
}
func (cs *Sink) Validate() error {
numEntries := 0
for _, i := range cs.Items {
e := i.Entry
if di, ok := cs.DoneItems[e.CanonicalPath]; ok {
switch e.Kind {
case savior.EntryKindFile:
if di.MinWrite != 0 {
return fmt.Errorf("checker.Sink: start of file missing (first write at byte %d): %s", di.MinWrite, e)
}
size := int64(len(i.Data))
if di.MaxWrite != size {
return fmt.Errorf("checker.Sink: end of file missing (last write ends at byte %d, should end at %d): %s", di.MaxWrite, size, e)
}
case savior.EntryKindSymlink:
if di.Linkname != e.Linkname {
return fmt.Errorf("checker.Sink: symlink points at '%s' instead of '%s': %s", di.Linkname, e.Linkname, e)
}
}
} else {
return fmt.Errorf("checker.Sink: entry neglected: %s", e)
}
numEntries++
}
log.Printf("checker.Sink: %d entries validated", numEntries)
return nil
}
func (cs *Sink) Mkdir(entry *savior.Entry) error {
return cs.withItem(entry, savior.EntryKindDir, func(item *Item, di *DoneItem) error {
// that's about it
return nil
})
}
func (cs *Sink) Symlink(entry *savior.Entry, linkname string) error {
return cs.withItem(entry, savior.EntryKindSymlink, func(item *Item, di *DoneItem) error {
// that's about it
if item.Entry.Linkname != linkname {
err := fmt.Errorf("%s: expected dest '%s', got '%s'", entry.CanonicalPath, item.Entry.Linkname, linkname)
return errors.WithStack(err)
}
di.Linkname = linkname
return nil
})
}
func (cs *Sink) GetWriter(entry *savior.Entry) (savior.EntryWriter, error) {
var ew savior.EntryWriter
err := cs.withItem(entry, savior.EntryKindFile, func(item *Item, di *DoneItem) error {
checkingWriter := NewWriter(item.Data)
checkingWriter.doneItem = di
if entry.WriteOffset != 0 {
_, err := checkingWriter.Seek(entry.WriteOffset, io.SeekStart)
if err != nil {
return errors.WithStack(err)
}
}
ew = &entryWriter{
entry: entry,
w: checkingWriter,
}
return nil
})
if err != nil {
return nil, errors.WithStack(err)
}
return ew, nil
}
func (cs *Sink) Preallocate(entry *savior.Entry) error {
return cs.withItem(entry, savior.EntryKindFile, func(item *Item, di *DoneItem) error {
// nothing to do
return nil
})
}
func (cs *Sink) Nuke() error {
cs.Reset()
return nil
}
// ===============================
type withItemFunc func(item *Item, di *DoneItem) error
func (cs *Sink) withItem(entry *savior.Entry, actualKind savior.EntryKind, cb withItemFunc) error {
item, ok := cs.Items[entry.CanonicalPath]
if !ok {
err := fmt.Errorf("%s: no such item", entry.CanonicalPath)
return errors.WithStack(err)
}
expectedKind := item.Entry.Kind
if item.Entry.Kind != actualKind {
err := fmt.Errorf("%s: expected kind %v, got %v", entry.CanonicalPath, expectedKind, actualKind)
return errors.WithStack(err)
}
di := cs.DoneItems[entry.CanonicalPath]
if di == nil {
di = &DoneItem{
MinWrite: math.MaxInt64,
}
cs.DoneItems[entry.CanonicalPath] = di
}
return cb(item, di)
}
func (cs *Sink) Close() error {
return nil
}
// ===============================
type entryWriter struct {
w io.Writer
entry *savior.Entry
}
var _ savior.EntryWriter = (*entryWriter)(nil)
func (ew *entryWriter) Write(buf []byte) (int, error) {
n, err := ew.w.Write(buf)
ew.entry.WriteOffset += int64(n)
return n, err
}
func (ew *entryWriter) Close() error {
if closer, ok := ew.w.(io.Closer); ok {
return closer.Close()
}
return nil
}
func (ew *entryWriter) Sync() error {
return nil
}