mirror of
https://github.com/golang/go
synced 2024-11-25 09:17:57 -07:00
os: IsNotExist() should also consider ERROR_PATH_NOT_FOUND on Windows
Also update documentation about IsExist() and IsNotExist(), they are not about files only. R=rsc CC=golang-dev https://golang.org/cl/5794073
This commit is contained in:
parent
d6ea81e0b9
commit
24ed667b33
@ -43,14 +43,14 @@ func NewSyscallError(syscall string, err error) error {
|
||||
return &SyscallError{syscall, err}
|
||||
}
|
||||
|
||||
// IsExist returns whether the error is known to report that a file already exists.
|
||||
// It is satisfied by ErrExist as well as some syscall errors.
|
||||
// IsExist returns whether the error is known to report that a file or directory
|
||||
// already exists. It is satisfied by ErrExist as well as some syscall errors.
|
||||
func IsExist(err error) bool {
|
||||
return isExist(err)
|
||||
}
|
||||
|
||||
// IsNotExist returns whether the error is known to report that a file does not exist.
|
||||
// It is satisfied by ErrNotExist as well as some syscall errors.
|
||||
// IsNotExist returns whether the error is known to report that a file or directory
|
||||
// does not exist. It is satisfied by ErrNotExist as well as some syscall errors.
|
||||
func IsNotExist(err error) bool {
|
||||
return isNotExist(err)
|
||||
}
|
||||
|
@ -5,8 +5,10 @@
|
||||
package os_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -24,8 +26,56 @@ func TestErrIsExist(t *testing.T) {
|
||||
t.Fatal("Open should have failed")
|
||||
return
|
||||
}
|
||||
if !os.IsExist(err) {
|
||||
t.Fatalf("os.IsExist does not work as expected for %#v", err)
|
||||
if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
|
||||
t.Fatal(s)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func testErrNotExist(name string) string {
|
||||
f, err := os.Open(name)
|
||||
if err == nil {
|
||||
f.Close()
|
||||
return "Open should have failed"
|
||||
}
|
||||
if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
|
||||
return s
|
||||
}
|
||||
|
||||
err = os.Chdir(name)
|
||||
if err == nil {
|
||||
return "Chdir should have failed"
|
||||
}
|
||||
if s := checkErrorPredicate("os.IsNotExist", os.IsNotExist, err); s != "" {
|
||||
return s
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestErrIsNotExist(t *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "_Go_ErrIsNotExist")
|
||||
if err != nil {
|
||||
t.Fatalf("create ErrIsNotExist tempdir: %s", err)
|
||||
return
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
name := filepath.Join(tmpDir, "NotExists")
|
||||
if s := testErrNotExist(name); s != "" {
|
||||
t.Fatal(s)
|
||||
return
|
||||
}
|
||||
|
||||
name = filepath.Join(name, "NotExists2")
|
||||
if s := testErrNotExist(name); s != "" {
|
||||
t.Fatal(s)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func checkErrorPredicate(predName string, pred func(error) bool, err error) string {
|
||||
if !pred(err) {
|
||||
return fmt.Sprintf("%s does not work as expected for %#v", predName, err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ func isExist(err error) bool {
|
||||
if pe, ok := err.(*PathError); ok {
|
||||
err = pe.Err
|
||||
}
|
||||
return err == syscall.EEXIST || err == syscall.ERROR_ALREADY_EXISTS ||
|
||||
return err == syscall.ERROR_ALREADY_EXISTS ||
|
||||
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
|
||||
}
|
||||
|
||||
@ -18,12 +18,13 @@ func isNotExist(err error) bool {
|
||||
if pe, ok := err.(*PathError); ok {
|
||||
err = pe.Err
|
||||
}
|
||||
return err == syscall.ENOENT || err == ErrNotExist
|
||||
return err == syscall.ERROR_FILE_NOT_FOUND ||
|
||||
err == syscall.ERROR_PATH_NOT_FOUND || err == ErrNotExist
|
||||
}
|
||||
|
||||
func isPermission(err error) bool {
|
||||
if pe, ok := err.(*PathError); ok {
|
||||
err = pe.Err
|
||||
}
|
||||
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
|
||||
return err == ErrPermission
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user