...

Source file src/archive/tar/reader.go

Documentation: archive/tar

     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.
     4  
     5  package tar
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"path/filepath"
    11  	"strconv"
    12  	"strings"
    13  	"time"
    14  )
    15  
    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.
    19  type Reader struct {
    20  	r    io.Reader
    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
    24  
    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.
    28  	err error
    29  }
    30  
    31  type fileReader interface {
    32  	io.Reader
    33  	fileState
    34  
    35  	WriteTo(io.Writer) (int64, error)
    36  }
    37  
    38  // NewReader creates a new [Reader] reading from r.
    39  func NewReader(r io.Reader) *Reader {
    40  	return &Reader{r: r, curr: &regFileReader{r, 0}}
    41  }
    42  
    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.
    46  // At the end of the archive, Next returns the error io.EOF.
    47  //
    48  // If Next encounters a non-local file name (as defined by [filepath.IsLocal])
    49  // and the GODEBUG environment variable contains `tarinsecurepath=0`,
    50  // Only file names are validated, not link targets.
    51  // Next returns the header with an [ErrInsecurePath] error.
    52  // A future version of Go may introduce this behavior by default.
    53  // Programs that want to accept non-local names can ignore
    54  // the [ErrInsecurePath] error and use the returned header.
    55  func (tr *Reader) Next() (*Header, error) {
    56  	if tr.err != nil {
    57  		return nil, tr.err
    58  	}
    59  	hdr, err := tr.next()
    60  	tr.err = err
    61  	if err == nil && !filepath.IsLocal(hdr.Name) {
    62  		if tarinsecurepath.Value() == "0" {
    63  			tarinsecurepath.IncNonDefault()
    64  			err = ErrInsecurePath
    65  		}
    66  	}
    67  	return hdr, err
    68  }
    69  
    70  func (tr *Reader) next() (*Header, error) {
    71  	var paxHdrs map[string]string
    72  	var gnuLongName, gnuLongLink string
    73  
    74  	// Externally, Next iterates through the tar archive as if it is a series of
    75  	// files. Internally, the tar format often uses fake "files" to add meta
    76  	// data that describes the next file. These meta data "files" should not
    77  	// normally be visible to the outside. As such, this loop iterates through
    78  	// one or more "header files" until it finds a "normal file".
    79  	format := FormatUSTAR | FormatPAX | FormatGNU
    80  	for {
    81  		// Discard the remainder of the file and any padding.
    82  		if err := discard(tr.r, tr.curr.physicalRemaining()); err != nil {
    83  			return nil, err
    84  		}
    85  		if _, err := tryReadFull(tr.r, tr.blk[:tr.pad]); err != nil {
    86  			return nil, err
    87  		}
    88  		tr.pad = 0
    89  
    90  		hdr, rawHdr, err := tr.readHeader()
    91  		if err != nil {
    92  			return nil, err
    93  		}
    94  		if err := tr.handleRegularFile(hdr); err != nil {
    95  			return nil, err
    96  		}
    97  		format.mayOnlyBe(hdr.Format)
    98  
    99  		// Check for PAX/GNU special headers and files.
   100  		switch hdr.Typeflag {
   101  		case TypeXHeader, TypeXGlobalHeader:
   102  			format.mayOnlyBe(FormatPAX)
   103  			paxHdrs, err = parsePAX(tr)
   104  			if err != nil {
   105  				return nil, err
   106  			}
   107  			if hdr.Typeflag == TypeXGlobalHeader {
   108  				mergePAX(hdr, paxHdrs)
   109  				return &Header{
   110  					Name:       hdr.Name,
   111  					Typeflag:   hdr.Typeflag,
   112  					Xattrs:     hdr.Xattrs,
   113  					PAXRecords: hdr.PAXRecords,
   114  					Format:     format,
   115  				}, nil
   116  			}
   117  			continue // This is a meta header affecting the next header
   118  		case TypeGNULongName, TypeGNULongLink:
   119  			format.mayOnlyBe(FormatGNU)
   120  			realname, err := readSpecialFile(tr)
   121  			if err != nil {
   122  				return nil, err
   123  			}
   124  
   125  			var p parser
   126  			switch hdr.Typeflag {
   127  			case TypeGNULongName:
   128  				gnuLongName = p.parseString(realname)
   129  			case TypeGNULongLink:
   130  				gnuLongLink = p.parseString(realname)
   131  			}
   132  			continue // This is a meta header affecting the next header
   133  		default:
   134  			// The old GNU sparse format is handled here since it is technically
   135  			// just a regular file with additional attributes.
   136  
   137  			if err := mergePAX(hdr, paxHdrs); err != nil {
   138  				return nil, err
   139  			}
   140  			if gnuLongName != "" {
   141  				hdr.Name = gnuLongName
   142  			}
   143  			if gnuLongLink != "" {
   144  				hdr.Linkname = gnuLongLink
   145  			}
   146  			if hdr.Typeflag == TypeRegA {
   147  				if strings.HasSuffix(hdr.Name, "/") {
   148  					hdr.Typeflag = TypeDir // Legacy archives use trailing slash for directories
   149  				} else {
   150  					hdr.Typeflag = TypeReg
   151  				}
   152  			}
   153  
   154  			// The extended headers may have updated the size.
   155  			// Thus, setup the regFileReader again after merging PAX headers.
   156  			if err := tr.handleRegularFile(hdr); err != nil {
   157  				return nil, err
   158  			}
   159  
   160  			// Sparse formats rely on being able to read from the logical data
   161  			// section; there must be a preceding call to handleRegularFile.
   162  			if err := tr.handleSparseFile(hdr, rawHdr); err != nil {
   163  				return nil, err
   164  			}
   165  
   166  			// Set the final guess at the format.
   167  			if format.has(FormatUSTAR) && format.has(FormatPAX) {
   168  				format.mayOnlyBe(FormatUSTAR)
   169  			}
   170  			hdr.Format = format
   171  			return hdr, nil // This is a file, so stop
   172  		}
   173  	}
   174  }
   175  
   176  // handleRegularFile sets up the current file reader and padding such that it
   177  // can only read the following logical data section. It will properly handle
   178  // special headers that contain no data section.
   179  func (tr *Reader) handleRegularFile(hdr *Header) error {
   180  	nb := hdr.Size
   181  	if isHeaderOnlyType(hdr.Typeflag) {
   182  		nb = 0
   183  	}
   184  	if nb < 0 {
   185  		return ErrHeader
   186  	}
   187  
   188  	tr.pad = blockPadding(nb)
   189  	tr.curr = &regFileReader{r: tr.r, nb: nb}
   190  	return nil
   191  }
   192  
   193  // handleSparseFile checks if the current file is a sparse format of any type
   194  // and sets the curr reader appropriately.
   195  func (tr *Reader) handleSparseFile(hdr *Header, rawHdr *block) error {
   196  	var spd sparseDatas
   197  	var err error
   198  	if hdr.Typeflag == TypeGNUSparse {
   199  		spd, err = tr.readOldGNUSparseMap(hdr, rawHdr)
   200  	} else {
   201  		spd, err = tr.readGNUSparsePAXHeaders(hdr)
   202  	}
   203  
   204  	// If sp is non-nil, then this is a sparse file.
   205  	// Note that it is possible for len(sp) == 0.
   206  	if err == nil && spd != nil {
   207  		if isHeaderOnlyType(hdr.Typeflag) || !validateSparseEntries(spd, hdr.Size) {
   208  			return ErrHeader
   209  		}
   210  		sph := invertSparseEntries(spd, hdr.Size)
   211  		tr.curr = &sparseFileReader{tr.curr, sph, 0}
   212  	}
   213  	return err
   214  }
   215  
   216  // readGNUSparsePAXHeaders checks the PAX headers for GNU sparse headers.
   217  // If they are found, then this function reads the sparse map and returns it.
   218  // This assumes that 0.0 headers have already been converted to 0.1 headers
   219  // by the PAX header parsing logic.
   220  func (tr *Reader) readGNUSparsePAXHeaders(hdr *Header) (sparseDatas, error) {
   221  	// Identify the version of GNU headers.
   222  	var is1x0 bool
   223  	major, minor := hdr.PAXRecords[paxGNUSparseMajor], hdr.PAXRecords[paxGNUSparseMinor]
   224  	switch {
   225  	case major == "0" && (minor == "0" || minor == "1"):
   226  		is1x0 = false
   227  	case major == "1" && minor == "0":
   228  		is1x0 = true
   229  	case major != "" || minor != "":
   230  		return nil, nil // Unknown GNU sparse PAX version
   231  	case hdr.PAXRecords[paxGNUSparseMap] != "":
   232  		is1x0 = false // 0.0 and 0.1 did not have explicit version records, so guess
   233  	default:
   234  		return nil, nil // Not a PAX format GNU sparse file.
   235  	}
   236  	hdr.Format.mayOnlyBe(FormatPAX)
   237  
   238  	// Update hdr from GNU sparse PAX headers.
   239  	if name := hdr.PAXRecords[paxGNUSparseName]; name != "" {
   240  		hdr.Name = name
   241  	}
   242  	size := hdr.PAXRecords[paxGNUSparseSize]
   243  	if size == "" {
   244  		size = hdr.PAXRecords[paxGNUSparseRealSize]
   245  	}
   246  	if size != "" {
   247  		n, err := strconv.ParseInt(size, 10, 64)
   248  		if err != nil {
   249  			return nil, ErrHeader
   250  		}
   251  		hdr.Size = n
   252  	}
   253  
   254  	// Read the sparse map according to the appropriate format.
   255  	if is1x0 {
   256  		return readGNUSparseMap1x0(tr.curr)
   257  	}
   258  	return readGNUSparseMap0x1(hdr.PAXRecords)
   259  }
   260  
   261  // mergePAX merges paxHdrs into hdr for all relevant fields of Header.
   262  func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {
   263  	for k, v := range paxHdrs {
   264  		if v == "" {
   265  			continue // Keep the original USTAR value
   266  		}
   267  		var id64 int64
   268  		switch k {
   269  		case paxPath:
   270  			hdr.Name = v
   271  		case paxLinkpath:
   272  			hdr.Linkname = v
   273  		case paxUname:
   274  			hdr.Uname = v
   275  		case paxGname:
   276  			hdr.Gname = v
   277  		case paxUid:
   278  			id64, err = strconv.ParseInt(v, 10, 64)
   279  			hdr.Uid = int(id64) // Integer overflow possible
   280  		case paxGid:
   281  			id64, err = strconv.ParseInt(v, 10, 64)
   282  			hdr.Gid = int(id64) // Integer overflow possible
   283  		case paxAtime:
   284  			hdr.AccessTime, err = parsePAXTime(v)
   285  		case paxMtime:
   286  			hdr.ModTime, err = parsePAXTime(v)
   287  		case paxCtime:
   288  			hdr.ChangeTime, err = parsePAXTime(v)
   289  		case paxSize:
   290  			hdr.Size, err = strconv.ParseInt(v, 10, 64)
   291  		default:
   292  			if strings.HasPrefix(k, paxSchilyXattr) {
   293  				if hdr.Xattrs == nil {
   294  					hdr.Xattrs = make(map[string]string)
   295  				}
   296  				hdr.Xattrs[k[len(paxSchilyXattr):]] = v
   297  			}
   298  		}
   299  		if err != nil {
   300  			return ErrHeader
   301  		}
   302  	}
   303  	hdr.PAXRecords = paxHdrs
   304  	return nil
   305  }
   306  
   307  // parsePAX parses PAX headers.
   308  // If an extended header (type 'x') is invalid, ErrHeader is returned.
   309  func parsePAX(r io.Reader) (map[string]string, error) {
   310  	buf, err := readSpecialFile(r)
   311  	if err != nil {
   312  		return nil, err
   313  	}
   314  	sbuf := string(buf)
   315  
   316  	// For GNU PAX sparse format 0.0 support.
   317  	// This function transforms the sparse format 0.0 headers into format 0.1
   318  	// headers since 0.0 headers were not PAX compliant.
   319  	var sparseMap []string
   320  
   321  	paxHdrs := make(map[string]string)
   322  	for len(sbuf) > 0 {
   323  		key, value, residual, err := parsePAXRecord(sbuf)
   324  		if err != nil {
   325  			return nil, ErrHeader
   326  		}
   327  		sbuf = residual
   328  
   329  		switch key {
   330  		case paxGNUSparseOffset, paxGNUSparseNumBytes:
   331  			// Validate sparse header order and value.
   332  			if (len(sparseMap)%2 == 0 && key != paxGNUSparseOffset) ||
   333  				(len(sparseMap)%2 == 1 && key != paxGNUSparseNumBytes) ||
   334  				strings.Contains(value, ",") {
   335  				return nil, ErrHeader
   336  			}
   337  			sparseMap = append(sparseMap, value)
   338  		default:
   339  			paxHdrs[key] = value
   340  		}
   341  	}
   342  	if len(sparseMap) > 0 {
   343  		paxHdrs[paxGNUSparseMap] = strings.Join(sparseMap, ",")
   344  	}
   345  	return paxHdrs, nil
   346  }
   347  
   348  // readHeader reads the next block header and assumes that the underlying reader
   349  // is already aligned to a block boundary. It returns the raw block of the
   350  // header in case further processing is required.
   351  //
   352  // The err will be set to io.EOF only when one of the following occurs:
   353  //   - Exactly 0 bytes are read and EOF is hit.
   354  //   - Exactly 1 block of zeros is read and EOF is hit.
   355  //   - At least 2 blocks of zeros are read.
   356  func (tr *Reader) readHeader() (*Header, *block, error) {
   357  	// Two blocks of zero bytes marks the end of the archive.
   358  	if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
   359  		return nil, nil, err // EOF is okay here; exactly 0 bytes read
   360  	}
   361  	if bytes.Equal(tr.blk[:], zeroBlock[:]) {
   362  		if _, err := io.ReadFull(tr.r, tr.blk[:]); err != nil {
   363  			return nil, nil, err // EOF is okay here; exactly 1 block of zeros read
   364  		}
   365  		if bytes.Equal(tr.blk[:], zeroBlock[:]) {
   366  			return nil, nil, io.EOF // normal EOF; exactly 2 block of zeros read
   367  		}
   368  		return nil, nil, ErrHeader // Zero block and then non-zero block
   369  	}
   370  
   371  	// Verify the header matches a known format.
   372  	format := tr.blk.getFormat()
   373  	if format == FormatUnknown {
   374  		return nil, nil, ErrHeader
   375  	}
   376  
   377  	var p parser
   378  	hdr := new(Header)
   379  
   380  	// Unpack the V7 header.
   381  	v7 := tr.blk.toV7()
   382  	hdr.Typeflag = v7.typeFlag()[0]
   383  	hdr.Name = p.parseString(v7.name())
   384  	hdr.Linkname = p.parseString(v7.linkName())
   385  	hdr.Size = p.parseNumeric(v7.size())
   386  	hdr.Mode = p.parseNumeric(v7.mode())
   387  	hdr.Uid = int(p.parseNumeric(v7.uid()))
   388  	hdr.Gid = int(p.parseNumeric(v7.gid()))
   389  	hdr.ModTime = time.Unix(p.parseNumeric(v7.modTime()), 0)
   390  
   391  	// Unpack format specific fields.
   392  	if format > formatV7 {
   393  		ustar := tr.blk.toUSTAR()
   394  		hdr.Uname = p.parseString(ustar.userName())
   395  		hdr.Gname = p.parseString(ustar.groupName())
   396  		hdr.Devmajor = p.parseNumeric(ustar.devMajor())
   397  		hdr.Devminor = p.parseNumeric(ustar.devMinor())
   398  
   399  		var prefix string
   400  		switch {
   401  		case format.has(FormatUSTAR | FormatPAX):
   402  			hdr.Format = format
   403  			ustar := tr.blk.toUSTAR()
   404  			prefix = p.parseString(ustar.prefix())
   405  
   406  			// For Format detection, check if block is properly formatted since
   407  			// the parser is more liberal than what USTAR actually permits.
   408  			notASCII := func(r rune) bool { return r >= 0x80 }
   409  			if bytes.IndexFunc(tr.blk[:], notASCII) >= 0 {
   410  				hdr.Format = FormatUnknown // Non-ASCII characters in block.
   411  			}
   412  			nul := func(b []byte) bool { return int(b[len(b)-1]) == 0 }
   413  			if !(nul(v7.size()) && nul(v7.mode()) && nul(v7.uid()) && nul(v7.gid()) &&
   414  				nul(v7.modTime()) && nul(ustar.devMajor()) && nul(ustar.devMinor())) {
   415  				hdr.Format = FormatUnknown // Numeric fields must end in NUL
   416  			}
   417  		case format.has(formatSTAR):
   418  			star := tr.blk.toSTAR()
   419  			prefix = p.parseString(star.prefix())
   420  			hdr.AccessTime = time.Unix(p.parseNumeric(star.accessTime()), 0)
   421  			hdr.ChangeTime = time.Unix(p.parseNumeric(star.changeTime()), 0)
   422  		case format.has(FormatGNU):
   423  			hdr.Format = format
   424  			var p2 parser
   425  			gnu := tr.blk.toGNU()
   426  			if b := gnu.accessTime(); b[0] != 0 {
   427  				hdr.AccessTime = time.Unix(p2.parseNumeric(b), 0)
   428  			}
   429  			if b := gnu.changeTime(); b[0] != 0 {
   430  				hdr.ChangeTime = time.Unix(p2.parseNumeric(b), 0)
   431  			}
   432  
   433  			// Prior to Go1.8, the Writer had a bug where it would output
   434  			// an invalid tar file in certain rare situations because the logic
   435  			// incorrectly believed that the old GNU format had a prefix field.
   436  			// This is wrong and leads to an output file that mangles the
   437  			// atime and ctime fields, which are often left unused.
   438  			//
   439  			// In order to continue reading tar files created by former, buggy
   440  			// versions of Go, we skeptically parse the atime and ctime fields.
   441  			// If we are unable to parse them and the prefix field looks like
   442  			// an ASCII string, then we fallback on the pre-Go1.8 behavior
   443  			// of treating these fields as the USTAR prefix field.
   444  			//
   445  			// Note that this will not use the fallback logic for all possible
   446  			// files generated by a pre-Go1.8 toolchain. If the generated file
   447  			// happened to have a prefix field that parses as valid
   448  			// atime and ctime fields (e.g., when they are valid octal strings),
   449  			// then it is impossible to distinguish between a valid GNU file
   450  			// and an invalid pre-Go1.8 file.
   451  			//
   452  			// See https://golang.org/issues/12594
   453  			// See https://golang.org/issues/21005
   454  			if p2.err != nil {
   455  				hdr.AccessTime, hdr.ChangeTime = time.Time{}, time.Time{}
   456  				ustar := tr.blk.toUSTAR()
   457  				if s := p.parseString(ustar.prefix()); isASCII(s) {
   458  					prefix = s
   459  				}
   460  				hdr.Format = FormatUnknown // Buggy file is not GNU
   461  			}
   462  		}
   463  		if len(prefix) > 0 {
   464  			hdr.Name = prefix + "/" + hdr.Name
   465  		}
   466  	}
   467  	return hdr, &tr.blk, p.err
   468  }
   469  
   470  // readOldGNUSparseMap reads the sparse map from the old GNU sparse format.
   471  // The sparse map is stored in the tar header if it's small enough.
   472  // If it's larger than four entries, then one or more extension headers are used
   473  // to store the rest of the sparse map.
   474  //
   475  // The Header.Size does not reflect the size of any extended headers used.
   476  // Thus, this function will read from the raw io.Reader to fetch extra headers.
   477  // This method mutates blk in the process.
   478  func (tr *Reader) readOldGNUSparseMap(hdr *Header, blk *block) (sparseDatas, error) {
   479  	// Make sure that the input format is GNU.
   480  	// Unfortunately, the STAR format also has a sparse header format that uses
   481  	// the same type flag but has a completely different layout.
   482  	if blk.getFormat() != FormatGNU {
   483  		return nil, ErrHeader
   484  	}
   485  	hdr.Format.mayOnlyBe(FormatGNU)
   486  
   487  	var p parser
   488  	hdr.Size = p.parseNumeric(blk.toGNU().realSize())
   489  	if p.err != nil {
   490  		return nil, p.err
   491  	}
   492  	s := blk.toGNU().sparse()
   493  	spd := make(sparseDatas, 0, s.maxEntries())
   494  	totalSize := len(s)
   495  	for totalSize < maxSpecialFileSize {
   496  		for i := 0; i < s.maxEntries(); i++ {
   497  			// This termination condition is identical to GNU and BSD tar.
   498  			if s.entry(i).offset()[0] == 0x00 {
   499  				break // Don't return, need to process extended headers (even if empty)
   500  			}
   501  			offset := p.parseNumeric(s.entry(i).offset())
   502  			length := p.parseNumeric(s.entry(i).length())
   503  			if p.err != nil {
   504  				return nil, p.err
   505  			}
   506  			var err error
   507  			spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
   508  			if err != nil {
   509  				return nil, err
   510  			}
   511  		}
   512  
   513  		if s.isExtended()[0] > 0 {
   514  			// There are more entries. Read an extension header and parse its entries.
   515  			if _, err := mustReadFull(tr.r, blk[:]); err != nil {
   516  				return nil, err
   517  			}
   518  			s = blk.toSparse()
   519  			totalSize += len(s)
   520  			continue
   521  		}
   522  		return spd, nil // Done
   523  	}
   524  	return nil, errSparseTooLong
   525  }
   526  
   527  // readGNUSparseMap1x0 reads the sparse map as stored in GNU's PAX sparse format
   528  // version 1.0. The format of the sparse map consists of a series of
   529  // newline-terminated numeric fields. The first field is the number of entries
   530  // and is always present. Following this are the entries, consisting of two
   531  // fields (offset, length). This function must stop reading at the end
   532  // boundary of the block containing the last newline.
   533  //
   534  // Note that the GNU manual says that numeric values should be encoded in octal
   535  // format. However, the GNU tar utility itself outputs these values in decimal.
   536  // As such, this library treats values as being encoded in decimal.
   537  func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {
   538  	var (
   539  		cntNewline int64
   540  		buf        bytes.Buffer
   541  		blk        block
   542  		totalSize  int
   543  	)
   544  
   545  	// feedTokens copies data in blocks from r into buf until there are
   546  	// at least cnt newlines in buf. It will not read more blocks than needed.
   547  	feedTokens := func(n int64) error {
   548  		for cntNewline < n {
   549  			totalSize += len(blk)
   550  			if totalSize > maxSpecialFileSize {
   551  				return errSparseTooLong
   552  			}
   553  			if _, err := mustReadFull(r, blk[:]); err != nil {
   554  				return err
   555  			}
   556  			buf.Write(blk[:])
   557  			for _, c := range blk {
   558  				if c == '\n' {
   559  					cntNewline++
   560  				}
   561  			}
   562  		}
   563  		return nil
   564  	}
   565  
   566  	// nextToken gets the next token delimited by a newline. This assumes that
   567  	// at least one newline exists in the buffer.
   568  	nextToken := func() string {
   569  		cntNewline--
   570  		tok, _ := buf.ReadString('\n')
   571  		return strings.TrimRight(tok, "\n")
   572  	}
   573  
   574  	// Parse for the number of entries.
   575  	// Use integer overflow resistant math to check this.
   576  	if err := feedTokens(1); err != nil {
   577  		return nil, err
   578  	}
   579  	numEntries, err := strconv.ParseInt(nextToken(), 10, 0) // Intentionally parse as native int
   580  	if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
   581  		return nil, ErrHeader
   582  	}
   583  
   584  	// Parse for all member entries.
   585  	// numEntries is trusted after this since feedTokens limits the number of
   586  	// tokens based on maxSpecialFileSize.
   587  	if err := feedTokens(2 * numEntries); err != nil {
   588  		return nil, err
   589  	}
   590  	spd := make(sparseDatas, 0, numEntries)
   591  	for i := int64(0); i < numEntries; i++ {
   592  		offset, err1 := strconv.ParseInt(nextToken(), 10, 64)
   593  		length, err2 := strconv.ParseInt(nextToken(), 10, 64)
   594  		if err1 != nil || err2 != nil {
   595  			return nil, ErrHeader
   596  		}
   597  		spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
   598  		if err != nil {
   599  			return nil, err
   600  		}
   601  	}
   602  	return spd, nil
   603  }
   604  
   605  // readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format
   606  // version 0.1. The sparse map is stored in the PAX headers.
   607  func readGNUSparseMap0x1(paxHdrs map[string]string) (sparseDatas, error) {
   608  	// Get number of entries.
   609  	// Use integer overflow resistant math to check this.
   610  	numEntriesStr := paxHdrs[paxGNUSparseNumBlocks]
   611  	numEntries, err := strconv.ParseInt(numEntriesStr, 10, 0) // Intentionally parse as native int
   612  	if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {
   613  		return nil, ErrHeader
   614  	}
   615  
   616  	// There should be two numbers in sparseMap for each entry.
   617  	sparseMap := strings.Split(paxHdrs[paxGNUSparseMap], ",")
   618  	if len(sparseMap) == 1 && sparseMap[0] == "" {
   619  		sparseMap = sparseMap[:0]
   620  	}
   621  	if int64(len(sparseMap)) != 2*numEntries {
   622  		return nil, ErrHeader
   623  	}
   624  
   625  	// Loop through the entries in the sparse map.
   626  	// numEntries is trusted now.
   627  	spd := make(sparseDatas, 0, numEntries)
   628  	for len(sparseMap) >= 2 {
   629  		offset, err1 := strconv.ParseInt(sparseMap[0], 10, 64)
   630  		length, err2 := strconv.ParseInt(sparseMap[1], 10, 64)
   631  		if err1 != nil || err2 != nil {
   632  			return nil, ErrHeader
   633  		}
   634  		spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})
   635  		if err != nil {
   636  			return nil, err
   637  		}
   638  		sparseMap = sparseMap[2:]
   639  	}
   640  	return spd, nil
   641  }
   642  
   643  func appendSparseEntry(spd sparseDatas, ent sparseEntry) (sparseDatas, error) {
   644  	if len(spd) >= maxSparseFileEntries {
   645  		return nil, errSparseTooLong
   646  	}
   647  	return append(spd, ent), nil
   648  }
   649  
   650  // Read reads from the current file in the tar archive.
   651  // It returns (0, io.EOF) when it reaches the end of that file,
   652  // until [Next] is called to advance to the next file.
   653  //
   654  // If the current file is sparse, then the regions marked as a hole
   655  // are read back as NUL-bytes.
   656  //
   657  // Calling Read on special types like [TypeLink], [TypeSymlink], [TypeChar],
   658  // [TypeBlock], [TypeDir], and [TypeFifo] returns (0, [io.EOF]) regardless of what
   659  // the [Header.Size] claims.
   660  func (tr *Reader) Read(b []byte) (int, error) {
   661  	if tr.err != nil {
   662  		return 0, tr.err
   663  	}
   664  	n, err := tr.curr.Read(b)
   665  	if err != nil && err != io.EOF {
   666  		tr.err = err
   667  	}
   668  	return n, err
   669  }
   670  
   671  // writeTo writes the content of the current file to w.
   672  // The bytes written matches the number of remaining bytes in the current file.
   673  //
   674  // If the current file is sparse and w is an io.WriteSeeker,
   675  // then writeTo uses Seek to skip past holes defined in Header.SparseHoles,
   676  // assuming that skipped regions are filled with NULs.
   677  // This always writes the last byte to ensure w is the right size.
   678  //
   679  // TODO(dsnet): Re-export this when adding sparse file support.
   680  // See https://golang.org/issue/22735
   681  func (tr *Reader) writeTo(w io.Writer) (int64, error) {
   682  	if tr.err != nil {
   683  		return 0, tr.err
   684  	}
   685  	n, err := tr.curr.WriteTo(w)
   686  	if err != nil {
   687  		tr.err = err
   688  	}
   689  	return n, err
   690  }
   691  
   692  // regFileReader is a fileReader for reading data from a regular file entry.
   693  type regFileReader struct {
   694  	r  io.Reader // Underlying Reader
   695  	nb int64     // Number of remaining bytes to read
   696  }
   697  
   698  func (fr *regFileReader) Read(b []byte) (n int, err error) {
   699  	if int64(len(b)) > fr.nb {
   700  		b = b[:fr.nb]
   701  	}
   702  	if len(b) > 0 {
   703  		n, err = fr.r.Read(b)
   704  		fr.nb -= int64(n)
   705  	}
   706  	switch {
   707  	case err == io.EOF && fr.nb > 0:
   708  		return n, io.ErrUnexpectedEOF
   709  	case err == nil && fr.nb == 0:
   710  		return n, io.EOF
   711  	default:
   712  		return n, err
   713  	}
   714  }
   715  
   716  func (fr *regFileReader) WriteTo(w io.Writer) (int64, error) {
   717  	return io.Copy(w, struct{ io.Reader }{fr})
   718  }
   719  
   720  // logicalRemaining implements fileState.logicalRemaining.
   721  func (fr regFileReader) logicalRemaining() int64 {
   722  	return fr.nb
   723  }
   724  
   725  // physicalRemaining implements fileState.physicalRemaining.
   726  func (fr regFileReader) physicalRemaining() int64 {
   727  	return fr.nb
   728  }
   729  
   730  // sparseFileReader is a fileReader for reading data from a sparse file entry.
   731  type sparseFileReader struct {
   732  	fr  fileReader  // Underlying fileReader
   733  	sp  sparseHoles // Normalized list of sparse holes
   734  	pos int64       // Current position in sparse file
   735  }
   736  
   737  func (sr *sparseFileReader) Read(b []byte) (n int, err error) {
   738  	finished := int64(len(b)) >= sr.logicalRemaining()
   739  	if finished {
   740  		b = b[:sr.logicalRemaining()]
   741  	}
   742  
   743  	b0 := b
   744  	endPos := sr.pos + int64(len(b))
   745  	for endPos > sr.pos && err == nil {
   746  		var nf int // Bytes read in fragment
   747  		holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
   748  		if sr.pos < holeStart { // In a data fragment
   749  			bf := b[:min(int64(len(b)), holeStart-sr.pos)]
   750  			nf, err = tryReadFull(sr.fr, bf)
   751  		} else { // In a hole fragment
   752  			bf := b[:min(int64(len(b)), holeEnd-sr.pos)]
   753  			nf, err = tryReadFull(zeroReader{}, bf)
   754  		}
   755  		b = b[nf:]
   756  		sr.pos += int64(nf)
   757  		if sr.pos >= holeEnd && len(sr.sp) > 1 {
   758  			sr.sp = sr.sp[1:] // Ensure last fragment always remains
   759  		}
   760  	}
   761  
   762  	n = len(b0) - len(b)
   763  	switch {
   764  	case err == io.EOF:
   765  		return n, errMissData // Less data in dense file than sparse file
   766  	case err != nil:
   767  		return n, err
   768  	case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:
   769  		return n, errUnrefData // More data in dense file than sparse file
   770  	case finished:
   771  		return n, io.EOF
   772  	default:
   773  		return n, nil
   774  	}
   775  }
   776  
   777  func (sr *sparseFileReader) WriteTo(w io.Writer) (n int64, err error) {
   778  	ws, ok := w.(io.WriteSeeker)
   779  	if ok {
   780  		if _, err := ws.Seek(0, io.SeekCurrent); err != nil {
   781  			ok = false // Not all io.Seeker can really seek
   782  		}
   783  	}
   784  	if !ok {
   785  		return io.Copy(w, struct{ io.Reader }{sr})
   786  	}
   787  
   788  	var writeLastByte bool
   789  	pos0 := sr.pos
   790  	for sr.logicalRemaining() > 0 && !writeLastByte && err == nil {
   791  		var nf int64 // Size of fragment
   792  		holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()
   793  		if sr.pos < holeStart { // In a data fragment
   794  			nf = holeStart - sr.pos
   795  			nf, err = io.CopyN(ws, sr.fr, nf)
   796  		} else { // In a hole fragment
   797  			nf = holeEnd - sr.pos
   798  			if sr.physicalRemaining() == 0 {
   799  				writeLastByte = true
   800  				nf--
   801  			}
   802  			_, err = ws.Seek(nf, io.SeekCurrent)
   803  		}
   804  		sr.pos += nf
   805  		if sr.pos >= holeEnd && len(sr.sp) > 1 {
   806  			sr.sp = sr.sp[1:] // Ensure last fragment always remains
   807  		}
   808  	}
   809  
   810  	// If the last fragment is a hole, then seek to 1-byte before EOF, and
   811  	// write a single byte to ensure the file is the right size.
   812  	if writeLastByte && err == nil {
   813  		_, err = ws.Write([]byte{0})
   814  		sr.pos++
   815  	}
   816  
   817  	n = sr.pos - pos0
   818  	switch {
   819  	case err == io.EOF:
   820  		return n, errMissData // Less data in dense file than sparse file
   821  	case err != nil:
   822  		return n, err
   823  	case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:
   824  		return n, errUnrefData // More data in dense file than sparse file
   825  	default:
   826  		return n, nil
   827  	}
   828  }
   829  
   830  func (sr sparseFileReader) logicalRemaining() int64 {
   831  	return sr.sp[len(sr.sp)-1].endOffset() - sr.pos
   832  }
   833  func (sr sparseFileReader) physicalRemaining() int64 {
   834  	return sr.fr.physicalRemaining()
   835  }
   836  
   837  type zeroReader struct{}
   838  
   839  func (zeroReader) Read(b []byte) (int, error) {
   840  	clear(b)
   841  	return len(b), nil
   842  }
   843  
   844  // mustReadFull is like io.ReadFull except it returns
   845  // io.ErrUnexpectedEOF when io.EOF is hit before len(b) bytes are read.
   846  func mustReadFull(r io.Reader, b []byte) (int, error) {
   847  	n, err := tryReadFull(r, b)
   848  	if err == io.EOF {
   849  		err = io.ErrUnexpectedEOF
   850  	}
   851  	return n, err
   852  }
   853  
   854  // tryReadFull is like io.ReadFull except it returns
   855  // io.EOF when it is hit before len(b) bytes are read.
   856  func tryReadFull(r io.Reader, b []byte) (n int, err error) {
   857  	for len(b) > n && err == nil {
   858  		var nn int
   859  		nn, err = r.Read(b[n:])
   860  		n += nn
   861  	}
   862  	if len(b) == n && err == io.EOF {
   863  		err = nil
   864  	}
   865  	return n, err
   866  }
   867  
   868  // readSpecialFile is like io.ReadAll except it returns
   869  // ErrFieldTooLong if more than maxSpecialFileSize is read.
   870  func readSpecialFile(r io.Reader) ([]byte, error) {
   871  	buf, err := io.ReadAll(io.LimitReader(r, maxSpecialFileSize+1))
   872  	if len(buf) > maxSpecialFileSize {
   873  		return nil, ErrFieldTooLong
   874  	}
   875  	return buf, err
   876  }
   877  
   878  // discard skips n bytes in r, reporting an error if unable to do so.
   879  func discard(r io.Reader, n int64) error {
   880  	// If possible, Seek to the last byte before the end of the data section.
   881  	// Do this because Seek is often lazy about reporting errors; this will mask
   882  	// the fact that the stream may be truncated. We can rely on the
   883  	// io.CopyN done shortly afterwards to trigger any IO errors.
   884  	var seekSkipped int64 // Number of bytes skipped via Seek
   885  	if sr, ok := r.(io.Seeker); ok && n > 1 {
   886  		// Not all io.Seeker can actually Seek. For example, os.Stdin implements
   887  		// io.Seeker, but calling Seek always returns an error and performs
   888  		// no action. Thus, we try an innocent seek to the current position
   889  		// to see if Seek is really supported.
   890  		pos1, err := sr.Seek(0, io.SeekCurrent)
   891  		if pos1 >= 0 && err == nil {
   892  			// Seek seems supported, so perform the real Seek.
   893  			pos2, err := sr.Seek(n-1, io.SeekCurrent)
   894  			if pos2 < 0 || err != nil {
   895  				return err
   896  			}
   897  			seekSkipped = pos2 - pos1
   898  		}
   899  	}
   900  
   901  	copySkipped, err := io.CopyN(io.Discard, r, n-seekSkipped)
   902  	if err == io.EOF && seekSkipped+copySkipped < n {
   903  		err = io.ErrUnexpectedEOF
   904  	}
   905  	return err
   906  }
   907  

View as plain text