From 1d51f656b54bd9bb97f9d412c40a5d3bbde0393a Mon Sep 17 00:00:00 2001 From: Trevor Slocum Date: Sun, 24 May 2020 22:14:28 -0700 Subject: [PATCH] Follow symbolic links --- CHANGELOG | 1 + audio.go | 2 +- go.mod | 4 ++-- go.sum | 6 ++++-- gui.go | 4 ++-- gui_list.go | 15 ++++++++------- library.go | 37 +++++++++++++++++++++++++++---------- 7 files changed, 45 insertions(+), 24 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bafaf98..81df7b7 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ - Add scrollbar to lists - Add --disable-mouse option - Center playing entry in queue +- Follow symbolic links 0.1.6: - Add mouse support to queue diff --git a/audio.go b/audio.go index 596605c..7ffd7db 100644 --- a/audio.go +++ b/audio.go @@ -190,7 +190,7 @@ func nextTrack() { queueNext() entry := selectedQueueEntry() - audioFile, err := openFile(entry.Path, entry.Metadata) + audioFile, err := openFile(entry.RealPath, entry.Metadata) if err != nil { statusText = err.Error() go func() { diff --git a/go.mod b/go.mod index 4d3980f..9c839a8 100644 --- a/go.mod +++ b/go.mod @@ -14,10 +14,10 @@ require ( github.com/mewkiz/pkg v0.0.0-20200411195739-f6b5e26764c3 // indirect github.com/pkg/errors v0.9.1 // indirect gitlab.com/tslocum/cbind v0.1.1 - gitlab.com/tslocum/cview v1.4.7-0.20200520223619-698a0adf93dd + gitlab.com/tslocum/cview v1.4.7-0.20200525044034-961c23145cae golang.org/x/exp v0.0.0-20200513190911-00229845015e // indirect golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect golang.org/x/mobile v0.0.0-20200329125638-4c31acba0007 // indirect - golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 // indirect + golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 8353c00..1929122 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,8 @@ gitlab.com/tslocum/cbind v0.1.1 h1:JXXtxMWHgWLvoF+QkrvcNvOQ59juy7OE1RhT7hZfdt0= gitlab.com/tslocum/cbind v0.1.1/go.mod h1:rX7vkl0pUSg/yy427MmD1FZAf99S7WwpUlxF/qTpPqk= gitlab.com/tslocum/cview v1.4.7-0.20200520223619-698a0adf93dd h1:XbQrX2nkpZ50i0m0o+qf+zajtA9Z2ohOZNuaCCU8Ayk= gitlab.com/tslocum/cview v1.4.7-0.20200520223619-698a0adf93dd/go.mod h1:PW2Ucec7oTYOfK4N+hqm/CKEN9B1PBidq5YJ3ZaeknU= +gitlab.com/tslocum/cview v1.4.7-0.20200525044034-961c23145cae h1:Q+q83u8BI8N908q1ogBQrK3j2k6/ILRHmpivc8LKl3k= +gitlab.com/tslocum/cview v1.4.7-0.20200525044034-961c23145cae/go.mod h1:87HOuFUfgmyY0zfg+7qKg6jyN9gI/znsa/KE2ZZHDSs= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -98,8 +100,8 @@ golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9 h1:YTzHMGlqJu67/uEo1lBv0n3wBXhXNeUbB1XfN2vmTm0= golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= -golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= +golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/gui.go b/gui.go index 25432fe..72d0665 100644 --- a/gui.go +++ b/gui.go @@ -157,7 +157,7 @@ func browseFolder(browse string) { autoSelect := -1 var entryPath string for i, entry := range mainFiles { - if !entry.File.IsDir() { + if !entry.File.IsDir() && entry.File.Mode()&os.ModeSymlink == 0 { continue } @@ -240,7 +240,7 @@ func updateMain() { for _, entry := range mainFiles { //length = "" - if entry.File.IsDir() { + if entry.File.IsDir() || entry.File.Mode()&os.ModeSymlink != 0 { line = strings.TrimSpace(entry.File.Name()) + "/" } else { line = entry.String() diff --git a/gui_list.go b/gui_list.go index b084cbc..b61ef78 100644 --- a/gui_list.go +++ b/gui_list.go @@ -1,6 +1,7 @@ package main import ( + "os" "path" "time" @@ -57,12 +58,12 @@ func listSelect(cursor int) { } entry := mainFiles[cursor-1] - if entry.File.IsDir() { - browseFolder(entry.Path) + if entry.File.IsDir() || entry.File.Mode()&os.ModeSymlink != 0 { + browseFolder(entry.RealPath) return } - audioFile, err := openFile(entry.Path, entry.Metadata) + audioFile, err := openFile(entry.RealPath, entry.Metadata) if err != nil { statusText = err.Error() go func() { @@ -84,11 +85,11 @@ func queueSelect(cursor int) { } entry := queueFiles[cursor] - if entry.File.IsDir() { + if entry.File.IsDir() || entry.File.Mode()&os.ModeSymlink != 0 { return } - audioFile, err := openFile(entry.Path, entry.Metadata) + audioFile, err := openFile(entry.RealPath, entry.Metadata) if err != nil { statusText = err.Error() go func() { @@ -113,8 +114,8 @@ func listQueue() { entry := selectedMainEntry() if entry == nil { return - } else if entry.File.IsDir() { - scanFiles := scanFolderRecursively(entry.Path) + } else if entry.File.IsDir() || entry.File.Mode()&os.ModeSymlink != 0 { + scanFiles := scanFolderRecursively(entry.RealPath) queueLock.Lock() queueFiles = append(queueFiles, scanFiles...) diff --git a/library.go b/library.go index 2fb37db..e0f9832 100644 --- a/library.go +++ b/library.go @@ -54,6 +54,7 @@ func readMetadata(f *os.File) *metadata { type libraryEntry struct { File os.FileInfo Path string + RealPath string Metadata *metadata } @@ -78,11 +79,25 @@ func scanFolder(scanPath string) []*libraryEntry { var entries []*libraryEntry for _, fileInfo := range files { p := path.Join(scanPath, fileInfo.Name()) + var r string - b := path.Base(fileInfo.Name()) - if fileInfo.IsDir() { + if fileInfo.Mode()&os.ModeSymlink != 0 { + r, err = os.Readlink(p) + if err != nil { + continue + } + + if !path.IsAbs(r) { + r = path.Join(scanPath, r) + } + } else { + r = p + } + + b := path.Base(p) + if fileInfo.IsDir() || fileInfo.Mode()&os.ModeSymlink != 0 { if b != "" && (b[0] != '.' || showHiddenFolders) { - entries = append(entries, &libraryEntry{File: fileInfo, Path: p, Metadata: &metadata{Title: strings.TrimSpace(fileInfo.Name())}}) + entries = append(entries, &libraryEntry{File: fileInfo, Path: p, RealPath: r, Metadata: &metadata{Title: strings.TrimSpace(fileInfo.Name())}}) } continue @@ -90,19 +105,21 @@ func scanFolder(scanPath string) []*libraryEntry { continue } - f, err := os.Open(p) + f, err := os.Open(r) if err != nil { continue } metadata := readMetadata(f) f.Close() - entries = append(entries, &libraryEntry{File: fileInfo, Path: p, Metadata: metadata}) + entries = append(entries, &libraryEntry{File: fileInfo, Path: p, RealPath: r, Metadata: metadata}) } sort.Slice(entries, func(i, j int) bool { - if entries[i].File.IsDir() != entries[j].File.IsDir() { - return entries[i].File.IsDir() + iDir := entries[i].File.IsDir() || entries[i].File.Mode()&os.ModeSymlink != 0 + jDir := entries[j].File.IsDir() || entries[j].File.Mode()&os.ModeSymlink != 0 + if iDir != jDir { + return iDir } if entries[i].Metadata.Album != "" && strings.ToLower(entries[i].Metadata.Album) == strings.ToLower(entries[j].Metadata.Album) && (entries[i].Metadata.Track > 0 || entries[j].Metadata.Track > 0) { @@ -120,14 +137,14 @@ func scanFolderRecursively(path string) []*libraryEntry { scanFiles := scanFolder(path) for _, entry := range scanFiles { - if !entry.File.IsDir() { + if !entry.File.IsDir() && entry.File.Mode()&os.ModeSymlink == 0 { continue } - entries = append(entries, scanFolderRecursively(entry.Path)...) + entries = append(entries, scanFolderRecursively(entry.RealPath)...) } for _, entry := range scanFiles { - if entry.File.IsDir() { + if entry.File.IsDir() || entry.File.Mode()&os.ModeSymlink != 0 { continue }