mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 14:45:29 +00:00
choose-files: Reject stale preview cache writes
Recheck the path key before updating the cache. Include the inode so a replacement with the same size and modification time gets a different key. Version the key to invalidate previously cached previews. Retry once when a changed path still resolves to a regular file.
This commit is contained in:
parent
24fdd01944
commit
bc7d4664d8
6 changed files with 132 additions and 20 deletions
|
|
@ -196,6 +196,9 @@ Detailed list of changes
|
|||
they cannot inject escape codes into the terminal. Fix hangs previewing
|
||||
special files and various preview caching bugs (:pull:`10843`, :pull:`10850`)
|
||||
|
||||
- :doc:`choose-files <kittens/choose-files>`: Fix incorrect cached previews
|
||||
when a file or symlink changes during rendering.
|
||||
|
||||
- IME: Move cursor position within pre-edit text (:iss:`10807`)
|
||||
|
||||
- Bash integration: Find system wide startup files by scanning the bash
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package choose_files
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
|
@ -190,11 +191,21 @@ func (p *ImagePreview) start_rendering(output chan<- render_data) {
|
|||
p.ready.Store(true)
|
||||
p.WakeupMainThread()
|
||||
}()
|
||||
result := p.render_with_cache()
|
||||
if errors.Is(result.err, disk_cache.ErrPathChanged) {
|
||||
// The original check in preview_for no longer describes this path.
|
||||
if info, err := os.Stat(p.abspath); err == nil && info.Mode().IsRegular() {
|
||||
result = p.render_with_cache()
|
||||
}
|
||||
}
|
||||
output <- result
|
||||
}
|
||||
|
||||
func (p *ImagePreview) render_with_cache() render_data {
|
||||
extra_key := p.renderer.CacheKeyExtra(p.abspath)
|
||||
key, ans, err := p.disk_cache.GetPath(p.abspath, extra_key)
|
||||
if err != nil {
|
||||
output <- render_data{err: err}
|
||||
return
|
||||
return render_data{err: err}
|
||||
}
|
||||
if len(ans) > 0 {
|
||||
mi := metadata{}
|
||||
|
|
@ -209,21 +220,18 @@ func (p *ImagePreview) start_rendering(output chan<- render_data) {
|
|||
}
|
||||
}
|
||||
if mi.custom != nil || mi.image != nil {
|
||||
output <- render_data{cached_data: ans, metadata: mi}
|
||||
return
|
||||
return render_data{cached_data: ans, metadata: mi}
|
||||
}
|
||||
}
|
||||
rdata, metadata, img, err := p.renderer.Render(p.abspath)
|
||||
rdata, mi, img, err := p.renderer.Render(p.abspath)
|
||||
if err != nil {
|
||||
output <- render_data{err: err}
|
||||
} else {
|
||||
ans, err = p.disk_cache.AddPath(p.abspath, extra_key, key, rdata)
|
||||
if err == nil {
|
||||
output <- render_data{cached_data: ans, metadata: metadata, img: img}
|
||||
} else {
|
||||
output <- render_data{err: err}
|
||||
}
|
||||
return render_data{err: err}
|
||||
}
|
||||
ans, err = p.disk_cache.AddPath(p.abspath, extra_key, key, rdata)
|
||||
if err != nil {
|
||||
return render_data{err: err}
|
||||
}
|
||||
return render_data{cached_data: ans, metadata: mi, img: img}
|
||||
}
|
||||
|
||||
type ImagePreviewRenderer uint
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package choose_files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
|
|
@ -17,13 +18,20 @@ import (
|
|||
|
||||
var _ = fmt.Print
|
||||
|
||||
type fake_preview_renderer struct{}
|
||||
type fake_preview_renderer struct {
|
||||
before_render func(string) error
|
||||
}
|
||||
|
||||
func (r fake_preview_renderer) String() string { return "fake_preview_renderer" }
|
||||
func (r fake_preview_renderer) Unmarshall(map[string]string) (any, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (r fake_preview_renderer) Render(string) (map[string][]byte, metadata, *images.ImageData, error) {
|
||||
func (r fake_preview_renderer) Render(path string) (map[string][]byte, metadata, *images.ImageData, error) {
|
||||
if r.before_render != nil {
|
||||
if err := r.before_render(path); err != nil {
|
||||
return nil, metadata{}, nil, err
|
||||
}
|
||||
}
|
||||
return map[string][]byte{"rendered": []byte("data")}, metadata{custom: "rendered"}, nil, nil
|
||||
}
|
||||
func (r fake_preview_renderer) ShowMetadata(*Handler, ShowData) int { return 0 }
|
||||
|
|
@ -134,6 +142,40 @@ func TestImagePreviewConcurrentRender(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestImagePreviewRetriesChangedFile(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
changes int
|
||||
}{
|
||||
{"stable on retry", 1},
|
||||
{"changes again", 2},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := new_test_image_preview(t)
|
||||
renders := 0
|
||||
p.renderer = fake_preview_renderer{before_render: func(path string) error {
|
||||
renders++
|
||||
if renders <= tc.changes {
|
||||
return os.WriteFile(path, []byte(strings.Repeat("updated ", renders)), 0o600)
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
p.start_rendering(p.render_channel)
|
||||
result := <-p.render_channel
|
||||
if renders != 2 {
|
||||
t.Fatalf("rendered %d times, want 2", renders)
|
||||
}
|
||||
if tc.changes == 2 {
|
||||
if !errors.Is(result.err, disk_cache.ErrPathChanged) {
|
||||
t.Fatalf("expected a file change error, got %v", result.err)
|
||||
}
|
||||
} else if result.err != nil || result.cached_data["rendered"] == "" {
|
||||
t.Fatalf("retry did not return a cached preview: %+v", result)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCmdPreviewConcurrentRendering(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
script := filepath.Join(dir, "preview.sh")
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package disk_cache
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
|
|
@ -9,6 +10,8 @@ import (
|
|||
|
||||
var _ = fmt.Print
|
||||
|
||||
var ErrPathChanged = errors.New("file changed while generating cached data")
|
||||
|
||||
type Entry struct {
|
||||
Key string
|
||||
Size int64
|
||||
|
|
@ -58,8 +61,8 @@ func (dc *DiskCache) Get(key string, items ...string) (ans map[string]string, er
|
|||
}
|
||||
|
||||
// GetPath returns the cache key for the file at path and the cached items for
|
||||
// that key. The key depends on the resolved path of the file, its size and its
|
||||
// modification time. Use extra_key to add anything else the cached data
|
||||
// that key. The key depends on the resolved path of the file, its inode, size
|
||||
// and modification time. Use extra_key to add anything else the cached data
|
||||
// depends on, such as the command used to generate it. extra_key is used
|
||||
// verbatim, it is not normalized in any way. Pass the same path and extra_key
|
||||
// to AddPath() when adding the items for key.
|
||||
|
|
@ -102,11 +105,19 @@ func (dc *DiskCache) Clear() (err error) {
|
|||
}
|
||||
|
||||
// AddPath adds items for the key returned by GetPath(path, extra_key).
|
||||
// The error wraps ErrPathChanged if the path now has a different cache key.
|
||||
func (dc *DiskCache) AddPath(path, extra_key, key string, items map[string][]byte) (ans map[string]string, err error) {
|
||||
if err = dc.lock(); err != nil {
|
||||
return
|
||||
}
|
||||
defer dc.unlock_and_set_err(&err)
|
||||
current_key, err := key_for_path(path, extra_key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if current_key != key {
|
||||
return nil, fmt.Errorf("%w: %s", ErrPathChanged, path)
|
||||
}
|
||||
return dc.add_path(path_map_key(path, extra_key), key, items)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -104,11 +104,13 @@ func key_for_path(path, extra_key string) (key string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
s, err := os.Stat(resolved_path)
|
||||
info, err := os.Stat(resolved_path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
data := fmt.Sprintf("%s\x00%d\x00%d", resolved_path, s.Size(), s.ModTime().UnixNano())
|
||||
s := get_file_state(info)
|
||||
// Version the key so entries written with weaker source validation miss.
|
||||
data := fmt.Sprintf("v2\x00%s\x00%d\x00%d\x00%d", resolved_path, s.Inode, s.Size, s.ModTime.UnixNano())
|
||||
if extra_key != "" {
|
||||
data += "\x00" + extra_key
|
||||
}
|
||||
|
|
|
|||
|
|
@ -129,6 +129,9 @@ func TestDiskCache(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
key, _, err := dc.GetPath(path, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = dc.AddPath(path, "", key, map[string][]byte{"1": []byte("1")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -139,7 +142,14 @@ func TestDiskCache(t *testing.T) {
|
|||
if err = os.WriteFile(path, []byte("changed contents"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = dc.AddPath(path, "", key, map[string][]byte{"1": []byte("2")}); err != nil {
|
||||
new_key, _, err := dc.GetPath(path, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if new_key == key {
|
||||
t.Fatal("expected a new key after changing the file")
|
||||
}
|
||||
if _, err = dc.AddPath(path, "", new_key, map[string][]byte{"1": []byte("2")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(1, len(dc.entries.PathMap)); diff != "" {
|
||||
|
|
@ -366,6 +376,42 @@ func TestDiskCachePathExtraKey(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDiskCacheRejectsStalePathKey(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
dc, err := NewDiskCache(filepath.Join(tdir, "cache"), 1024*1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(tdir, "source")
|
||||
if err = os.WriteFile(path, []byte("original"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key, _, err := dc.GetPath(path, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replacement := filepath.Join(tdir, "replacement")
|
||||
if err = os.WriteFile(replacement, []byte("replaced"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Chtimes(replacement, info.ModTime(), info.ModTime()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Rename(replacement, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = dc.AddPath(path, "", key, map[string][]byte{"data": []byte("preview")}); err == nil {
|
||||
t.Fatal("AddPath accepted a key for the replaced file")
|
||||
}
|
||||
if ans, err := dc.Get(key); err != nil || len(ans) != 0 {
|
||||
t.Fatalf("stale key should have no cached items, got %v, %v", ans, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiskCacheSharedPathEntries(t *testing.T) {
|
||||
tdir := t.TempDir()
|
||||
dc, err := NewDiskCache(filepath.Join(tdir, "cache"), 1024*1024)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue