* feat(maintenance): reviewed, read-only maintenance plans POST /prompt_manager/maintenance/preview reviews duplicate_prompts, optimize, unused_components or missing_links on its own mode=ro snapshot and answers a Plan (scope, protected reasons, backup policy, media, at most 50 detail rows). The fingerprint, targets and judged sources stay in a server-side PlanStore under a single-use token (15 min, 8 plans, 16 MiB, never truncated; a restart forgets them), bound to the database it read. The previews reuse the legacy queries: duplicate groups and the merge are shared with cleanup_duplicates, ORPHANED_PROMPTS_SQL is built from the #239 protection predicates the plan reports, and missing links are judged by the health check's link_states. Legacy /maintenance keeps its shape; vacuum now reports success:false when VACUUM fails, and cleanup_orphaned_images skips links under unavailable sources. * feat(maintenance): apply reviewed plans as Activity jobs POST /prompt_manager/maintenance/apply {token} starts a maintenance job (records admission, not cancellable, groups changed/protected/errors). The token is consumed on the loop turn the job starts: a second apply is 409 plan-used naming the job, a busy refusal keeps the token, an unknown or expired one is 410 plan-expired, and a restore since the review is 409 storage-changed. With the safety backup policy on (maintenance.safety_backup, default true, saved through the settings route with the revision), the job first writes a backup into a temp file beside the database, makes it a standalone rollback-journal file, verifies it, fsyncs it and renames it onto a unique <db>.backup_<YYYYmmdd_HHMMSS_ffffff>[_n] name; any failure leaves no file and nothing changed. The apply then rechecks the reviewed fingerprint inside BEGIN IMMEDIATE (plan-changed writes nothing), deletes in batches of 400 ids, counts changed and protected records, logs the backup by name only and prunes old safety backups. With the policy off the log and the status line say "No safety backup". * feat(maintenance): find and remove duplicate files through Activity Duplicate image files move onto the job registry: - POST /prompt_manager/jobs/duplicates finds content-identical media files over every effective source as a cancellable read job ("Find duplicate image files", group groups_found); the server holds the groups per job and GET /prompt_manager/maintenance/duplicates/{id} pages them ten at a time. - The duplicate_files preview binds a removal to the reviewed keepers. Every keeper and removal is checked again: containment as text before any filesystem access, then not a link, still inside its source, same size and hash. The plan lists the database links that move. - Apply runs duplicate_cleanup (records, not cancellable): the promised safety backup, then per file a recheck, the unlink with every handle closed and its thumbnail. A changed file is kept, the first failed unlink stops the job with a coded message (never the OS text), and only removed files have their image links and favorites moved to the kept copy. - The legacy scan, stream, status and delete routes keep their shapes on the same registry kinds; the hashing, grouping and thumbnail removal are shared with them. Coordinator rulings on Tasks 15-16: - Safety backups share the restore pool of 10 `<db>.backup_*` files; the pool is now also pruned when an apply fails after its backup. - Plans report the safety backup's size and the free disk space, and apply refuses up front (507 insufficient-space) when the backup or VACUUM does not fit, so no job fails halfway for space. * fix(maintenance): never remove the kept copy; move records before unlink Final review of #313: - C1: the duplicate find tells files apart by (st_dev, st_ino), so a source listed twice, a symlinked source inside another and a hardlink are one file. The preview and the job refuse a removal that os.path.samestat matches with its kept copy. - I1: each file's records move to its verified kept copy in one short transaction before that file is unlinked; a failed move deletes nothing and a failed unlink leaves the records on the identical kept copy. The status line counts files removed and not removed. - I2: links are looked up under every spelling of the source (the real path, the configured and monitored folders, ComfyUI's output). - I5: image tags join the kept copy as their union and the removed copy's pending, undecided Needs review items are settled; the plan says so. - I3: the VACUUM disk check measures SQLite's real temporary folder. - M1: the token is looked at again with no await before the job starts. - M2: the backup decision is made once and handed to the job. - M3: backups are linked into place; startup sweeps interrupted temps. - M4: Optimize runs alone (new "exclusive" admission) and its plan says the database is locked while VACUUM runs. - M5: the reviewed duplicate merge is strict: collisions and Library titles are protected, and errors roll back. The legacy cleanup is unchanged. - M7: 2,000 keep entries within a 1 MiB body. M8: event waits in tests. - M9: the preview binds files by size, mtime and inode without hashing; the job hashes right before each unlink. * fix(maintenance): protect one file reached through two mounts Re-review of #313, round 2: - R1: a removal on another device than its kept copy, with the same size and mtimes within 2 s, is protected when its inode is equal or its path within its source is the same ("May be the same file reached through another mount or share"), in the preview and again right before each unlink. The job holds the kept copy open during the unlink, so on Windows deleting the kept copy itself through another path fails. - R2: a file's spellings are found by containment: every configured, monitored, ComfyUI-output or listed folder whose stat identity is a folder above the file. Parent spellings of nested sources and case variants match; favorites move under every listed root holding the file. Lookups stay index searches. - m1: a failed unlink's row says the links now name the kept copy. - m2: a volume with no file ids reads "Can't confirm this is a separate file on this drive". * fix(maintenance): stat duplicate candidates with os.stat Python 3.10's Path.stat uses the os.stat bound when pathlib was imported, so the find and the per-file checks could see different stat calls. The find now calls os.stat directly, like every other check; the mount tests failed on py3.10 only. * fix(maintenance): recheck the kept copy right before each unlink Re-review 2 of #313: - n1: the cross-mount guard also matches when one path within its source ends with the other on a whole folder name (sources listed at different depths, e.g. 2024/x.png and x.png on a FUSE or noserverino mount). - n2: after a file's records move and right before its unlink, the held kept copy must still be the file at its path (fstat of the hold against os.stat) and the cross-mount guard must still hold; otherwise the file is kept, its records move back to it, and it counts as not removed. * test(maintenance): the kept-copy race tests hold on Windows On Windows the removal job holds the kept copy open without FILE_SHARE_DELETE, so the tests' attempt to delete or replace it during the record move is refused. The tests now assert that native outcome: the kept copy stays and the removal goes ahead. * fix(maintenance): restore a backup of any size the disk has room for The legacy restore refused the maintainer's own 1.6 GB safety backup with "File too large. Maximum size is 100MB", a fixed cap since #153. Free disk space now decides: the upload, its staged copy and the swap must fit, under a 64 GiB ceiling. A request with a Content-Length that does not fit is refused before anything is written, one without as soon as its running size does not, with 507 insufficient-space and a message without paths. The upload still streams and its temp file is removed on every path.
134 lines
5.0 KiB
Python
134 lines
5.0 KiB
Python
"""Every path a duplicate copy is recorded under (#249 System S6, Task 17).
|
|
|
|
The search walks each source's real path, but a record names a file the way
|
|
it was seen when it was written: under the folder the image monitor watches,
|
|
the configured spelling, ComfyUI's output folder, a symlink to it, or a
|
|
parent of a source listed later. ``Spellings`` finds them by containment:
|
|
for each known folder spelling ``S`` (resolving to the folder ``R``) and a
|
|
file ``P`` inside ``R``, the file is also recorded as ``S + relpath(P, R)``.
|
|
|
|
Folders are compared by ``os.stat`` identity (``st_dev``, ``st_ino``), not
|
|
by text, so a case variant on a case-insensitive filesystem (macOS, Windows)
|
|
or a symlinked spelling matches. A volume that reports no file id falls
|
|
back to ``normcase(realpath)``. Favorites are keyed by a listed root and the
|
|
path below it, so ``favorites`` lists every listed root that holds a file.
|
|
Blocking: each lookup stats the file's folders once (cached).
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from .images import _favorite_relative_key, _favorite_root_key
|
|
|
|
|
|
def _identity(path):
|
|
try:
|
|
info = os.stat(path)
|
|
except OSError:
|
|
return None
|
|
if info.st_ino:
|
|
return ("inode", info.st_dev, info.st_ino)
|
|
return ("path", os.path.normcase(os.path.realpath(str(path))))
|
|
|
|
|
|
class Spellings:
|
|
"""Folder spellings and listed roots, matched to files by containment."""
|
|
|
|
def __init__(self, folders, listed_roots):
|
|
self._cache = {}
|
|
self._folders = []
|
|
for spelled in dict.fromkeys(str(f) for f in folders if f):
|
|
identity = self._identity(spelled)
|
|
if identity is not None:
|
|
self._folders.append((spelled, identity))
|
|
self._listed = []
|
|
for root in dict.fromkeys(str(r) for r in listed_roots if r):
|
|
identity = self._identity(root)
|
|
if identity is not None:
|
|
self._listed.append((root, identity))
|
|
|
|
def _identity(self, path):
|
|
if path not in self._cache:
|
|
self._cache[path] = _identity(path)
|
|
return self._cache[path]
|
|
|
|
def _ancestors(self, path):
|
|
"""``{identity: folder}`` of every folder above ``path``, nearest first."""
|
|
found = {}
|
|
folder = os.path.dirname(os.path.abspath(str(path)))
|
|
while True:
|
|
identity = self._identity(folder)
|
|
if identity is not None:
|
|
found.setdefault(identity, folder)
|
|
parent = os.path.dirname(folder)
|
|
if parent == folder:
|
|
return found
|
|
folder = parent
|
|
|
|
def of(self, path):
|
|
"""``{folder spelling: path spelled under it}``; ``""`` is ``path`` itself."""
|
|
path = os.path.abspath(str(path))
|
|
spelled = {"": path}
|
|
ancestors = self._ancestors(path)
|
|
for folder, identity in self._folders:
|
|
inside = ancestors.get(identity)
|
|
if inside is not None:
|
|
spelled.setdefault(
|
|
folder, os.path.join(folder, os.path.relpath(path, inside))
|
|
)
|
|
return spelled
|
|
|
|
def favorites(self, path):
|
|
"""``{listed root: path below it (POSIX)}`` for each listed root holding it."""
|
|
path = os.path.abspath(str(path))
|
|
ancestors = self._ancestors(path)
|
|
held = {}
|
|
for root, identity in self._listed:
|
|
inside = ancestors.get(identity)
|
|
if inside is not None:
|
|
held[root] = Path(os.path.relpath(path, inside)).as_posix()
|
|
return held
|
|
|
|
|
|
def copy_move(target, kept_stat, spellings):
|
|
"""What ``move_copy_records`` moves for one removal.
|
|
|
|
A record found under a folder spelling moves to the kept copy under
|
|
the same spelling when that folder holds it too, else to its real
|
|
path. Its favorite moves under every listed root holding the file.
|
|
"""
|
|
removed = spellings.of(target["abs"])
|
|
kept = spellings.of(target["keeper_abs"])
|
|
targets = {
|
|
spelled: kept.get(folder, target["keeper_abs"])
|
|
for folder, spelled in removed.items()
|
|
}
|
|
kept_favorites = spellings.favorites(target["keeper_abs"])
|
|
favorites = []
|
|
for root, relative in spellings.favorites(target["abs"]).items():
|
|
kept_root, kept_rel = (
|
|
(root, kept_favorites[root])
|
|
if root in kept_favorites
|
|
else (target["keeper_root"], target["keeper_rel"])
|
|
)
|
|
favorites.append(
|
|
(
|
|
_favorite_root_key(root),
|
|
_favorite_relative_key(relative),
|
|
_favorite_root_key(kept_root),
|
|
_favorite_relative_key(kept_rel),
|
|
)
|
|
)
|
|
return {
|
|
"removed": list(removed.values()),
|
|
"kept": list(kept.values()),
|
|
"targets": targets,
|
|
"kept_path": target["keeper_abs"],
|
|
"favorites": favorites,
|
|
"removed_locator": target["locator"],
|
|
"kept_locator": target["keeper_locator"],
|
|
"sha256": target["hash"],
|
|
"kept_size": kept_stat.st_size,
|
|
"kept_mtime_ns": kept_stat.st_mtime_ns,
|
|
}
|