From 0dfd9a832ecfb6588eb0406e17d678d014848a07 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 9 Nov 2026 08:42:30 +0530 Subject: [PATCH] choose-files: Cleanup handling of special files in previews Follow up to the previous commit fixing hangs when previewing named pipes: - Skip previewing anything that is not a regular file (or a symlink to one) in preview_for, before any previewer opens it. This avoids unblocking and then breaking writers waiting on a FIFO and covers the non-text previewers (images, video, archives, calibre, commands) too. - Apply the text preview size limit to the symlink target instead of the symlink itself, and enforce it while reading so that files growing after stat() are also handled. - Pre-allocate the read buffer based on the file size, as os.ReadFile does. - Read ignore files during scanning with the same non-blocking reader, so a FIFO named .ignore or .gitignore cannot stall the scan. - Unblock goroutines stuck on FIFOs when tests time out. --- docs/changelog.rst | 3 +- kittens/choose_files/preview.go | 53 ++++++++-- kittens/choose_files/preview_test.go | 141 ++++++++++++++++++++++----- kittens/choose_files/scan.go | 4 +- 4 files changed, 165 insertions(+), 36 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index ebf230821..6875e52c6 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -126,7 +126,8 @@ Detailed list of changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :doc:`choose-files `: Fix a hang when previewing named - pipes as text + pipes and other special files, or when an ignore file is a named pipe. Also + respect the maximum size for text previews of files accessed via symlinks - :doc:`Custom shaders `: Add ``screen-shake`` and ``screen-shake-random`` shaders that shake the window in which a bell occurs, diff --git a/kittens/choose_files/preview.go b/kittens/choose_files/preview.go index 27a25b55c..71dfb362f 100644 --- a/kittens/choose_files/preview.go +++ b/kittens/choose_files/preview.go @@ -1,6 +1,7 @@ package choose_files import ( + "bytes" "fmt" "io" "io/fs" @@ -259,8 +260,15 @@ func (p *TextFilePreview) Render(h *Handler, x, y, width, height int) { h.lp.QueueWriteString(buf.String()) } -func read_regular_text_file(path string) ([]byte, error) { - // Opening a FIFO can block before we can inspect the opened file's mode. +// The maximum size of a file that will be read for a text preview +const MAX_TEXT_FILE_SIZE = 16 * 1024 * 1024 + +// Read the contents of path, failing if it is not a regular file or if it is +// larger than max_size bytes. A negative max_size means no limit. The file is +// opened in non-blocking mode and its type is checked on the opened +// descriptor, as opening a FIFO can otherwise block indefinitely and the path +// can be replaced by a special file after any earlier stat() calls. +func read_regular_file(path string, max_size int64) ([]byte, error) { f, err := os.OpenFile(path, os.O_RDONLY|unix.O_NONBLOCK, 0) if err != nil { return nil, err @@ -273,11 +281,31 @@ func read_regular_text_file(path string) ([]byte, error) { if !info.Mode().IsRegular() { return nil, fmt.Errorf("%q is not a regular file", path) } - return io.ReadAll(f) + too_large := func(size int64) error { + return fmt.Errorf("%q is too large (%s > %s)", path, humanize.Bytes(uint64(size)), humanize.Bytes(uint64(max_size))) + } + var r io.Reader = f + if max_size > -1 { + if info.Size() > max_size { + return nil, too_large(info.Size()) + } + // Guard against the file growing after the fstat() above + r = io.LimitReader(f, max_size+1) + } + // Pre-allocate based on the size, with some slack so that a file that is + // read completely does not need a final re-allocation to detect EOF. + buf := bytes.NewBuffer(make([]byte, 0, max(0, info.Size())+bytes.MinRead)) + if _, err = buf.ReadFrom(r); err != nil { + return nil, err + } + if max_size > -1 && int64(buf.Len()) > max_size { + return nil, too_large(int64(buf.Len())) + } + return buf.Bytes(), nil } func NewTextFilePreview(abspath string, metadata fs.FileInfo, highlighted_chan chan highlighed_data, sanitize func(string) string) Preview { - data, err := read_regular_text_file(abspath) + data, err := read_regular_file(abspath, MAX_TEXT_FILE_SIZE) if err != nil { return NewFileMetadataPreview(abspath, metadata) } @@ -301,7 +329,7 @@ func (s style_resolver) StyleName() string { func (s style_resolver) UseLightColors() bool { return s.light } func (s style_resolver) SyntaxAliases() map[string]string { return s.syntax_aliases } func (s style_resolver) TextForPath(path string) (string, error) { - ans, err := read_regular_text_file(path) + ans, err := read_regular_file(path, MAX_TEXT_FILE_SIZE) if err == nil { return utils.UnsafeBytesToString(ans), nil } @@ -351,6 +379,18 @@ func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Pr } return NewDirectoryPreview(abspath, s) } + // Only regular files (or symlinks to them) have previewable contents. + // Bail out before any previewer opens the file, as opening special files + // such as FIFOs can block or have side effects on other processes. + target := s + if s.Mode()&fs.ModeSymlink != 0 { + if target, err = os.Stat(abspath); err != nil { + return NewFileMetadataPreview(abspath, s) + } + } + if !target.Mode().IsRegular() { + return NewFileMetadataPreview(abspath, s) + } fname := filepath.Base(abspath) mt := utils.GuessMimeType(fname) for _, q := range previewers { @@ -358,8 +398,7 @@ func (pm *PreviewManager) preview_for(abspath string, ftype fs.FileMode) (ans Pr return NewCmdPreview(abspath, s, pm.settings, pm.WakeupMainThread, q) } } - const MAX_TEXT_FILE_SIZE = 16 * 1024 * 1024 - if s.Size() <= MAX_TEXT_FILE_SIZE && (utils.KnownTextualMimes[mt] || strings.HasPrefix(mt, "text/")) { + if target.Size() <= MAX_TEXT_FILE_SIZE && (utils.KnownTextualMimes[mt] || strings.HasPrefix(mt, "text/")) { ch := make(chan highlighed_data, 2) ans := NewTextFilePreview(abspath, s, ch, pm.highlighter.Sanitize) if p, ok := ans.(*TextFilePreview); ok { diff --git a/kittens/choose_files/preview_test.go b/kittens/choose_files/preview_test.go index 09f6d27f6..49c2cf8b2 100644 --- a/kittens/choose_files/preview_test.go +++ b/kittens/choose_files/preview_test.go @@ -1,25 +1,65 @@ package choose_files import ( + "errors" "os" "path/filepath" + "strings" "testing" "time" "golang.org/x/sys/unix" ) +const blocking_timeout = 2 * time.Second + +// Wait for result, failing the test if it takes too long. On timeout, the +// FIFO at fifo_path, if any, is opened for writing to unblock any reader +// stuck in open() so that the goroutine producing result does not leak. +func wait_for[T any](t *testing.T, result chan T, fifo_path, what string) (ans T) { + t.Helper() + select { + case ans = <-result: + return + case <-time.After(blocking_timeout): + if fifo_path != "" { + if f, err := os.OpenFile(fifo_path, os.O_WRONLY|unix.O_NONBLOCK, 0); err == nil { + f.Close() + } + } + t.Fatalf("%s blocked", what) + } + return +} + +func make_fifo(t *testing.T, path string) { + t.Helper() + if err := unix.Mkfifo(path, 0o600); err != nil { + t.Fatal(err) + } +} + func TestTextPreviewFileTypes(t *testing.T) { dir := t.TempDir() fifo_path := filepath.Join(dir, "pipe.txt") - if err := unix.Mkfifo(fifo_path, 0o600); err != nil { - t.Fatal(err) - } + make_fifo(t, fifo_path) + make_fifo(t, filepath.Join(dir, "pipe.png")) const content = "ordinary text\n" if err := os.WriteFile(filepath.Join(dir, "file.txt"), []byte(content), 0o600); err != nil { t.Fatal(err) } - for link, target := range map[string]string{"pipe-link.txt": "pipe.txt", "file-link.txt": "file.txt"} { + // A file over the size limit (with an extension not handled by calibre), + // which must not be previewed as text even via a symlink, whose own size + // is tiny + if err := os.WriteFile(filepath.Join(dir, "large.py"), nil, 0o600); err != nil { + t.Fatal(err) + } + if err := os.Truncate(filepath.Join(dir, "large.py"), MAX_TEXT_FILE_SIZE+1); err != nil { + t.Fatal(err) + } + for link, target := range map[string]string{ + "pipe-link.txt": "pipe.txt", "file-link.txt": "file.txt", "large-link.py": "large.py", "broken-link.txt": "missing.txt", + } { if err := os.Symlink(target, filepath.Join(dir, link)); err != nil { t.Fatal(err) } @@ -27,43 +67,42 @@ func TestTextPreviewFileTypes(t *testing.T) { pm := NewPreviewManager(make(chan error, 1), State{}, func() bool { return true }) for _, tc := range []struct { - name string - fifo bool + name string + metadata_only bool + fifo string }{ - {"pipe.txt", true}, {"pipe-link.txt", true}, - {"file.txt", false}, {"file-link.txt", false}, + {"pipe.txt", true, "pipe.txt"}, {"pipe-link.txt", true, "pipe.txt"}, {"pipe.png", true, "pipe.png"}, + {"large.py", true, ""}, {"large-link.py", true, ""}, {"broken-link.txt", true, ""}, + {"file.txt", false, ""}, {"file-link.txt", false, ""}, } { t.Run(tc.name, func(t *testing.T) { path := filepath.Join(dir, tc.name) + fifo := "" + if tc.fifo != "" { + fifo = filepath.Join(dir, tc.fifo) + } info, err := os.Lstat(path) if err != nil { t.Fatal(err) } result := make(chan Preview, 1) go func() { result <- pm.preview_for(path, info.Mode().Type()) }() - var p Preview - select { - case p = <-result: - case <-time.After(2 * time.Second): - t.Fatal("preview blocked") - } - if tc.fifo { + p := wait_for(t, result, fifo, "preview") + if tc.metadata_only { if _, ok := p.(*MessagePreview); !ok { t.Fatalf("expected a metadata preview, got %T", p) } + if fifo == "" { + return + } // The file can be replaced with a FIFO before highlighting opens it again. result := make(chan error, 1) go func() { _, err := (style_resolver{}).TextForPath(path) result <- err }() - select { - case err := <-result: - if err == nil { - t.Fatal("highlighting accepted a named pipe") - } - case <-time.After(2 * time.Second): - t.Fatal("highlighting blocked on a named pipe") + if err := wait_for(t, result, fifo, "highlighting"); err == nil { + t.Fatal("highlighting accepted a named pipe") } return } @@ -74,11 +113,59 @@ func TestTextPreviewFileTypes(t *testing.T) { if text.plain_text != content { t.Fatalf("expected %q, got %q", content, text.plain_text) } - select { - case <-text.highlighted_chan: - case <-time.After(2 * time.Second): - t.Fatal("text highlighting blocked") - } + wait_for(t, text.highlighted_chan, "", "text highlighting") }) } } + +func TestReadRegularFile(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "f") + const content = "0123456789" + if err := os.WriteFile(path, []byte(content), 0o600); err != nil { + t.Fatal(err) + } + for _, max_size := range []int64{-1, int64(len(content)), int64(len(content)) + 100} { + data, err := read_regular_file(path, max_size) + if err != nil { + t.Fatalf("max_size=%d: %s", max_size, err) + } + if string(data) != content { + t.Fatalf("max_size=%d: expected %q, got %q", max_size, content, data) + } + } + if _, err := read_regular_file(path, int64(len(content))-1); err == nil || !strings.Contains(err.Error(), "too large") { + t.Fatalf("expected a too large error, got: %v", err) + } + if _, err := read_regular_file(dir, -1); err == nil { + t.Fatal("reading a directory succeeded") + } + if _, err := read_regular_file(filepath.Join(dir, "missing"), -1); !errors.Is(err, os.ErrNotExist) { + t.Fatalf("expected a not exist error, got: %v", err) + } +} + +func TestScanIgnoreFileFIFO(t *testing.T) { + dir := t.TempDir() + fifo_path := filepath.Join(dir, ".ignore") + make_fifo(t, fifo_path) + if err := os.WriteFile(filepath.Join(dir, "file.txt"), nil, 0o600); err != nil { + t.Fatal(err) + } + ch := make(chan bool) + s := new_filesystem_scanner(dir, ch, nil) + done := make(chan bool, 1) + go func() { + for range ch { + } + done <- true + }() + s.Start() + wait_for(t, done, fifo_path, "scanning a directory with a FIFO ignore file") + if s.err != nil { + t.Fatal(s.err) + } + if n := s.Len(); n != 1 { + t.Fatalf("expected one scanned entry, got %d", n) + } +} diff --git a/kittens/choose_files/scan.go b/kittens/choose_files/scan.go index aa2b4acf5..588bee1c4 100644 --- a/kittens/choose_files/scan.go +++ b/kittens/choose_files/scan.go @@ -93,7 +93,9 @@ func new_filesystem_scanner(root_dir string, notify chan bool, filter_func func( ans.in_progress.Store(true) ans.keep_going.Store(true) ans.dir_reader = os.ReadDir - ans.file_reader = os.ReadFile + // Ignore files are read via a non-blocking open so that a FIFO with the + // name of an ignore file cannot stall the scan + ans.file_reader = func(path string) ([]byte, error) { return read_regular_file(path, -1) } ans.filter_func = utils.IfElse(filter_func == nil, accept_all, filter_func) ans.global_gitignore = ignorefiles.NewGitignore() ans.global_ignore = ignorefiles.NewGitignore()