-
Notifications
You must be signed in to change notification settings - Fork 266
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
windows: fixes unlinking symlink to dir, rmdir on opened empty dir. #1172
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3fb45ba
dirfs: ensures no error on unlinking symlink to dir
mathetake 5d22056
test
mathetake c46495b
fix
mathetake fe0fb55
fix
mathetake c3b212f
fix
mathetake e77e207
refine
mathetake 1e35027
this..
mathetake 0766741
make it real
mathetake e4a40d7
fix?
mathetake 0ca0dbd
ok
mathetake 1671b14
variable
mathetake 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,13 @@ | ||
//go:build !windows | ||
|
||
package platform | ||
|
||
import "syscall" | ||
|
||
func Unlink(name string) error { | ||
err := syscall.Unlink(name) | ||
if err = UnwrapOSError(err); err == syscall.EPERM { | ||
err = syscall.EISDIR | ||
} | ||
return err | ||
} |
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,58 @@ | ||
package platform | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"syscall" | ||
"testing" | ||
|
||
"github.com/tetratelabs/wazero/internal/testing/require" | ||
) | ||
|
||
func TestUnlink(t *testing.T) { | ||
t.Run("doesn't exist", func(t *testing.T) { | ||
name := "non-existent" | ||
err := Unlink(name) | ||
require.EqualErrno(t, syscall.ENOENT, err) | ||
}) | ||
|
||
t.Run("target: dir", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
dir := path.Join(tmpDir, "dir") | ||
require.NoError(t, os.Mkdir(dir, 0o700)) | ||
|
||
err := Unlink(dir) | ||
require.EqualErrno(t, syscall.EISDIR, err) | ||
|
||
require.NoError(t, os.Remove(dir)) | ||
}) | ||
|
||
t.Run("target: symlink to dir", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
// Create link target dir. | ||
subDirRealPath := path.Join(tmpDir, "subdir") | ||
require.NoError(t, os.Mkdir(subDirRealPath, 0o700)) | ||
|
||
// Create a symlink to the subdirectory. | ||
const symlinkName = "symlink-to-dir" | ||
require.NoError(t, os.Symlink("subdir", symlinkName)) | ||
|
||
// Unlinking the symlink should suceed. | ||
err := Unlink(symlinkName) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("file exists", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
|
||
name := path.Join(tmpDir, "unlink") | ||
|
||
require.NoError(t, os.WriteFile(name, []byte{}, 0o600)) | ||
|
||
require.NoError(t, Unlink(name)) | ||
_, err := os.Stat(name) | ||
require.Error(t, err) | ||
}) | ||
} |
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,25 @@ | ||
//go:build windows | ||
|
||
package platform | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func Unlink(name string) (err error) { | ||
err = syscall.Unlink(name) | ||
if err == nil { | ||
return | ||
} | ||
err = UnwrapOSError(err) | ||
if err == syscall.EPERM { | ||
lstat, errLstat := os.Lstat(name) | ||
if errLstat == nil && lstat.Mode()&os.ModeSymlink != 0 { | ||
err = UnwrapOSError(os.Remove(name)) | ||
} else { | ||
err = syscall.EISDIR | ||
} | ||
} | ||
return | ||
} |
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 |
---|---|---|
|
@@ -336,18 +336,23 @@ func TestDirFS_Rename(t *testing.T) { | |
} | ||
|
||
func TestDirFS_Rmdir(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
t.Run("doesn't exist", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
name := "rmdir" | ||
|
||
t.Run("doesn't exist", func(t *testing.T) { | ||
err := testFS.Rmdir(name) | ||
require.EqualErrno(t, syscall.ENOENT, err) | ||
}) | ||
|
||
t.Run("dir not empty", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
|
||
require.NoError(t, os.Mkdir(realPath, 0o700)) | ||
fileInDir := pathutil.Join(realPath, "file") | ||
require.NoError(t, os.WriteFile(fileInDir, []byte{}, 0o600)) | ||
|
@@ -358,13 +363,62 @@ func TestDirFS_Rmdir(t *testing.T) { | |
require.NoError(t, os.Remove(fileInDir)) | ||
}) | ||
|
||
t.Run("dir previously not empty", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
require.NoError(t, os.Mkdir(realPath, 0o700)) | ||
|
||
// Create a file and then delete it. | ||
fileInDir := pathutil.Join(realPath, "file") | ||
require.NoError(t, os.WriteFile(fileInDir, []byte{}, 0o600)) | ||
require.NoError(t, os.Remove(fileInDir)) | ||
|
||
// After deletion, try removing directory. | ||
err := testFS.Rmdir(name) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("dir empty", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
require.NoError(t, os.Mkdir(realPath, 0o700)) | ||
require.NoError(t, testFS.Rmdir(name)) | ||
_, err := os.Stat(realPath) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("dir empty while opening", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
require.NoError(t, os.Mkdir(realPath, 0o700)) | ||
|
||
f, err := testFS.OpenFile(name, platform.O_DIRECTORY, 0o700) | ||
require.NoError(t, err) | ||
defer func() { | ||
require.NoError(t, f.Close()) | ||
}() | ||
|
||
require.NoError(t, testFS.Rmdir(name)) | ||
_, err = os.Stat(realPath) | ||
require.Error(t, err) | ||
}) | ||
|
||
t.Run("not directory", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "rmdir" | ||
realPath := pathutil.Join(tmpDir, name) | ||
|
||
require.NoError(t, os.WriteFile(realPath, []byte{}, 0o600)) | ||
|
||
err := testFS.Rmdir(name) | ||
|
@@ -375,27 +429,55 @@ func TestDirFS_Rmdir(t *testing.T) { | |
} | ||
|
||
func TestDirFS_Unlink(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "unlink" | ||
realPath := pathutil.Join(tmpDir, name) | ||
|
||
t.Run("doesn't exist", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
name := "unlink" | ||
|
||
err := testFS.Unlink(name) | ||
require.EqualErrno(t, syscall.ENOENT, err) | ||
}) | ||
|
||
t.Run("not file", func(t *testing.T) { | ||
t.Run("target: dir", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
dir := "dir" | ||
realPath := pathutil.Join(tmpDir, dir) | ||
|
||
require.NoError(t, os.Mkdir(realPath, 0o700)) | ||
|
||
err := testFS.Unlink(name) | ||
err := testFS.Unlink(dir) | ||
require.EqualErrno(t, syscall.EISDIR, err) | ||
|
||
require.NoError(t, os.Remove(realPath)) | ||
}) | ||
|
||
t.Run("target: symlink to dir", func(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is the new one and only windows failing |
||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
// Create link target dir. | ||
subDirName := "subdir" | ||
subDirRealPath := pathutil.Join(tmpDir, subDirName) | ||
require.NoError(t, os.Mkdir(subDirRealPath, 0o700)) | ||
|
||
// Create a symlink to the subdirectory. | ||
const symlinkName = "symlink-to-dir" | ||
require.NoError(t, testFS.Symlink("subdir", symlinkName)) | ||
|
||
// Unlinking the symlink should suceed. | ||
err := testFS.Unlink(symlinkName) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("file exists", func(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
testFS := NewDirFS(tmpDir) | ||
|
||
name := "unlink" | ||
realPath := pathutil.Join(tmpDir, name) | ||
|
||
require.NoError(t, os.WriteFile(realPath, []byte{}, 0o600)) | ||
|
||
require.NoError(t, testFS.Unlink(name)) | ||
|
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
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.
lifted from the change made in Go 1.20 https://github.com/golang/go/blob/go1.20/src/syscall/syscall_windows.go#L308-L379 and I guess that's why we haven't encountered it before.