Skip to content

Commit

Permalink
syscall: let ENOSYS, ENOTSUP and EOPNOTSUPP implement errors.ErrUnsup…
Browse files Browse the repository at this point in the history
…ported

As suggested by Bryan, also update (Errno).Is on windows to include the
missing oserror cases that are covered on other platforms.

Quoting Bryan:
> Windows syscalls don't actually return those errors, but the dummy Errno
> constants defined on Windows should still have the same meaning as on
> Unix.

Updates #41198

Change-Id: I15441abde4a7ebaa3c6518262c052530cd2add4b
Reviewed-on: https://go-review.googlesource.com/c/go/+/476875
TryBot-Result: Gopher Robot <[email protected]>
Reviewed-by: Bryan Mills <[email protected]>
Run-TryBot: Tobias Klauser <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
tklauser committed Mar 16, 2023
1 parent 8377f20 commit 75c2e97
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
5 changes: 0 additions & 5 deletions src/internal/testenv/testenv_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ func syscallIsNotSupported(err error) bool {
var errno syscall.Errno
if errors.As(err, &errno) {
switch errno {
case syscall.ENOSYS, syscall.ENOTSUP:
// Explicitly not supported.
// TODO(#41198): remove these cases when errors.Is reports that they are
// equivalent to ErrUnsupported.
return true
case syscall.EPERM, syscall.EROFS:
// User lacks permission: either the call requires root permission and the
// user is not root, or the call is denied by a container security policy.
Expand Down
7 changes: 5 additions & 2 deletions src/syscall/syscall_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package syscall

import (
errorspkg "errors"
"internal/itoa"
"internal/oserror"
"sync"
Expand Down Expand Up @@ -47,8 +48,8 @@ const PathMax = 256
// err = errno
// }
//
// Errno values can be tested against error values from the os package
// using errors.Is. For example:
// Errno values can be tested against error values using errors.Is.
// For example:
//
// _, _, err := syscall.Syscall(...)
// if errors.Is(err, fs.ErrNotExist) ...
Expand All @@ -72,6 +73,8 @@ func (e Errno) Is(target error) bool {
return e == EEXIST || e == ENOTEMPTY
case oserror.ErrNotExist:
return e == ENOENT
case errorspkg.ErrUnsupported:
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
}
return false
}
Expand Down
4 changes: 2 additions & 2 deletions src/syscall/syscall_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const bitSize16 = 2

// ErrorString implements Error's String method by returning itself.
//
// ErrorString values can be tested against error values from the os package
// using errors.Is. For example:
// ErrorString values can be tested against error values using errors.Is.
// For example:
//
// _, _, err := syscall.Syscall(...)
// if errors.Is(err, fs.ErrNotExist) ...
Expand Down
7 changes: 5 additions & 2 deletions src/syscall/syscall_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package syscall

import (
errorspkg "errors"
"internal/bytealg"
"internal/itoa"
"internal/oserror"
Expand Down Expand Up @@ -97,8 +98,8 @@ func (m *mmapper) Munmap(data []byte) (err error) {
// err = errno
// }
//
// Errno values can be tested against error values from the os package
// using errors.Is. For example:
// Errno values can be tested against error values using errors.Is.
// For example:
//
// _, _, err := syscall.Syscall(...)
// if errors.Is(err, fs.ErrNotExist) ...
Expand All @@ -122,6 +123,8 @@ func (e Errno) Is(target error) bool {
return e == EEXIST || e == ENOTEMPTY
case oserror.ErrNotExist:
return e == ENOENT
case errorspkg.ErrUnsupported:
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP
}
return false
}
Expand Down
20 changes: 14 additions & 6 deletions src/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func UTF16PtrFromString(s string) (*uint16, error) {

// Errno is the Windows error number.
//
// Errno values can be tested against error values from the os package
// using errors.Is. For example:
// Errno values can be tested against error values using errors.Is.
// For example:
//
// _, _, err := syscall.Syscall(...)
// if errors.Is(err, fs.ErrNotExist) ...
Expand Down Expand Up @@ -147,17 +147,25 @@ const _ERROR_BAD_NETPATH = Errno(53)
func (e Errno) Is(target error) bool {
switch target {
case oserror.ErrPermission:
return e == ERROR_ACCESS_DENIED
return e == ERROR_ACCESS_DENIED ||
e == EACCES ||
e == EPERM
case oserror.ErrExist:
return e == ERROR_ALREADY_EXISTS ||
e == ERROR_DIR_NOT_EMPTY ||
e == ERROR_FILE_EXISTS
e == ERROR_FILE_EXISTS ||
e == EEXIST ||
e == ENOTEMPTY
case oserror.ErrNotExist:
return e == ERROR_FILE_NOT_FOUND ||
e == _ERROR_BAD_NETPATH ||
e == ERROR_PATH_NOT_FOUND
e == ERROR_PATH_NOT_FOUND ||
e == ENOENT
case errorspkg.ErrUnsupported:
return e == EWINDOWS
return e == ENOSYS ||
e == ENOTSUP ||
e == EOPNOTSUPP ||
e == EWINDOWS
}
return false
}
Expand Down

0 comments on commit 75c2e97

Please sign in to comment.