From 4b0c8b0136ee54e779b9eddf7ba00f6ecfeb9f6f Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Mon, 16 Jul 2018 13:05:25 -0700 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/archive/zip/writer.go | 11 ++++++++++- src/archive/zip/writer_test.go | 13 ++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/archive/zip/writer.go b/src/archive/zip/writer.go index 506148ee300..5f0c0a1a555 100644 --- a/src/archive/zip/writer.go +++ b/src/archive/zip/writer.go @@ -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") } diff --git a/src/archive/zip/writer_test.go b/src/archive/zip/writer_test.go index 468424c72ae..1fedfd85e82 100644 --- a/src/archive/zip/writer_test.go +++ b/src/archive/zip/writer_test.go @@ -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