Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #636 from krasi-georgiev/rename-bug
Browse files Browse the repository at this point in the history
fileutil.Replace - remove destination only when a directory.
  • Loading branch information
bwplotka authored Jun 24, 2019
2 parents d501ae4 + 251c3a4 commit b5f9f9f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// It is not atomic.
func Replace(from, to string) error {
if err := os.RemoveAll(to); err != nil {
return err
// Remove destination only if it is a dir otherwise leave it to os.Rename
// 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 {
return err
}
Expand Down

0 comments on commit b5f9f9f

Please sign in to comment.