Commit 0f0a51f1 authored by Alexander Menzhinsky's avatar Alexander Menzhinsky Committed by Brad Fitzpatrick

os: lstat oldname before renaming

Fixes #19647

Change-Id: Ife4f98cf2c55ee9490843797213dae2f2647b0a3
Reviewed-on: https://go-review.googlesource.com/40577Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1ea796ee
......@@ -20,11 +20,20 @@ func fixLongPath(path string) string {
func rename(oldname, newname string) error {
fi, err := Lstat(newname)
if err == nil && fi.IsDir() {
// if we cannot stat oldname we should
// return that error in favor of EEXIST
fi, err = Lstat(oldname)
if err != nil {
if pErr, ok := err.(*PathError); ok {
err = pErr.Err
}
return &LinkError{"rename", oldname, newname, err}
}
return &LinkError{"rename", oldname, newname, syscall.EEXIST}
}
e := syscall.Rename(oldname, newname)
if e != nil {
return &LinkError{"rename", oldname, newname, e}
err = syscall.Rename(oldname, newname)
if err != nil {
return &LinkError{"rename", oldname, newname, err}
}
return nil
}
......
......@@ -886,6 +886,18 @@ func TestRenameFailed(t *testing.T) {
}
}
func TestRenameNotExisting(t *testing.T) {
defer chtmpdir(t)()
from, to := "doesnt-exist", "dest"
Mkdir(to, 0777)
defer Remove(to)
if err := Rename(from, to); !IsNotExist(err) {
t.Errorf("Rename(%q, %q) = %v; want an IsNotExist error", from, to, err)
}
}
func TestRenameToDirFailed(t *testing.T) {
defer chtmpdir(t)()
from, to := "renamefrom", "renameto"
......
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