diff --git a/docs/changelog.rst b/docs/changelog.rst index 9b022450e..b676eb22b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 `: Fix custom previews being shared + between different symlinks to the same file + - :doc:`Pointer shapes `: Fix animated pointers restarting unnecessarily (:iss:`10838`) and the shape not updating when switching to or from the alternate screen diff --git a/kittens/choose_files/image_preview.go b/kittens/choose_files/image_preview.go index c2c3093e6..9a3befd4c 100644 --- a/kittens/choose_files/image_preview.go +++ b/kittens/choose_files/image_preview.go @@ -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 diff --git a/kittens/choose_files/image_preview_test.go b/kittens/choose_files/image_preview_test.go index 08aa99f48..b6ec13429 100644 --- a/kittens/choose_files/image_preview_test.go +++ b/kittens/choose_files/image_preview_test.go @@ -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") diff --git a/tools/disk_cache/api.go b/tools/disk_cache/api.go index b0be5033d..1f6175f95 100644 --- a/tools/disk_cache/api.go +++ b/tools/disk_cache/api.go @@ -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) diff --git a/tools/disk_cache/implementation.go b/tools/disk_cache/implementation.go index ebe77b2a2..b5d94f426 100644 --- a/tools/disk_cache/implementation.go +++ b/tools/disk_cache/implementation.go @@ -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 }