New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

archive/zip: FileInfoHeader does not handle directories #24082

Open
dsnet opened this Issue Feb 23, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@dsnet
Member

dsnet commented Feb 23, 2018

Consider the following:

fi, _ := os.Stat("/home/")
h, _ := zip.FileInfoHeader(fi)
fmt.Println(h.Name)

This currently prints:

home

However, ZIP treats directories as files with a trailing "/" in the name, which FileInfoHeader should automatically append if os.FileInfo.IsDir reports true.

This difference is minor since users are usually expected to replace the FileHeader.Name shortly afterwards anyways.

@dsnet dsnet added this to the Go1.11 milestone Feb 23, 2018

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Jun 13, 2018

@dsnet Please update milestone. Thanks.

@dsnet dsnet modified the milestones: Go1.11, Unplanned Jun 13, 2018

@gopherbot

This comment has been minimized.

gopherbot commented Jul 16, 2018

Change https://golang.org/cl/124155 mentions this issue: archive/zip: fix regression when writing directories

@dsnet

This comment has been minimized.

Member

dsnet commented Jul 16, 2018

We should fix this. I found several cases where users are explicitly doing something like this:

fh, err := zip.FileInfoHeader(fi)
if err != nil {
    return err
}
if fi.IsDir() {
    fh.Name += "/"
}

Also, per CL/124155, the function should avoid setting the size for directories.

EDIT: Will have to think about what happens with existing code, since this will result in "/" appended twice.

@dsnet dsnet modified the milestones: Unplanned, Go1.12 Jul 16, 2018

gopherbot pushed a commit that referenced this issue Jul 16, 2018

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>
@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Nov 29, 2018

@dsnet Is anything going to happen here for 1.12?

@dsnet

This comment has been minimized.

Member

dsnet commented Nov 29, 2018

This change has a risk of breaking people. Too late for 1.12. We'll submit early 1.13.

@dsnet dsnet modified the milestones: Go1.12, Go1.13 Nov 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment