src/archive/tar/reader.go GO 907 lines View on github.com → Search inside
1// Copyright 2009 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package tar67import (8	"bytes"9	"io"10	"path/filepath"11	"strconv"12	"strings"13	"time"14)1516// 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.19type Reader struct {20	r    io.Reader21	pad  int64      // Amount of padding (ignored) after current file entry22	curr fileReader // Reader for current file entry23	blk  block      // Buffer to use as temporary local storage2425	// err is a persistent error.26	// It is only the responsibility of every exported method of Reader to27	// ensure that this error is sticky.28	err error29}3031type fileReader interface {32	io.Reader33	fileState3435	WriteTo(io.Writer) (int64, error)36}3738// NewReader creates a new [Reader] reading from r.39func NewReader(r io.Reader) *Reader {40	return &Reader{r: r, curr: &regFileReader{r, 0}}41}4243// 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 ignore54// the [ErrInsecurePath] error and use the returned header.55func (tr *Reader) Next() (*Header, error) {56	if tr.err != nil {57		return nil, tr.err58	}59	hdr, err := tr.next()60	tr.err = err61	if err == nil && !filepath.IsLocal(hdr.Name) {62		if tarinsecurepath.Value() == "0" {63			tarinsecurepath.IncNonDefault()64			err = ErrInsecurePath65		}66	}67	return hdr, err68}6970func (tr *Reader) next() (*Header, error) {71	var paxHdrs map[string]string72	var gnuLongName, gnuLongLink string7374	// Externally, Next iterates through the tar archive as if it is a series of75	// files. Internally, the tar format often uses fake "files" to add meta76	// data that describes the next file. These meta data "files" should not77	// normally be visible to the outside. As such, this loop iterates through78	// one or more "header files" until it finds a "normal file".79	format := FormatUSTAR | FormatPAX | FormatGNU80	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, err84		}85		if _, err := tryReadFull(tr.r, tr.blk[:tr.pad]); err != nil {86			return nil, err87		}88		tr.pad = 08990		hdr, rawHdr, err := tr.readHeader()91		if err != nil {92			return nil, err93		}94		if err := tr.handleRegularFile(hdr); err != nil {95			return nil, err96		}97		format.mayOnlyBe(hdr.Format)9899		// 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, err106			}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				}, nil116			}117			continue // This is a meta header affecting the next header118		case TypeGNULongName, TypeGNULongLink:119			format.mayOnlyBe(FormatGNU)120			realname, err := readSpecialFile(tr)121			if err != nil {122				return nil, err123			}124125			var p parser126			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 header133		default:134			// The old GNU sparse format is handled here since it is technically135			// just a regular file with additional attributes.136137			if err := mergePAX(hdr, paxHdrs); err != nil {138				return nil, err139			}140			if gnuLongName != "" {141				hdr.Name = gnuLongName142			}143			if gnuLongLink != "" {144				hdr.Linkname = gnuLongLink145			}146			if hdr.Typeflag == TypeRegA {147				if strings.HasSuffix(hdr.Name, "/") {148					hdr.Typeflag = TypeDir // Legacy archives use trailing slash for directories149				} else {150					hdr.Typeflag = TypeReg151				}152			}153154			// 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, err158			}159160			// Sparse formats rely on being able to read from the logical data161			// section; there must be a preceding call to handleRegularFile.162			if err := tr.handleSparseFile(hdr, rawHdr); err != nil {163				return nil, err164			}165166			// Set the final guess at the format.167			if format.has(FormatUSTAR) && format.has(FormatPAX) {168				format.mayOnlyBe(FormatUSTAR)169			}170			hdr.Format = format171			return hdr, nil // This is a file, so stop172		}173	}174}175176// handleRegularFile sets up the current file reader and padding such that it177// can only read the following logical data section. It will properly handle178// special headers that contain no data section.179func (tr *Reader) handleRegularFile(hdr *Header) error {180	nb := hdr.Size181	if isHeaderOnlyType(hdr.Typeflag) {182		nb = 0183	}184	if nb < 0 {185		return ErrHeader186	}187188	tr.pad = blockPadding(nb)189	tr.curr = &regFileReader{r: tr.r, nb: nb}190	return nil191}192193// handleSparseFile checks if the current file is a sparse format of any type194// and sets the curr reader appropriately.195func (tr *Reader) handleSparseFile(hdr *Header, rawHdr *block) error {196	var spd sparseDatas197	var err error198	if hdr.Typeflag == TypeGNUSparse {199		spd, err = tr.readOldGNUSparseMap(hdr, rawHdr)200	} else {201		spd, err = tr.readGNUSparsePAXHeaders(hdr)202	}203204	// 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 ErrHeader209		}210		sph := invertSparseEntries(spd, hdr.Size)211		tr.curr = &sparseFileReader{tr.curr, sph, 0}212	}213	return err214}215216// 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 headers219// by the PAX header parsing logic.220func (tr *Reader) readGNUSparsePAXHeaders(hdr *Header) (sparseDatas, error) {221	// Identify the version of GNU headers.222	var is1x0 bool223	major, minor := hdr.PAXRecords[paxGNUSparseMajor], hdr.PAXRecords[paxGNUSparseMinor]224	switch {225	case major == "0" && (minor == "0" || minor == "1"):226		is1x0 = false227	case major == "1" && minor == "0":228		is1x0 = true229	case major != "" || minor != "":230		return nil, nil // Unknown GNU sparse PAX version231	case hdr.PAXRecords[paxGNUSparseMap] != "":232		is1x0 = false // 0.0 and 0.1 did not have explicit version records, so guess233	default:234		return nil, nil // Not a PAX format GNU sparse file.235	}236	hdr.Format.mayOnlyBe(FormatPAX)237238	// Update hdr from GNU sparse PAX headers.239	if name := hdr.PAXRecords[paxGNUSparseName]; name != "" {240		hdr.Name = name241	}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, ErrHeader250		}251		hdr.Size = n252	}253254	// Read the sparse map according to the appropriate format.255	if is1x0 {256		return readGNUSparseMap1x0(tr.curr)257	}258	return readGNUSparseMap0x1(hdr.PAXRecords)259}260261// mergePAX merges paxHdrs into hdr for all relevant fields of Header.262func mergePAX(hdr *Header, paxHdrs map[string]string) (err error) {263	for k, v := range paxHdrs {264		if v == "" {265			continue // Keep the original USTAR value266		}267		var id64 int64268		switch k {269		case paxPath:270			hdr.Name = v271		case paxLinkpath:272			hdr.Linkname = v273		case paxUname:274			hdr.Uname = v275		case paxGname:276			hdr.Gname = v277		case paxUid:278			id64, err = strconv.ParseInt(v, 10, 64)279			hdr.Uid = int(id64) // Integer overflow possible280		case paxGid:281			id64, err = strconv.ParseInt(v, 10, 64)282			hdr.Gid = int(id64) // Integer overflow possible283		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):]] = v297			}298		}299		if err != nil {300			return ErrHeader301		}302	}303	hdr.PAXRecords = paxHdrs304	return nil305}306307// parsePAX parses PAX headers.308// If an extended header (type 'x') is invalid, ErrHeader is returned.309func parsePAX(r io.Reader) (map[string]string, error) {310	buf, err := readSpecialFile(r)311	if err != nil {312		return nil, err313	}314	sbuf := string(buf)315316	// For GNU PAX sparse format 0.0 support.317	// This function transforms the sparse format 0.0 headers into format 0.1318	// headers since 0.0 headers were not PAX compliant.319	var sparseMap []string320321	paxHdrs := make(map[string]string)322	for len(sbuf) > 0 {323		key, value, residual, err := parsePAXRecord(sbuf)324		if err != nil {325			return nil, ErrHeader326		}327		sbuf = residual328329		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, ErrHeader336			}337			sparseMap = append(sparseMap, value)338		default:339			paxHdrs[key] = value340		}341	}342	if len(sparseMap) > 0 {343		paxHdrs[paxGNUSparseMap] = strings.Join(sparseMap, ",")344	}345	return paxHdrs, nil346}347348// readHeader reads the next block header and assumes that the underlying reader349// is already aligned to a block boundary. It returns the raw block of the350// 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.356func (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 read360	}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 read364		}365		if bytes.Equal(tr.blk[:], zeroBlock[:]) {366			return nil, nil, io.EOF // normal EOF; exactly 2 block of zeros read367		}368		return nil, nil, ErrHeader // Zero block and then non-zero block369	}370371	// Verify the header matches a known format.372	format := tr.blk.getFormat()373	if format == FormatUnknown {374		return nil, nil, ErrHeader375	}376377	var p parser378	hdr := new(Header)379380	// 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)390391	// 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())398399		var prefix string400		switch {401		case format.has(FormatUSTAR | FormatPAX):402			hdr.Format = format403			ustar := tr.blk.toUSTAR()404			prefix = p.parseString(ustar.prefix())405406			// For Format detection, check if block is properly formatted since407			// 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 NUL416			}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 = format424			var p2 parser425			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			}432433			// Prior to Go1.8, the Writer had a bug where it would output434			// an invalid tar file in certain rare situations because the logic435			// incorrectly believed that the old GNU format had a prefix field.436			// This is wrong and leads to an output file that mangles the437			// atime and ctime fields, which are often left unused.438			//439			// In order to continue reading tar files created by former, buggy440			// versions of Go, we skeptically parse the atime and ctime fields.441			// If we are unable to parse them and the prefix field looks like442			// an ASCII string, then we fallback on the pre-Go1.8 behavior443			// of treating these fields as the USTAR prefix field.444			//445			// Note that this will not use the fallback logic for all possible446			// files generated by a pre-Go1.8 toolchain. If the generated file447			// happened to have a prefix field that parses as valid448			// atime and ctime fields (e.g., when they are valid octal strings),449			// then it is impossible to distinguish between a valid GNU file450			// and an invalid pre-Go1.8 file.451			//452			// See https://golang.org/issues/12594453			// See https://golang.org/issues/21005454			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 = s459				}460				hdr.Format = FormatUnknown // Buggy file is not GNU461			}462		}463		if len(prefix) > 0 {464			hdr.Name = prefix + "/" + hdr.Name465		}466	}467	return hdr, &tr.blk, p.err468}469470// 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 used473// 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.478func (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 uses481	// the same type flag but has a completely different layout.482	if blk.getFormat() != FormatGNU {483		return nil, ErrHeader484	}485	hdr.Format.mayOnlyBe(FormatGNU)486487	var p parser488	hdr.Size = p.parseNumeric(blk.toGNU().realSize())489	if p.err != nil {490		return nil, p.err491	}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.err505			}506			var err error507			spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})508			if err != nil {509				return nil, err510			}511		}512513		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, err517			}518			s = blk.toSparse()519			totalSize += len(s)520			continue521		}522		return spd, nil // Done523	}524	return nil, errSparseTooLong525}526527// readGNUSparseMap1x0 reads the sparse map as stored in GNU's PAX sparse format528// version 1.0. The format of the sparse map consists of a series of529// newline-terminated numeric fields. The first field is the number of entries530// and is always present. Following this are the entries, consisting of two531// fields (offset, length). This function must stop reading at the end532// boundary of the block containing the last newline.533//534// Note that the GNU manual says that numeric values should be encoded in octal535// format. However, the GNU tar utility itself outputs these values in decimal.536// As such, this library treats values as being encoded in decimal.537func readGNUSparseMap1x0(r io.Reader) (sparseDatas, error) {538	var (539		cntNewline int64540		buf        bytes.Buffer541		blk        block542		totalSize  int543	)544545	// feedTokens copies data in blocks from r into buf until there are546	// 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 errSparseTooLong552			}553			if _, err := mustReadFull(r, blk[:]); err != nil {554				return err555			}556			buf.Write(blk[:])557			for _, c := range blk {558				if c == '\n' {559					cntNewline++560				}561			}562		}563		return nil564	}565566	// nextToken gets the next token delimited by a newline. This assumes that567	// 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	}573574	// 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, err578	}579	numEntries, err := strconv.ParseInt(nextToken(), 10, 0) // Intentionally parse as native int580	if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {581		return nil, ErrHeader582	}583584	// Parse for all member entries.585	// numEntries is trusted after this since feedTokens limits the number of586	// tokens based on maxSpecialFileSize.587	if err := feedTokens(2 * numEntries); err != nil {588		return nil, err589	}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, ErrHeader596		}597		spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})598		if err != nil {599			return nil, err600		}601	}602	return spd, nil603}604605// readGNUSparseMap0x1 reads the sparse map as stored in GNU's PAX sparse format606// version 0.1. The sparse map is stored in the PAX headers.607func 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 int612	if err != nil || numEntries < 0 || int(2*numEntries) < int(numEntries) {613		return nil, ErrHeader614	}615616	// 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, ErrHeader623	}624625	// 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, ErrHeader633		}634		spd, err = appendSparseEntry(spd, sparseEntry{Offset: offset, Length: length})635		if err != nil {636			return nil, err637		}638		sparseMap = sparseMap[2:]639	}640	return spd, nil641}642643func appendSparseEntry(spd sparseDatas, ent sparseEntry) (sparseDatas, error) {644	if len(spd) >= maxSparseFileEntries {645		return nil, errSparseTooLong646	}647	return append(spd, ent), nil648}649650// 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 hole655// 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 what659// the [Header.Size] claims.660func (tr *Reader) Read(b []byte) (int, error) {661	if tr.err != nil {662		return 0, tr.err663	}664	n, err := tr.curr.Read(b)665	if err != nil && err != io.EOF {666		tr.err = err667	}668	return n, err669}670671// 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/22735681func (tr *Reader) writeTo(w io.Writer) (int64, error) {682	if tr.err != nil {683		return 0, tr.err684	}685	n, err := tr.curr.WriteTo(w)686	if err != nil {687		tr.err = err688	}689	return n, err690}691692// regFileReader is a fileReader for reading data from a regular file entry.693type regFileReader struct {694	r  io.Reader // Underlying Reader695	nb int64     // Number of remaining bytes to read696}697698func (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.ErrUnexpectedEOF709	case err == nil && fr.nb == 0:710		return n, io.EOF711	default:712		return n, err713	}714}715716func (fr *regFileReader) WriteTo(w io.Writer) (int64, error) {717	return io.Copy(w, struct{ io.Reader }{fr})718}719720// logicalRemaining implements fileState.logicalRemaining.721func (fr regFileReader) logicalRemaining() int64 {722	return fr.nb723}724725// physicalRemaining implements fileState.physicalRemaining.726func (fr regFileReader) physicalRemaining() int64 {727	return fr.nb728}729730// sparseFileReader is a fileReader for reading data from a sparse file entry.731type sparseFileReader struct {732	fr  fileReader  // Underlying fileReader733	sp  sparseHoles // Normalized list of sparse holes734	pos int64       // Current position in sparse file735}736737func (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	}742743	b0 := b744	endPos := sr.pos + int64(len(b))745	for endPos > sr.pos && err == nil {746		var nf int // Bytes read in fragment747		holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()748		if sr.pos < holeStart { // In a data fragment749			bf := b[:min(int64(len(b)), holeStart-sr.pos)]750			nf, err = tryReadFull(sr.fr, bf)751		} else { // In a hole fragment752			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 remains759		}760	}761762	n = len(b0) - len(b)763	switch {764	case err == io.EOF:765		return n, errMissData // Less data in dense file than sparse file766	case err != nil:767		return n, err768	case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:769		return n, errUnrefData // More data in dense file than sparse file770	case finished:771		return n, io.EOF772	default:773		return n, nil774	}775}776777func (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 seek782		}783	}784	if !ok {785		return io.Copy(w, struct{ io.Reader }{sr})786	}787788	var writeLastByte bool789	pos0 := sr.pos790	for sr.logicalRemaining() > 0 && !writeLastByte && err == nil {791		var nf int64 // Size of fragment792		holeStart, holeEnd := sr.sp[0].Offset, sr.sp[0].endOffset()793		if sr.pos < holeStart { // In a data fragment794			nf = holeStart - sr.pos795			nf, err = io.CopyN(ws, sr.fr, nf)796		} else { // In a hole fragment797			nf = holeEnd - sr.pos798			if sr.physicalRemaining() == 0 {799				writeLastByte = true800				nf--801			}802			_, err = ws.Seek(nf, io.SeekCurrent)803		}804		sr.pos += nf805		if sr.pos >= holeEnd && len(sr.sp) > 1 {806			sr.sp = sr.sp[1:] // Ensure last fragment always remains807		}808	}809810	// If the last fragment is a hole, then seek to 1-byte before EOF, and811	// 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	}816817	n = sr.pos - pos0818	switch {819	case err == io.EOF:820		return n, errMissData // Less data in dense file than sparse file821	case err != nil:822		return n, err823	case sr.logicalRemaining() == 0 && sr.physicalRemaining() > 0:824		return n, errUnrefData // More data in dense file than sparse file825	default:826		return n, nil827	}828}829830func (sr sparseFileReader) logicalRemaining() int64 {831	return sr.sp[len(sr.sp)-1].endOffset() - sr.pos832}833func (sr sparseFileReader) physicalRemaining() int64 {834	return sr.fr.physicalRemaining()835}836837type zeroReader struct{}838839func (zeroReader) Read(b []byte) (int, error) {840	clear(b)841	return len(b), nil842}843844// mustReadFull is like io.ReadFull except it returns845// io.ErrUnexpectedEOF when io.EOF is hit before len(b) bytes are read.846func mustReadFull(r io.Reader, b []byte) (int, error) {847	n, err := tryReadFull(r, b)848	if err == io.EOF {849		err = io.ErrUnexpectedEOF850	}851	return n, err852}853854// tryReadFull is like io.ReadFull except it returns855// io.EOF when it is hit before len(b) bytes are read.856func tryReadFull(r io.Reader, b []byte) (n int, err error) {857	for len(b) > n && err == nil {858		var nn int859		nn, err = r.Read(b[n:])860		n += nn861	}862	if len(b) == n && err == io.EOF {863		err = nil864	}865	return n, err866}867868// readSpecialFile is like io.ReadAll except it returns869// ErrFieldTooLong if more than maxSpecialFileSize is read.870func readSpecialFile(r io.Reader) ([]byte, error) {871	buf, err := io.ReadAll(io.LimitReader(r, maxSpecialFileSize+1))872	if len(buf) > maxSpecialFileSize {873		return nil, ErrFieldTooLong874	}875	return buf, err876}877878// discard skips n bytes in r, reporting an error if unable to do so.879func 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 mask882	// the fact that the stream may be truncated. We can rely on the883	// io.CopyN done shortly afterwards to trigger any IO errors.884	var seekSkipped int64 // Number of bytes skipped via Seek885	if sr, ok := r.(io.Seeker); ok && n > 1 {886		// Not all io.Seeker can actually Seek. For example, os.Stdin implements887		// io.Seeker, but calling Seek always returns an error and performs888		// no action. Thus, we try an innocent seek to the current position889		// 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 err896			}897			seekSkipped = pos2 - pos1898		}899	}900901	copySkipped, err := io.CopyN(io.Discard, r, n-seekSkipped)902	if err == io.EOF && seekSkipped+copySkipped < n {903		err = io.ErrUnexpectedEOF904	}905	return err906}

Code quality findings 5

Ensure errors are handled or logged
warning correctness unhandled-error
if err != nil {
Ensure errors are handled or logged
warning correctness unhandled-error
if err != nil {
Ensure errors are handled or logged
warning correctness unhandled-error
if err != nil {
Range over slice copies each element by value; use index or pointer receiver for large structs to avoid copies
info performance copy-large-struct
for k, v := range paxHdrs {
Map created without size hint before being populated in a loop; provide capacity hint to reduce allocations
info performance map-without-size-hint
paxHdrs := make(map[string]string)

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.