Commit 51f3cbab authored by Guillaume J. Charmes's avatar Guillaume J. Charmes Committed by Ian Lance Taylor

archive/tar: Fix bug preventing untar

Do not use ustar format if we need the GNU one.
Change \000 to \x00 for consistency
Check for "ustar\x00" instead of "ustar\x00\x00" for conistency with tar
and compatiblity with archive generated with older code (which was ustar\x00\x20\x00)
Add test for long name + big file.

LGTM=iant
R=golang-codereviews, iant
CC=golang-codereviews
https://golang.org/cl/99050043
parent 8c2fefe8
...@@ -468,14 +468,14 @@ func (tr *Reader) readHeader() *Header { ...@@ -468,14 +468,14 @@ func (tr *Reader) readHeader() *Header {
// so its magic bytes, like the rest of the block, are NULs. // so its magic bytes, like the rest of the block, are NULs.
magic := string(s.next(8)) // contains version field as well. magic := string(s.next(8)) // contains version field as well.
var format string var format string
switch magic { switch {
case "ustar\x0000": // POSIX tar (1003.1-1988) case magic[:6] == "ustar\x00": // POSIX tar (1003.1-1988)
if string(header[508:512]) == "tar\x00" { if string(header[508:512]) == "tar\x00" {
format = "star" format = "star"
} else { } else {
format = "posix" format = "posix"
} }
case "ustar \x00": // old GNU tar case magic == "ustar \x00": // old GNU tar
format = "gnu" format = "gnu"
} }
......
...@@ -218,8 +218,8 @@ func (tw *Writer) writeHeader(hdr *Header, allowPax bool) error { ...@@ -218,8 +218,8 @@ func (tw *Writer) writeHeader(hdr *Header, allowPax bool) error {
tw.cString(prefixHeaderBytes, prefix, false, paxNone, nil) tw.cString(prefixHeaderBytes, prefix, false, paxNone, nil)
// Use the ustar magic if we used ustar long names. // Use the ustar magic if we used ustar long names.
if len(prefix) > 0 { if len(prefix) > 0 && !tw.usedBinary {
copy(header[257:265], []byte("ustar\000")) copy(header[257:265], []byte("ustar\x00"))
} }
} }
} }
......
...@@ -103,6 +103,29 @@ var writerTests = []*writerTest{ ...@@ -103,6 +103,29 @@ var writerTests = []*writerTest{
}, },
}, },
}, },
// The truncated test file was produced using these commands:
// dd if=/dev/zero bs=1048576 count=16384 > (longname/)*15 /16gig.txt
// tar -b 1 -c -f- (longname/)*15 /16gig.txt | dd bs=512 count=8 > writer-big-long.tar
{
file: "testdata/writer-big-long.tar",
entries: []*writerTestEntry{
{
header: &Header{
Name: strings.Repeat("longname/", 15) + "16gig.txt",
Mode: 0644,
Uid: 1000,
Gid: 1000,
Size: 16 << 30,
ModTime: time.Unix(1399583047, 0),
Typeflag: '0',
Uname: "guillaume",
Gname: "guillaume",
},
// fake contents
contents: strings.Repeat("\x00", 4<<10),
},
},
},
// This file was produced using gnu tar 1.17 // This file was produced using gnu tar 1.17
// gnutar -b 4 --format=ustar (longname/)*15 + file.txt // gnutar -b 4 --format=ustar (longname/)*15 + file.txt
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment