Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
os: make IsPermission, IsExist, and IsNotExist unwrap SyscallError too
Browse files Browse the repository at this point in the history
Fixes #12674

Change-Id: I82f53026dd2fc27bd7999d43c27932d683d92af6
Reviewed-on: https://go-review.googlesource.com/15730
Reviewed-by: Ian Lance Taylor <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
bradfitz committed Oct 10, 2015
1 parent 26074a6 commit 735c65f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/os/error_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
Expand All @@ -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")
}
Expand Down
19 changes: 19 additions & 0 deletions src/os/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions src/os/error_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions src/os/error_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ||
Expand All @@ -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
}

0 comments on commit 735c65f

Please sign in to comment.