Files
Vito 96ce361b82 fix(paths): a backslash inside a name works on Linux and macOS servers (#646) (#649)
* fix(paths): a backslash inside a name works on Linux and macOS servers (#646)

On POSIX a backslash is an ordinary character, and a Windows-style
filename_prefix makes a folder whose name holds one. The listing named such
a file, but the workspace and the tag, favorite, collection, delete,
thumbnail and folder-filter routes refused it or split the name at the
backslash. Separators are now the server's own: / always, \ only on
Windows. Windows keeps every refusal it had.

The listing marks a path holding a backslash with path_style "posix", and
the workspace accepts the backslash only with that mark. Scan rows,
duplicate keys and the folder tree no longer turn \ into /, so a\b.png and
a/b.png stay two files.

* fix(paths): Needs review and tagging take a backslash name too (#646)

Needs review built its own image entries without the POSIX mark, so Open
and Info still failed there for a name holding a backslash (Codex P2). The
review target now carries path_style like the Images listing, and imageOf
passes it on.

The capture store, which tagging and review read through, refused every
backslash; it now refuses one only on Windows. Before this, such a file
could not be tagged and never reached Needs review.

* fix(tests): shared-listing tests wait for the workspace cache build before teardown

A read that stores a listing starts the workspace cache build on a worker
thread, and cancelling its task at cleanup does not stop the thread. A test
that ended on that read could reach teardown while the thread opened a
database connection, and the leak check failed (queue run 44164234834,
py3.14). Teardown now stops new builds and waits for the running one. On
one busy CPU the test failed 8 of 60 runs before and 0 of 60 after.

* fix(tests): the pool-reuse test makes its first job run on both threads

ThreadPoolExecutor starts threads lazily and skips a new one while another
is idle. The first job's tiny tasks could all run on worker 0 and leave
worker 1 to start in the second job, so second <= first failed although the
pool was reused (queue run 44169912829: first {worker_0}, second
{worker_1}). A two-party barrier now makes the first job run on both.
2026-11-17 21:08:41 +00:00

84 lines
3.3 KiB
Python

"""The folders no media walker lists below an output root (#501).
``thumbnails`` holds PromptManager's own previews. A trash folder that sits
inside an output root (``D:\\$RECYCLE.BIN`` when ``D:\\`` is a root,
``.Trash-1000`` at a mount point, ``.Trashes`` on a macOS volume) holds files
the user threw away: they are not outputs, so they are never listed, scanned,
linked, tagged or previewed. Every walker asks these predicates instead of
testing names itself; only folders *below* a root are tested.
``in_trash_below_roots`` is the check for a path someone names (#543): it
resolves the path first, so a link or another spelling cannot get past it.
"""
import os
TRASH_FOLDER_NAMES = frozenset({".trash", ".trashes", "$recycle.bin"})
PREVIEW_FOLDER = "thumbnails"
def is_trash_folder(name: str) -> bool:
"""A trash folder name, in any case: ``.Trash``, ``.Trash-<uid>``,
``.Trashes`` or ``$RECYCLE.BIN``. Windows ignores a name's trailing dots
and spaces, so ``$RECYCLE.BIN.`` is that folder; they are ignored here too."""
folded = name.rstrip(" .").casefold()
if folded in TRASH_FOLDER_NAMES:
return True
return folded.startswith(".trash-") and folded[7:].isdigit()
def is_skipped_folder(name: str) -> bool:
"""A folder no walker descends: ``thumbnails`` (exact) or a trash folder."""
return name == PREVIEW_FOLDER or is_trash_folder(name)
def path_separators(pathmod=os.path) -> str:
"""The characters that separate names in a root-relative path on this server.
``/`` always; ``\\`` only on Windows, where it cannot be part of a name. On
POSIX ``\\`` is an ordinary character (a Windows-style ``filename_prefix``
makes such a folder, #646).
"""
return "/\\" if pathmod.sep == "\\" else "/"
def split_relative(relative: str, pathmod=os.path) -> list[str]:
"""*relative* split into its names at this server's separators (#646)."""
if pathmod.sep == "\\":
relative = relative.replace("\\", "/")
return relative.split("/")
def in_trash_folder(relative: str, pathmod=os.path) -> bool:
"""Whether a root-relative file path lies in a trash folder (#543).
Takes this server's separators (#646). Previews of it
(``thumbnails/.Trash-1000/...``) count.
"""
folders = split_relative(relative, pathmod)[:-1]
return any(is_trash_folder(part) for part in folders)
def in_trash_below_roots(path, roots) -> bool:
"""Whether *path*, resolved, is in a trash folder below every root holding it.
A source named like a trash folder is still a source, so a file it holds
with no trash folder below it is not in one. Paths compare resolved and
normcased. A path no root visibly holds tests its whole path: a spelling
difference must not fail open.
"""
real = os.path.normcase(os.path.realpath(path))
below = []
for root in roots:
real_root = os.path.normcase(os.path.realpath(root))
if real.startswith(os.path.join(real_root, "")):
below.append(os.path.relpath(real, real_root))
if not below:
return in_trash_folder(real)
return all(in_trash_folder(relative) for relative in below)
def in_skipped_folder(parts) -> bool:
"""Whether any of the folder names *parts* is skipped."""
return any(is_skipped_folder(part) for part in parts)