Commit ef805fe3 authored by Russ Cox's avatar Russ Cox

os: do not return Lstat errors from Readdir

This CL restores the Go 1.1.2 semantics for os.File's Readdir method.

The code in Go 1.1.2 was rewritten mainly because it looked buggy.
This new version attempts to be clearer but still provide the 1.1.2 results.

The important diff is not this CL's version against tip but this CL's version
against Go 1.1.2.

Go 1.1.2:

        names, err := f.Readdirnames(n)
        fi = make([]FileInfo, len(names))
        for i, filename := range names {
                fip, err := Lstat(dirname + filename)
                if err == nil {
                        fi[i] = fip
                } else {
                        fi[i] = &fileStat{name: filename}
                }
        }
        return fi, err

This CL:

        names, err := f.Readdirnames(n)
        fi = make([]FileInfo, len(names))
        for i, filename := range names {
                fip, lerr := lstat(dirname + filename)
                if lerr != nil {
                        fi[i] = &fileStat{name: filename}
                        continue
                }
                fi[i] = fip
        }
        return fi, err

The changes from Go 1.1.2 are stylistic, not semantic:
1. Use lstat instead of Lstat, for testing (done before this CL).
2. Make error handling in loop body look more like an error case.
3. Use separate error variable name in loop body, to be clear
   we are not trying to influence the final return result.

Fixes #6656.
Fixes #6680.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/18870043
parent b8be100d
...@@ -165,14 +165,11 @@ func (f *File) readdir(n int) (fi []FileInfo, err error) { ...@@ -165,14 +165,11 @@ func (f *File) readdir(n int) (fi []FileInfo, err error) {
fi = make([]FileInfo, len(names)) fi = make([]FileInfo, len(names))
for i, filename := range names { for i, filename := range names {
fip, lerr := lstat(dirname + filename) fip, lerr := lstat(dirname + filename)
if lerr == nil { if lerr != nil {
fi[i] = fip
} else {
fi[i] = &fileStat{name: filename} fi[i] = &fileStat{name: filename}
if err == nil { continue
err = lerr
}
} }
fi[i] = fip
} }
return fi, err return fi, err
} }
......
...@@ -92,8 +92,8 @@ func TestReaddirWithBadLstat(t *testing.T) { ...@@ -92,8 +92,8 @@ func TestReaddirWithBadLstat(t *testing.T) {
defer func() { *LstatP = Lstat }() defer func() { *LstatP = Lstat }()
dirs, err := handle.Readdir(-1) dirs, err := handle.Readdir(-1)
if err != ErrInvalid { if err != nil {
t.Fatalf("Expected Readdir to return ErrInvalid, got %v", err) t.Fatalf("Expected Readdir to return no error, got %v", err)
} }
foundfail := false foundfail := false
for _, dir := range dirs { for _, dir := range dirs {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment