check if src and dst are the same file before move

This commit is contained in:
Gokcehan 2019-02-26 21:25:34 +03:00
parent 0f47c5428b
commit 728c5fbb2c

17
nav.go
View File

@ -631,10 +631,23 @@ func moveAsync(ui *ui, srcs []string, dstDir string) {
errCount := 0 errCount := 0
for _, src := range srcs { for _, src := range srcs {
srcStat, err := os.Stat(src)
if err != nil {
errCount++
echo.args[0] = fmt.Sprintf("[%d] error: %s", errCount, err)
ui.exprChan <- echo
continue
}
dst := filepath.Join(dstDir, filepath.Base(src)) dst := filepath.Join(dstDir, filepath.Base(src))
_, err := os.Stat(dst) dstStat, err := os.Stat(dst)
if !os.IsNotExist(err) { if os.SameFile(srcStat, dstStat) {
errCount++
echo.args[0] = fmt.Sprintf("[%d] error: rename %s %s: source and destination are the same file", errCount, src, dst)
ui.exprChan <- echo
continue
} else if !os.IsNotExist(err) {
var newPath string var newPath string
for i := 1; !os.IsNotExist(err); i++ { for i := 1; !os.IsNotExist(err); i++ {
newPath = fmt.Sprintf("%s.~%d~", dst, i) newPath = fmt.Sprintf("%s.~%d~", dst, i)