1
2
3
4
5 package tar
6
7 import (
8 "bytes"
9 "io"
10 "path/filepath"
11 "strconv"
12 "strings"
13 "time"
14 )
15
16
17
18
19 type Reader struct {
20 r io.Reader
21 pad int64
22 curr fileReader
23 blk block
24
25
26
27
28 err error
29 }
30
31 type fileReader interface {
32 io.Reader
33 fileState
34
35 WriteTo(io.Writer) (int64, error)
36 }
37
38
39 func NewReader(r io.Reader) *Reader {
40 return &Reader{r: r, curr: ®FileReader{r, 0}}
41 }
42
43
44
45
46
47
48
49
50
51
52
53
54 func (tr *Reader) Next() (*Header, error) {
55 if tr.err != nil {
56 return nil, tr.err
57 }
58 hdr, err := tr.next()
59 tr.err = err
60 if err == nil && !filepath.IsLocal(hdr.Name) {
61 if tarinsecurepath.Value() == "0" {
62 tarinsecurepath.IncNonDefault()
63 err = ErrInsecurePath
64 }
65 }
66 return hdr, err
67 }
68
69 func (tr *Reader) next() (*Header, error) {
70 var paxHdrs map[string]string
71 var gnuLongName, gnuLongLink string
72
73
74
75
76
77
78 format := FormatUSTAR | FormatPAX | FormatGNU
79 for {
80
81 if err := discard(tr.r, tr.curr.physicalRemaining()); err != nil {
82 return nil, err
83 }
84 if _, err := tryReadFull(tr.r, tr.blk[:tr.pad]); err != nil {
85 return nil, err
86 }
87 tr.pad = 0
88
89 hdr, rawHdr, err := tr.readHeader()
90 if err != nil {
91 return nil, err
92 }
93 if err := tr.handleRegularFile(hdr); err != nil {
94 return nil, err
95 }
96 format.mayOnlyBe(hdr.Format)
97
98
99 switch hdr.Typeflag {
100 case TypeXHeader, TypeXGlobalHeader:
101 format.mayOnlyBe(FormatPAX)
102 paxHdrs, err = parsePAX(tr)
103 if err != nil {
104 return nil, err
105 }
106 if hdr.Typeflag == TypeXGlobalHeader {
107 mergePAX(hdr, paxHdrs)
108 return &Header{
109 Name: hdr.Name,
110 Typeflag: hdr.Typeflag,
111 Xattrs: hdr.Xattrs,
112 PAXRecords: hdr.PAXRecords,
113 Format: format,
114 }, nil
115 }
116 continue
117 case TypeGNULongName, TypeGNULongLink:
118 format.mayOnlyBe(FormatGNU)
119 realname, err := readSpecialFile(tr)
120 if err != nil {
121 return nil, err
122 }
123
124 var p parser
125 switch hdr.Typeflag {
126 case TypeGNULongName:
127 gnuLongName = p.parseString(realname)
128 case TypeGNULongLink:
129 gnuLongLink = p.parseString(realname)
130 }
131 continue
132 default:
133
134
135
136 if err := mergePAX(hdr, paxHdrs); err != nil {
137 return nil, err
138 }
139 if gnuLongName != "" {
140 hdr.Name = gnuLongName
141 }
142 if gnuLongLink != "" {
143 hdr.Linkname = gnuLongLink
144 }
145 if hdr.Typeflag == TypeRegA {
146 if strings.HasSuffix(hdr.Name, "/") {
147 hdr.Typeflag = TypeDir
148 } else {
149 hdr.Typeflag = TypeReg
150 }
151 }
152
153
154
155 if err := tr.handleRegularFile(hdr); err != nil {
156 return nil, err
157 }
158
159
160
161 if err := tr.handleSparseFile(hdr, rawHdr); err != nil {
162 return nil, err
163 }
164
165
166 if format.has(FormatUSTAR) && format.has(FormatPAX) {
167 format.mayOnlyBe(FormatUSTAR)
168 }
169 hdr.Format = format
170 return hdr, nil
171 }
172 }
173 }
174
175
176
177
178 func (tr *Reader) handleRegularFile(hdr *Header) error {
179 nb := hdr.Size
180 if isHeaderOnlyType(hdr.Typeflag) {
181 nb = 0
182 }
183 if nb < 0 {
184 return ErrHeader
185 }
186
187 tr.pad = blockPadding(nb)
188 tr.curr = ®FileReader{r: tr.r, nb: nb}
189 return nil
190 }
191
192
193
194 func (tr *Reader) handleSparseFile(hdr *Header, rawHdr *block) error {
195 var spd sparseDatas
196 var err error
197 if hdr.Typeflag == TypeGNUSparse {
198 spd, err = tr.readOldGNUSparseMap(hdr, rawHdr)
199 } else {
200 spd, err = tr.readGNUSparsePAXHeaders(hdr)
201 }
202
203
204
205 if err == nil && spd != nil {
206 if isHeaderOnlyType(hdr.Typeflag) || !validateSparseEntries(spd, hdr.Size) {
207 return ErrHeader
208 }
209 sph := invertSparseEntries(spd, hdr.Size)
210 tr.curr = &sparseFileReader{tr.curr, sph, 0}
211 }
212 return err
213 }
214
215
216
217
218
219 func (tr *Reader) readGNUSparsePAXHeaders(hdr *Header) (sparseDatas, error) {
220
221 var is1x0 bool
222 major, minor := hdr.PAXRecords[paxGNUSparseMajor], hdr.PAXRecords[paxGNUSparseMinor]
223 switch {
224 case major == "0" && (minor == "0" || minor == "1"):
225 is1x0 = false
226 case major == "1" && minor == "0":
227 is1x0 = true
228 case major != "" || minor != "":
229 return nil, nil
230 case hdr.PAXRecords[paxGNUSparseMap] != "":
231 is1x0 = false
232 default:
233 return nil, nil
234 }
235 hdr.Format.mayOnlyBe(FormatPAX)
236
237
238 if name := hdr.PAXRecords[paxGNUSparseName]; name != "" {
239 hdr.Name = name
240 }
241 size := hdr.PAXRecords[paxGNUSparseSize]
242 if size == "" {
243 size = hdr.PAXRecords[paxGNUSparseRealSize]
244 }
245 if size != "" {
246 n, err := strconv.ParseInt(size, 10, 64)
247 if err != nil {
248 return nil, ErrHeader
249 }
250 hdr.Size = n
251 }
252
253
254 if is1x0 {
255 return readGNUSparseMap1x0(tr.curr)
256 }
257 return readGNUSparseMap0x1(hdr.PAXRecords)
258 }
259
260
261 func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
262 for k, v := range paxHdrs {
263 if v == "" {
264 continue
265 }
266 var id64 int64
267 switch k {
268 case paxPath:
269 hdr.Name = v
270 case paxLinkpath:
271 hdr.Linkname = v
272 case paxUname:
273 hdr.Uname = v
274 case paxGname:
275 hdr.Gname = v
276 case paxUid:
277 id64, err = strconv.ParseInt(v, 10, 64)
278 hdr.Uid = int(id64)
279 case paxGid:
280 id64, err = strconv.ParseInt(v, 10, 64)
281 hdr.Gid = int(id64)
282 case paxAtime:
283 hdr.AccessTime, err = parsePAXTime(v)
284 case paxMtime:
285 hdr.ModTime, err = parsePAXTime(v)
286 case paxCtime:
287 hdr.ChangeTime, err = parsePAXTime(v)
288 case paxSize:
289 hdr.Size, err = strconv.ParseInt(v, 10, 64)
290 default:
291 if strings.HasPrefix(k, paxSchilyXattr) {
292 if hdr.Xattrs == nil {
293 hdr.Xattrs = make(map[string]string)
294 }
295 hdr.Xattrs[k[len(paxSchilyXattr):]] = v
296 }
297 }
298 if err != nil {
299 return ErrHeader
300 }
301 }
302 hdr.PAXRecords = paxHdrs
303 return nil
304 }
305
306
307
308 func parsePAX(r io.Reader) (map[string]string, error) {
309 buf, err := readSpecialFile(r)
310 if err != nil {
311 return nil, err
312 }
313 sbuf := string(buf)
314
315
316
317
318 var sparseMap []string
319
320 paxHdrs := make(map[string]string)
321 for len(sbuf) > 0 {
322 key, value, residual, err := parsePAXRecord(sbuf)
323 if err != nil {
324 return nil, ErrHeader
325 }
326 sbuf = residual
327
328 switch key {
329 case paxGNUSparseOffset, paxGNUSparseNumBytes:
330
331 if (len(sparseMap)%2 == 0 && key != paxGNUSparseOffset) ||
332 (len(sparseMap)%2 == 1 && key != paxGNUSparseNumBytes) ||
333 strings.Contains(value, ",") {
334 return nil, ErrHeader
335 }
336 sparseMap = append(sparseMap, value)
337 default:
338 paxHdrs[key] = value
339 }
340 }
341 if len(sparseMap) > 0 {
342 paxHdrs[paxGNUSparseMap] = strings.Join(sparseMap, ",")
343 }
344 return paxHdrs, nil
345 }
346
347
348
349
350
351
352
353
354
355 func (tr *Reader) readHeader() (*Header, *block, error) {
356
357 if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
358 return nil, nil, err
359 }
360 if bytes.Equal(tr.blk[:], zeroBlock[:]) {
361 if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
362 return nil, nil, err
363 }
364 if bytes.Equal(tr.blk[:], zeroBlock[:]) {
365 return nil, nil, io.EOF
366 }
367 return nil, nil, ErrHeader
368 }
369
370
371 format := tr.blk.getFormat()
372 if format == FormatUnknown {
373 return nil, nil, ErrHeader
374 }
375
376 var p parser
377 hdr := new(Header)
378
379
380 v7 := tr.blk.toV7()
381 hdr.Typeflag = v7.typeFlag()[0]
382 hdr.Name = p.parseString(v7.name())
383 hdr.Linkname = p.parseString(v7.linkName())
384 hdr.Size = p.parseNumeric(v7.size())
385 hdr.Mode = p.parseNumeric(v7.mode())
386 hdr.Uid = int(p.parseNumeric(v7.uid()))
387 hdr.Gid = int(p.parseNumeric(v7.gid()))
388 hdr.ModTime = time.Unix(p.parseNumeric(v7.modTime()), 0)
389
390
391 if format > formatV7 {
392 ustar := tr.blk.toUSTAR()
393 hdr.Uname = p.parseString(ustar.userName())
394 hdr.Gname = p.parseString(ustar.groupName())
395 hdr.Devmajor = p.parseNumeric(ustar.devMajor())
396 hdr.Devminor = p.parseNumeric(ustar.devMinor())
397
398 var prefix string
399 switch {
400 case format.has(FormatUSTAR | FormatPAX):
401 hdr.Format = format
402 ustar := tr.blk.toUSTAR()
403 prefix = p.parseString(ustar.prefix())
404
405
406
407 notASCII := func(r rune) bool { return r >= 0x80 }
408 if bytes.IndexFunc(tr.blk[:], notASCII) >= 0 {
409 hdr.Format = FormatUnknown
410 }
411 nul := func(b []byte) bool { return int(b[len(b)-1]) == 0 }
412 if !(nul(v7.size()) && nul(v7.mode()) && nul(v7.uid()) && nul(v7.gid()) &&
413 nul(v7.modTime()) && nul(ustar.devMajor()) && nul(ustar.devMinor())) {
414 hdr.Format = FormatUnknown
415 }
416 case format.has(formatSTAR):
417 star := tr.blk.toSTAR()
418 prefix = p.parseString(star.prefix())
419 hdr.AccessTime = time.Unix(p.parseNumeric(star.accessTime()), 0)
420 hdr.ChangeTime = time.Unix(p.parseNumeric(star.changeTime()), 0)
421 case format.has(FormatGNU):
422 hdr.Format = format
423 var p2 parser
424 gnu := tr.blk.toGNU()
425 if b := gnu.accessTime(); b[0] != 0 {
426 hdr.AccessTime = time.Unix(p2.parseNumeric(b), 0)
427 }
428 if b := gnu.changeTime(); b[0] != 0 {
429 hdr.ChangeTime = time.Unix(p2.parseNumeric(b), 0)
430 }
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 if p2.err != nil {
454 hdr.AccessTime, hdr.ChangeTime = time.Time{}, time.Time{}
455 ustar := tr.blk.toUSTAR()
456 if s := p.parseString(ustar.prefix()); isASCII(s) {
457 prefix = s
458 }
459 hdr.Format = FormatUnknown
460 }
461 }
462 if len(prefix) > 0 {
463 hdr.Name = prefix + "/" + hdr.Name
464 }
465 }
466 return hdr, &tr.blk, p.err
467 }
468
469
470
471
472
473
474
475
476
477 func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, error) {
478
479
480
481 if blk.getFormat() != FormatGNU {
482 return nil, ErrHeader
483 }
484 hdr.Format.mayOnlyBe(FormatGNU)
485
486 var p parser
487 hdr.Size = p.parseNumeric(blk.toGNU().realSize())
488 if p.err != nil {
489 return nil, p.err
490 }
491 s := blk.toGNU().sparse()
492 spd := make(sparseDatas, 0, s.maxEntries())
493 for {
494 for i := 0; i < s.maxEntries(); i++ {
495
496 if s.entry(i).offset()[0] == 0x00 {
497 break
498 }
499 offset := p.parseNumeric(s.entry(i).offset())
500 length := p.parseNumeric(s.entry(i).length())
501 if p.err != nil {
502 return nil, p.err
503 }
504 spd = append(spd, sparseEntry{Offset: offset, Length: length})
505 }
506
507 if s.isExtended()[0] > 0 {
508
509 if _, err := mustReadFull(tr.r, blk[:]); err != nil {
510 return nil, err
511 }
512 s = blk.toSparse()
513 continue
514 }
515 return spd, nil
516 }
517 }
518
519
520
521
522
523
524
525
526
527
528
529 func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
530 var (
531 cntNewline int64
532 buf bytes.Buffer
533 blk block
534 )
535
536
537
538 feedTokens := func(n int64) error {
539 for cntNewline < n {
540 if _, err := mustReadFull(r, blk[:]); err != nil {
541 return err
542 }
543 buf.Write(blk[:])
544 for _, c := range blk {
545 if c == '\n' {
546 cntNewline++
547 }
548 }
549 }
550 return nil
551 }
552
553
554
555 nextToken := func() string {
556 cntNewline--
557 tok, _ := buf.ReadString('\n')
558 return strings.TrimRight(tok, "\n")
559 }
560
561
562
563 if err := feedTokens(1); err != nil {
564 return nil, err
565 }
566 numEntries, err := strconv.ParseInt(nextToken(), 10, 0)
567 if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
568 return nil, ErrHeader
569 }
570
571
572
573
574 if err := feedTokens(2 * numEntries); err != nil {
575 return nil, err
576 }
577 spd := make(sparseDatas, 0, numEntries)
578 for i := int64(0); i < numEntries; i++ {
579 offset, err1 := strconv.ParseInt(nextToken(), 10, 64)
580 length, err2 := strconv.ParseInt(nextToken(), 10, 64)
581 if err1 != nil || err2 != nil {
582 return nil, ErrHeader
583 }
584 spd = append(spd, sparseEntry{Offset: offset, Length: length})
585 }
586 return spd, nil
587 }
588
589
590
591 func readGNUSparseMap0x1(paxHdrs map[string]string) (sparseDatas, error) {
592
593
594 numEntriesStr := paxHdrs[paxGNUSparseNumBlocks]
595 numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0)
596 if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
597 return nil, ErrHeader
598 }
599
600
601 sparseMap := strings.Split(paxHdrs[paxGNUSparseMap], ",")
602 if len(sparseMap) == 1 && sparseMap[0] == "" {
603 sparseMap = sparseMap[:0]
604 }
605 if int64(len(sparseMap)) != 2*numEntries {
606 return nil, ErrHeader
607 }
608
609
610
611 spd := make(sparseDatas, 0, numEntries)
612 for len(sparseMap) >= 2 {
613 offset, err1 := strconv.ParseInt(sparseMap[0], 10, 64)
614 length, err2 := strconv.ParseInt(sparseMap[1], 10, 64)
615 if err1 != nil || err2 != nil {
616 return nil, ErrHeader
617 }
618 spd = append(spd, sparseEntry{Offset: offset, Length: length})
619 sparseMap = sparseMap[2:]
620 }
621 return spd, nil
622 }
623
624
625
626
627
628
629
630
631
632
633
634 func (tr *Reader) Read(b []byte) (int, error) {
635 if tr.err != nil {
636 return 0, tr.err
637 }
638 n, err := tr.curr.Read(b)
639 if err != nil && err != io.EOF {
640 tr.err = err
641 }
642 return n, err
643 }
644
645
646
647
648
649
650
651
652
653
654
655 func (tr *Reader) writeTo(w io.Writer) (int64, error) {
656 if tr.err != nil {
657 return 0, tr.err
658 }
659 n, err := tr.curr.WriteTo(w)
660 if err != nil {
661 tr.err = err
662 }
663 return n, err
664 }
665
666
667 type regFileReader struct {
668 r io.Reader
669 nb int64
670 }
671
672 func (fr *regFileReader) Read(b []byte) (n int, err error) {
673 if int64(len(b)) > fr.nb {
674 b = b[:fr.nb]
675 }
676 if len(b) > 0 {
677 n, err = fr.r.Read(b)
678 fr.nb -= int64(n)
679 }
680 switch {
681 case err == io.EOF && fr.nb > 0:
682 return n, io.ErrUnexpectedEOF
683 case err == nil && fr.nb == 0:
684 return n, io.EOF
685 default:
686 return n, err
687 }
688 }
689
690 func (fr *regFileReader) WriteTo(w io.Writer) (int64, error) {
691 return io.Copy(w, struct{ io.Reader }{fr})
692 }
693
694
695 func (fr regFileReader) logicalRemaining() int64 {
696 return fr.nb
697 }
698
699
700 func (fr regFileReader) physicalRemaining() int64 {
701 return fr.nb
702 }
703
704
705 type sparseFileReader struct {
706 fr fileReader
707 sp sparseHoles
708 pos int64
709 }
710
711 func (sr *sparseFileReader) Read(b []byte) (n int, err error) {
712 finished := int64(len(b)) >= sr.logicalRemaining()
713 if finished {
714 b = b[:sr.logicalRemaining()]
715 }
716
717 b0 := b
718 endPos := sr.pos + int64(len(b))
719 for endPos > sr.pos && err == nil {
720 var nf int
721 holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
722 if sr.pos < holeStart {
723 bf := b[:min(int64(len(b)), holeStart-sr.pos)]
724 nf, err = tryReadFull(sr.fr, bf)
725 } else {
726 bf := b[:min(int64(len(b)), holeEnd-sr.pos)]
727 nf, err = tryReadFull(zeroReader{}, bf)
728 }
729 b = b[nf:]
730 sr.pos += int64(nf)
731 if sr.pos >= holeEnd && len(sr.sp) > 1 {
732 sr.sp = sr.sp[1:]
733 }
734 }
735
736 n = len(b0) - len(b)
737 switch {
738 case err == io.EOF:
739 return n, errMissData
740 case err != nil:
741 return n, err
742 case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:
743 return n, errUnrefData
744 case finished:
745 return n, io.EOF
746 default:
747 return n, nil
748 }
749 }
750
751 func (sr *sparseFileReader) WriteTo(w io.Writer) (n int64, err error) {
752 ws, ok := w.(io.WriteSeeker)
753 if ok {
754 if _, err := ws.Seek(0, io.SeekCurrent); err != nil {
755 ok = false
756 }
757 }
758 if !ok {
759 return io.Copy(w, struct{ io.Reader }{sr})
760 }
761
762 var writeLastByte bool
763 pos0 := sr.pos
764 for sr.logicalRemaining() > 0 && !writeLastByte && err == nil {
765 var nf int64
766 holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
767 if sr.pos < holeStart {
768 nf = holeStart - sr.pos
769 nf, err = io.CopyN(ws, sr.fr, nf)
770 } else {
771 nf = holeEnd - sr.pos
772 if sr.physicalRemaining() == 0 {
773 writeLastByte = true
774 nf--
775 }
776 _, err = ws.Seek(nf, io.SeekCurrent)
777 }
778 sr.pos += nf
779 if sr.pos >= holeEnd && len(sr.sp) > 1 {
780 sr.sp = sr.sp[1:]
781 }
782 }
783
784
785
786 if writeLastByte && err == nil {
787 _, err = ws.Write([]byte{0})
788 sr.pos++
789 }
790
791 n = sr.pos - pos0
792 switch {
793 case err == io.EOF:
794 return n, errMissData
795 case err != nil:
796 return n, err
797 case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:
798 return n, errUnrefData
799 default:
800 return n, nil
801 }
802 }
803
804 func (sr sparseFileReader) logicalRemaining() int64 {
805 return sr.sp[len(sr.sp)-1].endOffset() - sr.pos
806 }
807 func (sr sparseFileReader) physicalRemaining() int64 {
808 return sr.fr.physicalRemaining()
809 }
810
811 type zeroReader struct{}
812
813 func (zeroReader) Read(b []byte) (int, error) {
814 clear(b)
815 return len(b), nil
816 }
817
818
819
820 func mustReadFull(r io.Reader, b []byte) (int, error) {
821 n, err := tryReadFull(r, b)
822 if err == io.EOF {
823 err = io.ErrUnexpectedEOF
824 }
825 return n, err
826 }
827
828
829
830 func tryReadFull(r io.Reader, b []byte) (n int, err error) {
831 for len(b) > n && err == nil {
832 var nn int
833 nn, err = r.Read(b[n:])
834 n += nn
835 }
836 if len(b) == n && err == io.EOF {
837 err = nil
838 }
839 return n, err
840 }
841
842
843
844 func readSpecialFile(r io.Reader) ([]byte, error) {
845 buf, err := io.ReadAll(io.LimitReader(r, maxSpecialFileSize+1))
846 if len(buf) > maxSpecialFileSize {
847 return nil, ErrFieldTooLong
848 }
849 return buf, err
850 }
851
852
853 func discard(r io.Reader, n int64) error {
854
855
856
857
858 var seekSkipped int64
859 if sr, ok := r.(io.Seeker); ok && n > 1 {
860
861
862
863
864 pos1, err := sr.Seek(0, io.SeekCurrent)
865 if pos1 >= 0 && err == nil {
866
867 pos2, err := sr.Seek(n-1, io.SeekCurrent)
868 if pos2 < 0 || err != nil {
869 return err
870 }
871 seekSkipped = pos2 - pos1
872 }
873 }
874
875 copySkipped, err := io.CopyN(io.Discard, r, n-seekSkipped)
876 if err == io.EOF && seekSkipped+copySkipped < n {
877 err = io.ErrUnexpectedEOF
878 }
879 return err
880 }
881
View as plain text