Commit 735c65fa authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

os: make IsPermission, IsExist, and IsNotExist unwrap SyscallError too

Fixes #12674

Change-Id: I82f53026dd2fc27bd7999d43c27932d683d92af6
Reviewed-on: https://go-review.googlesource.com/15730Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 26074a64
...@@ -12,6 +12,8 @@ func isExist(err error) bool { ...@@ -12,6 +12,8 @@ func isExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return contains(err.Error(), " exists") return contains(err.Error(), " exists")
} }
...@@ -24,6 +26,8 @@ func isNotExist(err error) bool { ...@@ -24,6 +26,8 @@ func isNotExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return contains(err.Error(), "does not exist") || contains(err.Error(), "not found") || return contains(err.Error(), "does not exist") || contains(err.Error(), "not found") ||
contains(err.Error(), "has been removed") || contains(err.Error(), "no parent") contains(err.Error(), "has been removed") || contains(err.Error(), "no parent")
...@@ -37,6 +41,8 @@ func isPermission(err error) bool { ...@@ -37,6 +41,8 @@ func isPermission(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return contains(err.Error(), "permission denied") return contains(err.Error(), "permission denied")
} }
......
...@@ -93,6 +93,8 @@ var isExistTests = []struct { ...@@ -93,6 +93,8 @@ var isExistTests = []struct {
{&os.LinkError{Err: os.ErrPermission}, false, false}, {&os.LinkError{Err: os.ErrPermission}, false, false},
{&os.LinkError{Err: os.ErrExist}, true, false}, {&os.LinkError{Err: os.ErrExist}, true, false},
{&os.LinkError{Err: os.ErrNotExist}, false, true}, {&os.LinkError{Err: os.ErrNotExist}, false, true},
{&os.SyscallError{Err: os.ErrNotExist}, false, true},
{&os.SyscallError{Err: os.ErrExist}, true, false},
{nil, false, false}, {nil, false, false},
} }
...@@ -107,6 +109,23 @@ func TestIsExist(t *testing.T) { ...@@ -107,6 +109,23 @@ func TestIsExist(t *testing.T) {
} }
} }
var isPermissionTests = []struct {
err error
want bool
}{
{nil, false},
{&os.PathError{Err: os.ErrPermission}, true},
{&os.SyscallError{Err: os.ErrPermission}, true},
}
func TestIsPermission(t *testing.T) {
for _, tt := range isPermissionTests {
if got := os.IsPermission(tt.err); got != tt.want {
t.Errorf("os.IsPermission(%#v) = %v; want %v", tt.err, got, tt.want)
}
}
}
func TestErrPathNUL(t *testing.T) { func TestErrPathNUL(t *testing.T) {
f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00") f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
if err == nil { if err == nil {
......
...@@ -16,6 +16,8 @@ func isExist(err error) bool { ...@@ -16,6 +16,8 @@ func isExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return err == syscall.EEXIST || err == ErrExist return err == syscall.EEXIST || err == ErrExist
} }
...@@ -28,6 +30,8 @@ func isNotExist(err error) bool { ...@@ -28,6 +30,8 @@ func isNotExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return err == syscall.ENOENT || err == ErrNotExist return err == syscall.ENOENT || err == ErrNotExist
} }
...@@ -40,6 +44,8 @@ func isPermission(err error) bool { ...@@ -40,6 +44,8 @@ func isPermission(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
} }
...@@ -14,6 +14,8 @@ func isExist(err error) bool { ...@@ -14,6 +14,8 @@ func isExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return 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
...@@ -29,6 +31,8 @@ func isNotExist(err error) bool { ...@@ -29,6 +31,8 @@ func isNotExist(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return err == syscall.ERROR_FILE_NOT_FOUND || return err == syscall.ERROR_FILE_NOT_FOUND ||
err == _ERROR_BAD_NETPATH || err == _ERROR_BAD_NETPATH ||
...@@ -43,6 +47,8 @@ func isPermission(err error) bool { ...@@ -43,6 +47,8 @@ func isPermission(err error) bool {
err = pe.Err err = pe.Err
case *LinkError: case *LinkError:
err = pe.Err err = pe.Err
case *SyscallError:
err = pe.Err
} }
return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission
} }
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