mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-17 05:32:41 +00:00
choose-files: Cleanup previous PR
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Some checks are pending
CI / Linux (python=3.14 cc=clang sanitize=1) (push) Waiting to run
CI / Linux (python=3.12 cc=gcc sanitize=0) (push) Waiting to run
CI / Linux (python=3.13 cc=gcc sanitize=1) (push) Waiting to run
CI / Linux package (push) Waiting to run
CI / Bundle test (macos-latest) (push) Waiting to run
CI / Bundle test (ubuntu-latest) (push) Waiting to run
CI / macOS Brew (push) Waiting to run
CI / Test ./dev.sh and benchmark (push) Waiting to run
CodeQL / CodeQL-Build (actions, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, macos-latest) (push) Waiting to run
CodeQL / CodeQL-Build (c, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (go, ubuntu-latest) (push) Waiting to run
CodeQL / CodeQL-Build (python, ubuntu-latest) (push) Waiting to run
Depscan / Scan dependencies for vulnerabilities (push) Waiting to run
Always reply from the calibre worker, even when reading metadata raises or calibre emits stray output, so that the request does not block until it times out. Accept cached calibre metadata generated for a different path to the same file, as the disk cache key resolves symlinks. Otherwise a symlink and its target keep invalidating each other's cache entry.
This commit is contained in:
parent
245681507a
commit
a95310e8bc
3 changed files with 79 additions and 10 deletions
|
|
@ -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 <kittens/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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue