mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-18 06:04:49 +00:00
choose-files: Avoid hanging when previewing named pipes
Open text preview files in nonblocking mode and check the type on the opened descriptor before reading. Reuse the same read for syntax highlighting because it can reopen a path after the file changes.
This commit is contained in:
parent
de20cf35e7
commit
cdd278ee63
3 changed files with 108 additions and 2 deletions
|
|
@ -125,6 +125,9 @@ Detailed list of changes
|
|||
0.50.1 [unreleased]
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
- :doc:`choose-files <kittens/choose-files>`: Fix a hang when previewing named
|
||||
pipes as text
|
||||
|
||||
- :doc:`Custom shaders <custom-shaders>`: Add ``screen-shake`` and
|
||||
``screen-shake-random`` shaders that shake the window in which a bell occurs,
|
||||
for example, to shake the window when a shell command fails
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package choose_files
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"maps"
|
||||
"os"
|
||||
|
|
@ -20,6 +21,7 @@ import (
|
|||
"github.com/kovidgoyal/kitty/tools/utils/humanize"
|
||||
"github.com/kovidgoyal/kitty/tools/utils/style"
|
||||
"github.com/kovidgoyal/kitty/tools/wcswidth"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
|
|
@ -257,8 +259,25 @@ 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.
|
||||
f, err := os.OpenFile(path, os.O_RDONLY|unix.O_NONBLOCK, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
return nil, fmt.Errorf("%q is not a regular file", path)
|
||||
}
|
||||
return io.ReadAll(f)
|
||||
}
|
||||
|
||||
func NewTextFilePreview(abspath string, metadata fs.FileInfo, highlighted_chan chan highlighed_data, sanitize func(string) string) Preview {
|
||||
data, err := os.ReadFile(abspath)
|
||||
data, err := read_regular_text_file(abspath)
|
||||
if err != nil {
|
||||
return NewFileMetadataPreview(abspath, metadata)
|
||||
}
|
||||
|
|
@ -282,7 +301,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 := os.ReadFile(path)
|
||||
ans, err := read_regular_text_file(path)
|
||||
if err == nil {
|
||||
return utils.UnsafeBytesToString(ans), nil
|
||||
}
|
||||
|
|
|
|||
84
kittens/choose_files/preview_test.go
Normal file
84
kittens/choose_files/preview_test.go
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
package choose_files
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
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)
|
||||
}
|
||||
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"} {
|
||||
if err := os.Symlink(target, filepath.Join(dir, link)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
pm := NewPreviewManager(make(chan error, 1), State{}, func() bool { return true })
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
fifo bool
|
||||
}{
|
||||
{"pipe.txt", true}, {"pipe-link.txt", true},
|
||||
{"file.txt", false}, {"file-link.txt", false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
path := filepath.Join(dir, tc.name)
|
||||
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 {
|
||||
if _, ok := p.(*MessagePreview); !ok {
|
||||
t.Fatalf("expected a metadata preview, got %T", p)
|
||||
}
|
||||
// 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")
|
||||
}
|
||||
return
|
||||
}
|
||||
text, ok := p.(*TextFilePreview)
|
||||
if !ok {
|
||||
t.Fatalf("expected a text preview, got %T", p)
|
||||
}
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue