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 (
|
|
|
|
"bytes";
|
|
|
|
"io";
|
|
|
|
"os";
|
|
|
|
"strconv";
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
HeaderError os.Error = os.ErrorString("invalid tar header");
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Example:
|
2009-08-12 14:19:17 -06:00
|
|
|
// tr := tar.NewReader(r);
|
|
|
|
// for {
|
2009-06-09 00:22:56 -06:00
|
|
|
// hdr, err := tr.Next();
|
|
|
|
// if err != nil {
|
|
|
|
// // handle error
|
|
|
|
// }
|
|
|
|
// if hdr == nil {
|
|
|
|
// // end of tar archive
|
|
|
|
// break
|
|
|
|
// }
|
2009-07-08 19:31:14 -06:00
|
|
|
// io.Copy(tr, data);
|
2009-08-12 14:19:17 -06:00
|
|
|
// }
|
2009-06-09 00:22:56 -06:00
|
|
|
type Reader struct {
|
2009-10-06 12:42:55 -06:00
|
|
|
r io.Reader;
|
|
|
|
err os.Error;
|
|
|
|
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-06-09 00:22:56 -06:00
|
|
|
func NewReader(r io.Reader) *Reader {
|
2009-10-06 12:42:55 -06:00
|
|
|
return &Reader{r: r};
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Next advances to the next entry in the tar archive.
|
|
|
|
func (tr *Reader) Next() (*Header, os.Error) {
|
|
|
|
var hdr *Header;
|
|
|
|
if tr.err == nil {
|
|
|
|
tr.skipUnread();
|
|
|
|
}
|
|
|
|
if tr.err == nil {
|
|
|
|
hdr = tr.readHeader();
|
|
|
|
}
|
2009-10-06 12:42:55 -06:00
|
|
|
return hdr, tr.err;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse bytes as a NUL-terminated C-style string.
|
|
|
|
// If a NUL byte is not found then the whole slice is returned as a string.
|
|
|
|
func cString(b []byte) string {
|
|
|
|
n := 0;
|
|
|
|
for n < len(b) && b[n] != 0 {
|
|
|
|
n++;
|
|
|
|
}
|
2009-10-06 12:42:55 -06: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 {
|
2009-08-09 16:03:30 -06:00
|
|
|
// Removing leading spaces.
|
|
|
|
for len(b) > 0 && b[0] == ' ' {
|
|
|
|
b = b[1:len(b)];
|
|
|
|
}
|
|
|
|
// Removing trailing NULs and spaces.
|
|
|
|
for len(b) > 0 && (b[len(b)-1] == ' ' || b[len(b)-1] == '\x00') {
|
2009-10-06 12:42:55 -06:00
|
|
|
b = b[0 : len(b)-1];
|
2009-06-10 22:32:36 -06:00
|
|
|
}
|
2009-06-09 00:22:56 -06:00
|
|
|
x, err := strconv.Btoui64(cString(b), 8);
|
|
|
|
if err != nil {
|
|
|
|
tr.err = err;
|
|
|
|
}
|
2009-10-06 12:42:55 -06:00
|
|
|
return int64(x);
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2009-10-06 12:42:55 -06:00
|
|
|
type ignoreWriter struct{}
|
2009-10-06 15:55:39 -06:00
|
|
|
|
2009-06-09 00:22:56 -06:00
|
|
|
func (ignoreWriter) Write(b []byte) (n int, err os.Error) {
|
2009-10-06 12:42:55 -06:00
|
|
|
return len(b), nil;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Skip any unread bytes in the existing file entry, as well as any alignment padding.
|
|
|
|
func (tr *Reader) skipUnread() {
|
|
|
|
nr := tr.nb + tr.pad; // number of bytes to skip
|
|
|
|
|
2009-08-18 18:47:03 -06:00
|
|
|
if sr, ok := tr.r.(io.Seeker); ok {
|
2009-09-14 18:20:29 -06:00
|
|
|
_, tr.err = sr.Seek(nr, 1);
|
2009-06-09 00:22:56 -06:00
|
|
|
} else {
|
2009-09-14 18:20:29 -06:00
|
|
|
_, tr.err = io.Copyn(tr.r, ignoreWriter{}, nr);
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
tr.nb, tr.pad = 0, 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Reader) verifyChecksum(header []byte) bool {
|
|
|
|
if tr.err != nil {
|
2009-10-06 12:42:55 -06:00
|
|
|
return false;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
2009-07-08 18:15:18 -06:00
|
|
|
given := tr.octal(header[148:156]);
|
|
|
|
unsigned, signed := checksum(header);
|
2009-10-06 12:42:55 -06:00
|
|
|
return given == unsigned || given == signed;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tr *Reader) readHeader() *Header {
|
|
|
|
header := make([]byte, blockSize);
|
2009-09-14 18:20:29 -06:00
|
|
|
if _, tr.err = io.ReadFull(tr.r, header); tr.err != nil {
|
2009-10-06 12:42:55 -06:00
|
|
|
return nil;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Two blocks of zero bytes marks the end of the archive.
|
|
|
|
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-10-06 12:42:55 -06:00
|
|
|
return nil;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
if !bytes.Equal(header, zeroBlock[0:blockSize]) {
|
|
|
|
tr.err = HeaderError;
|
|
|
|
}
|
2009-10-06 12:42:55 -06:00
|
|
|
return nil;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !tr.verifyChecksum(header) {
|
|
|
|
tr.err = HeaderError;
|
2009-10-06 12:42:55 -06:00
|
|
|
return nil;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack
|
|
|
|
hdr := new(Header);
|
|
|
|
s := slicer(header);
|
|
|
|
|
|
|
|
hdr.Name = cString(s.next(100));
|
2009-06-10 22:32:36 -06:00
|
|
|
hdr.Mode = tr.octal(s.next(8));
|
|
|
|
hdr.Uid = tr.octal(s.next(8));
|
|
|
|
hdr.Gid = tr.octal(s.next(8));
|
|
|
|
hdr.Size = tr.octal(s.next(12));
|
|
|
|
hdr.Mtime = tr.octal(s.next(12));
|
2009-10-06 12:42:55 -06:00
|
|
|
s.next(8); // chksum
|
2009-06-09 00:22:56 -06:00
|
|
|
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-10-06 12:42:55 -06:00
|
|
|
magic := string(s.next(8)); // contains version field as well.
|
2009-06-10 22:32:36 -06:00
|
|
|
var format string;
|
|
|
|
switch magic {
|
2009-10-06 12:42:55 -06:00
|
|
|
case "ustar\x0000": // POSIX tar (1003.1-1988)
|
2009-06-10 22:32:36 -06:00
|
|
|
if string(header[508:512]) == "tar\x00" {
|
|
|
|
format = "star";
|
|
|
|
} else {
|
|
|
|
format = "posix";
|
2009-10-06 15:55:39 -06:00
|
|
|
}
|
2009-10-06 12:42:55 -06:00
|
|
|
case "ustar \x00": // old GNU tar
|
2009-06-10 22:32:36 -06:00
|
|
|
format = "gnu";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case "posix", "gnu", "star":
|
|
|
|
hdr.Uname = cString(s.next(32));
|
|
|
|
hdr.Gname = cString(s.next(32));
|
|
|
|
devmajor := s.next(8);
|
|
|
|
devminor := s.next(8);
|
|
|
|
if hdr.Typeflag == TypeChar || hdr.Typeflag == TypeBlock {
|
|
|
|
hdr.Devmajor = tr.octal(devmajor);
|
|
|
|
hdr.Devminor = tr.octal(devminor);
|
|
|
|
}
|
|
|
|
var prefix string;
|
|
|
|
switch format {
|
|
|
|
case "posix", "gnu":
|
|
|
|
prefix = cString(s.next(155));
|
|
|
|
case "star":
|
|
|
|
prefix = cString(s.next(131));
|
|
|
|
hdr.Atime = tr.octal(s.next(12));
|
|
|
|
hdr.Ctime = tr.octal(s.next(12));
|
|
|
|
}
|
|
|
|
if len(prefix) > 0 {
|
|
|
|
hdr.Name = prefix + "/" + hdr.Name;
|
|
|
|
}
|
|
|
|
}
|
2009-06-09 00:22:56 -06:00
|
|
|
|
|
|
|
if tr.err != nil {
|
|
|
|
tr.err = HeaderError;
|
2009-10-06 12:42:55 -06: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.
|
|
|
|
tr.nb = int64(hdr.Size);
|
2009-10-06 12:42:55 -06:00
|
|
|
tr.pad = -tr.nb & (blockSize-1); // blockSize is a power of two
|
2009-06-09 00:22:56 -06:00
|
|
|
|
2009-10-06 12:42:55 -06:00
|
|
|
return hdr;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads from the current entry in the tar archive.
|
|
|
|
// It returns 0, nil when it reaches the end of that entry,
|
|
|
|
// until Next is called to advance to the next entry.
|
|
|
|
func (tr *Reader) Read(b []uint8) (n int, err os.Error) {
|
|
|
|
if int64(len(b)) > tr.nb {
|
2009-10-06 12:42:55 -06:00
|
|
|
b = b[0 : tr.nb];
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|
|
|
|
n, err = tr.r.Read(b);
|
|
|
|
tr.nb -= int64(n);
|
|
|
|
tr.err = err;
|
2009-10-06 12:42:55 -06:00
|
|
|
return;
|
2009-06-09 00:22:56 -06:00
|
|
|
}
|