Commit 321c312d authored by Russ Cox's avatar Russ Cox

os: reject Rename("old", "new") where new is a directory

Unix rejects this when new is a non-empty directory.
Other systems reject this when new is a directory, empty or not.
Make Unix reject empty directory too.

Fixes #14527.

Change-Id: Ice24b8065264c91c22cba24aa73e142386c29c87
Reviewed-on: https://go-review.googlesource.com/31358
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 8fbfdad2
...@@ -12,6 +12,10 @@ import ( ...@@ -12,6 +12,10 @@ import (
) )
func rename(oldname, newname string) error { func rename(oldname, newname string) error {
fi, err := Lstat(newname)
if err == nil && fi.IsDir() {
return &LinkError{"rename", oldname, newname, syscall.EEXIST}
}
e := syscall.Rename(oldname, newname) e := syscall.Rename(oldname, newname)
if e != nil { if e != nil {
return &LinkError{"rename", oldname, newname, e} return &LinkError{"rename", oldname, newname, e}
......
...@@ -828,6 +828,37 @@ func TestRenameFailed(t *testing.T) { ...@@ -828,6 +828,37 @@ func TestRenameFailed(t *testing.T) {
} }
} }
func TestRenameToDirFailed(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
Remove(from)
Remove(to)
Mkdir(from, 0777)
Mkdir(to, 0777)
err := Rename(from, to)
switch err := err.(type) {
case *LinkError:
if err.Op != "rename" {
t.Errorf("rename %q, %q: err.Op: want %q, got %q", from, to, "rename", err.Op)
}
if err.Old != from {
t.Errorf("rename %q, %q: err.Old: want %q, got %q", from, to, from, err.Old)
}
if err.New != to {
t.Errorf("rename %q, %q: err.New: want %q, got %q", from, to, to, err.New)
}
case nil:
t.Errorf("rename %q, %q: expected error, got nil", from, to)
// cleanup whatever was placed in "renameto"
Remove(to)
default:
t.Errorf("rename %q, %q: expected %T, got %T %v", from, to, new(LinkError), err, err)
}
}
func exec(t *testing.T, dir, cmd string, args []string, expect string) { func exec(t *testing.T, dir, cmd string, args []string, expect string) {
r, w, err := Pipe() r, w, err := Pipe()
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment