os: fix rename on Plan 9

Rename should remove newname if the file already exists
and is not a directory.

Fixes #13844.

Change-Id: I85a5cc28e8d161637a8bc1de33f4a637d9154cd1
Reviewed-on: https://go-review.googlesource.com/18291
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
David du Colombier 2016-01-07 07:45:59 +01:00 committed by Russ Cox
parent 9a2d717fa9
commit 161f2e85ee
2 changed files with 10 additions and 4 deletions

View file

@ -339,7 +339,9 @@ func rename(oldname, newname string) error {
// If newname still contains slashes after removing the oldname
// prefix, the rename is cross-directory and must be rejected.
// This case is caught by d.Marshal below.
if lastIndex(newname, '/') >= 0 {
return &LinkError{"rename", oldname, newname, ErrInvalid}
}
var d syscall.Dir
@ -351,6 +353,13 @@ func rename(oldname, newname string) error {
if err != nil {
return &LinkError{"rename", oldname, newname, err}
}
// If newname already exists and is not a directory, rename replaces it.
f, err := Stat(dirname + newname)
if err == nil && !f.IsDir() {
Remove(dirname + newname)
}
if err = syscall.Wstat(oldname, buf[:n]); err != nil {
return &LinkError{"rename", oldname, newname, err}
}

View file

@ -773,9 +773,6 @@ func TestRename(t *testing.T) {
}
func TestRenameOverwriteDest(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Skip("skipping on plan9")
}
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
// Just in case.