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: Differentiate ReadDir result types #43872

Open
cugu opened this issue Jan 23, 2021 · 4 comments
Open

archive/zip: Differentiate ReadDir result types #43872

cugu opened this issue Jan 23, 2021 · 4 comments

Comments

@cugu
Copy link

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

@seankhliao seankhliao commented Jan 24, 2021

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

@gopherbot gopherbot commented Apr 19, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants