Skip to content

Commit 161f2e8

Browse files
0introrsc
authored andcommitted
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>
1 parent 9a2d717 commit 161f2e8

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/os/file_plan9.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ func rename(oldname, newname string) error {
339339

340340
// If newname still contains slashes after removing the oldname
341341
// prefix, the rename is cross-directory and must be rejected.
342-
// This case is caught by d.Marshal below.
342+
if lastIndex(newname, '/') >= 0 {
343+
return &LinkError{"rename", oldname, newname, ErrInvalid}
344+
}
343345

344346
var d syscall.Dir
345347

@@ -351,6 +353,13 @@ func rename(oldname, newname string) error {
351353
if err != nil {
352354
return &LinkError{"rename", oldname, newname, err}
353355
}
356+
357+
// If newname already exists and is not a directory, rename replaces it.
358+
f, err := Stat(dirname + newname)
359+
if err == nil && !f.IsDir() {
360+
Remove(dirname + newname)
361+
}
362+
354363
if err = syscall.Wstat(oldname, buf[:n]); err != nil {
355364
return &LinkError{"rename", oldname, newname, err}
356365
}

src/os/os_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -773,9 +773,6 @@ func TestRename(t *testing.T) {
773773
}
774774

775775
func TestRenameOverwriteDest(t *testing.T) {
776-
if runtime.GOOS == "plan9" {
777-
t.Skip("skipping on plan9")
778-
}
779776
defer chtmpdir(t)()
780777
from, to := "renamefrom", "renameto"
781778
// Just in case.

0 commit comments

Comments
 (0)