Skip to content
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
Open

archive/zip: FileInfoHeader does not handle directories #24082

dsnet opened this issue Feb 23, 2018 · 5 comments
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@dsnet
Copy link
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 ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 13, 2018
@ianlancetaylor
Copy link
Contributor

@dsnet Please update milestone. Thanks.

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

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

@dsnet
Copy link
Member Author

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
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
Copy link
Contributor

@dsnet Is anything going to happen here for 1.12?

@dsnet
Copy link
Member Author

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
@ianlancetaylor ianlancetaylor added the early-in-cycle A change that should be done early in the 3 month dev cycle. label Nov 29, 2018
@andybons andybons modified the milestones: Go1.13, Go1.14 Jul 8, 2019
@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
early-in-cycle A change that should be done early in the 3 month dev cycle. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants