-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
io/ioutil: ReadDir if long-path ends in backslash on Windows #17500
Comments
Try to display the filename package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Need directory as parameter")
return
}
dir := os.Args[1]
entries, err := ioutil.ReadDir(dir)
for _, f := range entries {
fmt.Println(f.Name())
}
fmt.Println("err", err)
} |
FWIW, "\?" is not a UNC path, but the long path prefix. Dup of #3358? |
\\?\c:\
root directories on Windows
I'm inclined to say this is WAI. The Windows documentation is pretty clear that ?\ turns off a bunch of path handling code; it looks like one of the things it turns off is stripping of the trailing slash character. I don't think Go should do anything above and beyond what the underlying system calls do to handle malformed paths. /cc @rsc @alexbrainman |
@ncw I cannot reproduce it here too. It displays files and returns no error on my Windows XP (386) and Windows 7 (amd64). I am using current tip, but I doubt it matters. Perhaps you could try and debug this. Thank you. Alex |
I've done a bit more testing. I can reproduce the problem on my other Windows 7 Pro 386 SP1 VM. On windows XP I seem to get the opposite results where However as @quentinmit suggested it does seem to be just whether you supply a trailing I've been unable to find a definitive answer as to whether trailing Maybe that is as it should be and Go shouldn't try and fixup Windows syscalls. |
I can reproduce that, thank you very much. The problem appears to be with the way we open file or directory. Windows does not have generic API that opens both. Instead we always try to open path as file first (see os.openFile function), and, if that fails, we assume we have directory here (os.openDir). Our logic assumes that directory cannot be opened with "file opening" API CreateFile, and that seems worked so far. But not in the issue above. We could, probably, "fix" this by adding some extra checks after CreateFile returns (we could try and call GetFileInformationByHandle, even check returned parameter FileAttributes field if succeeds). But, I think, we should avoid using long paths (#10577 (comment)) in general, so adding extra code for every file open does not sounds right to me. Alex |
I also agree it's WAI.
In general, we should figure out a complete story for handling
extended length paths in various Go APIs before changing
anything.
|
I think this is working as intended, but to the extent that it informs what @quentinmit's patch for long file names can do, it's relevant. At the least we should have tests for ReadDir of |
After spending days banging my head on this, I think I understand what's happening here. This is WAI; if you use a |
Ack, reading more closely, I see that a couple of you have reported that you need to omit the trailing slash even for the root directory. I'll keep this issue open until I've figured out under what conditions that is true. |
Okay, I think I have an explanation for the behavior you observed. Note that you do need a trailing slash on the root directory or |
CL https://golang.org/cl/32451 mentions this issue. |
What version of Go are you using (
go version
)?X:>go version
go version go1.7.1 windows/386
What operating system and processor architecture are you using (
go env
)?I am testing this on Windows 7 Pro 386 SP1 running under VirtualBox.
What did you do?
Build this program http://play.golang.org/p/rBGf11wDo7 (note that this is from #4601) and run it giving UNC paths to the root directory.
What did you expect to see?
I expected
readdir.exe \\?\c:\
to produce an output with some directory entries, not an error. It should have produced the same output as provided byreaddir.exe \\?\c:
What did you see instead?
The error
err open \\?\c:\: The system cannot find the path specified.
Note that this is possibly related to #4601
The text was updated successfully, but these errors were encountered: