2009-06-09 00:22:56 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tar
|
|
|
|
|
|
|
|
// TODO(dsymonds):
|
2009-07-06 23:59:31 -06:00
|
|
|
// - pax extensions
|
2009-06-09 00:22:56 -06:00
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"bytes"
|
2011-11-01 20:04:37 -06:00
|
|
|
"errors"
|
2009-12-15 16:33:31 -07:00
|
|
|
"io"
|
2011-04-27 16:57:22 -06:00
|
|
|
"io/ioutil"
|
2009-12-15 16:33:31 -07:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2013-02-10 17:36:29 -07:00
|
|
|
"strings"
|
2011-11-30 10:01:46 -07:00
|
|
|
"time"
|
2009-06-09 00:22:56 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2012-03-12 00:33:35 -06:00
|
|
|
ErrHeader = errors.New("archive/tar: invalid tar header")
|
2009-06-09 00:22:56 -06:00
|
|
|
)
|
|
|
|
|
2013-02-10 17:36:29 -07:00
|
|
|
const maxNanoSecondIntSize = 9
|
|
|
|
|
2009-06-09 00:22:56 -06:00
|
|
|
// A Reader provides sequential access to the contents of a tar archive.
|
2009-07-08 19:31:14 -06:00
|
|
|
// A tar archive consists of a sequence of files.
|
2009-06-09 00:22:56 -06:00
|
|
|
// The Next method advances to the next file in the archive (including the first),
|
|
|
|
// and then it can be treated as an io.Reader to access the file's data.
|
|
|
|
type Reader struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
r io.Reader
|
2011-11-01 20:04:37 -06:00
|
|
|
err error
|
2009-12-15 16:33:31 -07:00
|
|
|
nb int64 // number of unread bytes for current file entry
|
|
|
|
pad int64 // amount of padding (ignored) after current file entry
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2009-07-08 19:31:14 -06:00
|
|
|
// NewReader creates a new Reader reading from r.
|
2009-12-15 16:33:31 -07:00
|
|
|
func NewReader(r io.Reader) *Reader { return &Reader{r: r} }
|
2009-06-09 00:22:56 -06:00
|
|
|
|
|
|
|
// Next advances to the next entry in the tar archive.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (tr *Reader) Next() (*Header, error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
var hdr *Header
|
2009-06-09 00:22:56 -06:00
|
|
|
if tr.err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
tr.skipUnread()
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2013-02-10 17:36:29 -07:00
|
|
|
if tr.err != nil {
|
|
|
|
return hdr, tr.err
|
|
|
|
}
|
|
|
|
hdr = tr.readHeader()
|
|
|
|
if hdr == nil {
|
|
|
|
return hdr, tr.err
|
|
|
|
}
|
|
|
|
// Check for PAX/GNU header.
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case TypeXHeader:
|
|
|
|
// PAX extended header
|
|
|
|
headers, err := parsePAX(tr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// We actually read the whole file,
|
|
|
|
// but this skips alignment padding
|
|
|
|
tr.skipUnread()
|
2009-11-09 13:07:39 -07:00
|
|
|
hdr = tr.readHeader()
|
2013-02-10 17:36:29 -07:00
|
|
|
mergePAX(hdr, headers)
|
|
|
|
return hdr, nil
|
|
|
|
case TypeGNULongName:
|
|
|
|
// We have a GNU long name header. Its contents are the real file name.
|
|
|
|
realname, err := ioutil.ReadAll(tr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
hdr.Name = cString(realname)
|
|
|
|
return hdr, err
|
|
|
|
case TypeGNULongLink:
|
|
|
|
// We have a GNU long link header.
|
|
|
|
realname, err := ioutil.ReadAll(tr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
hdr.Linkname = cString(realname)
|
|
|
|
return hdr, err
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return hdr, tr.err
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2013-02-10 17:36:29 -07:00
|
|
|
// mergePAX merges well known headers according to PAX standard.
|
|
|
|
// In general headers with the same name as those found
|
|
|
|
// in the header struct overwrite those found in the header
|
|
|
|
// struct with higher precision or longer values. Esp. useful
|
|
|
|
// for name and linkname fields.
|
|
|
|
func mergePAX(hdr *Header, headers map[string]string) error {
|
|
|
|
for k, v := range headers {
|
|
|
|
switch k {
|
|
|
|
case "path":
|
|
|
|
hdr.Name = v
|
|
|
|
case "linkpath":
|
|
|
|
hdr.Linkname = v
|
|
|
|
case "gname":
|
|
|
|
hdr.Gname = v
|
|
|
|
case "uname":
|
|
|
|
hdr.Uname = v
|
|
|
|
case "uid":
|
|
|
|
uid, err := strconv.ParseInt(v, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Uid = int(uid)
|
|
|
|
case "gid":
|
|
|
|
gid, err := strconv.ParseInt(v, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Gid = int(gid)
|
|
|
|
case "atime":
|
|
|
|
t, err := parsePAXTime(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.AccessTime = t
|
|
|
|
case "mtime":
|
|
|
|
t, err := parsePAXTime(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.ModTime = t
|
|
|
|
case "ctime":
|
|
|
|
t, err := parsePAXTime(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.ChangeTime = t
|
|
|
|
case "size":
|
|
|
|
size, err := strconv.ParseInt(v, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
hdr.Size = int64(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parsePAXTime takes a string of the form %d.%d as described in
|
|
|
|
// the PAX specification.
|
|
|
|
func parsePAXTime(t string) (time.Time, error) {
|
|
|
|
buf := []byte(t)
|
|
|
|
pos := bytes.IndexByte(buf, '.')
|
|
|
|
var seconds, nanoseconds int64
|
|
|
|
var err error
|
|
|
|
if pos == -1 {
|
|
|
|
seconds, err = strconv.ParseInt(t, 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
seconds, err = strconv.ParseInt(string(buf[:pos]), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
nano_buf := string(buf[pos+1:])
|
|
|
|
// Pad as needed before converting to a decimal.
|
|
|
|
// For example .030 -> .030000000 -> 30000000 nanoseconds
|
|
|
|
if len(nano_buf) < maxNanoSecondIntSize {
|
|
|
|
// Right pad
|
|
|
|
nano_buf += strings.Repeat("0", maxNanoSecondIntSize-len(nano_buf))
|
|
|
|
} else if len(nano_buf) > maxNanoSecondIntSize {
|
|
|
|
// Right truncate
|
|
|
|
nano_buf = nano_buf[:maxNanoSecondIntSize]
|
|
|
|
}
|
|
|
|
nanoseconds, err = strconv.ParseInt(string(nano_buf), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ts := time.Unix(seconds, nanoseconds)
|
|
|
|
return ts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parsePAX parses PAX headers.
|
|
|
|
// If an extended header (type 'x') is invalid, ErrHeader is returned
|
|
|
|
func parsePAX(r io.Reader) (map[string]string, error) {
|
|
|
|
buf, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
headers := make(map[string]string)
|
|
|
|
// Each record is constructed as
|
|
|
|
// "%d %s=%s\n", length, keyword, value
|
|
|
|
for len(buf) > 0 {
|
|
|
|
// or the header was empty to start with.
|
|
|
|
var sp int
|
|
|
|
// The size field ends at the first space.
|
|
|
|
sp = bytes.IndexByte(buf, ' ')
|
|
|
|
if sp == -1 {
|
|
|
|
return nil, ErrHeader
|
|
|
|
}
|
|
|
|
// Parse the first token as a decimal integer.
|
|
|
|
n, err := strconv.ParseInt(string(buf[:sp]), 10, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrHeader
|
|
|
|
}
|
|
|
|
// Extract everything between the decimal and the n -1 on the
|
|
|
|
// beginning to to eat the ' ', -1 on the end to skip the newline.
|
|
|
|
var record []byte
|
|
|
|
record, buf = buf[sp+1:n-1], buf[n:]
|
|
|
|
// The first equals is guaranteed to mark the end of the key.
|
|
|
|
// Everything else is value.
|
|
|
|
eq := bytes.IndexByte(record, '=')
|
|
|
|
if eq == -1 {
|
|
|
|
return nil, ErrHeader
|
|
|
|
}
|
|
|
|
key, value := record[:eq], record[eq+1:]
|
|
|
|
headers[string(key)] = string(value)
|
|
|
|
}
|
|
|
|
return headers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cString parses bytes as a NUL-terminated C-style string.
|
2009-06-09 00:22:56 -06:00
|
|
|
// If a NUL byte is not found then the whole slice is returned as a string.
|
|
|
|
func cString(b []byte) string {
|
2009-12-15 16:33:31 -07:00
|
|
|
n := 0
|
2009-06-09 00:22:56 -06:00
|
|
|
for n < len(b) && b[n] != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
n++
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return string(b[0:n])
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2009-06-10 22:32:36 -06:00
|
|
|
func (tr *Reader) octal(b []byte) int64 {
|
2012-11-08 14:50:10 -07:00
|
|
|
// Check for binary format first.
|
|
|
|
if len(b) > 0 && b[0]&0x80 != 0 {
|
|
|
|
var x int64
|
|
|
|
for i, c := range b {
|
|
|
|
if i == 0 {
|
|
|
|
c &= 0x7f // ignore signal bit in first byte
|
|
|
|
}
|
|
|
|
x = x<<8 | int64(c)
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2013-05-14 14:40:42 -06:00
|
|
|
// Because unused fields are filled with NULs, we need
|
|
|
|
// to skip leading NULs. Fields may also be padded with
|
|
|
|
// spaces or NULs.
|
|
|
|
// So we remove leading and trailing NULs and spaces to
|
|
|
|
// be sure.
|
|
|
|
b = bytes.Trim(b, " \x00")
|
|
|
|
|
|
|
|
if len(b) == 0 {
|
|
|
|
return 0
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
2011-12-05 13:48:46 -07:00
|
|
|
x, err := strconv.ParseUint(cString(b), 8, 64)
|
2009-06-09 00:22:56 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
tr.err = err
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return int64(x)
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2013-02-10 17:36:29 -07:00
|
|
|
// skipUnread skips any unread bytes in the existing file entry, as well as any alignment padding.
|
2009-06-09 00:22:56 -06:00
|
|
|
func (tr *Reader) skipUnread() {
|
2009-12-15 16:33:31 -07:00
|
|
|
nr := tr.nb + tr.pad // number of bytes to skip
|
|
|
|
tr.nb, tr.pad = 0, 0
|
2009-08-18 18:47:03 -06:00
|
|
|
if sr, ok := tr.r.(io.Seeker); ok {
|
2011-04-04 14:53:52 -06:00
|
|
|
if _, err := sr.Seek(nr, os.SEEK_CUR); err == nil {
|
2009-12-14 12:35:02 -07:00
|
|
|
return
|
|
|
|
}
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2011-09-30 14:13:39 -06:00
|
|
|
_, tr.err = io.CopyN(ioutil.Discard, tr.r, nr)
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Reader) verifyChecksum(header []byte) bool {
|
|
|
|
if tr.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
given := tr.octal(header[148:156])
|
|
|
|
unsigned, signed := checksum(header)
|
|
|
|
return given == unsigned || given == signed
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Reader) readHeader() *Header {
|
2009-12-15 16:33:31 -07:00
|
|
|
header := make([]byte, blockSize)
|
2009-09-14 18:20:29 -06:00
|
|
|
if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Two blocks of zero bytes marks the end of the archive.
|
2009-11-09 22:09:34 -07:00
|
|
|
if bytes.Equal(header, zeroBlock[0:blockSize]) {
|
2009-09-14 18:20:29 -06:00
|
|
|
if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-14 12:35:02 -07:00
|
|
|
if bytes.Equal(header, zeroBlock[0:blockSize]) {
|
2011-11-01 20:04:37 -06:00
|
|
|
tr.err = io.EOF
|
2009-12-14 12:35:02 -07:00
|
|
|
} else {
|
2012-01-24 12:48:48 -07:00
|
|
|
tr.err = ErrHeader // zero block and then non-zero block
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return nil
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !tr.verifyChecksum(header) {
|
2012-01-24 12:48:48 -07:00
|
|
|
tr.err = ErrHeader
|
2009-12-15 16:33:31 -07:00
|
|
|
return nil
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack
|
2009-12-15 16:33:31 -07:00
|
|
|
hdr := new(Header)
|
|
|
|
s := slicer(header)
|
|
|
|
|
|
|
|
hdr.Name = cString(s.next(100))
|
|
|
|
hdr.Mode = tr.octal(s.next(8))
|
2010-04-22 15:01:33 -06:00
|
|
|
hdr.Uid = int(tr.octal(s.next(8)))
|
|
|
|
hdr.Gid = int(tr.octal(s.next(8)))
|
2009-12-15 16:33:31 -07:00
|
|
|
hdr.Size = tr.octal(s.next(12))
|
2011-11-30 10:01:46 -07:00
|
|
|
hdr.ModTime = time.Unix(tr.octal(s.next(12)), 0)
|
2009-12-15 16:33:31 -07:00
|
|
|
s.next(8) // chksum
|
|
|
|
hdr.Typeflag = s.next(1)[0]
|
|
|
|
hdr.Linkname = cString(s.next(100))
|
2009-06-10 22:32:36 -06:00
|
|
|
|
|
|
|
// The remainder of the header depends on the value of magic.
|
2009-07-06 23:59:31 -06:00
|
|
|
// The original (v7) version of tar had no explicit magic field,
|
|
|
|
// so its magic bytes, like the rest of the block, are NULs.
|
2009-12-15 16:33:31 -07:00
|
|
|
magic := string(s.next(8)) // contains version field as well.
|
|
|
|
var format string
|
2009-06-10 22:32:36 -06:00
|
|
|
switch magic {
|
2009-12-15 16:33:31 -07:00
|
|
|
case "ustar\x0000": // POSIX tar (1003.1-1988)
|
2009-06-10 22:32:36 -06:00
|
|
|
if string(header[508:512]) == "tar\x00" {
|
2009-11-09 13:07:39 -07:00
|
|
|
format = "star"
|
2009-06-10 22:32:36 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
format = "posix"
|
2009-10-06 15:55:39 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
case "ustar \x00": // old GNU tar
|
2009-11-09 13:07:39 -07:00
|
|
|
format = "gnu"
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case "posix", "gnu", "star":
|
2009-12-15 16:33:31 -07:00
|
|
|
hdr.Uname = cString(s.next(32))
|
|
|
|
hdr.Gname = cString(s.next(32))
|
|
|
|
devmajor := s.next(8)
|
|
|
|
devminor := s.next(8)
|
2009-06-10 22:32:36 -06:00
|
|
|
if hdr.Typeflag == TypeChar || hdr.Typeflag == TypeBlock {
|
2009-12-15 16:33:31 -07:00
|
|
|
hdr.Devmajor = tr.octal(devmajor)
|
|
|
|
hdr.Devminor = tr.octal(devminor)
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
var prefix string
|
2009-06-10 22:32:36 -06:00
|
|
|
switch format {
|
|
|
|
case "posix", "gnu":
|
2009-11-09 13:07:39 -07:00
|
|
|
prefix = cString(s.next(155))
|
2009-06-10 22:32:36 -06:00
|
|
|
case "star":
|
2009-12-15 16:33:31 -07:00
|
|
|
prefix = cString(s.next(131))
|
2011-11-30 10:01:46 -07:00
|
|
|
hdr.AccessTime = time.Unix(tr.octal(s.next(12)), 0)
|
|
|
|
hdr.ChangeTime = time.Unix(tr.octal(s.next(12)), 0)
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
|
|
|
if len(prefix) > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
hdr.Name = prefix + "/" + hdr.Name
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
|
|
|
}
|
2009-06-09 00:22:56 -06:00
|
|
|
|
|
|
|
if tr.err != nil {
|
2012-01-24 12:48:48 -07:00
|
|
|
tr.err = ErrHeader
|
2009-12-15 16:33:31 -07:00
|
|
|
return nil
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum value of hdr.Size is 64 GB (12 octal digits),
|
|
|
|
// so there's no risk of int64 overflowing.
|
2009-12-15 16:33:31 -07:00
|
|
|
tr.nb = int64(hdr.Size)
|
|
|
|
tr.pad = -tr.nb & (blockSize - 1) // blockSize is a power of two
|
2009-06-09 00:22:56 -06:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return hdr
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads from the current entry in the tar archive.
|
2011-11-03 15:01:30 -06:00
|
|
|
// It returns 0, io.EOF when it reaches the end of that entry,
|
2009-06-09 00:22:56 -06:00
|
|
|
// until Next is called to advance to the next entry.
|
2011-11-01 20:04:37 -06:00
|
|
|
func (tr *Reader) Read(b []byte) (n int, err error) {
|
2009-12-14 12:35:02 -07:00
|
|
|
if tr.nb == 0 {
|
|
|
|
// file consumed
|
2011-11-01 20:04:37 -06:00
|
|
|
return 0, io.EOF
|
2009-12-14 12:35:02 -07:00
|
|
|
}
|
|
|
|
|
2009-06-09 00:22:56 -06:00
|
|
|
if int64(len(b)) > tr.nb {
|
2009-11-09 22:09:34 -07:00
|
|
|
b = b[0:tr.nb]
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
n, err = tr.r.Read(b)
|
|
|
|
tr.nb -= int64(n)
|
2009-12-14 12:35:02 -07:00
|
|
|
|
2011-11-01 20:04:37 -06:00
|
|
|
if err == io.EOF && tr.nb > 0 {
|
2009-12-14 12:35:02 -07:00
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
tr.err = err
|
|
|
|
return
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|