-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.go
662 lines (538 loc) · 13.6 KB
/
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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
package siva
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"path"
"sort"
"strings"
"time"
)
var (
IndexSignature = []byte{'I', 'B', 'A'}
ErrInvalidIndexEntry = errors.New("invalid index entry")
ErrInvalidSignature = errors.New("invalid signature")
ErrEmptyIndex = errors.New("empty index")
ErrUnsupportedIndexVersion = errors.New("unsupported index version")
ErrCRC32Missmatch = errors.New("crc32 mismatch")
)
const (
IndexVersion uint8 = 1
indexFooterSize = 24
)
// Index contains all the files on a siva file, including duplicate files or
// even does flagged as deleted.
type Index []*IndexEntry
// ReadFrom reads an Index from a given reader, the position where the current
// block ends is required since we are reading the index from the end of the
// file
func (i *Index) ReadFrom(r io.ReadSeeker, endBlock uint64) error {
if _, err := r.Seek(int64(endBlock)-indexFooterSize, io.SeekStart); err != nil {
return &IndexReadError{err}
}
f, err := i.readFooter(r)
if err != nil {
return &IndexReadError{err}
}
startingPos := int64(f.IndexSize) + indexFooterSize
if _, err := r.Seek(-startingPos, io.SeekCurrent); err != nil {
return &IndexReadError{err}
}
defer sort.Sort(i)
err = i.readIndex(r, f, endBlock)
if err != nil {
return &IndexReadError{err}
}
return nil
}
func (i *Index) readFooter(r io.Reader) (*IndexFooter, error) {
f := &IndexFooter{}
if err := f.ReadFrom(r); err != nil {
return nil, err
}
return f, nil
}
func (i *Index) readIndex(r io.Reader, f *IndexFooter, endBlock uint64) error {
hr := newHashedReader(r)
if err := i.readSignature(hr); err != nil {
return err
}
if err := i.readEntries(hr, f, endBlock); err != nil {
return err
}
if f.CRC32 != hr.Checkshum() {
return ErrCRC32Missmatch
}
return nil
}
func (i *Index) readSignature(r io.Reader) error {
sig := make([]byte, 3)
if _, err := r.Read(sig); err != nil {
return err
}
if !bytes.Equal(sig, IndexSignature) {
return ErrInvalidSignature
}
var version uint8
if err := binary.Read(r, binary.BigEndian, &version); err != nil {
return err
}
if version != IndexVersion {
return ErrUnsupportedIndexVersion
}
return nil
}
func (i *Index) readEntries(r io.Reader, f *IndexFooter, endBlock uint64) error {
for j := 0; j < int(f.EntryCount); j++ {
e := &IndexEntry{}
if err := e.ReadFrom(r); err != nil {
return err
}
e.absStart = (endBlock - f.BlockSize) + e.Start
*i = append(*i, e)
}
return nil
}
// WriteTo writes the Index to a io.Writer
func (i *Index) WriteTo(w io.Writer) error {
if len(*i) == 0 {
return ErrEmptyIndex
}
hw := newHashedWriter(w)
f := &IndexFooter{
EntryCount: uint32(len(*i)),
}
if _, err := hw.Write(IndexSignature); err != nil {
return &IndexWriteError{err}
}
if err := binary.Write(hw, binary.BigEndian, IndexVersion); err != nil {
return &IndexWriteError{err}
}
var blockSize uint64
for _, e := range *i {
blockSize += e.Size
if err := e.WriteTo(hw); err != nil {
return &IndexWriteError{err}
}
}
f.IndexSize = uint64(hw.Position())
f.BlockSize = blockSize + f.IndexSize + indexFooterSize
f.CRC32 = hw.Checksum()
if err := f.WriteTo(hw); err != nil {
return &IndexWriteError{err}
}
return nil
}
// Len implements sort.Interface.
func (s Index) Len() int { return len(s) }
// Swap implements sort.Interface.
func (s Index) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Less implements sort.Interface.
func (s Index) Less(i, j int) bool { return s[i].absStart < s[j].absStart }
// Filter returns a filtered version of the current Index removing duplicates
// keeping the latest versions and filtering all the deleted files
func (i *Index) Filter() Index {
index := i.filter()
sort.Sort(index)
return index
}
func (i *Index) filter() Index {
var f Index
seen := make(map[string]bool)
for j := len(*i) - 1; j >= 0; j-- {
e := (*i)[j]
if _, ok := seen[e.Name]; ok {
continue
}
seen[e.Name] = true
if e.Flags&FlagDeleted != 0 {
continue
}
f = append(f, e)
}
return f
}
// ToSafePaths creates a new index where all entry names are transformed to safe
// paths using the top-level `ToSafePath` function. If you are using siva to
// extract files to the file-system, you should either use this function or
// perform your own validation and normalization.
func (i *Index) ToSafePaths() Index {
f := make(Index, len(*i))
for idx, e := range *i {
e = &*e
e.Name = ToSafePath(e.Name)
f[idx] = e
}
return f
}
// Find returns the first IndexEntry with the given name, if any
func (i Index) Find(name string) *IndexEntry {
name = ToSafePath(name)
for _, e := range i {
if e.Name == name {
return e
}
}
return nil
}
// Glob returns all index entries whose name matches pattern or nil if there is
// no matching entry. The syntax of patterns is the same as in filepath.Match.
func (i Index) Glob(pattern string) ([]*IndexEntry, error) {
pattern = ToSafePath(pattern)
matches := []*IndexEntry{}
for _, e := range i {
m, err := path.Match(pattern, e.Name)
if err != nil {
return nil, err
}
if m {
matches = append(matches, e)
}
}
return matches, nil
}
// OrderedIndex is a specialized index lexicographically ordered. It has
// methods to add or delete IndexEntries and maintain its order. Also has
// as faster Find method.
type OrderedIndex Index
// Pos gets the position of the file in the index or where it should be
// inserted if it's not already there.
func (o OrderedIndex) Pos(path string) int {
if len(o) == 0 {
return 0
}
pos := sort.Search(len(o), func(i int) bool {
return o[i].Name >= path
})
return pos
}
// Update adds or deletes an IndexEntry to the index depending on the
// FlagDeleted value.
func (o OrderedIndex) Update(e *IndexEntry) OrderedIndex {
if e == nil {
return o
}
if e.Flags&FlagDeleted == 0 {
return o.Add(e)
}
return o.Delete(e.Name)
}
// Add returns an updated index with the new IndexEntry.
func (o OrderedIndex) Add(e *IndexEntry) OrderedIndex {
if e == nil {
return o
}
if len(o) == 0 {
return OrderedIndex{e}
}
path := e.Name
pos := o.Pos(path)
if pos < len(o) && o[pos].Name == path {
o[pos] = e
return o
}
if pos == len(o) {
return append(o, e)
}
return append(o[:pos], append(Index{e}, o[pos:]...)...)
}
// Delete returns an updated index with the IndexEntry for the path deleted.
func (o OrderedIndex) Delete(path string) OrderedIndex {
if len(o) == 0 {
return o
}
pos := o.Pos(path)
if pos < len(o) && o[pos].Name != path {
return o
}
return append(o[:pos], o[pos+1:]...)
}
// Find returns the IndexEntry for a path or nil. This version is faster than
// Index.Find.
func (o OrderedIndex) Find(path string) *IndexEntry {
if len(o) == 0 {
return nil
}
pos := o.Pos(path)
if pos >= 0 && pos < len(o) && o[pos].Name == path {
return o[pos]
}
return nil
}
func globPrefix(pattern string) string {
slash := false
for i, c := range pattern {
switch c {
case '\\':
slash = true
continue
case '*', '?', '[':
if !slash {
return pattern[:i]
}
}
slash = false
}
return pattern
}
// Glob returns all index entries whose name matches pattern or nil if there is
// no matching entry. The syntax of patterns is the same as in filepath.Match.
func (o OrderedIndex) Glob(pattern string) ([]*IndexEntry, error) {
pattern = ToSafePath(pattern)
prefix := globPrefix(pattern)
matches := []*IndexEntry{}
first := o.Pos(prefix)
if first == -1 {
return matches, nil
}
for _, e := range o[first:] {
if !strings.HasPrefix(e.Name, prefix) {
break
}
m, err := path.Match(pattern, e.Name)
if err != nil {
return nil, err
}
if m {
matches = append(matches, e)
}
}
return matches, nil
}
// Sort orders the index lexicographically.
func (o OrderedIndex) Sort() {
sort.Sort(o)
}
// Len implements sort.Interface.
func (s OrderedIndex) Len() int { return len(s) }
// Swap implements sort.Interface.
func (s OrderedIndex) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Less implements sort.Interface.
func (s OrderedIndex) Less(i, j int) bool { return s[i].Name < s[j].Name }
type IndexEntry struct {
Header
Start uint64
Size uint64
CRC32 uint32
// absStart stores the absolute starting position of the entry in the file
// across all the blocks in the file, is calculate on-the-fly, so that's
// why is not stored
absStart uint64
}
// WriteTo writes the IndexEntry to an io.Writer
func (e *IndexEntry) WriteTo(w io.Writer) error {
if e.Name == "" {
return ErrInvalidIndexEntry
}
name := []byte(e.Name)
length := uint32(len(name))
if err := binary.Write(w, binary.BigEndian, length); err != nil {
return err
}
if _, err := w.Write(name); err != nil {
return err
}
return writeBinary(w, []interface{}{
e.Mode,
e.ModTime.UnixNano(),
e.Start,
e.Size,
e.CRC32,
e.Flags,
})
}
// ReadFrom reads a IndexEntry entry from an io.Reader
func (e *IndexEntry) ReadFrom(r io.Reader) error {
var length uint32
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
return err
}
filename := make([]byte, length)
if _, err := r.Read(filename); err != nil {
return err
}
var nsec int64
err := readBinary(r, []interface{}{
&e.Mode,
&nsec,
&e.Start,
&e.Size,
&e.CRC32,
&e.Flags,
})
e.Name = string(filename)
e.ModTime = time.Unix(0, nsec)
return err
}
type IndexFooter struct {
EntryCount uint32
IndexSize uint64
BlockSize uint64
CRC32 uint32
}
// ReadFrom reads a IndexFooter entry from an io.Reader
func (f *IndexFooter) ReadFrom(r io.Reader) error {
return readBinary(r, []interface{}{
&f.EntryCount,
&f.IndexSize,
&f.BlockSize,
&f.CRC32,
})
}
// WriteTo writes the IndexFooter to an io.Writer
func (f *IndexFooter) WriteTo(w io.Writer) error {
return writeBinary(w, []interface{}{
f.EntryCount,
f.IndexSize,
f.BlockSize,
f.CRC32,
})
}
func writeBinary(w io.Writer, data []interface{}) error {
for _, v := range data {
err := binary.Write(w, binary.BigEndian, v)
if err != nil {
return err
}
}
return nil
}
func readBinary(r io.Reader, data []interface{}) error {
for _, v := range data {
err := binary.Read(r, binary.BigEndian, v)
if err != nil {
return err
}
}
return nil
}
// readIndex loads the index at offset's position or at the end of the file if
// the offset is 0. It uses readIndexAt to load each of the indexes in the
// chain.
func readIndex(r io.ReadSeeker, offset uint64) (Index, error) {
endLastBlock := offset
if endLastBlock == 0 {
ofs, err := r.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
endLastBlock = uint64(ofs)
}
if endLastBlock == 0 {
return nil, ErrEmptyIndex
}
i, err := readIndexAt(r, endLastBlock)
if err != nil {
return i, err
}
sort.Sort(i)
return i, nil
}
func readIndexAt(r io.ReadSeeker, offset uint64) (Index, error) {
i := make(Index, 0)
if err := i.ReadFrom(r, offset); err != nil {
return nil, err
}
if len(i) == 0 || i[0].absStart == 0 {
return i, nil
}
previ, err := readIndexAt(r, i[0].absStart)
if err != nil {
return nil, err
}
i = append(i, previ...)
return i, nil
}
type IndexReadError struct {
Err error
}
func (e *IndexReadError) Error() string {
return fmt.Sprintf("index read failed: %s", e.Err.Error())
}
type IndexWriteError struct {
Err error
}
func (e *IndexWriteError) Error() string {
return fmt.Sprintf("index write failed: %s", e.Err.Error())
}
// ToSafePath transforms a filesystem path to one that is safe to
// use as a relative path on the native filesystem:
//
// - Removes drive and network share on Windows.
// - Does regular clean up (removing `/./` parts).
// - Removes any leading `../`.
// - Removes leading `/`.
//
// This is a convenience function to implement siva file extractors that are not
// vulnerable to zip slip and similar vulnerabilities. However, for Windows
// absolute paths (with drive or network share) it does not give consistent
// results across platforms.
//
// If your application relies on using absolute paths, you should not use this
// and you are encouraged to do your own validation and normalization.
func ToSafePath(p string) string {
volume := volumeName(p)
if volume != "" {
p = strings.Replace(p, volume, "", 1)
}
p = toSlash(p)
p = path.Join(posixSeparator, p)
return p[1:]
}
// These functions are copied from golang library path/filepath/path_windows.go
// as we need them also in posix systems.
const (
posixSeparator = "/"
windowsSeparator = "\\"
)
func toSlash(path string) string {
return strings.Replace(path, windowsSeparator, posixSeparator, -1)
}
func fromSlash(path string) string {
return strings.Replace(path, posixSeparator, windowsSeparator, -1)
}
func volumeName(path string) string {
return path[:volumeNameLen(path)]
}
func isSlash(c uint8) bool {
return c == '\\' || c == '/'
}
// volumeNameLen returns length of the leading volume name on Windows.
// It returns 0 elsewhere.
func volumeNameLen(path string) int {
if len(path) < 2 {
return 0
}
// with drive letter
c := path[0]
if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
return 2
}
// is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) &&
!isSlash(path[2]) && path[2] != '.' {
// first, leading `\\` and next shouldn't be `\`. its server name.
for n := 3; n < l-1; n++ {
// second, next '\' shouldn't be repeated.
if isSlash(path[n]) {
n++
// third, following something characters. its share name.
if !isSlash(path[n]) {
if path[n] == '.' {
break
}
for ; n < l; n++ {
if isSlash(path[n]) {
break
}
}
return n
}
break
}
}
}
return 0
}