From 4d66163297403e1f4a85fa9601886eae31f551ac Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 13 Aug 2015 15:32:17 -0400 Subject: [PATCH] archive/tar: a []byte copy needed for GNU LongLink --- archive/tar/reader.go | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/archive/tar/reader.go b/archive/tar/reader.go index f817956..c72e002 100644 --- a/archive/tar/reader.go +++ b/archive/tar/reader.go @@ -154,60 +154,48 @@ func (tr *Reader) Next() (*Header, error) { } return hdr, nil case TypeGNULongName: - var b *bytes.Buffer - if tr.RawAccounting { - b = bytes.NewBuffer(tr.RawBytes()) - } // 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 } + var buf []byte if tr.RawAccounting { - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { - return nil, err - } if _, err = tr.rawBytes.Write(realname); err != nil { return nil, err } - b.Reset() - b.Write(tr.RawBytes()) + buf = make([]byte, tr.rawBytes.Len()) + copy(buf[:], tr.RawBytes()) } hdr, err := tr.Next() // since the above call to Next() resets the buffer, we need to throw the bytes over if tr.RawAccounting { - b.Write(tr.RawBytes()) - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { + buf = append(buf, tr.RawBytes()...) + if _, err = tr.rawBytes.Write(buf); err != nil { return nil, err } } hdr.Name = cString(realname) return hdr, err case TypeGNULongLink: - var b *bytes.Buffer - if tr.RawAccounting { - b = bytes.NewBuffer(tr.RawBytes()) - } // We have a GNU long link header. realname, err := ioutil.ReadAll(tr) if err != nil { return nil, err } + var buf []byte if tr.RawAccounting { - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { - return nil, err - } if _, err = tr.rawBytes.Write(realname); err != nil { return nil, err } - b.Reset() - b.Write(tr.RawBytes()) + buf = make([]byte, tr.rawBytes.Len()) + copy(buf[:], tr.RawBytes()) } hdr, err := tr.Next() // since the above call to Next() resets the buffer, we need to throw the bytes over if tr.RawAccounting { - b.Write(tr.RawBytes()) - if _, err = tr.rawBytes.Write(b.Bytes()); err != nil { + buf = append(buf, tr.RawBytes()...) + if _, err = tr.rawBytes.Write(buf); err != nil { return nil, err } }