1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // Reader provides sequential access to the contents of a tar archive.
17 // Reader.Next advances to the next file in the archive (including the first),
18 // and then Reader can be treated as an io.Reader to access the file's data.
21 pad int64 // Amount of padding (ignored) after current file entry
22 curr fileReader // Reader for current file entry
23 blk block // Buffer to use as temporary local storage
25 // err is a persistent error.
26 // It is only the responsibility of every exported method of Reader to
27 // ensure that this error is sticky.
31 type fileReader interface {
35 WriteTo(io.Writer) (int64, error)
38 // NewReader creates a new Reader reading from r.
39 func NewReader(r io.Reader) *Reader {
40 return &Reader{r: r, curr: ®FileReader{r, 0}}
43 // Next advances to the next entry in the tar archive.
44 // The Header.Size determines how many bytes can be read for the next file.
45 // Any remaining data in the current file is automatically discarded.
47 // io.EOF is returned at the end of the input.
48 func (tr *Reader) Next() (*Header, error) {
57 func (tr *Reader) next() (*Header, error) {
58 var paxHdrs map[string]string
59 var gnuLongName, gnuLongLink string
61 // Externally, Next iterates through the tar archive as if it is a series of
62 // files. Internally, the tar format often uses fake "files" to add meta
63 // data that describes the next file. These meta data "files" should not
64 // normally be visible to the outside. As such, this loop iterates through
65 // one or more "header files" until it finds a "normal file".
66 format := FormatUSTAR | FormatPAX | FormatGNU
68 // Discard the remainder of the file and any padding.
69 if err := discard(tr.r, tr.curr.PhysicalRemaining()); err != nil {
72 if _, err := tryReadFull(tr.r, tr.blk[:tr.pad]); err != nil {
77 hdr, rawHdr, err := tr.readHeader()
81 if err := tr.handleRegularFile(hdr); err != nil {
84 format.mayOnlyBe(hdr.Format)
86 // Check for PAX/GNU special headers and files.
88 case TypeXHeader, TypeXGlobalHeader:
89 format.mayOnlyBe(FormatPAX)
90 paxHdrs, err = parsePAX(tr)
94 if hdr.Typeflag == TypeXGlobalHeader {
95 mergePAX(hdr, paxHdrs)
98 Typeflag: hdr.Typeflag,
100 PAXRecords: hdr.PAXRecords,
104 continue // This is a meta header affecting the next header
105 case TypeGNULongName, TypeGNULongLink:
106 format.mayOnlyBe(FormatGNU)
107 realname, err := ioutil.ReadAll(tr)
113 switch hdr.Typeflag {
114 case TypeGNULongName:
115 gnuLongName = p.parseString(realname)
116 case TypeGNULongLink:
117 gnuLongLink = p.parseString(realname)
119 continue // This is a meta header affecting the next header
121 // The old GNU sparse format is handled here since it is technically
122 // just a regular file with additional attributes.
124 if err := mergePAX(hdr, paxHdrs); err != nil {
127 if gnuLongName != "" {
128 hdr.Name = gnuLongName
130 if gnuLongLink != "" {
131 hdr.Linkname = gnuLongLink
133 if hdr.Typeflag == TypeRegA {
134 if strings.HasSuffix(hdr.Name, "/") {
135 hdr.Typeflag = TypeDir // Legacy archives use trailing slash for directories
137 hdr.Typeflag = TypeReg
141 // The extended headers may have updated the size.
142 // Thus, setup the regFileReader again after merging PAX headers.
143 if err := tr.handleRegularFile(hdr); err != nil {
147 // Sparse formats rely on being able to read from the logical data
148 // section; there must be a preceding call to handleRegularFile.
149 if err := tr.handleSparseFile(hdr, rawHdr); err != nil {
153 // Set the final guess at the format.
154 if format.has(FormatUSTAR) && format.has(FormatPAX) {
155 format.mayOnlyBe(FormatUSTAR)
158 return hdr, nil // This is a file, so stop
163 // handleRegularFile sets up the current file reader and padding such that it
164 // can only read the following logical data section. It will properly handle
165 // special headers that contain no data section.
166 func (tr *Reader) handleRegularFile(hdr *Header) error {
168 if isHeaderOnlyType(hdr.Typeflag) {
175 tr.pad = blockPadding(nb)
176 tr.curr = ®FileReader{r: tr.r, nb: nb}
180 // handleSparseFile checks if the current file is a sparse format of any type
181 // and sets the curr reader appropriately.
182 func (tr *Reader) handleSparseFile(hdr *Header, rawHdr *block) error {
185 if hdr.Typeflag == TypeGNUSparse {
186 spd, err = tr.readOldGNUSparseMap(hdr, rawHdr)
188 spd, err = tr.readGNUSparsePAXHeaders(hdr)
191 // If sp is non-nil, then this is a sparse file.
192 // Note that it is possible for len(sp) == 0.
193 if err == nil && spd != nil {
194 if isHeaderOnlyType(hdr.Typeflag) || !validateSparseEntries(spd, hdr.Size) {
197 sph := invertSparseEntries(spd, hdr.Size)
198 tr.curr = &sparseFileReader{tr.curr, sph, 0}
203 // readGNUSparsePAXHeaders checks the PAX headers for GNU sparse headers.
204 // If they are found, then this function reads the sparse map and returns it.
205 // This assumes that 0.0 headers have already been converted to 0.1 headers
206 // by the PAX header parsing logic.
207 func (tr *Reader) readGNUSparsePAXHeaders(hdr *Header) (sparseDatas, error) {
208 // Identify the version of GNU headers.
210 major, minor := hdr.PAXRecords[paxGNUSparseMajor], hdr.PAXRecords[paxGNUSparseMinor]
212 case major == "0" && (minor == "0" || minor == "1"):
214 case major == "1" && minor == "0":
216 case major != "" || minor != "":
217 return nil, nil // Unknown GNU sparse PAX version
218 case hdr.PAXRecords[paxGNUSparseMap] != "":
219 is1x0 = false // 0.0 and 0.1 did not have explicit version records, so guess
221 return nil, nil // Not a PAX format GNU sparse file.
223 hdr.Format.mayOnlyBe(FormatPAX)
225 // Update hdr from GNU sparse PAX headers.
226 if name := hdr.PAXRecords[paxGNUSparseName]; name != "" {
229 size := hdr.PAXRecords[paxGNUSparseSize]
231 size = hdr.PAXRecords[paxGNUSparseRealSize]
234 n, err := strconv.ParseInt(size, 10, 64)
236 return nil, ErrHeader
241 // Read the sparse map according to the appropriate format.
243 return readGNUSparseMap1x0(tr.curr)
245 return readGNUSparseMap0x1(hdr.PAXRecords)
248 // mergePAX merges paxHdrs into hdr for all relevant fields of Header.
249 func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
250 for k, v := range paxHdrs {
252 continue // Keep the original USTAR value
265 id64, err = strconv.ParseInt(v, 10, 64)
266 hdr.Uid = int(id64) // Integer overflow possible
268 id64, err = strconv.ParseInt(v, 10, 64)
269 hdr.Gid = int(id64) // Integer overflow possible
271 hdr.AccessTime, err = parsePAXTime(v)
273 hdr.ModTime, err = parsePAXTime(v)
275 hdr.ChangeTime, err = parsePAXTime(v)
277 hdr.Size, err = strconv.ParseInt(v, 10, 64)
279 if strings.HasPrefix(k, paxSchilyXattr) {
280 if hdr.Xattrs == nil {
281 hdr.Xattrs = make(map[string]string)
283 hdr.Xattrs[k[len(paxSchilyXattr):]] = v
290 hdr.PAXRecords = paxHdrs
294 // parsePAX parses PAX headers.
295 // If an extended header (type 'x') is invalid, ErrHeader is returned
296 func parsePAX(r io.Reader) (map[string]string, error) {
297 buf, err := ioutil.ReadAll(r)
303 // For GNU PAX sparse format 0.0 support.
304 // This function transforms the sparse format 0.0 headers into format 0.1
305 // headers since 0.0 headers were not PAX compliant.
306 var sparseMap []string
308 paxHdrs := make(map[string]string)
310 key, value, residual, err := parsePAXRecord(sbuf)
312 return nil, ErrHeader
317 case paxGNUSparseOffset, paxGNUSparseNumBytes:
318 // Validate sparse header order and value.
319 if (len(sparseMap)%2 == 0 && key != paxGNUSparseOffset) ||
320 (len(sparseMap)%2 == 1 && key != paxGNUSparseNumBytes) ||
321 strings.Contains(value, ",") {
322 return nil, ErrHeader
324 sparseMap = append(sparseMap, value)
329 if len(sparseMap) > 0 {
330 paxHdrs[paxGNUSparseMap] = strings.Join(sparseMap, ",")
335 // readHeader reads the next block header and assumes that the underlying reader
336 // is already aligned to a block boundary. It returns the raw block of the
337 // header in case further processing is required.
339 // The err will be set to io.EOF only when one of the following occurs:
340 // * Exactly 0 bytes are read and EOF is hit.
341 // * Exactly 1 block of zeros is read and EOF is hit.
342 // * At least 2 blocks of zeros are read.
343 func (tr *Reader) readHeader() (*Header, *block, error) {
344 // Two blocks of zero bytes marks the end of the archive.
345 if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
346 return nil, nil, err // EOF is okay here; exactly 0 bytes read
348 if bytes.Equal(tr.blk[:], zeroBlock[:]) {
349 if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
350 return nil, nil, err // EOF is okay here; exactly 1 block of zeros read
352 if bytes.Equal(tr.blk[:], zeroBlock[:]) {
353 return nil, nil, io.EOF // normal EOF; exactly 2 block of zeros read
355 return nil, nil, ErrHeader // Zero block and then non-zero block
358 // Verify the header matches a known format.
359 format := tr.blk.GetFormat()
360 if format == FormatUnknown {
361 return nil, nil, ErrHeader
367 // Unpack the V7 header.
369 hdr.Typeflag = v7.TypeFlag()[0]
370 hdr.Name = p.parseString(v7.Name())
371 hdr.Linkname = p.parseString(v7.LinkName())
372 hdr.Size = p.parseNumeric(v7.Size())
373 hdr.Mode = p.parseNumeric(v7.Mode())
374 hdr.Uid = int(p.parseNumeric(v7.UID()))
375 hdr.Gid = int(p.parseNumeric(v7.GID()))
376 hdr.ModTime = time.Unix(p.parseNumeric(v7.ModTime()), 0)
378 // Unpack format specific fields.
379 if format > formatV7 {
380 ustar := tr.blk.USTAR()
381 hdr.Uname = p.parseString(ustar.UserName())
382 hdr.Gname = p.parseString(ustar.GroupName())
383 hdr.Devmajor = p.parseNumeric(ustar.DevMajor())
384 hdr.Devminor = p.parseNumeric(ustar.DevMinor())
388 case format.has(FormatUSTAR | FormatPAX):
390 ustar := tr.blk.USTAR()
391 prefix = p.parseString(ustar.Prefix())
393 // For Format detection, check if block is properly formatted since
394 // the parser is more liberal than what USTAR actually permits.
395 notASCII := func(r rune) bool { return r >= 0x80 }
396 if bytes.IndexFunc(tr.blk[:], notASCII) >= 0 {
397 hdr.Format = FormatUnknown // Non-ASCII characters in block.
399 nul := func(b []byte) bool { return int(b[len(b)-1]) == 0 }
400 if !(nul(v7.Size()) && nul(v7.Mode()) && nul(v7.UID()) && nul(v7.GID()) &&
401 nul(v7.ModTime()) && nul(ustar.DevMajor()) && nul(ustar.DevMinor())) {
402 hdr.Format = FormatUnknown // Numeric fields must end in NUL
404 case format.has(formatSTAR):
405 star := tr.blk.STAR()
406 prefix = p.parseString(star.Prefix())
407 hdr.AccessTime = time.Unix(p.parseNumeric(star.AccessTime()), 0)
408 hdr.ChangeTime = time.Unix(p.parseNumeric(star.ChangeTime()), 0)
409 case format.has(FormatGNU):
413 if b := gnu.AccessTime(); b[0] != 0 {
414 hdr.AccessTime = time.Unix(p2.parseNumeric(b), 0)
416 if b := gnu.ChangeTime(); b[0] != 0 {
417 hdr.ChangeTime = time.Unix(p2.parseNumeric(b), 0)
420 // Prior to Go1.8, the Writer had a bug where it would output
421 // an invalid tar file in certain rare situations because the logic
422 // incorrectly believed that the old GNU format had a prefix field.
423 // This is wrong and leads to an output file that mangles the
424 // atime and ctime fields, which are often left unused.
426 // In order to continue reading tar files created by former, buggy
427 // versions of Go, we skeptically parse the atime and ctime fields.
428 // If we are unable to parse them and the prefix field looks like
429 // an ASCII string, then we fallback on the pre-Go1.8 behavior
430 // of treating these fields as the USTAR prefix field.
432 // Note that this will not use the fallback logic for all possible
433 // files generated by a pre-Go1.8 toolchain. If the generated file
434 // happened to have a prefix field that parses as valid
435 // atime and ctime fields (e.g., when they are valid octal strings),
436 // then it is impossible to distinguish between an valid GNU file
437 // and an invalid pre-Go1.8 file.
439 // See https://golang.org/issues/12594
440 // See https://golang.org/issues/21005
442 hdr.AccessTime, hdr.ChangeTime = time.Time{}, time.Time{}
443 ustar := tr.blk.USTAR()
444 if s := p.parseString(ustar.Prefix()); isASCII(s) {
447 hdr.Format = FormatUnknown // Buggy file is not GNU
451 hdr.Name = prefix + "/" + hdr.Name
454 return hdr, &tr.blk, p.err
457 // readOldGNUSparseMap reads the sparse map from the old GNU sparse format.
458 // The sparse map is stored in the tar header if it's small enough.
459 // If it's larger than four entries, then one or more extension headers are used
460 // to store the rest of the sparse map.
462 // The Header.Size does not reflect the size of any extended headers used.
463 // Thus, this function will read from the raw io.Reader to fetch extra headers.
464 // This method mutates blk in the process.
465 func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, error) {
466 // Make sure that the input format is GNU.
467 // Unfortunately, the STAR format also has a sparse header format that uses
468 // the same type flag but has a completely different layout.
469 if blk.GetFormat() != FormatGNU {
470 return nil, ErrHeader
472 hdr.Format.mayOnlyBe(FormatGNU)
475 hdr.Size = p.parseNumeric(blk.GNU().RealSize())
479 s := blk.GNU().Sparse()
480 spd := make(sparseDatas, 0, s.MaxEntries())
482 for i := 0; i < s.MaxEntries(); i++ {
483 // This termination condition is identical to GNU and BSD tar.
484 if s.Entry(i).Offset()[0] == 0x00 {
485 break // Don't return, need to process extended headers (even if empty)
487 offset := p.parseNumeric(s.Entry(i).Offset())
488 length := p.parseNumeric(s.Entry(i).Length())
492 spd = append(spd, sparseEntry{Offset: offset, Length: length})
495 if s.IsExtended()[0] > 0 {
496 // There are more entries. Read an extension header and parse its entries.
497 if _, err := mustReadFull(tr.r, blk[:]); err != nil {
503 return spd, nil // Done
507 // readGNUSparseMap1x0 reads the sparse map as stored in GNU's PAX sparse format
508 // version 1.0. The format of the sparse map consists of a series of
509 // newline-terminated numeric fields. The first field is the number of entries
510 // and is always present. Following this are the entries, consisting of two
511 // fields (offset, length). This function must stop reading at the end
512 // boundary of the block containing the last newline.
514 // Note that the GNU manual says that numeric values should be encoded in octal
515 // format. However, the GNU tar utility itself outputs these values in decimal.
516 // As such, this library treats values as being encoded in decimal.
517 func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
524 // feedTokens copies data in blocks from r into buf until there are
525 // at least cnt newlines in buf. It will not read more blocks than needed.
526 feedTokens := func(n int64) error {
528 if _, err := mustReadFull(r, blk[:]); err != nil {
532 for _, c := range blk {
541 // nextToken gets the next token delimited by a newline. This assumes that
542 // at least one newline exists in the buffer.
543 nextToken := func() string {
545 tok, _ := buf.ReadString('\n')
546 return strings.TrimRight(tok, "\n")
549 // Parse for the number of entries.
550 // Use integer overflow resistant math to check this.
551 if err := feedTokens(1); err != nil {
554 numEntries, err := strconv.ParseInt(nextToken(), 10, 0) // Intentionally parse as native int
555 if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
556 return nil, ErrHeader
559 // Parse for all member entries.
560 // numEntries is trusted after this since a potential attacker must have
561 // committed resources proportional to what this library used.
562 if err := feedTokens(2 * numEntries); err != nil {
565 spd := make(sparseDatas, 0, numEntries)
566 for i := int64(0); i < numEntries; i++ {
567 offset, err1 := strconv.ParseInt(nextToken(), 10, 64)
568 length, err2 := strconv.ParseInt(nextToken(), 10, 64)
569 if err1 != nil || err2 != nil {
570 return nil, ErrHeader
572 spd = append(spd, sparseEntry{Offset: offset, Length: length})
577 // readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format
578 // version 0.1. The sparse map is stored in the PAX headers.
579 func readGNUSparseMap0x1(paxHdrs map[string]string) (sparseDatas, error) {
580 // Get number of entries.
581 // Use integer overflow resistant math to check this.
582 numEntriesStr := paxHdrs[paxGNUSparseNumBlocks]
583 numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0) // Intentionally parse as native int
584 if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
585 return nil, ErrHeader
588 // There should be two numbers in sparseMap for each entry.
589 sparseMap := strings.Split(paxHdrs[paxGNUSparseMap], ",")
590 if len(sparseMap) == 1 && sparseMap[0] == "" {
591 sparseMap = sparseMap[:0]
593 if int64(len(sparseMap)) != 2*numEntries {
594 return nil, ErrHeader
597 // Loop through the entries in the sparse map.
598 // numEntries is trusted now.
599 spd := make(sparseDatas, 0, numEntries)
600 for len(sparseMap) >= 2 {
601 offset, err1 := strconv.ParseInt(sparseMap[0], 10, 64)
602 length, err2 := strconv.ParseInt(sparseMap[1], 10, 64)
603 if err1 != nil || err2 != nil {
604 return nil, ErrHeader
606 spd = append(spd, sparseEntry{Offset: offset, Length: length})
607 sparseMap = sparseMap[2:]
612 // Read reads from the current file in the tar archive.
613 // It returns (0, io.EOF) when it reaches the end of that file,
614 // until Next is called to advance to the next file.
616 // If the current file is sparse, then the regions marked as a hole
617 // are read back as NUL-bytes.
619 // Calling Read on special types like TypeLink, TypeSymlink, TypeChar,
620 // TypeBlock, TypeDir, and TypeFifo returns (0, io.EOF) regardless of what
621 // the Header.Size claims.
622 func (tr *Reader) Read(b []byte) (int, error) {
626 n, err := tr.curr.Read(b)
627 if err != nil && err != io.EOF {
633 // writeTo writes the content of the current file to w.
634 // The bytes written matches the number of remaining bytes in the current file.
636 // If the current file is sparse and w is an io.WriteSeeker,
637 // then writeTo uses Seek to skip past holes defined in Header.SparseHoles,
638 // assuming that skipped regions are filled with NULs.
639 // This always writes the last byte to ensure w is the right size.
641 // TODO(dsnet): Re-export this when adding sparse file support.
642 // See https://golang.org/issue/22735
643 func (tr *Reader) writeTo(w io.Writer) (int64, error) {
647 n, err := tr.curr.WriteTo(w)
654 // regFileReader is a fileReader for reading data from a regular file entry.
655 type regFileReader struct {
656 r io.Reader // Underlying Reader
657 nb int64 // Number of remaining bytes to read
660 func (fr *regFileReader) Read(b []byte) (n int, err error) {
661 if int64(len(b)) > fr.nb {
665 n, err = fr.r.Read(b)
669 case err == io.EOF && fr.nb > 0:
670 return n, io.ErrUnexpectedEOF
671 case err == nil && fr.nb == 0:
678 func (fr *regFileReader) WriteTo(w io.Writer) (int64, error) {
679 return io.Copy(w, struct{ io.Reader }{fr})
682 func (fr regFileReader) LogicalRemaining() int64 {
686 func (fr regFileReader) PhysicalRemaining() int64 {
690 // sparseFileReader is a fileReader for reading data from a sparse file entry.
691 type sparseFileReader struct {
692 fr fileReader // Underlying fileReader
693 sp sparseHoles // Normalized list of sparse holes
694 pos int64 // Current position in sparse file
697 func (sr *sparseFileReader) Read(b []byte) (n int, err error) {
698 finished := int64(len(b)) >= sr.LogicalRemaining()
700 b = b[:sr.LogicalRemaining()]
704 endPos := sr.pos + int64(len(b))
705 for endPos > sr.pos && err == nil {
706 var nf int // Bytes read in fragment
707 holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
708 if sr.pos < holeStart { // In a data fragment
709 bf := b[:min(int64(len(b)), holeStart-sr.pos)]
710 nf, err = tryReadFull(sr.fr, bf)
711 } else { // In a hole fragment
712 bf := b[:min(int64(len(b)), holeEnd-sr.pos)]
713 nf, err = tryReadFull(zeroReader{}, bf)
717 if sr.pos >= holeEnd && len(sr.sp) > 1 {
718 sr.sp = sr.sp[1:] // Ensure last fragment always remains
725 return n, errMissData // Less data in dense file than sparse file
728 case sr.LogicalRemaining() == 0 && sr.PhysicalRemaining() > 0:
729 return n, errUnrefData // More data in dense file than sparse file
737 func (sr *sparseFileReader) WriteTo(w io.Writer) (n int64, err error) {
738 ws, ok := w.(io.WriteSeeker)
740 if _, err := ws.Seek(0, io.SeekCurrent); err != nil {
741 ok = false // Not all io.Seeker can really seek
745 return io.Copy(w, struct{ io.Reader }{sr})
748 var writeLastByte bool
750 for sr.LogicalRemaining() > 0 && !writeLastByte && err == nil {
751 var nf int64 // Size of fragment
752 holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
753 if sr.pos < holeStart { // In a data fragment
754 nf = holeStart - sr.pos
755 nf, err = io.CopyN(ws, sr.fr, nf)
756 } else { // In a hole fragment
757 nf = holeEnd - sr.pos
758 if sr.PhysicalRemaining() == 0 {
762 _, err = ws.Seek(nf, io.SeekCurrent)
765 if sr.pos >= holeEnd && len(sr.sp) > 1 {
766 sr.sp = sr.sp[1:] // Ensure last fragment always remains
770 // If the last fragment is a hole, then seek to 1-byte before EOF, and
771 // write a single byte to ensure the file is the right size.
772 if writeLastByte && err == nil {
773 _, err = ws.Write([]byte{0})
780 return n, errMissData // Less data in dense file than sparse file
783 case sr.LogicalRemaining() == 0 && sr.PhysicalRemaining() > 0:
784 return n, errUnrefData // More data in dense file than sparse file
790 func (sr sparseFileReader) LogicalRemaining() int64 {
791 return sr.sp[len(sr.sp)-1].endOffset() - sr.pos
793 func (sr sparseFileReader) PhysicalRemaining() int64 {
794 return sr.fr.PhysicalRemaining()
797 type zeroReader struct{}
799 func (zeroReader) Read(b []byte) (int, error) {
806 // mustReadFull is like io.ReadFull except it returns
807 // io.ErrUnexpectedEOF when io.EOF is hit before len(b) bytes are read.
808 func mustReadFull(r io.Reader, b []byte) (int, error) {
809 n, err := tryReadFull(r, b)
811 err = io.ErrUnexpectedEOF
816 // tryReadFull is like io.ReadFull except it returns
817 // io.EOF when it is hit before len(b) bytes are read.
818 func tryReadFull(r io.Reader, b []byte) (n int, err error) {
819 for len(b) > n && err == nil {
821 nn, err = r.Read(b[n:])
824 if len(b) == n && err == io.EOF {
830 // discard skips n bytes in r, reporting an error if unable to do so.
831 func discard(r io.Reader, n int64) error {
832 // If possible, Seek to the last byte before the end of the data section.
833 // Do this because Seek is often lazy about reporting errors; this will mask
834 // the fact that the stream may be truncated. We can rely on the
835 // io.CopyN done shortly afterwards to trigger any IO errors.
836 var seekSkipped int64 // Number of bytes skipped via Seek
837 if sr, ok := r.(io.Seeker); ok && n > 1 {
838 // Not all io.Seeker can actually Seek. For example, os.Stdin implements
839 // io.Seeker, but calling Seek always returns an error and performs
840 // no action. Thus, we try an innocent seek to the current position
841 // to see if Seek is really supported.
842 pos1, err := sr.Seek(0, io.SeekCurrent)
843 if pos1 >= 0 && err == nil {
844 // Seek seems supported, so perform the real Seek.
845 pos2, err := sr.Seek(n-1, io.SeekCurrent)
846 if pos2 < 0 || err != nil {
849 seekSkipped = pos2 - pos1
853 copySkipped, err := io.CopyN(ioutil.Discard, r, n-seekSkipped)
854 if err == io.EOF && seekSkipped+copySkipped < n {
855 err = io.ErrUnexpectedEOF