Skip to content

net/http: Readdir method of DotFileHiding example does not conform to the doc #67697

@mkch

Description

@mkch

Go version

go version go1.22.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\user\AppData\Local\go-build
set GOENV=C:\Users\user\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\user\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\user\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\user\code\dotFileHidingFile\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\user\AppData\Local\Temp\go-build416355838=/tmp/go-build -gno-record-gcc-switches

What did you do?

Visit https://pkg.go.dev/net/http#example-FileServer-DotFileHiding

What did you see happen?

func (f dotFileHidingFile) Readdir(n int) (fis []fs.FileInfo, err error) {
	files, err := f.File.Readdir(n)
	for _, file := range files { // Filters out the dot files
		if !strings.HasPrefix(file.Name(), ".") {
			fis = append(fis, file)
		}
	}
	return
}

The implementation of dotFileHidingFile.Readdir method does not conform to the doc of *os.File.ReadDir:

If n > 0 ... if Readdir returns an empty slice, it will return a non-nil error explaining why ...

If n>0 and f.File.Readdir(n) returns dot files only, fis will be an empty slice while err is nil(not a non-nil error explaining why).

What did you expect to see?

Something like

func (f dotFileHidingFile) Readdir(n int) (fis []fs.FileInfo, err error) {
	var files []fs.FileInfo
	for {
		files, err = f.File.Readdir(n)
		// Filters out the dot files
		fis = slices.DeleteFunc(files, func(file fs.FileInfo) bool {
			return strings.HasPrefix(file.Name(), ".")
		})
		if err != nil || n <= 0 || (n > 0 && len(fis) > 0) {
			break
		}
	}
	return
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions