Skip to content
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

platform: adjusts errno on dangling symlink on Windows #1130

Merged
merged 3 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/platform/open_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package platform
import (
"os"
"path"
"path/filepath"
"syscall"
"testing"

Expand All @@ -27,4 +28,19 @@ func TestOpenFile_Errors(t *testing.T) {
_, err = OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o666)
require.ErrorIs(t, err, syscall.EEXIST)
})

// This is similar to https://github.com/WebAssembly/wasi-testsuite/blob/dc7f8d27be1030cd4788ebdf07d9b57e5d23441e/tests/rust/src/bin/dangling_symlink.rs
t.Run("dangling symlinks", func(t *testing.T) {
target := filepath.Join(tmp, "target")
symlink := filepath.Join(tmp, "dangling_symlink_symlink.cleanup")

err := os.Symlink(target, symlink)
require.NoError(t, err)

_, err = OpenFile(symlink, O_DIRECTORY|O_NOFOLLOW, 0o0666)
require.ErrorIs(t, err, syscall.ENOTDIR)

_, err = OpenFile(symlink, O_NOFOLLOW, 0o0666)
require.ErrorIs(t, err, syscall.ELOOP)
})
}
23 changes: 22 additions & 1 deletion internal/platform/open_file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const (
)

func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
isDir := flag&O_DIRECTORY > 0
flag &= ^(O_DIRECTORY | O_NOFOLLOW) // erase placeholders

fd, err := open(name, flag|syscall.O_CLOEXEC, uint32(perm))
if err == nil {
return os.NewFile(uintptr(fd), name), nil
Expand All @@ -40,6 +43,25 @@ func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
err = syscall.ENOENT
} else if errors.Is(err, syscall.ERROR_FILE_EXISTS) {
err = syscall.EEXIST
} else if notFound := errors.Is(err, syscall.ERROR_FILE_NOT_FOUND); notFound && isDir {
// Either symlink or hard link directory not found. We change the returned errno depending on
// if it is symlink or not to have consistent behavior across OSes.
st, e := os.Lstat(name)
if e == nil && st.Mode()&os.ModeSymlink != 0 {
// Dangling symlink dir must raise ENOTIDR.
err = syscall.ENOTDIR
} else {
err = syscall.ENOENT
}
} else if notFound {
// Either symlink or hard link file not found. We change the returned errno depending on
// if it is symlink or not to have consistent behavior across OSes.
st, e := os.Lstat(name)
if e == nil && st.Mode()&os.ModeSymlink != 0 {
err = syscall.ELOOP
} else {
err = syscall.ENOENT
}
}
}
return f, err
Expand All @@ -48,7 +70,6 @@ func OpenFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
// The following is lifted from syscall_windows.go to add support for setting FILE_SHARE_DELETE.
// https://github.com/golang/go/blob/go1.20/src/syscall/syscall_windows.go#L308-L379
func open(path string, mode int, perm uint32) (fd syscall.Handle, err error) {
mode &= ^(O_DIRECTORY | O_NOFOLLOW) // erase placeholders
if len(path) == 0 {
return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
}
Expand Down