mirror of
https://github.com/golang/go
synced 2024-11-18 12:04:57 -07:00
os: lstat oldname before renaming
Fixes #19647 Change-Id: Ife4f98cf2c55ee9490843797213dae2f2647b0a3 Reviewed-on: https://go-review.googlesource.com/40577 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
1ea796ee69
commit
0f0a51f1d1
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user