-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
windows: fixes unlinking symlink to dir, rmdir on opened empty dir. (#…
…1172) Signed-off-by: Takeshi Yoneda <[email protected]>
- Loading branch information
Showing
7 changed files
with
212 additions
and
18 deletions.
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
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