未验证 提交 b5f9f9f0 编写于 作者: B Bartek Płotka 提交者: GitHub

Merge pull request #636 from krasi-georgiev/rename-bug

fileutil.Replace - remove destination only when a directory.
...@@ -128,9 +128,19 @@ func Rename(from, to string) error { ...@@ -128,9 +128,19 @@ func Rename(from, to string) error {
// Replace moves a file or directory to a new location and deletes any previous data. // Replace moves a file or directory to a new location and deletes any previous data.
// It is not atomic. // It is not atomic.
func Replace(from, to string) error { func Replace(from, to string) error {
if err := os.RemoveAll(to); err != nil { // Remove destination only if it is a dir otherwise leave it to os.Rename
return err // as it replaces the destination file and is atomic.
{
f, err := os.Stat(to)
if !os.IsNotExist(err) {
if err == nil && f.IsDir() {
if err := os.RemoveAll(to); err != nil {
return err
}
}
}
} }
if err := os.Rename(from, to); err != nil { if err := os.Rename(from, to); err != nil {
return err return err
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册