choose-files: Keep custom preview caches separate for symlinks

Custom preview commands receive the selected path. Include that path
in their cache key so that symlinks to the same file can have different
previews. Keep the resolved target in the key so retargeting a link
still invalidates its entry.
This commit is contained in:
Zhizhen He 2026-11-11 23:32:42 +08:00
parent 14f82c6fcb
commit a1f5604cc2
No known key found for this signature in database
GPG key ID: B5A215D74FCFD6A7
5 changed files with 91 additions and 12 deletions

View file

@ -157,6 +157,9 @@ Detailed list of changes
files, various preview caching bugs and e-book previews showing the wrong book
(:pull:`10843`)
- :doc:`choose-files <kittens/choose-files>`: Fix custom previews being shared
between different symlinks to the same file
- :doc:`Pointer shapes <pointer-shapes>`: Fix animated pointers restarting
unnecessarily (:iss:`10838`) and the shape not updating when switching to or
from the alternate screen

View file

@ -186,7 +186,12 @@ func (p *ImagePreview) start_rendering(output chan<- render_data) {
p.ready.Store(true)
p.WakeupMainThread()
}()
key, ans, err := p.disk_cache.GetPath(p.abspath)
get_path := p.disk_cache.GetPath
if _, is_command := p.renderer.(cmd_renderer); is_command {
// A custom preview command sees the selected path and can produce different output for each alias.
get_path = p.disk_cache.GetPathWithAlias
}
key, ans, err := get_path(p.abspath)
if err != nil {
output <- render_data{err: err}
return

View file

@ -354,6 +354,64 @@ func TestCmdPreviewDiskCache(t *testing.T) {
}
}
func TestCmdPreviewDiskCacheSymlinks(t *testing.T) {
dir := t.TempDir()
first_target := filepath.Join(dir, "first.txt")
other_target := filepath.Join(dir, "other.txt")
mtime := time.Unix(1700000000, 0)
for _, path := range []string{first_target, other_target} {
// Give both targets identical sizes and mtimes so retargeting must change the cache key.
if err := os.WriteFile(path, []byte(filepath.Base(path)+"\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(path, mtime, mtime); err != nil {
t.Fatal(err)
}
}
one, two := filepath.Join(dir, "one.txt"), filepath.Join(dir, "two.txt")
for _, path := range []string{one, two} {
if err := os.Symlink(first_target, path); err != nil {
t.Fatal(err)
}
}
runs_log := filepath.Join(dir, "runs")
t.Setenv("KITTY_TEST_CMD_PREVIEW_RUNS", runs_log)
script := filepath.Join(dir, "preview.sh")
body := `#!/bin/sh
echo run >> "$KITTY_TEST_CMD_PREVIEW_RUNS"
IFS= read -r target < "$1"
printf '{"lines":["%s:%s"]}\n' "${1##*/}" "$target"
`
if err := os.WriteFile(script, []byte(body), 0o700); err != nil {
t.Fatal(err)
}
cache_dir := filepath.Join(dir, "cache")
renderer := cmd_renderer{cmdline: []string{script}}
check := func(path, target string) {
t.Helper()
hd := render_through_cache(t, path, cache_dir, renderer)
want := filepath.Base(path) + ":" + filepath.Base(target)
if r, ok := hd.metadata.custom.(*CmdResult); !ok || len(r.Lines) != 1 || r.Lines[0] != want {
t.Errorf("preview for %q should contain %q, got %#v", filepath.Base(path), want, hd.metadata.custom)
}
}
for _, path := range []string{one, two, one, two} {
check(path, first_target)
}
if err := os.Remove(one); err != nil {
t.Fatal(err)
}
if err := os.Symlink(other_target, one); err != nil {
t.Fatal(err)
}
check(one, other_target)
check(two, first_target)
if n := count_runs(t, runs_log); n != 3 {
t.Errorf("preview command ran %d times, want 3 for the two links and the retargeted link", n)
}
}
func TestCmdPreviewInvalidJSON(t *testing.T) {
dir := t.TempDir()
script := filepath.Join(dir, "preview.sh")

View file

@ -58,11 +58,20 @@ func (dc *DiskCache) Get(key string, items ...string) (ans map[string]string, er
}
func (dc *DiskCache) GetPath(path string, items ...string) (key string, ans map[string]string, err error) {
return dc.get_path(path, false, items...)
}
// GetPathWithAlias includes the supplied path as well as the resolved file in the cache key.
func (dc *DiskCache) GetPathWithAlias(path string, items ...string) (key string, ans map[string]string, err error) {
return dc.get_path(path, true, items...)
}
func (dc *DiskCache) get_path(path string, include_alias bool, items ...string) (key string, ans map[string]string, err error) {
if err = dc.lock(); err != nil {
return
}
defer dc.unlock_and_set_err(&err)
if key, err = key_for_path(path); err != nil {
if key, err = key_for_path(path, include_alias); err != nil {
return "", nil, err
}
ans, err = dc.get(key, items)

View file

@ -95,19 +95,23 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
return ans, nil
}
func key_for_path(path string) (key string, err error) {
if path, err = filepath.EvalSymlinks(path); err != nil {
return
}
if path, err = filepath.Abs(path); err != nil {
return
}
s, err := os.Stat(path)
func key_for_path(path string, include_alias bool) (key string, err error) {
resolved_path, err := filepath.EvalSymlinks(path)
if err != nil {
return
}
data := fmt.Sprintf("%s\x00%d\x00%d", path, s.Size(), s.ModTime().UnixNano())
if resolved_path, err = filepath.Abs(resolved_path); err != nil {
return
}
s, err := os.Stat(resolved_path)
if err != nil {
return
}
data := fmt.Sprintf("%s\x00%d\x00%d", resolved_path, s.Size(), s.ModTime().UnixNano())
if include_alias {
data += "\x00" + path
}
sum := sha256.Sum256(utils.UnsafeStringToBytes(data))
return hex.EncodeToString(sum[:]), nil
}