Skip to content

Commit

Permalink
Merge pull request #491 from wassup05/broken-symlink
Browse files Browse the repository at this point in the history
use `filepath.EvalSymlinks` instead of `isBrokenSymlink`
  • Loading branch information
yorukot authored Nov 28, 2024
2 parents 32d6064 + 46d204a commit bf90f87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
35 changes: 10 additions & 25 deletions src/internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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.", ""})
Expand Down Expand Up @@ -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 {

Expand Down
11 changes: 7 additions & 4 deletions src/internal/handle_panel_movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,21 @@ func (m *model) enterPanel() {
}

if fileInfo.Mode()&os.ModeSymlink != 0 {
if isBrokenSymlink(panel.element[panel.cursor].location) {
targetPath, err := filepath.EvalSymlinks(panel.element[panel.cursor].location)
if err != nil {
return
}

linkPath, _ := os.Readlink(panel.element[panel.cursor].location)
targetInfo, err := os.Lstat(targetPath)

absLinkPath, err := filepath.Abs(linkPath)
if err != nil {
return
}

m.fileModel.filePanels[m.filePanelFocusIndex].location = absLinkPath
if targetInfo.IsDir() {
m.fileModel.filePanels[m.filePanelFocusIndex].location = targetPath
}

return
}

Expand Down

0 comments on commit bf90f87

Please sign in to comment.