Skip to content

fix: never let post-import cleanup delete library content - #353

Merged
kikootwo merged 6 commits into
mainfrom
fix/242-guard-import-cleanup
Oct 19, 2026
Merged

kikootwo merged 6 commits into
mainfrom
fix/242-guard-import-cleanup

Conversation

@kikootwo

Copy link
Copy Markdown
Owner

Closes #242

What changed

  • Cleanup after an import can no longer delete anything inside your media library.
    • Download cleanup only runs for downloads RMAB itself completed: the job carries its own download record, that record is completed, and its stored path matches the job's path. Manual and bulk imports never trigger it.
    • Every delete goes through a guard that resolves symlinks and second mounts of the same folder, and refuses anything inside the library, the media root, or a configured download root itself.
  • "Remove source after import" never removes a folder inside the library. With selected files, only those files are removed. An in-place re-import keeps its files and cover art (the cover was being copied onto itself and truncated).
  • Ebook imports no longer delete their source folder. Only the imported file is removed, and only when it was a direct download.
  • A failed manual or bulk import is marked failed once, with a message to run it again, instead of being retried as a different download.
  • If the configured download folders can't be read, nothing is deleted.

Why

#242 (thanks @azerbe): after a manual import from the library, the "remove after processing" cleanup deleted the imported folder. Importing from the media root removed the whole library.

Evidence

End-to-end on disposable Docker stacks (generated audio, fake indexer and download clients, real Audiobookshelf), with three layouts: usenet with a second mount of the media volume, torrent, and usenet with remote path mapping.

  • 14 cases plus smoke all pass on this branch.
  • On main 99fabde, 11 of the delete cases lose library files, including the media root case.
  • The 3 control cases (a seeding torrent is kept; a usenet source is removed after import, with and without path mapping) pass on both.

The per-case table is in the gist.

Full logs: https://gist.github.com/kikootwo/f76c76ece312b6ae0bec8e1eaa5a4337

Checks

  • Image build (dockerfile.unified): PASS
  • Unit suite (Node 20, 2712 tests): PASS
  • E2E (14 cleanup cases, smoke, 3 failure-handling checks): PASS

Risk and rollback

Cleanup now declines in more situations than before. When it declines, the files stay where they are and the log says why. Organize jobs that were already queued when you upgrade skip download cleanup once (their files stay, and client history isn't archived). Reverting this PR restores the previous cleanup.

🤖 Generated with Claude Code

kikootwo and others added 6 commits October 19, 2026 13:13
Moves cleanupDownloadAfterOrganize and cleanupSourceAfterOrganize out of
organize-files.processor.ts into organize-cleanup.ts unchanged, so the
processor shrinks and the cleanup paths can be tested on their own. No
behavior change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After a manual import, "remove after processing" cleanup took the
request's newest download history row regardless of where the job came
from and recursively deleted the import folder. A folder picked from the
media library, or the media root itself, was wiped. The opt-in source
cleanup also unlinked the library files when source and target were the
same folder, and an in-place re-import truncated cover.jpg to 0 bytes by
copying it onto itself. The ebook branch removed the whole import folder,
which can also hold the audiobook.

- Download cleanup now runs only for downloads RMAB completed: the job
  must carry its downloadHistoryId (monitor, retry, direct download and
  request retry pass it; manual and bulk import never do), the row must
  be completed and its stored download path must equal the job path.
  Missing indexer protocols fall back to the client's protocol.
- retry-failed-imports stores a client-reported (mapped) path on the row
  before queueing so retried downloads stay eligible; a failed write only
  skips cleanup.
- New assertSafeToDelete guard used by both cleanups refuses the organize
  target (equal, containing or inside), any configured root (media,
  download, per-client custom and mapped paths, bookdrop), and anything in
  the media dir unless it sits in a download root nested inside it.
  Paths are realpath-canonicalized and compared by dev+inode as well.
- Ebook source cleanup removes only the imported ebook file.
- copyFile is a no-op when source and destination are the same file, and
  the organizer skips audio and cover art that are already in place.
- media_dir resolution is shared between the organizer and cleanup.

Fixes #242

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The ebook organizer no longer deletes its source. Direct HTTP download
  files are removed afterwards by cleanupDirectDownloadSource, which uses
  the same provenance check as download cleanup (the job's own
  downloadHistoryId row, completed, stored path == job path) and the
  delete guard.
- The ebook branch decides seeding vs direct from the job's own download
  row instead of the request's newest row.
- Ebook source cleanup removes the file the organizer actually read
  (the original EPUB only when the Kindle fix produced a temp copy), so a
  folder with several EPUBs loses only the imported one.
- Docs: note that organize jobs queued before upgrading skip download
  cleanup once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nual

- A manual or bulk import (no downloadHistoryId) that fails is marked
  failed with a message to re-run it, instead of going to
  awaiting_import, where the retry job would re-queue the request's last
  client download with its cleanup.
- Whole-folder source cleanup never removes a folder inside the media
  library; with selectedFiles only those files are removed.
- The empty-parent sweep is bounded by the nearest configured root and
  never removes a root (download_dir, client custom or mapped paths,
  media_dir, bookdrop) or climbs from a download root into the library.
- Cleanup aborts when the download client list cannot be read.
- Deletes act on the canonical path the guard checked; a symlink
  candidate is removed as a link.
- Windows drive roots normalize to "C:/" instead of "C:." so the current
  directory is never mistaken for an ancestor.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Manual and bulk imports are told apart from download jobs exactly
  (no downloadHistoryId and a boolean cleanupSource). When one fails, the
  request is marked failed and a success:false result is returned, so
  queue retries no longer re-run it or send repeated notifications.
  Download jobs queued before this change keep their import retry.
- The failure message reads "Import from <path> did not complete: ...",
  and the notification text leaves the server path out.
- The delete guard reads download_dir and download_clients straight from
  the database and parses without a fallback; any error aborts the
  delete.
- Trailing separators are stripped before the symlink check.
- Guard root collection, guarded delete and the parent sweep move to
  organize-cleanup-guard.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kikootwo
kikootwo merged commit 0c86f22 into main Oct 19, 2026
2 checks passed
@kikootwo
kikootwo deleted the fix/242-guard-import-cleanup branch October 19, 2026 20:16
@kikootwo kikootwo mentioned this pull request Oct 19, 2026
kikootwo added a commit that referenced this pull request Oct 19, 2026
Brings in #353 (delete guard), #355 (no Prowlarr key in logs), #358
(startup hardening, app-start.sh) and v1.2.4, so the e2e harness runs
on this branch include those fixes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kikootwo added a commit that referenced this pull request Oct 19, 2026
…e2e-harness

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Imports from media library conflict with indexer settings for file removal after download

1 participant