From 6422bd7492545640fa5de28818fa0caf1728f3f0 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 12 Feb 2021 20:50:48 +0300 Subject: [PATCH] Fix symlink handling (#581) * Fix replace dialog for symlinks If the oldPath is a symlink to the newPath or vice versa, than os.Stat() would resolve this symlink, and both the oldStat and newStat would be the same. Hence, the replace dialog would not appear and the newPath file would be overwritten by the oldPath file whilst the oldPath would be deleted. It is the same story when the oldPath and newPath are both symlinks to the same file. * Fix completion in the case of broken symlinks If the current directory contains broken symlinks then matchFile() would return at first broken symlink. Let's consider the following example: $ ls -F ~/ broken@ dir/ file The broken@ is a symlink to ~/foo - non existent file. If one would enter the following command in lf: :cd ~/ it would not suggest possible completion options because matchFile() would return as soon as it meet the broken symlink. * Don't resolve symlinks when move files/dirs (#571) This will allow to move broken symlinks. * Fix symlinks copying/moving (#571) Copy symlinks, do not try to resolve it and copy the file pointed by this symlink. Also this allows to copy symlink to directory. --- complete.go | 14 ++++++++++---- copy.go | 11 ++++++++++- eval.go | 4 ++-- nav.go | 2 +- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/complete.go b/complete.go index 84110744..2b3fb1b5 100644 --- a/complete.go +++ b/complete.go @@ -246,10 +246,16 @@ func matchFile(s string) (matches []string, longest string) { } for _, f := range files { - f, err := os.Stat(filepath.Join(dir, f.Name())) + name := filepath.Join(dir, f.Name()) + f, err := os.Stat(name) if err != nil { - log.Printf("getting file information: %s", err) - return + fl, err := os.Lstat(name) + if err == nil && fl.Mode() & os.ModeSymlink != 0 { + continue + } else { + log.Printf("getting file information: %s", err) + return + } } _, last := filepath.Split(s) @@ -257,7 +263,7 @@ func matchFile(s string) (matches []string, longest string) { continue } - name := f.Name() + name = f.Name() if isRoot(s) || filepath.Base(s) != s { name = filepath.Join(filepath.Dir(unescape(s)), f.Name()) } diff --git a/copy.go b/copy.go index fbf11f8f..28f655ae 100644 --- a/copy.go +++ b/copy.go @@ -11,7 +11,7 @@ func copySize(srcs []string) (int64, error) { var total int64 for _, src := range srcs { - _, err := os.Stat(src) + _, err := os.Lstat(src) if os.IsNotExist(err) { return total, fmt.Errorf("src does not exist: %q", src) } @@ -112,6 +112,15 @@ func copyAll(srcs []string, dstDir string) (nums chan int64, errs chan error) { errs <- fmt.Errorf("mkdir: %s", err) } nums <- info.Size() + } else if info.Mode() & os.ModeSymlink != 0 { /* Symlink */ + if rlink, err := os.Readlink(path); err != nil { + errs <- fmt.Errorf("symlink: %s", err) + } else { + if err := os.Symlink(rlink, newPath); err != nil { + errs <- fmt.Errorf("symlink: %s", err) + } + } + nums <- info.Size() } else { if err := copyFile(path, newPath, info, nums); err != nil { errs <- fmt.Errorf("copy: %s", err) diff --git a/eval.go b/eval.go index f2b31f9f..993ea12b 100644 --- a/eval.go +++ b/eval.go @@ -1328,13 +1328,13 @@ func (e *callExpr) eval(app *app, args []string) { return } - oldStat, err := os.Stat(oldPath) + oldStat, err := os.Lstat(oldPath) if err != nil { app.ui.echoerrf("rename: %s", err) return } - if newStat, err := os.Stat(newPath); !os.IsNotExist(err) && !os.SameFile(oldStat, newStat) { + if newStat, err := os.Lstat(newPath); !os.IsNotExist(err) && !os.SameFile(oldStat, newStat) { app.ui.cmdPrefix = "replace '" + newPath + "' ? [y/N] " return } diff --git a/nav.go b/nav.go index 3533cf2b..885ab6e8 100644 --- a/nav.go +++ b/nav.go @@ -824,7 +824,7 @@ func (nav *nav) moveAsync(ui *ui, srcs []string, dstDir string) { for _, src := range srcs { nav.moveCountChan <- 1 - srcStat, err := os.Stat(src) + srcStat, err := os.Lstat(src) if err != nil { errCount++ echo.args[0] = fmt.Sprintf("[%d] %s", errCount, err)