1
0
mirror of https://github.com/golang/go synced 2024-11-23 16:10:05 -07:00

archive/zip: fix regression when writing directories

Several adjustments:

1) When encoding the FileHeader for a directory, explicitly set all of the sizes
to zero regardless of their prior values. These values are currently populated
by FileInfoHeader as it calls os.FileInfo.Size regardless of whether the file is
a directory or not. We avoid fixing FileInfoHeader now as it is too late in the
release cycle (see #24082).

We silently adjust slightly wrong FileHeader fields as opposed to returning
an error because the CreateHeader method already does such mutations
(e.g., for UTF-8 detection, data descriptor, etc).

2) Have dirWriter.Write only return an error if some number of bytes are written.
Some code still call Write for both normal files and directories, but just pass
an empty []byte to Write for directories.

Change-Id: I85492a31356107fcf76dc89ceb00a28853754289
Reviewed-on: https://go-review.googlesource.com/124155
Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Joe Tsai 2018-07-16 13:05:25 -07:00 committed by Joe Tsai
parent c80897b691
commit 4b0c8b0136
2 changed files with 20 additions and 4 deletions

View File

@ -336,6 +336,12 @@ func (w *Writer) CreateHeader(fh *FileHeader) (io.Writer, error) {
fh.Method = Store
fh.Flags &^= 0x8 // we will not write a data descriptor
// Explicitly clear sizes as they have no meaning for directories.
fh.CompressedSize = 0
fh.CompressedSize64 = 0
fh.UncompressedSize = 0
fh.UncompressedSize64 = 0
ow = dirWriter{}
} else {
fh.Flags |= 0x8 // we will write a data descriptor
@ -419,7 +425,10 @@ func (w *Writer) compressor(method uint16) Compressor {
type dirWriter struct{}
func (dirWriter) Write([]byte) (int, error) {
func (dirWriter) Write(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
return 0, errors.New("zip: write to directory")
}

View File

@ -306,21 +306,28 @@ func TestWriterDir(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if _, err := dw.Write(nil); err != nil {
t.Errorf("Write(nil) to directory: got %v, want nil", err)
}
if _, err := dw.Write([]byte("hello")); err == nil {
t.Error("Write to directory: got nil error, want non-nil")
t.Error(`Write("hello") to directory: got nil error, want non-nil`)
}
}
func TestWriterDirAttributes(t *testing.T) {
var buf bytes.Buffer
w := NewWriter(&buf)
if _, err := w.Create("dir/"); err != nil {
if _, err := w.CreateHeader(&FileHeader{
Name: "dir/",
Method: Deflate,
CompressedSize64: 1234,
UncompressedSize64: 5678,
}); err != nil {
t.Fatal(err)
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
b := buf.Bytes()
var sig [4]byte