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

os: file.Readdir(-1) stops reading on first file error #24581

Closed
BryceDFisher opened this issue Mar 28, 2018 · 3 comments
Closed

os: file.Readdir(-1) stops reading on first file error #24581

BryceDFisher opened this issue Mar 28, 2018 · 3 comments

Comments

@BryceDFisher
Copy link

@BryceDFisher BryceDFisher commented Mar 28, 2018

Please answer these questions before submitting your issue. Thanks!

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

go1.10

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN="/home/fisherb/go/bin"
GOCACHE="/home/fisherb/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/fisherb/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build933376266=/tmp/go-build -gno-record-gcc-switches"

What did you do?

The os.File.Readdir(-1) function returns an error and stops reading the contents of the directory on the first failure to Lstat an object in the directory. This is confusing since the os.File.Readdir(-1) function will still return the items in the directory that it had already gotten a status without an error.

This is happening to me when os.File.Readdir(-1) encounters a file in a directory that it does not have permission to stat. It rightly returns http: error reading directory: lstat 'full file path': permission denied, but I would expect it to continue iterating over the remaining files in the directory.

What did you expect to see?

I expected to see all of the items (files, links, directories, etc) in the directory being read that can be stated without error.

What did you see instead?

Only the files before the first error were returned. Any remaining files that would have been read without error were omitted.

@ianlancetaylor ianlancetaylor changed the title file.Readdir(-1) stops reading on first file error os: file.Readdir(-1) stops reading on first file error Mar 28, 2018
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 28, 2018

This behavior is explicitly documented. The workaround is to use Readdirnames. Since it's been documented this way since Go 1, we aren't going to change it now.

@BryceDFisher

This comment has been minimized.

Copy link
Author

@BryceDFisher BryceDFisher commented Mar 28, 2018

The problem is that many internal functions, such as the http.FileServer use os.File.Readdir(-1), which was my use case. In this case, I either have to completely make my own version of http.FileServer, or fix it here.

@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Mar 28, 2018

If there is a problem with http.FileServer, let's fix that.

I don't see how we can change the behavior of os.(*File).Readdir now.

@golang golang locked and limited conversation to collaborators Mar 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.