mirror of
https://github.com/golang/go
synced 2024-11-19 15:44:44 -07:00
archive/tar: properly handle header-only "files" in Writer
Certain special type-flags, specifically 1, 2, 3, 4, 5, 6, do not have a data section. Thus, regardless of what the size field says, we should not attempt to write any data for these special types. The relevant PAX and USTAR specification says: <<< If the typeflag field is set to specify a file to be of type 1 (a link) or 2 (a symbolic link), the size field shall be specified as zero. If the typeflag field is set to specify a file of type 5 (directory), the size field shall be interpreted as described under the definition of that record type. No data logical records are stored for types 1, 2, or 5. If the typeflag field is set to 3 (character special file), 4 (block special file), or 6 (FIFO), the meaning of the size field is unspecified by this volume of POSIX.1-2008, and no data logical records shall be stored on the medium. Additionally, for type 6, the size field shall be ignored when reading. If the typeflag field is set to any other value, the number of logical records written following the header shall be (size+511)/512, ignoring any fraction in the result of the division. >>> Fixes #15565 Change-Id: Id11886b723b3b13deb15221dca51c25cd778a6b5 Reviewed-on: https://go-review.googlesource.com/55553 Reviewed-by: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
17fa5a7c9f
commit
e098e5142d
@ -94,7 +94,7 @@ func (tw *Writer) writeUSTARHeader(hdr *Header) error {
|
||||
if f.err != nil {
|
||||
return f.err // Should never happen since header is validated
|
||||
}
|
||||
return tw.writeRawHeader(blk, hdr.Size)
|
||||
return tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag)
|
||||
}
|
||||
|
||||
func (tw *Writer) writePAXHeader(hdr *Header, paxHdrs map[string]string) error {
|
||||
@ -133,7 +133,7 @@ func (tw *Writer) writePAXHeader(hdr *Header, paxHdrs map[string]string) error {
|
||||
if f.err != nil && len(paxHdrs) == 0 {
|
||||
return f.err // Should never happen, otherwise PAX headers would be used
|
||||
}
|
||||
return tw.writeRawHeader(blk, hdr.Size)
|
||||
return tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag)
|
||||
}
|
||||
|
||||
func (tw *Writer) writeGNUHeader(hdr *Header) error {
|
||||
@ -151,7 +151,7 @@ func (tw *Writer) writeGNUHeader(hdr *Header) error {
|
||||
if f.err != nil {
|
||||
return f.err // Should never happen since header is validated
|
||||
}
|
||||
return tw.writeRawHeader(blk, hdr.Size)
|
||||
return tw.writeRawHeader(blk, hdr.Size, hdr.Typeflag)
|
||||
}
|
||||
|
||||
type (
|
||||
@ -219,7 +219,7 @@ func (tw *Writer) writeRawFile(name, data string, flag byte, format int) error {
|
||||
}
|
||||
|
||||
// Write the header and data.
|
||||
if err := tw.writeRawHeader(&tw.blk, int64(len(data))); err != nil {
|
||||
if err := tw.writeRawHeader(&tw.blk, int64(len(data)), flag); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := io.WriteString(tw, data)
|
||||
@ -228,15 +228,17 @@ func (tw *Writer) writeRawFile(name, data string, flag byte, format int) error {
|
||||
|
||||
// writeRawHeader writes the value of blk, regardless of its value.
|
||||
// It sets up the Writer such that it can accept a file of the given size.
|
||||
func (tw *Writer) writeRawHeader(blk *block, size int64) error {
|
||||
// If the flag is a special header-only flag, then the size is treated as zero.
|
||||
func (tw *Writer) writeRawHeader(blk *block, size int64, flag byte) error {
|
||||
if err := tw.Flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tw.w.Write(blk[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO(dsnet): Set Size implicitly to zero for header-only entries.
|
||||
// See https://golang.org/issue/15565
|
||||
if isHeaderOnlyType(flag) {
|
||||
size = 0
|
||||
}
|
||||
tw.nb = size
|
||||
tw.pad = -size & (blockSize - 1) // blockSize is a power of two
|
||||
return nil
|
||||
|
@ -553,6 +553,25 @@ func TestValidTypeflagWithPAXHeader(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteHeaderOnly(t *testing.T) {
|
||||
tw := NewWriter(new(bytes.Buffer))
|
||||
hdr := &Header{Name: "dir/", Typeflag: TypeDir}
|
||||
if err := tw.WriteHeader(hdr); err != nil {
|
||||
t.Fatalf("WriteHeader() = %v, want nil", err)
|
||||
}
|
||||
if _, err := tw.Write([]byte{0x00}); err != ErrWriteTooLong {
|
||||
t.Fatalf("Write() = %v, want %v", err, ErrWriteTooLong)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteNegativeSize(t *testing.T) {
|
||||
tw := NewWriter(new(bytes.Buffer))
|
||||
hdr := &Header{Name: "small.txt", Size: -1}
|
||||
if err := tw.WriteHeader(hdr); err != ErrHeader {
|
||||
t.Fatalf("WriteHeader() = nil, want %v", ErrHeader)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteAfterClose(t *testing.T) {
|
||||
var buffer bytes.Buffer
|
||||
tw := NewWriter(&buffer)
|
||||
|
Loading…
Reference in New Issue
Block a user