Cleanup previous PR

Drop the redundant v2 prefix from the disk cache key, as the key format
already changed when the inode was added.

Render the file the cache key was computed from, so that retargeting a
symlink while rendering, even if it is restored before AddPath() checks
the key again, cannot store the preview of one file under the key of
another. Renderers that need the selected path, custom preview commands
and calibre, opt out via NeedsSelectedPath(). The selected path is also
used when the target has a different extension, as renderers may detect
the format from it.
This commit is contained in:
Kovid Goyal 2026-11-17 19:11:40 +05:30
parent bc7d4664d8
commit 517f418f9c
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
7 changed files with 126 additions and 11 deletions

View file

@ -299,6 +299,10 @@ func resolve_path(path string) (string, error) {
// checks that cached metadata belongs to the selected book.
func (c calibre_renderer) CacheKeyExtra(string) string { return "" }
// NeedsSelectedPath returns true as calibre reads metadata from the file name
// and from an .opf file next to it.
func (c calibre_renderer) NeedsSelectedPath() bool { return true }
func (c calibre_renderer) Unmarshall(m map[string]string) (any, error) {
data, err := os.ReadFile(m[CALIBRE_METADATA_KEY])
if err != nil {

View file

@ -35,6 +35,10 @@ func (c cmd_renderer) CacheKeyExtra(path string) string {
return fmt.Sprintf("%q", append(slices.Clone(c.cmdline), path))
}
// NeedsSelectedPath returns true as the command is documented to receive the
// selected path.
func (c cmd_renderer) NeedsSelectedPath() bool { return true }
type CmdResult struct {
Lines []string `json:"lines"`
Image string `json:"image"`

View file

@ -136,6 +136,8 @@ func (c ffmpeg_renderer) CacheKeyExtra(string) string {
return fmt.Sprintf("%d:%d:%g:%d", video_width, video_fps, video_duration, video_encoding_quality)
}
func (c ffmpeg_renderer) NeedsSelectedPath() bool { return false }
func (c ffmpeg_renderer) Unmarshall(m map[string]string) (any, error) {
data, err := os.ReadFile(m[FFMPEG_METADATA_KEY])
if err != nil {

View file

@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
@ -50,6 +51,10 @@ type PreviewRenderer interface {
// path that the rendered output depends on. It is added to the disk cache
// key. Return an empty string if the output depends only on the file contents.
CacheKeyExtra(path string) string
// NeedsSelectedPath returns true if Render() must be passed the selected
// path rather than the file it resolves to, for example, because its
// output depends on the name or location of the selected path.
NeedsSelectedPath() bool
}
type render_data struct {
@ -203,7 +208,7 @@ func (p *ImagePreview) start_rendering(output chan<- render_data) {
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)
key, target, ans, err := p.disk_cache.GetPathWithTarget(p.abspath, extra_key)
if err != nil {
return render_data{err: err}
}
@ -223,7 +228,7 @@ func (p *ImagePreview) render_with_cache() render_data {
return render_data{cached_data: ans, metadata: mi}
}
}
rdata, mi, img, err := p.renderer.Render(p.abspath)
rdata, mi, img, err := p.renderer.Render(p.path_to_render(target))
if err != nil {
return render_data{err: err}
}
@ -234,6 +239,20 @@ func (p *ImagePreview) render_with_cache() render_data {
return render_data{cached_data: ans, metadata: mi, img: img}
}
// path_to_render returns the path to pass to the renderer. This is target,
// the file the cache key describes, so that retargeting a symlink while
// rendering cannot store the preview of one file under the key of another.
// Renderers may detect the file format from its extension, so the selected
// path is used if target has a different extension. When the selected path is
// used, AddPath() detects changes instead, except for a symlink retargeted and
// then restored while rendering.
func (p *ImagePreview) path_to_render(target string) string {
if p.renderer.NeedsSelectedPath() || !strings.EqualFold(filepath.Ext(target), filepath.Ext(p.abspath)) {
return p.abspath
}
return target
}
type ImagePreviewRenderer uint
func (p ImagePreviewRenderer) String() string { return "ImagePreviewRenderer" }
@ -259,6 +278,7 @@ func (p ImagePreviewRenderer) Render(abspath string) (ans map[string][]byte, m m
func (p ImagePreviewRenderer) Unmarshall(map[string]string) (any, error) { return nil, nil }
func (p ImagePreviewRenderer) CacheKeyExtra(string) string { return "" }
func (p ImagePreviewRenderer) NeedsSelectedPath() bool { return false }
func (p ImagePreviewRenderer) ShowMetadata(h *Handler, s ShowData) int {
offset := 0

View file

@ -36,6 +36,7 @@ func (r fake_preview_renderer) Render(path string) (map[string][]byte, metadata,
}
func (r fake_preview_renderer) ShowMetadata(*Handler, ShowData) int { return 0 }
func (r fake_preview_renderer) CacheKeyExtra(string) string { return "" }
func (r fake_preview_renderer) NeedsSelectedPath() bool { return false }
func new_test_image_preview(t *testing.T) *ImagePreview {
t.Helper()
@ -176,6 +177,81 @@ func TestImagePreviewRetriesChangedFile(t *testing.T) {
}
}
func resolved(t *testing.T, path string) string {
t.Helper()
ans, err := filepath.EvalSymlinks(path)
if err != nil {
t.Fatal(err)
}
return ans
}
func TestImagePreviewRendersSymlinkTarget(t *testing.T) {
for _, tc := range []struct {
name, target_name string
want_target bool
}{
{"same extension", "target.png", true},
{"different extension", "target.blob", false},
} {
t.Run(tc.name, func(t *testing.T) {
p := new_test_image_preview(t)
dir := filepath.Dir(p.abspath)
target := filepath.Join(dir, tc.target_name)
other := filepath.Join(dir, "other"+filepath.Ext(tc.target_name))
for _, path := range []string{target, other} {
if err := os.WriteFile(path, []byte(filepath.Base(path)), 0o600); err != nil {
t.Fatal(err)
}
}
link := filepath.Join(dir, "link.png")
retarget := func(dest string) error {
temp := link + ".temp"
if err := os.Symlink(dest, temp); err != nil {
return err
}
return os.Rename(temp, link)
}
if err := retarget(target); err != nil {
t.Fatal(err)
}
p.abspath = link
var rendered_path, rendered_contents string
p.renderer = fake_preview_renderer{before_render: func(path string) error {
// Retarget the link to another file while rendering and
// restore it before AddPath() checks the key again.
if err := retarget(other); err != nil {
return err
}
data, err := os.ReadFile(path)
rendered_path, rendered_contents = path, string(data)
if rerr := retarget(target); rerr != nil {
return rerr
}
return err
}}
p.start_rendering(p.render_channel)
if result := <-p.render_channel; result.err != nil {
t.Fatalf("unexpected error: %v", result.err)
}
if !tc.want_target {
// Renderers can detect the format from the extension, so
// they get the selected path when the target has a different one.
if rendered_path != link {
t.Fatalf("rendered %s, want %s", rendered_path, link)
}
return
}
if want := resolved(t, target); rendered_path != want {
t.Fatalf("rendered %s, want %s", rendered_path, want)
}
if rendered_contents != filepath.Base(target) {
t.Fatalf("rendered the contents %q of the wrong file", rendered_contents)
}
})
}
}
func TestCmdPreviewConcurrentRendering(t *testing.T) {
dir := t.TempDir()
script := filepath.Join(dir, "preview.sh")

View file

@ -67,12 +67,21 @@ func (dc *DiskCache) Get(key string, items ...string) (ans map[string]string, er
// verbatim, it is not normalized in any way. Pass the same path and extra_key
// to AddPath() when adding the items for key.
func (dc *DiskCache) GetPath(path, extra_key string, items ...string) (key string, ans map[string]string, err error) {
key, _, ans, err = dc.GetPathWithTarget(path, extra_key, items...)
return
}
// GetPathWithTarget is the same as GetPath() except that it also returns the
// absolute path that path resolved to when computing key. Generate the data to
// cache from target rather than path, so that the data describes the same file
// as key, even if a symlink in path is changed concurrently.
func (dc *DiskCache) GetPathWithTarget(path, extra_key string, items ...string) (key, target 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, extra_key); err != nil {
return "", nil, err
if key, target, err = key_for_path(path, extra_key); err != nil {
return "", "", nil, err
}
if ans, err = dc.get(key, items); err == nil && len(ans) > 0 {
// Record which entry path now uses so that its previous entry is
@ -111,7 +120,7 @@ func (dc *DiskCache) AddPath(path, extra_key, key string, items map[string][]byt
return
}
defer dc.unlock_and_set_err(&err)
current_key, err := key_for_path(path, extra_key)
current_key, _, err := key_for_path(path, extra_key)
if err != nil {
return nil, err
}

View file

@ -95,9 +95,10 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
return ans, nil
}
func key_for_path(path, extra_key string) (key string, err error) {
resolved_path, err := filepath.EvalSymlinks(path)
if err != nil {
// key_for_path returns the cache key for path and the absolute path of the
// file it resolves to, whose state the key describes.
func key_for_path(path, extra_key string) (key, resolved_path string, err error) {
if resolved_path, err = filepath.EvalSymlinks(path); err != nil {
return
}
if resolved_path, err = filepath.Abs(resolved_path); err != nil {
@ -109,13 +110,12 @@ func key_for_path(path, extra_key string) (key string, err error) {
return
}
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())
data := fmt.Sprintf("%s\x00%d\x00%d\x00%d", resolved_path, s.Inode, s.Size, s.ModTime.UnixNano())
if extra_key != "" {
data += "\x00" + extra_key
}
sum := sha256.Sum256(utils.UnsafeStringToBytes(data))
return hex.EncodeToString(sum[:]), nil
return hex.EncodeToString(sum[:]), resolved_path, nil
}
// lock() and unlock() must be paired: unlock() must only be called after a