Debian Codegraph @

format.go

raw
1 package desync
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "math"
8 "os"
9 "reflect"
10 "sort"
11 "strings"
12 "time"
13 )
14
15 type FormatHeader struct {
16 Size uint64
17 Type uint64
18 }
19
20 type FormatEntry struct {
21 FormatHeader
22 FeatureFlags uint64
23 Mode os.FileMode
24 Flags uint64
25 UID int
26 GID int
27 MTime time.Time
28 }
29
30 type FormatUser struct {
31 FormatHeader
32 Name string
33 }
34
35 type FormatGroup struct {
36 FormatHeader
37 Name string
38 }
39
40 type FormatXAttr struct {
41 FormatHeader
42 NameAndValue string
43 }
44
45 type FormatSELinux struct {
46 FormatHeader
47 Label string
48 }
49
50 type FormatFilename struct {
51 FormatHeader
52 Name string
53 }
54
55 type FormatSymlink struct {
56 FormatHeader
57 Target string
58 }
59
60 type FormatDevice struct {
61 FormatHeader
62 Major uint64
63 Minor uint64
64 }
65
66 type FormatPayload struct {
67 FormatHeader
68 Data io.Reader
69 }
70
71 type FormatGoodbye struct {
72 FormatHeader
73 Items []FormatGoodbyeItem
74 }
75
76 type FormatGoodbyeItem struct {
77 Offset uint64
78 Size uint64
79 Hash uint64 // The last item in a list has the CaFormatGoodbyeTailMarker here
80 }
81
82 type FormatFCaps struct {
83 FormatHeader
84 Data []byte
85 }
86
87 type FormatACLUser struct {
88 FormatHeader
89 UID uint64
90 Permissions uint64
91 Name string
92 }
93
94 type FormatACLGroup struct {
95 FormatHeader
96 GID uint64
97 Permissions uint64
98 Name string
99 }
100
101 type FormatACLGroupObj struct {
102 FormatHeader
103 Permissions uint64
104 }
105
106 type FormatACLDefault struct {
107 FormatHeader
108 UserObjPermissions uint64
109 GroupObjPermissions uint64
110 OtherPermissions uint64
111 MaskPermissions uint64
112 }
113
114 type FormatIndex struct {
115 FormatHeader
116 FeatureFlags uint64
117 ChunkSizeMin uint64
118 ChunkSizeAvg uint64
119 ChunkSizeMax uint64
120 }
121
122 type FormatTable struct {
123 FormatHeader
124 Items []FormatTableItem
125 }
126
127 type FormatTableItem struct {
128 Offset uint64
129 Chunk ChunkID
130 }
131
132 // FormatDecoder is used to parse and break up a stream of casync format elements
133 // found in archives or index files.
134 type FormatDecoder struct {
135 r reader
136 advance io.Reader
137 }
138
139 func NewFormatDecoder(r io.Reader) FormatDecoder {
140 return FormatDecoder{r: reader{r}}
141 }
142
143 // Next returns the next format element from the stream. If an element
144 // contains a reader, that reader should be used before any subsequent calls as
145 // it'll be invalidated then. Returns nil when the end is reached.
146 func (d *FormatDecoder) Next() (any, error) {
147 // If we previously returned a reader, make sure we advance all the way in
148 // case the caller didn't read it all.
149 if d.advance != nil {
150 io.Copy(io.Discard, d.advance)
151 d.advance = nil
152 }
153 hdr, err := d.r.ReadHeader()
154 if err != nil {
155 if err == io.EOF {
156 return nil, nil
157 }
158 return nil, err
159 }
160 switch hdr.Type {
161 case CaFormatEntry:
162 if hdr.Size != 64 {
163 return nil, InvalidFormat{}
164 }
165 e := FormatEntry{FormatHeader: hdr}
166 e.FeatureFlags, err = d.r.ReadUint64()
167 if err != nil {
168 return nil, err
169 }
170 mode, err := d.r.ReadUint64()
171 if err != nil {
172 return nil, err
173 }
174 e.Mode = StatModeToFilemode(uint32(mode))
175 e.Flags, err = d.r.ReadUint64()
176 if err != nil {
177 return nil, err
178 }
179 uid, err := d.r.ReadUint64()
180 if err != nil {
181 return nil, err
182 }
183 e.UID = int(uid)
184 gid, err := d.r.ReadUint64()
185 if err != nil {
186 return nil, err
187 }
188 e.GID = int(gid)
189 mtime, err := d.r.ReadUint64()
190 if err != nil {
191 return nil, err
192 }
193 e.MTime = time.Unix(0, int64(mtime))
194 return e, nil
195
196 case CaFormatUser:
197 b := make([]byte, hdr.Size-16)
198 if _, err = io.ReadFull(d.r, b); err != nil {
199 return nil, err
200 }
201 // Strip off the 0 byte
202 b = b[:len(b)-1]
203 return FormatUser{FormatHeader: hdr, Name: string(b)}, nil
204
205 case CaFormatGroup:
206 b := make([]byte, hdr.Size-16)
207 if _, err = io.ReadFull(d.r, b); err != nil {
208 return nil, err
209 }
210 // Strip off the 0 byte
211 b = b[:len(b)-1]
212 return FormatGroup{FormatHeader: hdr, Name: string(b)}, nil
213
214 case CaFormatXAttr:
215 b := make([]byte, hdr.Size-16)
216 if _, err = io.ReadFull(d.r, b); err != nil {
217 return nil, err
218 }
219 // Strip off the 0 byte
220 b = b[:len(b)-1]
221 return FormatXAttr{FormatHeader: hdr, NameAndValue: string(b)}, nil
222
223 case CaFormatSELinux:
224 b := make([]byte, hdr.Size-16)
225 if _, err = io.ReadFull(d.r, b); err != nil {
226 return nil, err
227 }
228 // Strip off the 0 byte
229 b = b[:len(b)-1]
230 return FormatSELinux{FormatHeader: hdr, Label: string(b)}, nil
231
232 case CaFormatFilename:
233 b := make([]byte, hdr.Size-16)
234 if _, err = io.ReadFull(d.r, b); err != nil {
235 return nil, err
236 }
237 // Strip off the 0 byte
238 b = b[:len(b)-1]
239 return FormatFilename{FormatHeader: hdr, Name: string(b)}, nil
240
241 case CaFormatSymlink:
242 b := make([]byte, hdr.Size-16)
243 if _, err = io.ReadFull(d.r, b); err != nil {
244 return nil, err
245 }
246 // Strip off the 0 byte
247 b = b[:len(b)-1]
248 return FormatSymlink{FormatHeader: hdr, Target: string(b)}, nil
249
250 case CaFormatDevice:
251 if hdr.Size != 32 {
252 return nil, InvalidFormat{}
253 }
254 e := FormatDevice{FormatHeader: hdr}
255 e.Major, err = d.r.ReadUint64()
256 if err != nil {
257 return nil, err
258 }
259 e.Minor, err = d.r.ReadUint64()
260 if err != nil {
261 return nil, err
262 }
263 return e, nil
264
265 case CaFormatPayload:
266 size := hdr.Size - 16
267 r := io.LimitReader(d.r, int64(size))
268 // Record the reader to be read fully on the next iteration if the caller
269 // didn't do it
270 d.advance = r
271 return FormatPayload{FormatHeader: hdr, Data: r}, nil
272
273 case CaFormatFCaps:
274 b := make([]byte, hdr.Size-16)
275 if _, err = io.ReadFull(d.r, b); err != nil {
276 return nil, err
277 }
278 return FormatFCaps{FormatHeader: hdr, Data: b}, nil
279
280 case CaFormatACLUser:
281 e := FormatACLUser{FormatHeader: hdr}
282 e.UID, err = d.r.ReadUint64()
283 if err != nil {
284 return nil, err
285 }
286 e.Permissions, err = d.r.ReadUint64()
287 if err != nil {
288 return nil, err
289 }
290 b := make([]byte, hdr.Size-32)
291 if _, err = io.ReadFull(d.r, b); err != nil {
292 return nil, err
293 }
294 // Strip off the 0 byte
295 b = b[:len(b)-1]
296 e.Name = string(b)
297 return e, nil
298
299 case CaFormatACLGroup:
300 e := FormatACLGroup{FormatHeader: hdr}
301 e.GID, err = d.r.ReadUint64()
302 if err != nil {
303 return nil, err
304 }
305 e.Permissions, err = d.r.ReadUint64()
306 if err != nil {
307 return nil, err
308 }
309 b := make([]byte, hdr.Size-32)
310 if _, err = io.ReadFull(d.r, b); err != nil {
311 return nil, err
312 }
313 // Strip off the 0 byte
314 b = b[:len(b)-1]
315 e.Name = string(b)
316 return e, nil
317
318 case CaFormatACLGroupObj:
319 e := FormatACLGroupObj{FormatHeader: hdr}
320 e.Permissions, err = d.r.ReadUint64()
321 if err != nil {
322 return nil, err
323 }
324 return e, nil
325
326 case CaFormatACLDefault:
327 e := FormatACLDefault{FormatHeader: hdr}
328 e.UserObjPermissions, err = d.r.ReadUint64()
329 if err != nil {
330 return nil, err
331 }
332 e.GroupObjPermissions, err = d.r.ReadUint64()
333 if err != nil {
334 return nil, err
335 }
336 e.OtherPermissions, err = d.r.ReadUint64()
337 if err != nil {
338 return nil, err
339 }
340 e.MaskPermissions, err = d.r.ReadUint64()
341 if err != nil {
342 return nil, err
343 }
344 return e, nil
345
346 case CaFormatGoodbye:
347 n := (hdr.Size - 16) / 24
348 items := make([]FormatGoodbyeItem, n)
349 e := FormatGoodbye{FormatHeader: hdr, Items: items}
350 for i := range n {
351 items[i].Offset, err = d.r.ReadUint64()
352 if err != nil {
353 return nil, err
354 }
355 items[i].Size, err = d.r.ReadUint64()
356 if err != nil {
357 return nil, err
358 }
359 items[i].Hash, err = d.r.ReadUint64()
360 if err != nil {
361 return nil, err
362 }
363 }
364 // Ensure we have the tail marker in the last item
365 if len(items) < 1 || items[len(items)-1].Hash != CaFormatGoodbyeTailMarker {
366 return nil, InvalidFormat{"tail marker not found"}
367 }
368 return e, nil
369
370 case CaFormatIndex:
371 e := FormatIndex{FormatHeader: hdr}
372 e.FeatureFlags, err = d.r.ReadUint64()
373 if err != nil {
374 return nil, err
375 }
376 e.ChunkSizeMin, err = d.r.ReadUint64()
377 if err != nil {
378 return nil, err
379 }
380 e.ChunkSizeAvg, err = d.r.ReadUint64()
381 if err != nil {
382 return nil, err
383 }
384 e.ChunkSizeMax, err = d.r.ReadUint64()
385 if err != nil {
386 return nil, err
387 }
388 return e, nil
389
390 case CaFormatTable:
391 // The length should be set to MAX_UINT64
392 if hdr.Size != math.MaxUint64 {
393 return nil, InvalidFormat{"expected size MAX_UINT64 in format table"}
394 }
395
396 e := FormatTable{FormatHeader: hdr}
397 var items []FormatTableItem
398 for {
399 offset, err := d.r.ReadUint64()
400 if err != nil {
401 return nil, err
402 }
403 if offset == 0 {
404 break
405 }
406 chunk, err := d.r.ReadID()
407 if err != nil {
408 return nil, err
409 }
410 items = append(items, FormatTableItem{Offset: offset, Chunk: chunk})
411 }
412 e.Items = items
413 // Confirm that the last element really is the tail marker
414 var x uint64
415 x, err = d.r.ReadUint64() // zero fill 2
416 if err != nil {
417 return nil, err
418 }
419 if x != 0 {
420 return nil, InvalidFormat{"tail marker not found"}
421 }
422 if _, err = d.r.ReadUint64(); err != nil { // index offset
423 return nil, err
424 }
425 if _, err = d.r.ReadUint64(); err != nil { // size
426 return nil, err
427 }
428 x, err = d.r.ReadUint64() // marker
429 if err != nil {
430 return nil, err
431 }
432 if x != CaFormatTableTailMarker {
433 return nil, InvalidFormat{"tail marker not found"}
434 }
435 return e, nil
436
437 default:
438 return nil, fmt.Errorf("unsupported header type %x", hdr.Type)
439 }
440 }
441
442 // FormatEncoder takes casync format elements and encodes them into a stream.
443 type FormatEncoder struct {
444 w writer
445 }
446
447 func NewFormatEncoder(w io.Writer) FormatEncoder {
448 return FormatEncoder{w: writer{w}}
449 }
450
451 func (e *FormatEncoder) Encode(v any) (int64, error) {
452 switch t := v.(type) {
453 case FormatEntry:
454 return e.w.WriteUint64(
455 t.Size,
456 t.Type,
457 t.FeatureFlags,
458 uint64(FilemodeToStatMode(t.Mode)),
459 t.Flags,
460 uint64(t.UID),
461 uint64(t.GID),
462 uint64(t.MTime.UnixNano()),
463 )
464
465 case FormatUser:
466 n, err := e.w.WriteUint64(t.Size, t.Type)
467 if err != nil {
468 return n, err
469 }
470 n1, err := io.Copy(e.w, strings.NewReader(t.Name+"\x00"))
471 return n + n1, err
472
473 case FormatGroup:
474 n, err := e.w.WriteUint64(t.Size, t.Type)
475 if err != nil {
476 return n, err
477 }
478 n1, err := io.Copy(e.w, strings.NewReader(t.Name+"\x00"))
479 return n + n1, err
480
481 case FormatXAttr:
482 n, err := e.w.WriteUint64(t.Size, t.Type)
483 if err != nil {
484 return n, err
485 }
486 n1, err := io.Copy(e.w, strings.NewReader(t.NameAndValue+"\x00"))
487 return n + n1, err
488
489 case FormatSELinux:
490 n, err := e.w.WriteUint64(t.Size, t.Type)
491 if err != nil {
492 return n, err
493 }
494 n1, err := io.Copy(e.w, strings.NewReader(t.Label+"\x00"))
495 return n + n1, err
496
497 case FormatFilename:
498 n, err := e.w.WriteUint64(t.Size, t.Type)
499 if err != nil {
500 return n, err
501 }
502 n1, err := io.Copy(e.w, strings.NewReader(t.Name+"\x00"))
503 return n + n1, err
504
505 case FormatSymlink:
506 n, err := e.w.WriteUint64(t.Size, t.Type)
507 if err != nil {
508 return n, err
509 }
510 n1, err := io.Copy(e.w, strings.NewReader(t.Target+"\x00"))
511 return n + n1, err
512
513 case FormatDevice:
514 return e.w.WriteUint64(
515 t.Size,
516 t.Type,
517 t.Major,
518 t.Minor,
519 )
520
521 case FormatPayload:
522 n, err := e.w.WriteUint64(t.Size, t.Type)
523 if err != nil {
524 return n, err
525 }
526 n1, err := io.Copy(e.w, t.Data)
527 return n + n1, err
528
529 case FormatFCaps:
530 n, err := e.w.WriteUint64(t.Size, t.Type)
531 if err != nil {
532 return n, err
533 }
534 n1, err := io.Copy(e.w, bytes.NewReader(t.Data))
535 return n + n1, err
536
537 case FormatACLUser:
538 n, err := e.w.WriteUint64(t.Size, t.Type, t.UID, t.Permissions)
539 if err != nil {
540 return n, err
541 }
542 n1, err := io.Copy(e.w, strings.NewReader(t.Name+"\x00"))
543 return n + n1, err
544
545 case FormatACLGroup:
546 n, err := e.w.WriteUint64(t.Size, t.Type, t.GID, t.Permissions)
547 if err != nil {
548 return n, err
549 }
550 n1, err := io.Copy(e.w, strings.NewReader(t.Name+"\x00"))
551 return n + n1, err
552
553 case FormatACLGroupObj:
554 return e.w.WriteUint64(t.Size, t.Type, t.Permissions)
555
556 case FormatACLDefault:
557 return e.w.WriteUint64(
558 t.Size,
559 t.Type,
560 t.UserObjPermissions,
561 t.GroupObjPermissions,
562 t.OtherPermissions,
563 t.MaskPermissions,
564 )
565
566 case FormatGoodbye:
567 // Write the header first
568 n, err := e.w.WriteUint64(t.Size, t.Type)
569 if err != nil {
570 return n, err
571 }
572 // Now the goodbye entries, needs to contain a tail marker
573 for _, item := range t.Items {
574 n1, err := e.w.WriteUint64(item.Offset, item.Size, item.Hash)
575 if err != nil {
576 return n + n1, err
577 }
578 n += n1
579 }
580 return n, nil
581
582 case FormatIndex:
583 return e.w.WriteUint64(
584 t.Size,
585 t.Type,
586 t.FeatureFlags,
587 t.ChunkSizeMin,
588 t.ChunkSizeAvg,
589 t.ChunkSizeMax,
590 )
591
592 case FormatTable:
593 // Write the header first
594 n, err := e.w.WriteUint64(t.Size, t.Type)
595 if err != nil {
596 return n, err
597 }
598 // Now the table items
599 for _, item := range t.Items {
600 n1, err := e.w.WriteUint64(item.Offset)
601 if err != nil {
602 return n + n1, err
603 }
604 n += n1
605 n2, err := e.w.WriteID(item.Chunk)
606 if err != nil {
607 return n + n2, err
608 }
609 n += n2
610 }
611 // Add a tail record, the decoder strips that off, so best we add this here
612 // to keep it consistent
613 n3, err := e.w.WriteUint64(
614 0, // zero fill1
615 0, // zero fill2
616 uint64(48), // index offset
617 uint64(n+40), // table size, without index
618 CaFormatTableTailMarker,
619 )
620 return n + n3, err
621
622 default:
623 return 0, fmt.Errorf("unsupported format element '%s'", reflect.TypeOf(v))
624 }
625 }
626
627 // Create a balanced BST of goodbye items in catar. Modifies the input slice.
628 func makeGoodbyeBST(in []FormatGoodbyeItem) []FormatGoodbyeItem {
629 // Sort the list by hash (primary) and offset (secondary)
630 sort.Slice(in, func(i, j int) bool {
631 switch {
632 case in[i].Hash < in[j].Hash:
633 return true
634 case in[i].Hash > in[j].Hash:
635 return false
636 default:
637 return in[i].Offset < in[j].Offset
638 }
639 })
640
641 // Convert the sorted array into a complete BST in array representation
642 out := make([]FormatGoodbyeItem, len(in))
643 e := uint(math.Log2(float64(len(in))) + 1)
644 bst(in, out, 0, e)
645 return out
646 }
647
648 func bst(in, out []FormatGoodbyeItem, i int, e uint) {
649 if len(in) == 0 {
650 return
651 }
652 p := 1 << (e - 1)
653 q := p << 1
654
655 var k int
656 if len(in) >= p-1+p/2 {
657 k = (q - 2) / 2
658 } else {
659 v := p - 1 + p/2 - len(in)
660 k = (q-2)/2 - v
661 }
662
663 out[i] = in[k]
664 bst(in[:k], out, 2*i+1, e-1)
665 bst(in[k+1:], out, 2*i+2, e-1)
666 }