From d7ab3971413b8c175f8105fe53dbc21cc1829367 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Tue, 26 Nov 2024 11:21:49 +0530 Subject: [PATCH 1/3] used filepath.EvalSymlinks --- src/internal/function.go | 35 ++++++++------------------- src/internal/handle_panel_movement.go | 8 +----- 2 files changed, 11 insertions(+), 32 deletions(-) diff --git a/src/internal/function.go b/src/internal/function.go index 90998cea..2c7c3fec 100644 --- a/src/internal/function.go +++ b/src/internal/function.go @@ -59,12 +59,12 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO switch sortOptions.options[sortOptions.selected] { case "Name": order = func(i, j int) bool { - _, errI := os.ReadDir(location+"/"+files[i].Name()); - _, errJ := os.ReadDir(location+"/"+files[j].Name()); - if (files[i].IsDir()||errI==nil) && (!files[j].IsDir()&&errJ!=nil) { + _, errI := os.ReadDir(location + "/" + files[i].Name()) + _, errJ := os.ReadDir(location + "/" + files[j].Name()) + if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) { return true } - if (!files[i].IsDir()&&errI!=nil) && (files[j].IsDir()||errJ==nil) { + if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) { return false } if Config.CaseSensitiveSort { @@ -79,13 +79,13 @@ func returnFolderElement(location string, displayDotFile bool, sortOptions sortO // Files sorted by size fileInfoI, _ := files[i].Info() fileInfoJ, _ := files[j].Info() - _, errI := os.ReadDir(location+"/"+files[i].Name()); - _, errJ := os.ReadDir(location+"/"+files[j].Name()); + _, errI := os.ReadDir(location + "/" + files[i].Name()) + _, errJ := os.ReadDir(location + "/" + files[j].Name()) - if (files[i].IsDir()||errI==nil) && (!files[j].IsDir()&&errJ!=nil) { + if (files[i].IsDir() || errI == nil) && (!files[j].IsDir() && errJ != nil) { return true } - if (!files[i].IsDir()&&errI!=nil) && (files[j].IsDir()||errJ==nil) { + if (!files[i].IsDir() && errI != nil) && (files[j].IsDir() || errJ == nil) { return false } if files[i].IsDir() && files[j].IsDir() { @@ -351,7 +351,8 @@ func (m *model) returnMetaData() { fileInfo, err := os.Stat(filePath) if isSymlink(filePath) { - if isBrokenSymlink(filePath) { + _, err := filepath.EvalSymlinks(filePath) + if err != nil { m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"Link file is broken!", ""}) } else { m.fileMetaData.metaData = append(m.fileMetaData.metaData, [2]string{"This is a link file.", ""}) @@ -473,22 +474,6 @@ func countFiles(dirPath string) (int, error) { return count, err } -// Check whether is broken recursive symlinks -func isBrokenSymlink(filePath string) bool { - linkPath, err := os.Readlink(filePath) - if err != nil { - return true - } - - absLinkPath, err := filepath.Abs(linkPath) - if err != nil { - return true - } - - _, err = os.Stat(absLinkPath) - return err != nil -} - // Check whether is symlinks func isSymlink(filePath string) bool { diff --git a/src/internal/handle_panel_movement.go b/src/internal/handle_panel_movement.go index c2035cd7..65458a55 100644 --- a/src/internal/handle_panel_movement.go +++ b/src/internal/handle_panel_movement.go @@ -75,13 +75,7 @@ func (m *model) enterPanel() { } if fileInfo.Mode()&os.ModeSymlink != 0 { - if isBrokenSymlink(panel.element[panel.cursor].location) { - return - } - - linkPath, _ := os.Readlink(panel.element[panel.cursor].location) - - absLinkPath, err := filepath.Abs(linkPath) + absLinkPath, err := filepath.EvalSymlinks(panel.element[panel.cursor].location) if err != nil { return } From c74aac2a5594248c9829a9271f92ddff2e4aed29 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Tue, 26 Nov 2024 12:17:03 +0530 Subject: [PATCH 2/3] prevent from entering the target if it is a file --- src/internal/handle_panel_movement.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/internal/handle_panel_movement.go b/src/internal/handle_panel_movement.go index 65458a55..7885959c 100644 --- a/src/internal/handle_panel_movement.go +++ b/src/internal/handle_panel_movement.go @@ -75,12 +75,21 @@ func (m *model) enterPanel() { } if fileInfo.Mode()&os.ModeSymlink != 0 { - absLinkPath, err := filepath.EvalSymlinks(panel.element[panel.cursor].location) + targetPath, err := filepath.EvalSymlinks(panel.element[panel.cursor].location) if err != nil { return } - m.fileModel.filePanels[m.filePanelFocusIndex].location = absLinkPath + targetInfo, err := os.Lstat(targetPath) + + if err != nil { + return + } + + if targetInfo.IsDir() { + m.fileModel.filePanels[m.filePanelFocusIndex].location = absLinkPath + } + return } From 46d204a05e1c482a83e325a4c7c18c6e6936eb78 Mon Sep 17 00:00:00 2001 From: "supritsj@Arch" Date: Wed, 27 Nov 2024 22:30:02 +0530 Subject: [PATCH 3/3] fix typo --- src/internal/handle_panel_movement.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/handle_panel_movement.go b/src/internal/handle_panel_movement.go index 7885959c..4803bbfd 100644 --- a/src/internal/handle_panel_movement.go +++ b/src/internal/handle_panel_movement.go @@ -87,7 +87,7 @@ func (m *model) enterPanel() { } if targetInfo.IsDir() { - m.fileModel.filePanels[m.filePanelFocusIndex].location = absLinkPath + m.fileModel.filePanels[m.filePanelFocusIndex].location = targetPath } return