diff --git a/apps/web/tests/ui/library-review.spec.ts b/apps/web/tests/ui/library-review.spec.ts index f77cd967..371f6412 100644 --- a/apps/web/tests/ui/library-review.spec.ts +++ b/apps/web/tests/ui/library-review.spec.ts @@ -28,6 +28,10 @@ test("library review links unmatched items to Dewarr and Hardcover books", async page.on("pageerror", (e) => errors.push(e.message)); const matchSearches: string[] = []; const matches: { asset: string; body: unknown }[] = []; + let releaseNextSearch!: () => void; + const nextSearch = new Promise((resolve) => { + releaseNextSearch = resolve; + }); let imported = false; const items = [ { kind: "asset", asset: asset("a1", "Untagged Folder", ["title"]) }, @@ -115,6 +119,7 @@ test("library review links unmatched items to Dewarr and Hardcover books", async data = { items: shown, total: shown.length }; } else if (path === "/api/catalog/works") { matchSearches.push(url.searchParams.get("q") || ""); + if (url.searchParams.get("q") === "The Long Way") await nextSearch; data = { items: [work], total: 1 }; } else if (path.endsWith("/match") && method === "POST") { const id = path.split("/").at(-2)!; @@ -214,9 +219,16 @@ test("library review links unmatched items to Dewarr and Hardcover books", async part_total: null, }, }); - await expect(page.getByRole("status")).toContainText( - "Linked “Untagged Folder”", - ); + try { + // Saving and continuing can announce success while the next lookup is pending. + await expect(detail.getByRole("status")).toHaveText("Searching catalog…"); + await expect( + page.getByRole("status").filter({ hasText: "Linked “Untagged Folder”" }), + ).toBeVisible(); + } finally { + releaseNextSearch(); + } + await expect(detail.getByRole("status")).toHaveCount(0); await expect(cards).toHaveCount(2); await expect( nav.getByRole("link", { name: /^Library review/ }), diff --git a/docs/LIBRARY-FOLDERS.md b/docs/LIBRARY-FOLDERS.md index 6d9686d5..c3805453 100644 --- a/docs/LIBRARY-FOLDERS.md +++ b/docs/LIBRARY-FOLDERS.md @@ -56,7 +56,7 @@ Templates accept `{{` and `}}` for literal braces. `{author}/{title}[ {{{narrato ## Staging and mount boundaries -Staging must share the library's mount so complete folders can be published safely. New Audiobookshelf destinations use the reserved direct hidden child, avoiding any requirement to write to the library's parent. Grimmory uses it when its watcher is disabled; otherwise staging remains outside the watched library. Existing valid staging is retained. Only this private namespace is name-specific; user library and download names are unrestricted. New routes keep journals/locks in application storage, independent of media permissions. +Staging must share the library's mount so complete folders can be published safely. New Audiobookshelf destinations use the reserved direct hidden child, avoiding any requirement to write to the library's parent. Grimmory uses it when its watcher is disabled; otherwise staging remains outside the watched library. Existing valid staging is retained. The private staging namespace and `.book-search-root` identity marker names are reserved; ordinary library and download names are unrestricted. New routes keep journals/locks in application storage, independent of media permissions. Two routes using the same local root reuse the same valid staging route. A saved automatic staging choice that now overlaps the selected library incorrectly is reselected. An explicit `BOOK_IMPORT_STAGING_ROOT` override remains authoritative and receives a path-specific error if invalid. Unfinished imports continue to block changing their saved storage; historical journals are retained. diff --git a/docs/NATIVE-LIBRARY-IMPORTS.md b/docs/NATIVE-LIBRARY-IMPORTS.md index c310f856..595fb41b 100644 --- a/docs/NATIVE-LIBRARY-IMPORTS.md +++ b/docs/NATIVE-LIBRARY-IMPORTS.md @@ -18,6 +18,15 @@ Activating Bookdrop changes the administrator's personal ebook destination. It p The connection needs access to Bookdrop and its queue. Folder verification checks local publication access, the selected backend configuration, and queue readability; it does not prove that the two services share a filesystem. Grimmory does not expose its configured Bookdrop root through this API, so Dewarr cannot prove that the chosen path is watched until a delivered EPUB appears in the queue. **Refresh review status** checks for that exact intake path and size. +Bookdrop queue reads support both legacy top-level paging and Grimmory 3.5.0's +nested Spring `page` metadata, including an empty queue. Reads stop at 100 pages +or 10,000 files and reject inconsistent page sizes/counts, repeated or unordered +IDs, and malformed files. A queue changing during a read must be refreshed; +these pages do not provide a transactional snapshot. **Send to Bookdrop on +completion** controls delivery timing. Turning it off still requires Bookdrop +access and folder verification. Select the separate direct-library workflow for +direct imports. + Review metadata and approve the import in Grimmory. Sync the resulting library in Dewarr, then use **Link reviewed copy** in the download's import review. Candidates must be complete, matched copies of the selected edition on the original Grimmory server. Resolve an edition mismatch in library review before linking. Queue disappearance alone does not establish whether a file was imported, discarded or moved. **Mark rejected** retains the receipt and suppresses another handoff; it does not delete anything in Grimmory. A confirmed or rejected handoff is never automatically resent. Publication receipts also protect against a restart after Grimmory consumes a file but before Dewarr records delivery. Uncertain outcomes require review rather than automatic replacement. Pending human review does not occupy a worker or trigger repeated scans. diff --git a/docs/STORAGE-COMPATIBILITY.md b/docs/STORAGE-COMPATIBILITY.md index 4e3b7d0a..a8f2ad24 100644 --- a/docs/STORAGE-COMPATIBILITY.md +++ b/docs/STORAGE-COMPATIBILITY.md @@ -277,3 +277,47 @@ API/worker activation journeys passed through `scripts/check.py`. The latter cov qBittorrent and slskd on local, simulated SMB, and read-only sources, including copy selection, backend mapping, and import readiness. They used an isolated PostgreSQL database that was removed afterward. Lint and formatting checks passed. + +## NOR-94: library roots whose directory inode changes + +The [Unraid report](https://github.com/logabell/dewarr/issues/61) describes the +library root switching its reported inode after a first write to a cache-backed +user share. [Unraid user shares](https://docs.unraid.net/unraid-os/using-unraid-to/manage-storage/shares/) +combine folders through FUSE. Replaying that root-only inode transition against +real local publication operations reproduced both the final publication failure +and the retry failure. This does not establish how every FUSE filesystem behaves. + +New publications create a persistent `.book-search-root` file in the destination +root. Its random token, device and **file inode**, together with the root device, +identify the destination independently of the root directory inode. Initialization +writes and flushes a temporary file before publishing it without replacement; +concurrent creators use the winning marker. A malformed or linked marker is held +for review. The marker contains no credentials and is not a media file. Keep it +with the original library; do not copy it to provision another destination. + +Publication, seeding moves, combining, cancellation and companion placement use +the same marker-based destination lock. New receipts require the original marker, +even if the directory inode still matches. A copied token, different device, +missing/changed marker or symlink does not authorize publication. Source files, +item folders, staged files and publication ownership still have their existing +checks. Recovery scans only read this evidence; they never initialize markers. +Stop old workers when upgrading so all writers use the same locking protocol. + +Older receipts without a root marker retain strict device/inode matching. They +are never silently rebound to the current folder. If an old receipt is stale, +**Cancel import** can remove only a still-present, verified owned stage and +record cancellation; then start a new import. It preserves the source and does +not remove anything from the library. Missing stages, published receipts, +unconfirmed staging and seeding moves require review instead. Interrupted +cancellation remains retryable from its journal. + +The regression checks cover root changes during publication and retry, a stable +shared lock, concurrent marker initialization, actual directory replacement, +copied/modified markers, symlinks, read-only recovery, Bookdrop restart recovery, +combining/separating, seeding moves and cancellation followed by a fresh import. +API/worker checks also retain both ebook/audiobook import orders and multiple +narrations. They simulate the reported identity transition on local files; +**no live Unraid or cache-mover interoperability test was available**. If the +filesystem also changes the marker file's identity, existing receipts remain +held for review. This fix does not trust a token alone or certify that broader +filesystem behavior. diff --git a/services/app/adapters/grimmory.py b/services/app/adapters/grimmory.py index 0a0329d2..51b430ef 100644 --- a/services/app/adapters/grimmory.py +++ b/services/app/adapters/grimmory.py @@ -537,30 +537,91 @@ async def metadata_persistence(self) -> dict: async def bookdrop_files(self) -> list[dict]: rows, seen = [], set() + snapshot = None for page in range(100): value = await self.request( "GET", "api/v1/bookdrop/files", params={"page": page, "size": 100, "sort": "id,asc"} ) content = value.get("content") - if not isinstance(content, list) or type(value.get("last")) is not bool: + fields = ("number", "size", "totalElements", "totalPages") + nested = "page" in value + metadata = value.get("page") if nested else value + has_metadata = nested or any(field in value for field in fields) + if not isinstance(content, list) or len(content) > 100: raise AdapterError( FailureKind.PARSER, "Grimmory returned an invalid Bookdrop queue." ) + if has_metadata: + if not isinstance(metadata, dict) or any( + type(metadata.get(field)) is not int for field in fields + ): + raise AdapterError(FailureKind.PARSER, "Bookdrop returned invalid pagination.") + number, size, total, pages = (metadata[field] for field in fields) + if ( + number != page + or not 1 <= size <= 100 + or total < 0 + or pages != (total + size - 1) // size + or (total == 0 and page != 0) + or (total > 0 and page >= pages) + or len(content) != min(size, max(0, total - page * size)) + ): + raise AdapterError( + FailureKind.PARSER, "Bookdrop returned inconsistent pagination." + ) + if pages > 100 or total > 10_000: + raise AdapterError( + FailureKind.PARSER, "Bookdrop queue exceeds the supported review limit." + ) + current = (nested, size, total, pages) + last = page + 1 >= pages + # If both representations are present, neither may contradict the other. + if "last" in value and (type(value["last"]) is not bool or value["last"] != last): + raise AdapterError( + FailureKind.PARSER, "Bookdrop returned inconsistent pagination." + ) + if nested and any( + field in value + and (type(value[field]) is not int or value[field] != metadata[field]) + for field in fields + ): + raise AdapterError( + FailureKind.PARSER, "Bookdrop returned inconsistent pagination." + ) + else: + # Older servers expose only content/last. Retain that contract, + # with the same row/page bounds and progress checks. + last = value.get("last") + if type(last) is not bool or (not last and not content): + raise AdapterError(FailureKind.PARSER, "Bookdrop returned invalid pagination.") + current = (False, None, None, None) + if snapshot is not None and snapshot != current: + raise AdapterError( + FailureKind.UNCERTAIN, + "Bookdrop changed while reading its queue; refresh again.", + ) + snapshot = current for row in content: if ( not isinstance(row, dict) or type(row.get("id")) is not int + or row["id"] <= 0 or not isinstance(row.get("filePath"), str) + or not row["filePath"].startswith("/") or type(row.get("fileSize")) is not int - or row["id"] in seen + or row["fileSize"] < 0 ): raise AdapterError( - FailureKind.PARSER, + FailureKind.PARSER, "Grimmory returned an invalid Bookdrop file." + ) + if row["id"] in seen or (rows and row["id"] <= rows[-1]["id"]): + raise AdapterError( + FailureKind.UNCERTAIN, "Bookdrop changed while reading its queue; refresh again.", ) seen.add(row["id"]) rows.append(row) - if value["last"]: + if last: return rows raise AdapterError(FailureKind.PARSER, "Bookdrop queue exceeds the supported review limit.") diff --git a/services/app/importing/bookdrop.py b/services/app/importing/bookdrop.py index 1c11b91e..4023cf26 100644 --- a/services/app/importing/bookdrop.py +++ b/services/app/importing/bookdrop.py @@ -9,9 +9,9 @@ from app.importing.publication import ( PublicationError, entry_lock, - object_id, private_staging, read_receipt, + same_destination, specification_fingerprint, ) @@ -32,9 +32,9 @@ def recovered_handoff(spec): if not receipt: return None with directory(spec.destination_root) as destination: - if receipt.get("spec_hash") != specification_fingerprint(spec) or receipt.get( - "destination_identity" - ) != object_id(destination): + if receipt.get("spec_hash") != specification_fingerprint(spec) or not same_destination( + destination, receipt.get("destination_identity") + ): raise PublicationError( "Bookdrop receipt or intake mount changed; review the existing handoff" ) diff --git a/services/app/importing/cancel_files.py b/services/app/importing/cancel_files.py index 609c7aab..1632686c 100644 --- a/services/app/importing/cancel_files.py +++ b/services/app/importing/cancel_files.py @@ -1,6 +1,5 @@ """Stop one unpublished item under the same filesystem lock used by publication.""" -import json import os from contextlib import ExitStack, nullcontext from uuid import uuid4 @@ -9,16 +8,18 @@ from app.importing.publication import ( PUBLICATION_MARKER, PublicationError, + destination_identity, + destination_lock, entry_lock, generated_files, has_publication_marker, needs_hardlink, object_id, private_staging, - publication_lock, published_names, read_receipt, remove_publication_marker, + same_destination, same_object, specification_fingerprint, sync_directory, @@ -110,7 +111,7 @@ def cancel_files(spec, *, guard=nullcontext, checkpoint=lambda _: None): ): with ( entry_lock(staging, spec.entry_id), - publication_lock(staging, json.dumps(object_id(root), sort_keys=True)), + destination_lock(staging, root), guard(), ): receipt = read_receipt(staging, name) @@ -130,21 +131,49 @@ def cancel_files(spec, *, guard=nullcontext, checkpoint=lambda _: None): "schema_version": 1, "entry_id": str(spec.entry_id), "spec_hash": specification_fingerprint(spec), - "destination_identity": object_id(root), + "destination_identity": destination_identity(root), "stage_name": "item-" + uuid4().hex, "state": "cancelled", } # A durable tombstone also fences a delayed pre-cancellation worker. write_receipt(staging, name, receipt, create=True) return receipt - if ( - receipt.get("entry_id") != str(spec.entry_id) - or receipt.get("spec_hash") != specification_fingerprint(spec) - or receipt.get("destination_identity") != object_id(root) - ): + if receipt.get("entry_id") != str(spec.entry_id) or receipt.get( + "spec_hash" + ) != specification_fingerprint(spec): raise PublicationError("Cancellation journal or destination identity changed") if receipt["state"] == "cancelled": return receipt + if not same_destination(root, receipt.get("destination_identity")): + # Never rebind an old receipt to a new root. An owned stage still + # present proves this atomic publication has not left staging. + # Explicit cancellation can clean only that stage, allowing a new + # plan; missing/published/rename evidence remains held for review. + if ( + spec.mode == "rename" + or receipt["state"] not in {"preparing", "prepared", "cancelling"} + or not receipt.get("stage_identity") + or receipt.get("unconfirmed_stages") + ): + raise PublicationError( + "Destination identity changed; review the existing publication " + "before replanning" + ) + if receipt["state"] != "cancelling": + try: + with beneath(staging, receipt["stage_name"], folder=True) as folder: + if not same_object(folder, receipt["stage_identity"]): + raise PublicationError("Cancellation staging identity changed") + except FileNotFoundError: + raise PublicationError( + "Staged item is missing; publication may have completed elsewhere" + ) from None + receipt["state"] = "cancelling" + write_receipt(staging, name, receipt) + remove_stage(staging, receipt, spec, checkpoint) + receipt["state"] = "cancelled" + write_receipt(staging, name, receipt) + return receipt if spec.mode == "rename": return cancel_renamed(root, staging, name, receipt, spec) try: diff --git a/services/app/importing/colocate.py b/services/app/importing/colocate.py index 0b2a6b71..9fa399b5 100644 --- a/services/app/importing/colocate.py +++ b/services/app/importing/colocate.py @@ -6,7 +6,6 @@ import asyncio import errno -import json import os import time from contextlib import nullcontext @@ -38,10 +37,10 @@ from app.importing.publication import ( PublicationBusy, PublicationError, + destination_lock, no_replace, object_id, private_staging, - publication_lock, sync_directory, write_all, ) @@ -119,7 +118,7 @@ def place_companion( Path(target["staging_path"]), Path(target["journal_path"]) if target.get("journal_path") else None, ) as staging, - publication_lock(staging, json.dumps(object_id(root), sort_keys=True)), + destination_lock(staging, root), beneath(root, str(relative.parent), folder=True) as folder, ): if object_id(folder) != configuration["target_folder_identity"]: diff --git a/services/app/importing/combine.py b/services/app/importing/combine.py index 30a077a8..3c47acdf 100644 --- a/services/app/importing/combine.py +++ b/services/app/importing/combine.py @@ -8,7 +8,6 @@ import asyncio import errno -import json import os import stat from collections import Counter @@ -47,13 +46,15 @@ HARDLINKS_UNSUPPORTED, PublicationError, conflicting_name, + destination_identity, + destination_lock, destination_parent, journal_fd, no_replace, object_id, private_staging, - publication_lock, read_receipt, + same_destination, same_object, sync_directory, write_all, @@ -429,11 +430,11 @@ def journal(spec): ): if same_object(staging, object_id(destination)): raise CombineError("Staging and library refer to the same folder") - with publication_lock(staging, json.dumps(object_id(destination), sort_keys=True)): + with destination_lock(staging, destination): receipt = read_receipt(staging, receipt_name(spec)) if receipt is not None and ( receipt.get("spec_hash") != spec_hash(spec) - or receipt.get("destination_identity") != object_id(destination) + or not same_destination(destination, receipt.get("destination_identity")) ): raise CombineError("The combine journal no longer matches this library folder") yield staging, destination, receipt @@ -466,7 +467,7 @@ def publish_combined(spec, *, checkpoint=lambda _: None): "combine_id": str(spec.combine_id), "spec_hash": spec_hash(spec), "state": "preparing", - "destination_identity": object_id(destination), + "destination_identity": destination_identity(destination), } write_receipt(staging, receipt_name(spec), receipt, create=True) if receipt["state"] not in {"preparing", "staged"}: @@ -502,7 +503,7 @@ def publish_combined(spec, *, checkpoint=lambda _: None): if conflicting_name(parent, leaf) is not None: raise CombineSkipped(f"The folder {spec.folder} already exists in the library") with directory(spec.destination_root) as current: - if not same_object(current, receipt["destination_identity"]): + if not same_destination(current, receipt["destination_identity"]): raise CombineError("The library folder changed before publication") with beneath(staging, stage_name(spec), folder=True) as current: if not same_object(current, receipt["stage_identity"]): diff --git a/services/app/importing/layout.py b/services/app/importing/layout.py index f92a3bd2..cd8e807e 100644 --- a/services/app/importing/layout.py +++ b/services/app/importing/layout.py @@ -3,6 +3,7 @@ from pathlib import Path STAGING_NAME = ".book-search-staging" +ROOT_MARKER = ".book-search-root" def overlaps(left: Path, right: Path) -> bool: @@ -48,8 +49,12 @@ def check_library_layout(roots: dict[str, Path], sources: dict[str, Path], stagi def library_relative(value: str) -> None: # Reserve this namespace even on case-insensitive media shares. Import and # combine targets/sources must never address journals or incomplete media. - if value.split("/", 1)[0].casefold() == STAGING_NAME.casefold(): - raise ValueError("The private staging folder is reserved; choose a library item path") + first = value.split("/", 1)[0].casefold() + if first == STAGING_NAME.casefold() or first.startswith(ROOT_MARKER): + raise ValueError( + "The private staging folder and root identity marker are reserved; " + "choose a library item path" + ) def check_staging_backend(kind: str, library: Path, staging: Path, *, watcher_enabled=None) -> None: diff --git a/services/app/importing/publication.py b/services/app/importing/publication.py index 33ed2b0d..f05994b2 100644 --- a/services/app/importing/publication.py +++ b/services/app/importing/publication.py @@ -33,7 +33,7 @@ relative_parts, source_scope, ) -from app.importing.layout import library_relative, overlaps, unsafe_roots +from app.importing.layout import ROOT_MARKER, library_relative, overlaps, unsafe_roots from app.importing.naming import StrictModel, collision_key, fingerprint @@ -176,6 +176,109 @@ def same_object(fd, expected): return object_id(fd) == {key: expected[key] for key in ("device", "inode")} +# A file's inode and random token anchor the root independently of a union +# directory's changing inode. Copying the token to a replacement is insufficient. +def _root_marker(root, *, create=False): + try: + with beneath(root, ROOT_MARKER) as fd: + info = os.fstat(fd) + if info.st_nlink != 1 or info.st_size != 33: + raise PublicationError( + "Library identity marker is invalid; review the mounted folder" + ) + content = os.read(fd, 34) + if not re.fullmatch(rb"[a-f0-9]{32}\n", content): + raise PublicationError( + "Library identity marker is invalid; review the mounted folder" + ) + return {**object_id(fd), "token": content.decode().strip()} + except FileNotFoundError: + if not create: + return None + except InspectionError as error: + if isinstance(error, PublicationError): + raise + raise PublicationError("Library identity marker is not a regular file") from error + temporary = ROOT_MARKER + "-" + uuid4().hex + ".tmp" + fd = os.open( + temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o666, dir_fd=root + ) + owned = object_id(fd) + try: + try: + write_all(fd, (uuid4().hex + "\n").encode()) + os.fsync(fd) + finally: + os.close(fd) + try: + no_replace(root, temporary, root, ROOT_MARKER) + except FileExistsError: + pass # Another worker won; read its complete marker, never overwrite it. + sync_directory(root) + finally: + try: + current = os.stat(temporary, dir_fd=root, follow_symlinks=False) + if {"device": current.st_dev, "inode": current.st_ino} != owned: + raise PublicationError( + "Temporary library identity marker changed; preserved for review" + ) + os.unlink(temporary, dir_fd=root) + except FileNotFoundError: + pass + marker = _root_marker(root) + if marker is None: + raise PublicationError("Library identity marker disappeared during initialization") + return marker + + +def destination_identity(root, *, create=False): + marker = _root_marker(root, create=create) + value = object_id(root) # Creating the marker can itself change the root inode. + if marker is not None: + if marker["device"] != value["device"]: + raise PublicationError("Library identity marker is on another mount") + value["root_marker"] = marker + return value + + +def same_destination(root, expected): + if not isinstance(expected, dict): + return False + actual = object_id(root) + if actual["device"] != expected.get("device"): + return False + if "root_marker" not in expected: + return actual == expected # Old journals never gain new authority implicitly. + marker = expected["root_marker"] + if not isinstance(marker, dict) or not marker.get("token"): + return False + try: + return marker["device"] == actual["device"] and _root_marker(root) == marker + except (OSError, InspectionError, KeyError): + return False + + +@contextmanager +def destination_lock(staging, root): + """One stable lock for publication, combination, cancellation and companions.""" + expected = destination_identity(root, create=True) + key = json.dumps(expected["root_marker"], sort_keys=True) + with publication_lock(staging, key) as pause: + if not same_destination(root, expected): + raise PublicationError("Library identity marker changed while acquiring its lock") + + @contextmanager + def checked_pause(): + with pause(): + yield + if not same_destination(root, expected): + raise PublicationError( + "Library identity marker changed while publication was paused" + ) + + yield checked_pause + + def publication_marker_content(receipt): token = receipt.get("publication_marker") if not token: @@ -1072,12 +1175,12 @@ def remaining_import_bytes(spec, *, timeout=600): private_staging(spec.staging_root, spec.journal_root) as staging, directory(spec.destination_root) as target, ): - with publication_lock(staging, json.dumps(object_id(target), sort_keys=True)): + with destination_lock(staging, target): receipt = read_receipt(staging, str(spec.entry_id) + ".json") if receipt: - if receipt.get("spec_hash") != specification_fingerprint(spec) or receipt.get( - "destination_identity" - ) != object_id(target): + if receipt.get("spec_hash") != specification_fingerprint( + spec + ) or not same_destination(target, receipt.get("destination_identity")): raise PublicationError("Publication settings or destination identity changed") if spec.mode == "rename": return remaining_rename_bytes(target, receipt, spec, deadline) @@ -1135,13 +1238,13 @@ def load_rename_plan(spec): private_staging(spec.staging_root, spec.journal_root) as staging, directory(spec.destination_root) as target, ): - with publication_lock(staging, json.dumps(object_id(target), sort_keys=True)): + with destination_lock(staging, target): receipt = read_receipt(staging, str(spec.entry_id) + ".json") if receipt is None or "rename_plan" not in receipt: return None - if receipt.get("spec_hash") != specification_fingerprint(spec) or receipt.get( - "destination_identity" - ) != object_id(target): + if receipt.get("spec_hash") != specification_fingerprint(spec) or not same_destination( + target, receipt.get("destination_identity") + ): raise PublicationError("Publication settings or destination identity changed") return receipt["rename_plan"] @@ -1155,7 +1258,7 @@ def remember_rename_plan(spec, plan): ): if same_object(staging, object_id(target)): raise PublicationError("Staging and library refer to the same directory") - with publication_lock(staging, json.dumps(object_id(target), sort_keys=True)): + with destination_lock(staging, target): receipt = read_receipt(staging, receipt_name) if receipt is None: receipt = { @@ -1164,14 +1267,14 @@ def remember_rename_plan(spec, plan): "spec_hash": spec_hash, "stage_name": "rename-" + spec.entry_id.hex, "state": "preparing", - "destination_identity": object_id(target), + "destination_identity": destination_identity(target), "rename_plan": plan, } write_receipt(staging, receipt_name, receipt, create=True) return plan - if receipt.get("spec_hash") != spec_hash or receipt.get( - "destination_identity" - ) != object_id(target): + if receipt.get("spec_hash") != spec_hash or not same_destination( + target, receipt.get("destination_identity") + ): raise PublicationError("Publication settings or destination identity changed") stored = receipt.get("rename_plan") if stored and stored != plan: @@ -1233,7 +1336,7 @@ def publish_renamed( ): if same_object(staging, object_id(destination)): raise PublicationError("Staging and library refer to the same directory") - with publication_lock(staging, json.dumps(object_id(destination), sort_keys=True)): + with destination_lock(staging, destination): receipt = read_receipt(staging, receipt_name) if receipt is None: receipt = { @@ -1242,12 +1345,12 @@ def publish_renamed( "spec_hash": spec_hash, "stage_name": "rename-" + spec.entry_id.hex, "state": "preparing", - "destination_identity": object_id(destination), + "destination_identity": destination_identity(destination), } write_receipt(staging, receipt_name, receipt, create=True) - if receipt.get("spec_hash") != spec_hash or receipt.get( - "destination_identity" - ) != object_id(destination): + if receipt.get("spec_hash") != spec_hash or not same_destination( + destination, receipt.get("destination_identity") + ): raise PublicationError("Publication settings or destination identity changed") if receipt["state"] in {"cancelling", "cancelled"}: raise PublicationError("This import was cancelled; review a new plan") @@ -1264,7 +1367,7 @@ def publish_renamed( with publication_guard(): checkpoint("before-publish") with directory(spec.destination_root) as current: - if not same_object(current, receipt["destination_identity"]): + if not same_destination(current, receipt["destination_identity"]): raise PublicationError( "Destination mount changed before publication" ) @@ -1323,7 +1426,7 @@ def publish_item( raise PublicationError("Staging and library refer to the same directory") with ( entry_lock(staging, spec.entry_id), - publication_lock(staging, json.dumps(object_id(destination), sort_keys=True)) as pause, + destination_lock(staging, destination) as pause, ): receipt = read_receipt(staging, receipt_name) if receipt is None: @@ -1333,12 +1436,12 @@ def publish_item( "spec_hash": spec_hash, "stage_name": "item-" + uuid4().hex, "state": "preparing", - "destination_identity": object_id(destination), + "destination_identity": destination_identity(destination), } write_receipt(staging, receipt_name, receipt, create=True) - if receipt.get("spec_hash") != spec_hash or receipt.get( - "destination_identity" - ) != object_id(destination): + if receipt.get("spec_hash") != spec_hash or not same_destination( + destination, receipt.get("destination_identity") + ): raise PublicationError("Publication settings or destination identity changed") if receipt["state"] in {"cancelling", "cancelled"}: raise PublicationError("This import was cancelled; review a new plan") @@ -1402,7 +1505,7 @@ def publish_item( # Recheck current permissions/lease immediately before rename. checkpoint("before-publish") with directory(spec.destination_root) as current: - if not same_object(current, receipt["destination_identity"]): + if not same_destination(current, receipt["destination_identity"]): raise PublicationError( "Destination mount changed before publication" ) diff --git a/services/app/importing/recovery.py b/services/app/importing/recovery.py index 71f357c5..ab610043 100644 --- a/services/app/importing/recovery.py +++ b/services/app/importing/recovery.py @@ -11,12 +11,14 @@ PublicationSpec, checked_source, conversion_inputs, + destination_identity, has_publication_marker, journal_fd, object_id, private_staging, published_names, read_receipt, + same_destination, same_object, specification_fingerprint, verify_item, @@ -69,6 +71,7 @@ def read_publication(entry, roots): private_staging(spec.staging_root, spec.journal_root) as staging, directory(spec.destination_root) as destination, ): + root_identity = destination_identity(destination) receipt = read_receipt(staging, str(entry["id"]) + ".json") evidence["journal_present"] = receipt is not None if receipt is not None: @@ -76,7 +79,7 @@ def read_publication(entry, roots): not isinstance(receipt, dict) or receipt.get("entry_id") != str(entry["id"]) or receipt.get("spec_hash") != specification_fingerprint(spec) - or receipt.get("destination_identity") != object_id(destination) + or not same_destination(destination, receipt.get("destination_identity")) ): raise ScanHeld("Publication journal identity does not match the frozen import") evidence["journal_digest"] = digest(receipt) @@ -165,7 +168,7 @@ def read_publication(entry, roots): private_staging(spec.staging_root, spec.journal_root) as current_staging, ): if ( - object_id(current_destination) != object_id(destination) + not same_destination(current_destination, root_identity) or object_id(current_staging) != object_id(staging) or object_id(journal_fd(current_staging)) != object_id(journal_fd(staging)) ): diff --git a/services/app/importing/seeding_rename.py b/services/app/importing/seeding_rename.py index 870f02ba..a4e63e77 100644 --- a/services/app/importing/seeding_rename.py +++ b/services/app/importing/seeding_rename.py @@ -5,7 +5,6 @@ """ import asyncio -import json import os import re import time @@ -33,11 +32,10 @@ from app.importing.naming import AUDIO, EBOOK, fingerprint from app.importing.publication import ( PublicationError, + destination_lock, generated_files, load_rename_plan, - object_id, private_staging, - publication_lock, remember_rename_plan, verify_item_media, ) @@ -523,7 +521,7 @@ def seeding_lock(spec): private_staging(spec.staging_root, spec.journal_root) as staging, directory(spec.destination_root) as root, ): - with publication_lock(staging, json.dumps(object_id(root), sort_keys=True)): + with destination_lock(staging, root): yield diff --git a/tests/contracts/test_grimmory.py b/tests/contracts/test_grimmory.py index ceb94738..ef2efac1 100644 --- a/tests/contracts/test_grimmory.py +++ b/tests/contracts/test_grimmory.py @@ -177,6 +177,8 @@ def __init__(self, root=None): }, } self.bookdrop_queue = [] + self.bookdrop_paging = "nested" + self.bookdrop_pages = [] self.allowed = [] self.admin = True self.manage = False @@ -248,7 +250,22 @@ def handle(self, request): if path == "/api/v1/settings": return httpx.Response(200, json={"metadataPersistenceSettings": self.persistence}) if path == "/api/v1/bookdrop/files": - return httpx.Response(200, json={"content": self.bookdrop_queue, "last": True}) + page = int(request.url.params["page"]) + size = int(request.url.params["size"]) + self.bookdrop_pages.append(page) + content = self.bookdrop_queue[page * size : (page + 1) * size] + total = len(self.bookdrop_queue) + metadata = { + "size": size, + "number": page, + "totalElements": total, + "totalPages": (total + size - 1) // size, + } + if self.bookdrop_paging == "nested": + payload = {"content": content, "page": metadata} + else: + payload = {"content": content, **metadata, "last": (page + 1) * size >= total} + return httpx.Response(200, json=payload) if path == "/api/v1/version": return httpx.Response(200, json={"current": self.version, "latest": "3.5.0"}) if path == "/api/v1/libraries": @@ -781,3 +798,151 @@ async def test_bookdrop_rejects_internal_staging_and_final_library_root(tmp_path staging_root=fixture.root.parent / "stage", ) assert not list(fixture.root.iterdir()) + + +@pytest.mark.parametrize("paging", ["nested", "legacy"]) +@pytest.mark.parametrize("count", [0, 1, 201]) +async def test_bookdrop_reads_empty_single_and_multiple_pages(paging, count): + fixture = GrimmoryFixture() + fixture.bookdrop_paging = paging + fixture.bookdrop_queue = [ + {"id": i + 1, "filePath": f"/intake/book-{i}.epub", "fileSize": 1024} for i in range(count) + ] + async with fixture.client() as adapter: + assert await adapter.bookdrop_files() == fixture.bookdrop_queue + assert fixture.bookdrop_pages == list(range(max(1, (count + 99) // 100))) + + +@pytest.mark.parametrize( + "invalid", + [ + "missing-size", + "boolean-number", + "wrong-number", + "negative-total", + "wrong-pages", + "zero-size", + "oversized-page", + "truncated-content", + "false-last", + "conflicting-total", + "changed-total", + "duplicate-id", + "out-of-order", + "negative-file-size", + "relative-path", + "non-object-page", + "changed-format", + "over-limit", + ], +) +async def test_bookdrop_rejects_incomplete_or_changing_queue(invalid): + fixture = GrimmoryFixture() + fixture.bookdrop_queue = [ + {"id": i + 1, "filePath": f"/intake/book-{i}.epub", "fileSize": 1024} for i in range(101) + ] + original = fixture.handle + + def handle(request): + response = original(request) + if request.url.path != "/api/v1/bookdrop/files": + return response + value = response.json() + page = int(request.url.params["page"]) + meta = value["page"] + if page == 0: + if invalid == "missing-size": + del meta["size"] + elif invalid == "boolean-number": + meta["number"] = False + elif invalid == "wrong-number": + meta["number"] = 1 + elif invalid == "negative-total": + meta["totalElements"] = -1 + elif invalid == "wrong-pages": + meta["totalPages"] = 1 + elif invalid == "zero-size": + meta["size"] = 0 + elif invalid == "oversized-page": + value["content"].append(fixture.bookdrop_queue[-1]) + elif invalid == "truncated-content": + value["content"].pop() + elif invalid == "false-last": + value["last"] = True + elif invalid == "conflicting-total": + value["totalElements"] = 1 + elif invalid == "out-of-order": + value["content"].reverse() + elif invalid == "negative-file-size": + value["content"][0]["fileSize"] = -1 + elif invalid == "relative-path": + value["content"][0]["filePath"] = "book.epub" + elif invalid == "non-object-page": + value["page"] = None + elif invalid == "over-limit": + meta.update(totalElements=10_001, totalPages=101) + else: + if invalid == "changed-total": + meta["totalElements"] = 102 + value["content"].append({"id": 102, "filePath": "/intake/new.epub", "fileSize": 1}) + elif invalid == "duplicate-id": + value["content"][0]["id"] = 100 + elif invalid == "changed-format": + value = {"content": value["content"], "last": True} + return httpx.Response(200, json=value) + + fixture.handle = handle + async with fixture.client() as adapter: + with pytest.raises(AdapterError) as error: + await adapter.bookdrop_files() + assert error.value.kind in {FailureKind.PARSER, FailureKind.UNCERTAIN} + assert len(fixture.bookdrop_pages) <= 2 + + +@pytest.mark.parametrize("mode", ["empty", "nonfinal-empty", "unending"]) +async def test_bookdrop_legacy_last_only_is_bounded(mode): + requests = [] + fixture = GrimmoryFixture() + original = fixture.handle + + def handle(request): + if request.url.path != "/api/v1/bookdrop/files": + return original(request) + page = int(request.url.params["page"]) + requests.append(page) + content = ( + [] + if mode != "unending" + else [{"id": page + 1, "filePath": f"/intake/{page}.epub", "fileSize": 1}] + ) + return httpx.Response(200, json={"content": content, "last": mode == "empty"}) + + fixture.handle = handle + async with fixture.client() as adapter: + if mode == "empty": + assert await adapter.bookdrop_files() == [] + else: + with pytest.raises(AdapterError): + await adapter.bookdrop_files() + assert len(requests) == (100 if mode == "unending" else 1) + + +async def test_bookdrop_permission_is_required_but_direct_library_uses_its_own_checks(tmp_path): + fixture = GrimmoryFixture(tmp_path.resolve()) + fixture.admin = False + fixture.manage = True + async with fixture.client() as adapter: + with pytest.raises(PublicationError, match="access to Bookdrop"): + await verify_backend( + adapter, + None, + "/intake", + fixture.root, + "ebook", + workflow="bookdrop", + staging_root=fixture.root.parent / "stage", + ) + assert fixture.bookdrop_pages == [] + direct = await verify_backend(adapter, "7", "/books", fixture.root, "ebook") + assert direct["configuration_validated"] + assert fixture.bookdrop_pages == [] diff --git a/tests/filesystem_fixtures.py b/tests/filesystem_fixtures.py index 01d7d36e..eeabcbab 100644 --- a/tests/filesystem_fixtures.py +++ b/tests/filesystem_fixtures.py @@ -170,3 +170,34 @@ def install(*roots): monkeypatch.setattr(os, "unlink", unlink) return install + + +@pytest.fixture +def changing_root_inode(monkeypatch): + """Model a union mount switching the backing branch for one directory.""" + actual = os.fstat + roots = {} + + class RemappedStat: + def __init__(self, value, inode): + self.value, self.st_ino = value, inode + + def __getattr__(self, name): + return getattr(self.value, name) + + def fstat(fd): + value = actual(fd) + shift = roots.get((value.st_dev, value.st_ino), 0) + return RemappedStat(value, value.st_ino + shift) if shift else value + + def configure(path): + value = path.stat() + key = value.st_dev, value.st_ino + + def change(): + roots[key] = roots.get(key, 0) + 100 + + return change + + monkeypatch.setattr(os, "fstat", fstat) + return configure diff --git a/tests/integration/test_ebook_colocation.py b/tests/integration/test_ebook_colocation.py index cddc561f..ec46561a 100644 --- a/tests/integration/test_ebook_colocation.py +++ b/tests/integration/test_ebook_colocation.py @@ -1,6 +1,5 @@ # ruff: noqa: F811 import asyncio -import json from pathlib import Path from types import SimpleNamespace from uuid import UUID, uuid4 @@ -26,7 +25,7 @@ from app.importing.destinations import destination_configuration from app.importing.filesystem import directory from app.importing.naming import fingerprint -from app.importing.publication import object_id, private_staging, publication_lock +from app.importing.publication import destination_lock, private_staging from app.jobs.queue import get_queue from tests.integration.test_import_execution import ( # noqa: F401 destination_route, @@ -195,7 +194,7 @@ def contended_place(identifier, configuration, expected, **kwargs): Path(target["staging_path"]), Path(target["journal_path"]) if target.get("journal_path") else None, ) as stage, - publication_lock(stage, json.dumps(object_id(root), sort_keys=True)), + destination_lock(stage, root), ): return place(identifier, configuration, expected, **kwargs) return place(identifier, configuration, expected, **kwargs) diff --git a/tests/integration/test_import_cancellation.py b/tests/integration/test_import_cancellation.py index 502e3a3c..490a1cb8 100644 --- a/tests/integration/test_import_cancellation.py +++ b/tests/integration/test_import_cancellation.py @@ -1,4 +1,6 @@ import asyncio +import json +from pathlib import Path from uuid import UUID import pytest @@ -7,6 +9,7 @@ from app.db.models import ImportEntry, ImportRun, User, Version, Work from app.importing import cancellation, execution from app.jobs.queue import get_queue +from tests.filesystem_fixtures import changing_root_inode # noqa: F401 from tests.integration.test_import_destinations import route as destination_route # noqa: F401 from tests.integration.test_import_execution import ready_route, start # noqa: F401 from tests.integration.test_import_inspections import submit @@ -336,3 +339,42 @@ async def test_cancellation_history_blocks_lossy_downgrade(client, admin, databa finally: assert (await migrate("upgrade", "head")).returncode == 0 await get_engine().dispose() + + +async def test_stale_legacy_root_can_stop_owned_stage_and_start_new_import( + client, + admin, + database, + ready_route, # noqa: F811 + changing_root_inode, # noqa: F811 +): + route = ready_route + run = (await start(client, route)).json() + entry = run["entries"][0] + + def interrupted(phase): + if phase == "prepared": + raise execution.PublicationError("Old worker stopped before publication") + + await execution.execute(UUID(entry["operation_id"]), checkpoint=interrupted) + await get_queue().run_worker_async(wait=False, concurrency=1) + async with database() as db: + saved = await db.get(ImportEntry, UUID(entry["id"])) + journal = ( + Path(saved.specification["journal_root"] or saved.specification["staging_root"]) + / f"{saved.id}.json" + ) + receipt = json.loads(journal.read_text()) + receipt["destination_identity"].pop("root_marker") + journal.write_text(json.dumps(receipt)) + changing_root_inode(route["target"])() + assert (await cancel(client, run)).status_code == 202 + await get_queue().run_worker_async(wait=False, concurrency=1) + assert (await state(client, run))["state"] == "cancelled" + assert not list(route["target"].rglob("*.epub")) + replacement = (await start(client, route, "new-root-identity")).json() + assert replacement["entries"][0]["state"] == "queued" + await get_queue().run_worker_async(wait=False, concurrency=1) + assert (await state(client, replacement))["state"] == "confirmed" + assert len(list(route["target"].rglob("*.epub"))) == 1 + assert (route["source"] / "pack/book.epub").exists() diff --git a/tests/integration/test_import_execution.py b/tests/integration/test_import_execution.py index edaf7604..b4a2d0ff 100644 --- a/tests/integration/test_import_execution.py +++ b/tests/integration/test_import_execution.py @@ -28,6 +28,7 @@ from app.jobs.queue import get_queue from app.jobs.tasks import schedule_import_confirmation from tests.abs_import_fixture import ScanningBackend +from tests.filesystem_fixtures import changing_root_inode # noqa: F401 from tests.integration.test_import_destinations import route as destination_route # noqa: F401 from tests.integration.test_import_destinations import start_probe from tests.integration.test_import_inspections import submit @@ -628,3 +629,42 @@ def interrupted(phase): assert ( await client.get(f"/api/organization/inspections/{ready_route['plan']['inspection_id']}") ).json()["plan_id"] == run["plan_id"] + + +@pytest.mark.parametrize("interrupt", [False, True]) +async def test_root_inode_transition_finishes_import_and_retry( + client, + admin, + database, + ready_route, + changing_root_inode, # noqa: F811 + interrupt, # noqa: F811 +): + route = ready_route + change = changing_root_inode(route["target"]) + run = (await start(client, route)).json() + entry = run["entries"][0] + + def transition(phase): + if phase == "prepared": + change() + if interrupt: + raise execution.PublicationError("Worker interrupted after staging") + + await execution.execute(UUID(entry["operation_id"]), checkpoint=transition) + await get_queue().run_worker_async(wait=False, concurrency=1) + if interrupt: + held = (await client.get(f"/api/organization/imports/{run['id']}")).json()["entries"][0] + assert held["state"] == "held" and held["can_retry"] + response = await client.post( + f"/api/organization/imports/{run['id']}/entries/{entry['id']}/retry" + ) + assert response.status_code == 202, response.text + await get_queue().run_worker_async(wait=False, concurrency=1) + current = (await client.get(f"/api/organization/imports/{run['id']}")).json()["entries"][0] + assert current["state"] == "confirmed", current["message"] + original = route["source"] / "pack/book.epub" + published = list(route["target"].rglob("*.epub")) + assert len(published) == 1 + assert published[0].read_bytes() == original.read_bytes() + assert published[0].stat().st_ino == original.stat().st_ino diff --git a/tests/unit/test_combine_parts.py b/tests/unit/test_combine_parts.py index 260361f9..d7f39b4d 100644 --- a/tests/unit/test_combine_parts.py +++ b/tests/unit/test_combine_parts.py @@ -234,4 +234,8 @@ def test_single_file_parts_become_discs(tmp_path): restore_parts(spec) mark(spec, "separated") assert remove_combined(spec) - assert sorted(path.name for path in root.iterdir()) == ["Dark Age 1.m4b", "Dark Age 2.m4b"] + assert sorted(path.name for path in root.iterdir()) == [ + ".book-search-root", + "Dark Age 1.m4b", + "Dark Age 2.m4b", + ] diff --git a/tests/unit/test_destination_identity.py b/tests/unit/test_destination_identity.py new file mode 100644 index 00000000..1b99f6e2 --- /dev/null +++ b/tests/unit/test_destination_identity.py @@ -0,0 +1,281 @@ +"""Root-inode transitions with real files, journals and publication operations.""" +# ruff: noqa: F811 + +import json + +import pytest + +from app.importing import publication +from tests.filesystem_fixtures import changing_root_inode # noqa: F401 +from tests.unit.test_import_publication import specification # noqa: F401 + + +@pytest.mark.parametrize("mode", ["copy", "hardlink"]) +@pytest.mark.parametrize("interrupt", [False, True]) +def test_root_inode_change_allows_fresh_publication_and_retry( + specification, changing_root_inode, mode, interrupt +): + spec = specification.model_copy(update={"mode": mode}) + change = changing_root_inode(spec.destination_root) + original = spec.source_root / "pack/book.epub" + before = original.read_bytes() + + def checkpoint(phase): + if phase == "prepared": + change() + if interrupt: + raise RuntimeError("worker stopped") + + if interrupt: + with pytest.raises(RuntimeError, match="worker stopped"): + publication.publish_item(spec, checkpoint=checkpoint) + assert publication.remaining_import_bytes(spec) == 0 + result = publication.publish_item(spec) + else: + result = publication.publish_item(spec, checkpoint=checkpoint) + assert result["state"] == "published" + assert publication.publish_item(spec) == result + assert len(list(spec.destination_root.rglob("*.epub"))) == 1 + published = spec.destination_root / spec.folder / spec.files[0].name + assert published.read_bytes() == original.read_bytes() == before + assert (published.stat().st_ino == original.stat().st_ino) == (mode == "hardlink") + + +def stop_at(phase): + def checkpoint(current): + if current == phase: + raise RuntimeError("worker stopped") + + return checkpoint + + +@pytest.mark.parametrize("replacement", ["empty", "copied-marker", "symlink", "changed-device"]) +def test_replaced_root_is_rejected_before_publication(specification, monkeypatch, replacement): + import shutil + + spec = specification + old = spec.destination_root.with_name("old-library") + + def replace(phase): + if phase != "before-publish": + return + if replacement == "changed-device": + actual = publication.object_id + with publication.directory(spec.destination_root) as root: + identity = actual(root) + + def changed(fd): + value = actual(fd) + return {**value, "device": value["device"] + 1} if value == identity else value + + monkeypatch.setattr(publication, "object_id", changed) + return + spec.destination_root.rename(old) + if replacement == "symlink": + spec.destination_root.symlink_to(old, target_is_directory=True) + else: + spec.destination_root.mkdir() + if replacement == "copied-marker": + shutil.copy2( + old / publication.ROOT_MARKER, spec.destination_root / publication.ROOT_MARKER + ) + + with pytest.raises((publication.PublicationError, OSError)): + publication.publish_item(spec, checkpoint=replace) + assert not list(spec.destination_root.rglob("*.epub")) + assert not list(old.rglob("*.epub")) + assert (spec.source_root / "pack/book.epub").exists() + assert len(list(spec.staging_root.glob("item-*"))) == 1 + + +@pytest.mark.parametrize("change", ["missing", "token", "copy", "symlink", "hardlink", "fifo"]) +def test_marker_replacement_is_not_trusted_even_with_unchanged_root(specification, change): + import os + + spec = specification + with pytest.raises(RuntimeError): + publication.publish_item(spec, checkpoint=stop_at("prepared")) + marker = spec.destination_root / publication.ROOT_MARKER + old = spec.destination_root.with_name("saved-marker") + marker.rename(old) + if change == "token": + old.rename(marker) + marker.write_text("0" * 32 + "\n") + elif change == "copy": + marker.write_bytes(old.read_bytes()) + elif change == "symlink": + marker.symlink_to(old) + elif change == "hardlink": + os.link(old, marker) + elif change == "fifo": + os.mkfifo(marker) + with pytest.raises((publication.PublicationError, OSError)): + publication.publish_item(spec) + assert not list(spec.destination_root.rglob("*.epub")) + assert (spec.source_root / "pack/book.epub").exists() + + +def test_changed_root_keeps_one_destination_lock(specification, changing_root_inode): + from uuid import uuid4 + + spec = specification + change = changing_root_inode(spec.destination_root) + with ( + publication.private_staging(spec.staging_root) as staging, + publication.directory(spec.destination_root) as root, + ): + with publication.destination_lock(staging, root): + change() + with pytest.raises(publication.PublicationBusy): + publication.publish_item( + spec.model_copy(update={"entry_id": uuid4(), "folder": "Other book"}) + ) + assert publication.publish_item(spec)["state"] == "published" + assert len(list(spec.staging_root.glob("*.json"))) == 1 + + +@pytest.mark.parametrize("phase", ["prepared", "published-before-receipt", "complete"]) +def test_read_only_recovery_and_bookdrop_keep_receipts_across_root_change( + specification, changing_root_inode, phase +): + from app.importing.bookdrop import recovered_handoff + from app.importing.recovery import read_publication + from tests.unit.test_recovery_file_observations import inputs + + spec = specification + if phase == "complete": + publication.publish_item(spec) + else: + with pytest.raises(RuntimeError): + publication.publish_item(spec, checkpoint=stop_at(phase)) + changing_root_inode(spec.destination_root)() + + def snapshot(): + return { + str(p): (p.read_bytes(), p.stat().st_mtime_ns) + for p in spec.destination_root.parent.rglob("*") + if p.is_file() + } + + before = snapshot() + state, _, _, receipt = read_publication(*inputs(spec)) + assert state == ("staged" if phase == "prepared" else "published") + if phase != "prepared": + if phase == "published-before-receipt": + assert recovered_handoff(spec)["state"] == "prepared" + else: + assert recovered_handoff(spec)["state"] == "published" + assert snapshot() == before + assert receipt is not None + + +@pytest.mark.parametrize("phase", ["prepared", "published-before-receipt", "complete"]) +def test_legacy_receipt_is_not_rebound_and_only_owned_stage_can_be_cancelled( + specification, changing_root_inode, phase +): + from uuid import uuid4 + + from app.importing.cancel_files import cancel_files + + spec = specification + if phase == "complete": + publication.publish_item(spec) + else: + with pytest.raises(RuntimeError): + publication.publish_item(spec, checkpoint=stop_at(phase)) + path = spec.staging_root / f"{spec.entry_id}.json" + receipt = json.loads(path.read_text()) + receipt["destination_identity"].pop("root_marker") + path.write_text(json.dumps(receipt)) + changing_root_inode(spec.destination_root)() + with pytest.raises(publication.PublicationError, match="identity changed"): + publication.publish_item(spec) + assert json.loads(path.read_text())["destination_identity"] == receipt["destination_identity"] + if phase == "prepared": + assert cancel_files(spec)["state"] == "cancelled" + assert cancel_files(spec)["state"] == "cancelled" + assert not list(spec.staging_root.glob("item-*")) + assert ( + publication.publish_item(spec.model_copy(update={"entry_id": uuid4()}))["state"] + == "published" + ) + else: + with pytest.raises(publication.PublicationError): + cancel_files(spec) + assert len(list(spec.destination_root.rglob("*.epub"))) == 1 + assert (spec.source_root / "pack/book.epub").exists() + + +def test_legacy_receipt_on_unchanged_root_still_retries(specification): + spec = specification + with pytest.raises(RuntimeError): + publication.publish_item(spec, checkpoint=stop_at("prepared")) + path = spec.staging_root / f"{spec.entry_id}.json" + receipt = json.loads(path.read_text()) + receipt["destination_identity"].pop("root_marker") + path.write_text(json.dumps(receipt)) + result = publication.publish_item(spec) + assert result["state"] == "published" + assert result["destination_identity"] == receipt["destination_identity"] + + +def test_combine_and_separation_survive_root_inode_change(tmp_path, changing_root_inode): + from app.importing import combine + from tests.unit.test_combine_parts import context, library + + root, stage = library(tmp_path) + spec = combine.build_spec(context(root, stage)) + change = changing_root_inode(root) + + def checkpoint(phase): + if phase == "staged": + change() + + assert combine.publish_combined(spec, checkpoint=checkpoint)["state"] == "published" + assert combine.publish_combined(spec)["state"] == "published" + combine.mark(spec, "handed-over") + assert combine.cleanup_parts(spec) == [] + combine.mark(spec, "done") + combine.restore_parts(spec) + combine.mark(spec, "separated") + assert combine.remove_combined(spec) + assert len(list(root.rglob("*.mp3"))) == 6 + + +def test_seeding_rename_plan_survives_root_inode_change(specification, changing_root_inode): + import os + + spec = specification.model_copy(update={"mode": "rename"}) + plan = {"files": ["First Harbor.epub"]} + assert publication.remember_rename_plan(spec, plan) == plan + changing_root_inode(spec.destination_root)() + assert publication.load_rename_plan(spec) == plan + leaf = spec.destination_root / spec.folder + leaf.mkdir(parents=True) + os.rename(spec.source_root / "pack/book.epub", leaf / "First Harbor.epub") + assert publication.publish_item(spec)["state"] == "published" + assert publication.publish_item(spec)["state"] == "published" + + +def test_concurrent_marker_initialization_publishes_one_identity(specification, monkeypatch): + from concurrent.futures import ThreadPoolExecutor + from threading import Barrier + + barrier = Barrier(2) + actual = publication.no_replace + + def create(*args): + barrier.wait(timeout=5) + return actual(*args) + + monkeypatch.setattr(publication, "no_replace", create) + + def initialize(): + with publication.directory(specification.destination_root) as root: + return publication.destination_identity(root, create=True) + + with ThreadPoolExecutor(max_workers=2) as pool: + futures = [pool.submit(initialize) for _ in range(2)] + results = [future.result(timeout=10) for future in futures] + assert results[0] == results[1] + assert [p.name for p in specification.destination_root.iterdir()] == [publication.ROOT_MARKER] diff --git a/tests/unit/test_library_mount_staging.py b/tests/unit/test_library_mount_staging.py index 7309ef42..f7fd96b0 100644 --- a/tests/unit/test_library_mount_staging.py +++ b/tests/unit/test_library_mount_staging.py @@ -6,7 +6,7 @@ from app.importing.cancel_files import cancel_files from app.importing.filesystem import identity -from app.importing.layout import STAGING_NAME +from app.importing.layout import ROOT_MARKER, STAGING_NAME from app.importing.publication import PublicationSpec, publish_item from app.importing.recovery import journal_census from tests.filesystem_fixtures import group_mapped_media # noqa: F401 @@ -84,7 +84,9 @@ def interrupt(phase): with pytest.raises(RuntimeError, match="interrupted"): publish_item(spec, checkpoint=interrupt) - assert list(spec.destination_root.iterdir()) == [stage] + marker = spec.destination_root / ROOT_MARKER + assert set(spec.destination_root.iterdir()) == {stage, marker} + marker_before = marker.read_bytes(), marker.stat().st_ino assert any( item.get("entry_id") == str(spec.entry_id) for item in journal_census(stage, journals) ) @@ -100,7 +102,8 @@ def interrupt(phase): assert publish_item(spec) == receipt else: assert cancel_files(spec)["state"] == "cancelled" - assert list(spec.destination_root.iterdir()) == [stage] + assert set(spec.destination_root.iterdir()) == {stage, marker} + assert (marker.read_bytes(), marker.stat().st_ino) == marker_before if journals: assert not list(stage.glob("lock-*")) assert (stage / "untrusted.json").read_text() == "not a control journal"