Skip to content

archive/zip: Differentiate ReadDir result types #43872

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

Closed
cugu opened this issue Jan 23, 2021 · 5 comments
Closed

archive/zip: Differentiate ReadDir result types #43872

cugu opened this issue Jan 23, 2021 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@cugu
Copy link

cugu commented Jan 23, 2021

What version of Go are you using (go version)?

Current HEAD

$ go version
go version devel +cd99385ff4 Sat Jan 23 12:38:15 2021 +0000 darwin/amd64

What did you do?

reader, _ := zip.OpenReader("test.zip")
root, _ := reader.Open(".")
entries, _ := root.(fs.ReadDirFile).ReadDir(0)
for _, entry := range entries {
    fmt.Printf("%s: %T\n", entry.Name(), entry)
}

Results in:

folder: *zip.fileListEntry
folder: zip.headerFileInfo

I understand that you might want both results (s. #40354), so my question is:

Is there a good way to differentiate those internal types, if you just need one?

It escalates a little with fs.WalkDir:

reader, _ := zip.OpenReader("test.zip")
fs.WalkDir(reader, ".", func(path string, d fs.DirEntry, err error) error {
    fmt.Println(path)
    return nil
})
folder
folder/file.txt
folder/subfolder
folder/subfolder/subfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder
folder/subfolder/subfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder
folder/file.txt
folder/subfolder
folder/subfolder/subfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder
folder/subfolder/subfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt
folder/subfolder/subsubfolder
folder/subfolder/subsubfolder/subsubfile.txt

@rsc

@cugu
Copy link
Author

cugu commented Jan 24, 2021

Ok this seem like an issue, fs.Stat chooses one of both types at random.

for i := 0; i < 10; i++ {
	reader, _ := zip.OpenReader("go1.15.7.windows-386.zip")
	info, _ := fs.Stat(reader, "go")
	fmt.Printf("%T\n", info)
}

Results in:

zip.headerFileInfo
*zip.fileListEntry
zip.headerFileInfo
*zip.fileListEntry
zip.headerFileInfo
*zip.fileListEntry
*zip.fileListEntry
*zip.fileListEntry
*zip.fileListEntry
zip.headerFileInfo

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 24, 2021
@seankhliao
Copy link
Member

I think there should be a continue in the inner for loop?

for _, file := range r.File {
name := toValidName(file.Name)
for dir := path.Dir(name); dir != "."; dir = path.Dir(dir) {
dirs[dir] = true
}
r.fileList = append(r.fileList, fileListEntry{name, file})
}
for dir := range dirs {
r.fileList = append(r.fileList, fileListEntry{dir + "/", nil})
}

@cugu
Copy link
Author

cugu commented Jan 24, 2021

zip.headerFileInfo objects do have a meaningful ModTime() while *zip.fileListEntry do not. So I would prefer to get the zip.headerFileInfo objects on default and maybe have another method for *zip.fileListEntry?

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/311530 mentions this issue: archive/zip: only return directory once via io/fs.FS

@gopherbot
Copy link
Contributor

Change https://golang.org/cl/315249 mentions this issue: [release-branch.go1.16] archive/zip: only return directory once via io/fs.FS

gopherbot pushed a commit that referenced this issue Apr 30, 2021

Verified

This commit was signed with the committer’s verified signature. The key has expired.
LnL7 Daiderd Jordan
…o/fs.FS

While we're here fix the ModTime value for directories.

For #43872
For #45345
Fixes #45347

Change-Id: I155e6517713ef6a9482b9431f1167a44337c6ad2
Reviewed-on: https://go-review.googlesource.com/c/go/+/311530
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
(cherry picked from commit 87e4dcd)
Reviewed-on: https://go-review.googlesource.com/c/go/+/315249
Trust: Jeremy Faller <jeremy@golang.org>
@golang golang locked and limited conversation to collaborators Apr 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

3 participants