2020-12-11 19:17:07 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2021-01-09 13:50:14 +01:00
|
|
|
"fmt"
|
2020-12-11 19:17:07 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Moves a file to a new destination.
|
2021-01-09 13:50:14 +01:00
|
|
|
func Move(src, dest string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("move: %s (panic)", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), ModeDir); err != nil {
|
2020-12-11 19:39:42 +01:00
|
|
|
return err
|
2020-12-11 19:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(src, dest); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := Copy(src, dest); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|