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:
Zhizhen He 2026-11-17 20:47:47 +08:00
parent 24fdd01944
commit bc7d4664d8
6 changed files with 132 additions and 20 deletions

View file

@ -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)
}

View file

@ -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
}

View file

@ -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)