-
Notifications
You must be signed in to change notification settings - Fork 715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
internal/unix: add Errno wrapper for Windows #1659
Merged
+227
−48
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package unix | ||
|
||
import ( | ||
"syscall" | ||
|
||
linux "golang.org/x/sys/unix" | ||
) | ||
|
||
type Errno = syscall.Errno | ||
|
||
const ( | ||
E2BIG = linux.E2BIG | ||
EACCES = linux.EACCES | ||
EAGAIN = linux.EAGAIN | ||
EBADF = linux.EBADF | ||
EEXIST = linux.EEXIST | ||
EFAULT = linux.EFAULT | ||
EILSEQ = linux.EILSEQ | ||
EINTR = linux.EINTR | ||
EINVAL = linux.EINVAL | ||
ENODEV = linux.ENODEV | ||
ENOENT = linux.ENOENT | ||
ENOSPC = linux.ENOSPC | ||
EOPNOTSUPP = linux.EOPNOTSUPP | ||
EPERM = linux.EPERM | ||
EPOLLIN = linux.EPOLLIN | ||
ESRCH = linux.ESRCH | ||
ESTALE = linux.ESTALE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package unix | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TestErrnoIsUnix(t *testing.T) { | ||
qt.Assert(t, qt.ErrorIs(EPERM, unix.EPERM)) | ||
qt.Assert(t, qt.ErrorIs(ENOENT, unix.ENOENT)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//go:build !linux && !windows | ||
|
||
package unix | ||
|
||
import "syscall" | ||
|
||
type Errno = syscall.Errno | ||
|
||
// Errnos are distinct and non-zero. | ||
const ( | ||
E2BIG Errno = iota + 1 | ||
EACCES | ||
EAGAIN | ||
EBADF | ||
EEXIST | ||
EFAULT | ||
EILSEQ | ||
EINTR | ||
EINVAL | ||
ENODEV | ||
ENOENT | ||
ENOSPC | ||
ENOTSUP | ||
ENOTSUPP | ||
EOPNOTSUPP | ||
EPERM | ||
ESRCH | ||
ESTALE | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package unix | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-quicktest/qt" | ||
) | ||
|
||
func TestErrno(t *testing.T) { | ||
qt.Assert(t, qt.ErrorIs(ENOENT, os.ErrNotExist)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package unix | ||
|
||
// The code in this file is derived from syscall_unix.go in the Go source code, | ||
// licensed under the MIT license. | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=Errno -tags=windows -output=errno_string_windows.go | ||
|
||
// Windows specific constants for Unix errnos. | ||
// | ||
// The values do not always match Linux, for example EILSEQ and EOPNOTSUPP. | ||
// | ||
// See https://learn.microsoft.com/en-us/cpp/c-runtime-library/errno-constants?view=msvc-170 | ||
const ( | ||
EPERM Errno = 1 | ||
ENOENT Errno = 2 | ||
ESRCH Errno = 3 | ||
EINTR Errno = 4 | ||
E2BIG Errno = 7 | ||
EBADF Errno = 9 | ||
EAGAIN Errno = 11 | ||
EACCES Errno = 13 | ||
EFAULT Errno = 14 | ||
EEXIST Errno = 17 | ||
ENODEV Errno = 19 | ||
EINVAL Errno = 22 | ||
ENFILE Errno = 23 | ||
EMFILE Errno = 24 | ||
ENOSPC Errno = 28 | ||
ENOSYS Errno = 40 | ||
ENOTEMPTY Errno = 41 | ||
EILSEQ Errno = 42 | ||
ENOTSUP Errno = 129 | ||
EOPNOTSUPP Errno = 130 | ||
ETIMEDOUT Errno = 138 | ||
EWOULDBLOCK Errno = 140 | ||
) | ||
|
||
// These constants do not exist on Windows and therefore have a non-zero | ||
// dummy value. | ||
const ( | ||
ENOTSUPP Errno = Errno(syscall.APPLICATION_ERROR) + iota | ||
ESTALE | ||
) | ||
|
||
// Errno is a Windows compatibility shim for Unix errnos. | ||
type Errno uintptr | ||
|
||
func (e Errno) Error() string { | ||
return e.String() | ||
} | ||
|
||
func (e Errno) Is(target error) bool { | ||
switch target { | ||
case os.ErrPermission: | ||
return e == EACCES || e == EPERM | ||
case os.ErrExist: | ||
return e == EEXIST || e == ENOTEMPTY | ||
case os.ErrNotExist: | ||
return e == ENOENT | ||
case errors.ErrUnsupported: | ||
return e == ENOSYS || e == ENOTSUP || e == EOPNOTSUPP | ||
} | ||
return false | ||
} | ||
|
||
func (e Errno) Temporary() bool { | ||
return e == EINTR || e == EMFILE || e == ENFILE || e.Timeout() | ||
} | ||
|
||
func (e Errno) Timeout() bool { | ||
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you using these in other Windows-specific code? I don't think an external caller can get ahold of these methods since this is
internal/unix.Errno
. Unless we need them to satisfy some interface, I propose we remove them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We expose these indirectly because we wrap original errno from
bpf()
and friends. It'd be surprising / a footgun if those errors behaved different between platforms, I think.