os: convert poll.ErrFileClosed to ErrClosed for Stat

Fixes #66665

Change-Id: I3e3b7433d245daa997d7d502c2ef8978af6664fb
Reviewed-on: https://go-review.googlesource.com/c/go/+/576119
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Ian Lance Taylor 2024-04-03 13:33:18 -07:00 committed by Gopher Robot
parent 885fdfc082
commit 968e1ce1e4
2 changed files with 25 additions and 1 deletions

View file

@ -5,6 +5,7 @@
package os_test
import (
"errors"
"internal/testenv"
"io/fs"
"os"
@ -337,3 +338,26 @@ func TestStatConsole(t *testing.T) {
testStatAndLstat(t, `\\.\`+name, params)
}
}
func TestClosedStat(t *testing.T) {
// Historically we do not seem to match ErrClosed on non-Unix systems.
switch runtime.GOOS {
case "windows", "plan9":
t.Skipf("skipping on %s", runtime.GOOS)
}
t.Parallel()
f, err := os.Open("testdata/hello")
if err != nil {
t.Fatal(err)
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
_, err = f.Stat()
if err == nil {
t.Error("Stat succeeded on closed File")
} else if !errors.Is(err, os.ErrClosed) {
t.Errorf("error from Stat on closed file did not match ErrClosed: %q, type %T", err, err)
}
}

View file

@ -19,7 +19,7 @@ func (f *File) Stat() (FileInfo, error) {
var fs fileStat
err := f.pfd.Fstat(&fs.sys)
if err != nil {
return nil, &PathError{Op: "stat", Path: f.name, Err: err}
return nil, f.wrapErr("stat", err)
}
fillFileStatFromSys(&fs, f.name)
return &fs, nil