1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:14:43 -07:00

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

Fixes #12674

Change-Id: I82f53026dd2fc27bd7999d43c27932d683d92af6
Reviewed-on: https://go-review.googlesource.com/15730
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-10-10 04:15:48 +00:00
parent 26074a642f
commit 735c65fa9c
4 changed files with 37 additions and 0 deletions

View File

@ -12,6 +12,8 @@ func isExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), " exists")
}
@ -24,6 +26,8 @@ func isNotExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), "does not exist") || contains(err.Error(), "not found") ||
contains(err.Error(), "has been removed") || contains(err.Error(), "no parent")
@ -37,6 +41,8 @@ func isPermission(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return contains(err.Error(), "permission denied")
}

View File

@ -93,6 +93,8 @@ var isExistTests = []struct {
{&os.LinkError{Err: os.ErrPermission}, false, false},
{&os.LinkError{Err: os.ErrExist}, true, false},
{&os.LinkError{Err: os.ErrNotExist}, false, true},
{&os.SyscallError{Err: os.ErrNotExist}, false, true},
{&os.SyscallError{Err: os.ErrExist}, true, false},
{nil, false, false},
}
@ -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) {
f, err := ioutil.TempFile("", "_Go_ErrPathNUL\x00")
if err == nil {

View File

@ -16,6 +16,8 @@ func isExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.EEXIST || err == ErrExist
}
@ -28,6 +30,8 @@ func isNotExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.ENOENT || err == ErrNotExist
}
@ -40,6 +44,8 @@ func isPermission(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.EACCES || err == syscall.EPERM || err == ErrPermission
}

View File

@ -14,6 +14,8 @@ func isExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.ERROR_ALREADY_EXISTS ||
err == syscall.ERROR_FILE_EXISTS || err == ErrExist
@ -29,6 +31,8 @@ func isNotExist(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.ERROR_FILE_NOT_FOUND ||
err == _ERROR_BAD_NETPATH ||
@ -43,6 +47,8 @@ func isPermission(err error) bool {
err = pe.Err
case *LinkError:
err = pe.Err
case *SyscallError:
err = pe.Err
}
return err == syscall.ERROR_ACCESS_DENIED || err == ErrPermission
}