fixed the err handling and better comment
Signed-off-by: Krasi Georgiev <8903888+krasi-georgiev@users.noreply.github.com>
This commit is contained in:
parent
5d27fc48a8
commit
ef9b3461c5
|
@ -128,19 +128,18 @@ 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 {
|
||||||
// Remove destionation only if it is a dir.
|
// Remove destination only if it is a dir otherwise leave it to os.Rename
|
||||||
// Otherwise os.Rename replaces the destination file and is atomic.
|
// as it replaces the destination when it is file and is atomic.
|
||||||
{
|
{
|
||||||
f, err := os.Stat(to)
|
f, err := os.Stat(to)
|
||||||
if err != nil {
|
if !os.IsNotExist(err) {
|
||||||
return err
|
if err == nil && f.IsDir() {
|
||||||
}
|
|
||||||
if f.IsDir() {
|
|
||||||
if err := os.RemoveAll(to); err != nil {
|
if err := os.RemoveAll(to); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := os.Rename(from, to); err != nil {
|
if err := os.Rename(from, to); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue