mirror of
https://github.com/golang/go
synced 2024-11-25 14:37:56 -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}
|
return &SyscallError{syscall, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsExist returns whether the error is known to report that a file already exists.
|
// IsExist returns whether the error is known to report that a file or directory
|
||||||
// It is satisfied by ErrExist as well as some syscall errors.
|
// already exists. It is satisfied by ErrExist as well as some syscall errors.
|
||||||
func IsExist(err error) bool {
|
func IsExist(err error) bool {
|
||||||
return isExist(err)
|
return isExist(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsNotExist returns whether the error is known to report that a file does not exist.
|
// IsNotExist returns whether the error is known to report that a file or directory
|
||||||
// It is satisfied by ErrNotExist as well as some syscall errors.
|
// does not exist. It is satisfied by ErrNotExist as well as some syscall errors.
|
||||||
func IsNotExist(err error) bool {
|
func IsNotExist(err error) bool {
|
||||||
return isNotExist(err)
|
return isNotExist(err)
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,10 @@
|
|||||||
package os_test
|
package os_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,8 +26,56 @@ func TestErrIsExist(t *testing.T) {
|
|||||||
t.Fatal("Open should have failed")
|
t.Fatal("Open should have failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !os.IsExist(err) {
|
if s := checkErrorPredicate("os.IsExist", os.IsExist, err); s != "" {
|
||||||
t.Fatalf("os.IsExist does not work as expected for %#v", err)
|
t.Fatal(s)
|
||||||
return
|
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 {
|
if pe, ok := err.(*PathError); ok {
|
||||||
err = pe.Err
|
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
|
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,12 +18,13 @@ func isNotExist(err error) bool {
|
|||||||
if pe, ok := err.(*PathError); ok {
|
if pe, ok := err.(*PathError); ok {
|
||||||
err = pe.Err
|
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 {
|
func isPermission(err error) bool {
|
||||||
if pe, ok := err.(*PathError); ok {
|
if pe, ok := err.(*PathError); ok {
|
||||||
err = pe.Err
|
err = pe.Err
|
||||||
}
|
}
|
||||||
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
|
return err == ErrPermission
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user