-
Notifications
You must be signed in to change notification settings - Fork 11
/
writer.go
337 lines (277 loc) · 8.59 KB
/
writer.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
package nar
import (
"bytes"
"fmt"
"io"
"path/filepath"
"github.com/nix-community/go-nix/pkg/wire"
)
// Writer provides sequential writing of a NAR (Nix Archive) file.
// Writer.WriteHeader begins a new file with the provided Header,
// and then Writer can be treated as an io.Writer to supply that
// file's data.
type Writer struct {
w io.Writer
contentWriter io.WriteCloser
// channels used by the goroutine to communicate back to WriteHeader and Close.
doneWritingHeader chan struct{} // goroutine is done writing that header, WriteHeader() can return.
errors chan error // there were errors while writing
// whether we closed
closed bool
// this is used to send new headers to write to the emitter
headers chan *Header
}
// NewWriter creates a new Writer writing to w.
func NewWriter(w io.Writer) (*Writer, error) {
// write magic
err := wire.WriteString(w, narVersionMagic1)
if err != nil {
return nil, err
}
narWriter := &Writer{
w: w,
doneWritingHeader: make(chan struct{}),
errors: make(chan error),
closed: false,
headers: make(chan *Header),
}
// kick off the goroutine
go func() {
// wait for the first WriteHeader() call
header, ok := <-narWriter.headers
// immediate Close(), without ever calling WriteHeader()
// as an empty nar is invalid, we return an error
if !ok {
narWriter.errors <- fmt.Errorf("unexpected Close()")
close(narWriter.errors)
return
}
// ensure the first item received always has a "/" as path.
if header.Path != "/" {
narWriter.errors <- fmt.Errorf("first header always needs to have a / as path")
close(narWriter.errors)
return
}
excessHdr, err := narWriter.emitNode(header)
if err != nil {
narWriter.errors <- err
}
if excessHdr != nil {
narWriter.errors <- fmt.Errorf("additional header detected: %+v", excessHdr)
}
close(narWriter.errors)
}()
return narWriter, nil
}
// emitNode writes one NAR node. It'll internally consume one or more headers.
// in case the header received a header that's not inside its own jurisdiction,
// it'll return it, assuming an upper level will handle it.
func (nw *Writer) emitNode(currentHeader *Header) (*Header, error) {
// write a opening (
err := wire.WriteString(nw.w, "(")
if err != nil {
return nil, err
}
// write type
err = wire.WriteString(nw.w, "type")
if err != nil {
return nil, err
}
// store the current type in a var, we access it more often later.
currentType := currentHeader.Type
err = wire.WriteString(nw.w, currentType.String())
if err != nil {
return nil, err
}
if currentType == TypeRegular { //nolint:nestif
// if the executable bit is set…
if currentHeader.Executable {
// write the executable token.
err = wire.WriteString(nw.w, "executable")
if err != nil {
return nil, err
}
// write the placeholder
err = wire.WriteBytes(nw.w, []byte{})
if err != nil {
return nil, err
}
}
// write the contents keyword
err = wire.WriteString(nw.w, "contents")
if err != nil {
return nil, err
}
nw.contentWriter, err = wire.NewBytesWriter(nw.w, uint64(currentHeader.Size))
if err != nil {
return nil, err
}
}
// The directory case doesn't write anything special after ( type directory .
// We need to inspect the next header before figuring out whether to list entries or not.
if currentType == TypeSymlink || currentType == TypeDirectory { //nolint:nestif
if currentType == TypeSymlink {
// write the target keyword
err = wire.WriteString(nw.w, "target")
if err != nil {
return nil, err
}
// write the target location. Make sure to convert slashes.
err = wire.WriteString(nw.w, filepath.ToSlash(currentHeader.LinkTarget))
if err != nil {
return nil, err
}
}
// setup a dummy content write, that's not connected to the main writer,
// and will fail if you write anything to it.
var b bytes.Buffer
nw.contentWriter, err = wire.NewBytesWriter(&b, 0)
if err != nil {
return nil, err
}
}
// return from WriteHeader()
nw.doneWritingHeader <- struct{}{}
// wait till we receive a new header
nextHeader, ok := <-nw.headers
// Close the content writer to finish the packet and write possible padding
// This is a no-op for symlinks and directories, as the contentWriter is limited to 0 bytes,
// and not connected to the main writer.
// The writer itself will already ensure we wrote the right amount of bytes
err = nw.contentWriter.Close()
if err != nil {
return nil, err
}
// if this was the last header, write the closing ) and return
if !ok {
err = wire.WriteString(nw.w, ")")
if err != nil {
return nil, err
}
return nil, err
}
// This is a loop, as nextHeader can either be what we received above,
// or in the case of a directory, something returned when recursing up.
for {
// if this was the last header, write the closing ) and return
if nextHeader == nil {
err = wire.WriteString(nw.w, ")")
if err != nil {
return nil, err
}
return nil, err
}
// compare Path of the received header.
// It needs to be lexicographically greater the previous one.
if !PathIsLexicographicallyOrdered(currentHeader.Path, nextHeader.Path) {
return nil, fmt.Errorf(
"received %v, which isn't lexicographically greater than the previous one %v",
nextHeader.Path,
currentHeader.Path,
)
}
// calculate the relative path between the previous and now-read header,
// which will become the new node name.
nodeName, err := filepath.Rel(currentHeader.Path, nextHeader.Path)
if err != nil {
return nil, err
}
// make sure we're using slashes
nodeName = filepath.ToSlash(nodeName)
// if the received header is something further up, or a sibling, we're done here.
if len(nodeName) > 2 && (nodeName[0:2] == "..") {
// write the closing )
err = wire.WriteString(nw.w, ")")
if err != nil {
return nil, err
}
// bounce further work up to above
return nextHeader, nil
}
// in other cases, it describes something below.
// This only works if we previously were in a directory.
if currentHeader.Type != TypeDirectory {
return nil, fmt.Errorf("received descending path %v, but we're a %v", nextHeader.Path, currentHeader.Type.String())
}
// ensure the name is valid. At this point, there should be no more slashes,
// as we already recursed up.
if !IsValidNodeName(nodeName) {
return nil, fmt.Errorf("name `%v` is invalid, as it contains a slash", nodeName)
}
// write the entry keyword
err = wire.WriteString(nw.w, "entry")
if err != nil {
return nil, err
}
// write a opening (
err = wire.WriteString(nw.w, "(")
if err != nil {
return nil, err
}
// write a opening name
err = wire.WriteString(nw.w, "name")
if err != nil {
return nil, err
}
// write the node name
err = wire.WriteString(nw.w, nodeName)
if err != nil {
return nil, err
}
// write the node keyword
err = wire.WriteString(nw.w, "node")
if err != nil {
return nil, err
}
// Emit the node inside. It'll consume another node, which is what we'll
// handle in the next loop iteration.
nextHeader, err = nw.emitNode(nextHeader)
if err != nil {
return nil, err
}
// write the closing ) (from entry)
err = wire.WriteString(nw.w, ")")
if err != nil {
return nil, err
}
}
}
// WriteHeader writes hdr and prepares to accept the file's contents. The
// Header.Size determines how many bytes can be written for the next file. If
// the current file is not fully written, then this returns an error. This
// implicitly flushes any padding necessary before writing the header.
func (nw *Writer) WriteHeader(hdr *Header) error {
if err := hdr.Validate(); err != nil {
return fmt.Errorf("unable to write header: %w", err)
}
nw.headers <- hdr
select {
case err := <-nw.errors:
return err
case <-nw.doneWritingHeader:
}
return nil
}
// Write writes to the current file in the NAR.
// Write returns the ErrWriteTooLong if more than Header.Size bytes
// are written after WriteHeader.
//
// Calling Write on special types like TypeLink, TypeSymlink, TypeChar,
// TypeBlock, TypeDir, and TypeFifo returns (0, ErrWriteTooLong) regardless of
// what the Header.Size claims.
func (nw *Writer) Write(b []byte) (int, error) {
return nw.contentWriter.Write(b)
}
// Close closes the NAR file.
// If the current file (from a prior call to WriteHeader) is not fully
// written, then this returns an error.
func (nw *Writer) Close() error {
if nw.closed {
return fmt.Errorf("already closed")
}
// signal the emitter this was the last one
close(nw.headers)
nw.closed = true
// wait for it to signal its done (by closing errors)
return <-nw.errors
}