adds experimental sys.Errno to begin decoupling from the syscall package (#1582)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2023-07-17 08:13:29 +08:00
committed by GitHub
parent 1dafce0b2a
commit 2f8dd23097
94 changed files with 1591 additions and 1374 deletions

View File

@@ -4,10 +4,10 @@ import (
"os"
"syscall"
"github.com/tetratelabs/wazero/internal/platform"
"github.com/tetratelabs/wazero/experimental/sys"
)
func rename(from, to string) syscall.Errno {
func rename(from, to string) sys.Errno {
if from == to {
return 0
}
@@ -18,7 +18,7 @@ func rename(from, to string) syscall.Errno {
} else {
fromIsDir = fromStat.Mode.IsDir()
}
if toStat, errno := stat(to); errno == syscall.ENOENT {
if toStat, errno := stat(to); errno == sys.ENOENT {
return syscallRename(from, to) // file or dir to not-exist is ok
} else if errno != 0 {
return errno // failed to stat to
@@ -29,20 +29,20 @@ func rename(from, to string) syscall.Errno {
// Now, handle known cases
switch {
case !fromIsDir && toIsDir: // file to dir
return syscall.EISDIR
return sys.EISDIR
case !fromIsDir && !toIsDir: // file to file
// Use os.Rename instead of syscall.Rename to overwrite a file.
// This uses MoveFileEx instead of MoveFile (used by syscall.Rename).
return platform.UnwrapOSError(os.Rename(from, to))
return sys.UnwrapOSError(os.Rename(from, to))
case fromIsDir && !toIsDir: // dir to file
return syscall.ENOTDIR
return sys.ENOTDIR
default: // dir to dir
// We can't tell if a directory is empty or not, via stat information.
// Reading the directory is expensive, as it can buffer large amounts
// of data on fail. Instead, speculatively try to remove the directory.
// This is only one syscall and won't buffer anything.
if errno := rmdir(to); errno == 0 || errno == syscall.ENOENT {
if errno := rmdir(to); errno == 0 || errno == sys.ENOENT {
return syscallRename(from, to)
} else {
return errno
@@ -50,6 +50,6 @@ func rename(from, to string) syscall.Errno {
}
}
func syscallRename(from string, to string) syscall.Errno {
return platform.UnwrapOSError(syscall.Rename(from, to))
func syscallRename(from string, to string) sys.Errno {
return sys.UnwrapOSError(syscall.Rename(from, to))
}