diff --git a/docs/changelog.rst b/docs/changelog.rst index fe3167d49..78bff2966 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -148,6 +148,9 @@ Detailed list of changes commands not being cached or receiving the wrong path, and respect the maximum text preview size for symlinks +- :doc:`choose-files `: Fix e-book previews sometimes + showing the metadata and cover of a different book (:pull:`10843`) + - Bash integration: Source the system wide profile and bashrc files that bash was actually built to use, found by scanning the bash executable, instead of guessing their locations in :file:`/etc`. Fixes system startup files not diff --git a/kittens/choose_files/calibre.go b/kittens/choose_files/calibre.go index 8b35de44b..d48648a84 100644 --- a/kittens/choose_files/calibre.go +++ b/kittens/choose_files/calibre.go @@ -88,15 +88,24 @@ def serve(): read_metadata = cli.one def reply_with_id(line, lock): - request_id = json.loads(line)['id'] + request = json.loads(line) + request_id = request['id'] stdout = sys.__stdout__ - with io.StringIO() as output: - try: - sys.__stdout__ = output - read_metadata(line, nullcontext()) - finally: - sys.__stdout__ = stdout - response = json.loads(output.getvalue()) + # Always reply, otherwise the request blocks until it times out. + try: + with io.StringIO() as output: + try: + sys.__stdout__ = output + read_metadata(line, nullcontext()) + finally: + sys.__stdout__ = stdout + # The response is the last line, ignore any stray output before it. + lines = output.getvalue().splitlines() + response = json.loads(lines[-1]) if lines else None + if not isinstance(response, dict): + raise ValueError('no metadata response from calibre') + except Exception as e: + response = {'path': request['path'], 'error': f'reading metadata failed: {e}'} with lock: print(json.dumps({'id': request_id, 'response': response}), file=stdout, flush=True) @@ -259,11 +268,15 @@ type calibre_renderer struct { } func unmarshal_calibre_response(data []byte, path string) (*CalibreResponse, error) { + return unmarshal_calibre_response_for(data, path, func(p string) bool { return p == path }) +} + +func unmarshal_calibre_response_for(data []byte, path string, is_for_path func(string) bool) (*CalibreResponse, error) { var ans CalibreResponse if err := json.Unmarshal(data, &ans); err != nil { return nil, err } - if ans.Path != path { + if !is_for_path(ans.Path) { return nil, fmt.Errorf("calibre metadata is for %q, expected %q", ans.Path, path) } if ans.Error != "" { @@ -272,12 +285,34 @@ func unmarshal_calibre_response(data []byte, path string) (*CalibreResponse, err return &ans, nil } +// resolve_path resolves a path the same way the disk cache does when +// computing its key, so that paths sharing a cache entry compare equal. +func resolve_path(path string) (string, error) { + path, err := filepath.EvalSymlinks(path) + if err != nil { + return "", err + } + return filepath.Abs(path) +} + func (c calibre_renderer) Unmarshall(m map[string]string) (any, error) { data, err := os.ReadFile(m[CALIBRE_METADATA_KEY]) if err != nil { return nil, err } - ans, err := unmarshal_calibre_response(data, c.path) + // The disk cache key resolves symlinks, so the cached metadata may + // have been generated for a different path to the same file. + ans, err := unmarshal_calibre_response_for(data, c.path, func(cached_path string) bool { + if cached_path == c.path { + return true + } + a, err := resolve_path(cached_path) + if err != nil { + return false + } + b, err := resolve_path(c.path) + return err == nil && a == b + }) if err != nil { return nil, err } diff --git a/kittens/choose_files/calibre_test.go b/kittens/choose_files/calibre_test.go index 14e0c8be9..6fd74b62b 100644 --- a/kittens/choose_files/calibre_test.go +++ b/kittens/choose_files/calibre_test.go @@ -116,3 +116,34 @@ func TestCalibreCachedMetadataPath(t *testing.T) { t.Fatalf("expected cached title %q, got %q", "Book", title) } } + +func TestCalibreCachedMetadataSymlink(t *testing.T) { + tdir := t.TempDir() + book := filepath.Join(tdir, "book.epub") + link := filepath.Join(tdir, "link.epub") + other := filepath.Join(tdir, "other.epub") + for _, p := range []string{book, other} { + if err := os.WriteFile(p, []byte("x"), 0600); err != nil { + t.Fatal(err) + } + } + if err := os.Symlink(book, link); err != nil { + t.Fatal(err) + } + data, err := json.Marshal(CalibreResponse{Path: link, Metadata: CalibreMetadata{Title: "Book"}}) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(tdir, CALIBRE_METADATA_KEY) + if err := os.WriteFile(path, data, 0600); err != nil { + t.Fatal(err) + } + files := map[string]string{CALIBRE_METADATA_KEY: path} + // The disk cache shares one entry between a symlink and its target + if _, err := (calibre_renderer{path: book}).Unmarshall(files); err != nil { + t.Fatalf("rejected metadata cached via a symlink: %v", err) + } + if _, err := (calibre_renderer{path: other}).Unmarshall(files); err == nil { + t.Fatal("accepted metadata for another book") + } +}