summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShenghou Ma <minux.ma@gmail.com>2012-03-14 23:54:40 +0800
committerShenghou Ma <minux.ma@gmail.com>2012-03-14 23:54:40 +0800
commita68a242c0236501299d703c74edb202013935981 (patch)
treefef37c4a6c9d7bff6622c75d42b073fe66691c65
parent8265bd30231a03a3f5d3c5725efa2c1e4fbc49d4 (diff)
downloadgo-a68a242c0236501299d703c74edb202013935981.tar.gz
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 http://codereview.appspot.com/5794073
-rw-r--r--src/pkg/os/error.go8
-rw-r--r--src/pkg/os/error_test.go54
-rw-r--r--src/pkg/os/error_windows.go7
3 files changed, 60 insertions, 9 deletions
diff --git a/src/pkg/os/error.go b/src/pkg/os/error.go
index 54c2dc639..b88e49400 100644
--- a/src/pkg/os/error.go
+++ b/src/pkg/os/error.go
@@ -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)
}
diff --git a/src/pkg/os/error_test.go b/src/pkg/os/error_test.go
index 8218f861a..42f846fa3 100644
--- a/src/pkg/os/error_test.go
+++ b/src/pkg/os/error_test.go
@@ -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 ""
+}
diff --git a/src/pkg/os/error_windows.go b/src/pkg/os/error_windows.go
index b8b894b5a..5d692b073 100644
--- a/src/pkg/os/error_windows.go
+++ b/src/pkg/os/error_windows.go
@@ -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
}