From d7994440406f056aa20fc44b3cb2426f3fa7130f Mon Sep 17 00:00:00 2001 From: Scrotadamus Date: Fri, 7 Mar 2025 10:06:31 +0100 Subject: bug fix, doc.HasHighlights only if file is pdf - renamed checkFile to HasHighlights - HasHighlights will use ghligh/document to open and then check for highlights only if the file specified has pdf magic header. This fixes bug where goroutines block because of hang on of document.GhlighDoc (may be caused by poppler low level implementation) Changes to be committed: modified: ls.go --- cmd/ls.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/cmd/ls.go b/cmd/ls.go index bcdbe66..cfe31be 100644 --- a/cmd/ls.go +++ b/cmd/ls.go @@ -4,6 +4,7 @@ Copyright © 2025 NAME HERE package cmd import ( + "bytes" "context" "fmt" "os" @@ -90,7 +91,34 @@ func lsArgs(args []string) []string { return args } -func checkFile(path string) bool { +// returns true if file specified in path start with the PDF magic header +func isPDF(path string) bool { + file, err := os.Open(path) + if err != nil { + // We only care about printing permissions errors + // and things like that + fmt.Fprintf(os.Stderr, "%s\n", err) + return false + } + + defer file.Close() + + header := make([]byte, 5) + _, err = file.Read(header) + if err != nil { + return false + } + + return bytes.Equal(header, []byte("%PDF-")) +} + +// returns true if file contains at least one highlight or is tagged with "ls" (is ls-able by ghligh) +func HasHighlights(path string) bool { + // Ensure is a pdf file, might block for other kind of files + if !isPDF(path) { + return false + } + doc, err := document.Open(path) if err != nil { return false @@ -134,16 +162,18 @@ var lsCmd = &cobra.Command{ var wg sync.WaitGroup var found bool + for file := range ch { wg.Add(1) go func(f string) { defer wg.Done() - if checkFile(f) { + if HasHighlights(f) { found = true fmt.Printf("%s\n", f) } }(file) } + wg.Wait() check, err := cmd.Flags().GetBool("check") -- cgit v1.2.3