Repository navigation
fix: an audiobook request delete removes only the files that request placed - #551
Merged
Merged
Conversation
…placed The audiobook delete no longer removes the whole book folder. It removes the audio the import hashed, RMAB's stale partial copies, and cover.jpg plus OS and Audiobookshelf leftovers only when nothing else is left but ebooks recorded by active ebook requests. The folder goes only when empty; the result lists what stayed (folderKept) and the admin table shows it. A delivered ebook request of the same book stays, unlinked, and the book keeps its ebook's folder and format. An ebook request delete removes a folder it leaves holding only junk and clears the stored path if it was that folder. The automatic and scheduled ebook request checks look by book. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Request deletion, ebook sidecar and file organization docs describe the new delete. The e2e library delete guard cases E-a, E-b and R6 now expect the folder to stay with what it still holds; a new #526 spec covers the ebook request that stays, the delete in both orders and the warning toast. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ebook sibling Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ete-scope Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lete A delivered ebook request stays, unlinked, after its audiobook request is deleted. The next audiobook import of the book now sets its parent to the new request (compare-and-set on a null parent, skipped when the new request already has an ebook request), so Fetch e-book, select-ebook and interactive search find it by parent instead of opening a second one. Also: - the stored ebook folder kept for the book is the newest delivered ebook request's (ordered by createdAt) - a reported-issue replace keeps the stored path and format while a delivered ebook request of the book remains - a failed rmdir of the folder an ebook delete emptied is logged, not reported as files kept - a failed rmdir of the emptied book folder is reported as the folder's error (folderKept.error), not listed as something the folder holds - cover.jpg and junk kept only because other files stay beside them are labelled "kept with the other files" Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- file-organization.md, request-deletion.md: the ebook request delete also removes the junk files and junk folders of a folder nothing else is left in, then the emptied folder and empty parents (before, only the recorded files). The audiobook delete never removes more than the former whole-folder delete. - The by-book ebook request check also sees other users' ebook requests on the book; a re-request's import links the surviving ebook request. - Limits: a delivered legacy ebook child keeps no stored path; another user's Download button 404s after the delete; an AppleDouble file with an audio extension counts as audio. - e2e 526a: select-ebook must answer 400 "already exists"; 526b expects the cover's new label. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…succeeded When deleteRequest fails, the book row may still point at the old audio folder, so the replace clears the stored path as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Linux gives a folder made right after a delete the freed inode number, so the junk walk's dev/ino re-check could pass on a different folder. Each walked folder now stays open from the walk until its own rmdir, which keeps its inode number taken; every plan is released on all paths. The request delete service tests now mock the book folder step and give the guard one path on every OS: on Windows they had taken the link-only branch, on Linux the folder step with an fs mock that lacks readdir. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Deleting an audiobook request with its files removed the whole book folder. The check before it only proves that the folder's audio is this import, so everything else in the folder went with it: the ebook the same book's ebook request placed there, the user's own files, NAS and Audiobookshelf leftovers.
Now the delete removes the audio of that import, and the folder only once nothing else is in it. Whatever stays is listed in the result and in the admin table's warning toast.
Changes
src/lib/utils/book-folder-delete.ts(new): the audiobook delete inside a folder the guard allows.filesHash(the same set the guard hashed; any change keeps everything).cover.jpg(a regular file, never a link) and the junk list only when nothing else is left but those and ebooks recorded by active ebook requests.rmdir).<name> could not be removed (...), or "partly removed" after the first file). ENOENT counts as already gone. Everyrmdirerror is ignored; a non-ENOTEMPTY one of the emptied folder is logged and returned asfolderKept.error(messageRequest deleted; the emptied book folder could not be removed (EBUSY)), never listed as something the folder holds.cover.jpgand junk kept only because other files stay beside them are labelledkept with the other files.src/lib/utils/book-folder-junk.ts(new): the junk list (.DS_Store,Thumbs.db,desktop.ini,._*,metadata.json,metadata.abs; folders@eaDir,.@__thumb) and a limited walk for the folders: lstat only, links never followed, at most 3 levels and 500 entries, and any link, special file, audio or ebook file keeps the whole folder. Each walked folder is re-checked before it is emptied and stays open until its own removal, so on a local filesystem a folder made in its place can't pass that check with a reused inode number (Linux reuses a freed one at once). A swap after the check, or a filesystem that keeps a folder's number by path (mergerfs, NFS), is not covered; only junk-list names are removed either way.src/lib/services/request-delete-files.ts: D1 calls the above (a symlinked title folder still loses only the link). D2 (ebook delete): when the folder it unlinked a recorded file from holds nothing but the junk list, it also unlinks those junk files, empties the junk folders (the same limited walk), removes the folder and sweeps empty parents. A failedrmdirof that folder is only logged: the recorded file is gone, so it is not "files kept" (filesDeleted: true, success toast).src/lib/services/request-delete-children.ts(new),request-delete.service.ts:parentRequestIdcleared, so select-ebook, the download anddownloadAvailablekeep working.filesHashand the library link are cleared and the status goes back torequested, as before. With several delivered ebook requests, the newest (createdAt desc) whose recorded ebook is on disk.folderKeptresult:{ folder, files: [{ name, why }], more }(first 20).src/app/api/admin/requests/[id]/route.ts,RecentRequestsTable.tsx:details.folderKept; the warning toast shows the message.organize-files.processor.ts(createEbookRequestIfEnabled),find-missing-ebooks.processor.ts: "this book already has an ebook request" is checked by book (audiobookId), not by parent. Without it, an ebook request that outlived its audiobook request would get a second one beside it after a re-request.createEbookRequestIfEnabledfirst callsrelinkParentlessEbookRequest(request-delete-children.ts), whatever the ebook settings: the book's newest parentless active ebook request gets the new audiobook request as its parent (compare-and-set onparentRequestId: null, so of two imports only one claims it; skipped when the new request already has an ebook request). Fetch e-book, select-ebook and interactive search look the ebook request up by parent, so after a re-request they find the survivor instead of opening a second one. The survivor can be another user's ebook request on the same book, as main's ebook linking by book already allows.reported-issue.service.ts: a replace keeps the book'sfilePath/fileFormat(which the delete pointed at the surviving ebook's folder) while a delivered ebook request of the book remains and the old request's delete succeeded; otherwise it clears them as before.library-delete-guard.ts: header wording only; the guard's checks are unchanged.request-deletion.md(D1 table, junk list, child rule,folderKept, Fixed Issues Deleting an audiobook request also deletes the ebook and other non-audio files in its book folder #526),ebook-sidecar.md,file-organization.md.What stays and why
ebook of another requestRMAB has no record that it placed itcover.jpg,.DS_Store,@eaDir/,metadata.json...kept with the other filesan import is still writing itDeleting both requests, in either order, leaves no folder. A book whose ebook was imported before #497 (no record) keeps that ebook and its cover, listed, as #497 already keeps it.
Tests
tests/services/request-delete.book-folder.test.ts(12, real temp folders). Covers: audio + cover + recorded epub (the ebook request stays, unlinked; the book keeps the ebook's folder); a user file keeps the cover; junk only;@eaDirholding a link; legacy ebook; a subfolder; stale vs fresh partial; a symlinked title folder; an undelivered child; both delete orders; a pre-Deleting an ebook request removes ebook files RMAB didn't download #497 book.tests/utils/book-folder-junk.test.ts(31): the junk names, the walk's limits, a folder deleted and made again or moved aside before removal, the D1 plan (unreadable folder, unhashed audio, recorded ebook through a symlinked path), the D2 empty-folder step.request-delete.library-guard.test.ts(later-file failure, ENOENT, rmdir EBUSY);organize-ebook-on-disk.test.ts(no second ebook request beside a parentless one);find-missing-ebooks.processor.test.ts(the lookup is by book);admin-requests.routes.test.ts(folderKeptpassed through).tests/processors/organize-ebook-relink.test.ts(6): the real organize processor and the real fetch-ebook route over one in-memory requests table. A re-request's import links the survivor and creates none (also with auto-grab off); Fetch e-book on the new request then answers "E-book request already exists (status: downloaded)" with the survivor's id and creates nothing; a survivor another import claims between lookup and write (compare-and-set), or one already linked elsewhere, is left alone; nothing is linked when the new request already has its own ebook request.request-delete.book-folder.test.ts(two delivered ebook requests in different folders: the book keeps the newest one's);reported-issue.replace-guard.test.ts(the replace keeps the stored path while a delivered ebook request remains, with or without a record; clears it for an undelivered one, or when the delete itself failed);request-delete.ebook-placement-guard.test.ts(a failed rmdir of the emptied ebook folder: success, nofilesKeptReason).book-folder-junk.test.ts(29) does not load. Per file: book-folder 8 of 12, library-guard 3 of 25, admin routes 1 of 29, find-missing-ebooks 1 of 22, organize-ebook-on-disk 1 of 9.Changed existing assertions:
request-delete.book-folder.test.ts(5 cases): acover.jpgkept beside user files is listed askept with the other files(wasRMAB has no record that it placed it).request-delete.library-guard.test.tsrmdir EBUSY:folderKepthas no files anderror: 'could not be removed (EBUSY)', messageRequest deleted; the emptied book folder could not be removed (EBUSY)(was the folderBook/listed as its own content).526-audiobook-delete.spec.ts526a: select-ebook must answer 400 with "already exists" (was: anything but 404); 526b: the toast text has the cover's new label.request-delete.library-guard.test.ts"removes a default-template folder": the folder no longer holds an unrecordedBook.epub(that file now stays; covered by the new legacy-ebook case).request-delete.library-guard.test.tsEACCES case: the failure is injected intounlink(wasrm). The reason is nowBook.m4b could not be removed (permission denied)(was "the folder was partly removed..."), and the audio and cover are checked to still be there. The "partly removed" reason is now asserted for a failure on the second file.library-delete-guard.spec.tsE-a and E-b: the folder stays holding the ebook request's epub (was removed); E-a's book row keeps the ebook's folder andfileFormatepub (was cleared).library-delete-guard.spec.tsR6: the root-owned subfolder is kept and listed while the audio goes, the ABS item is deleted and the row cleared (was: delete refused as partly removed, ABS item and row kept).Checks
npm run test(head 42fadc3, Linux, Node 20 container): 9306 passed, 72 skipped, 0 failed.npx tsc --noEmitand the e2e tsconfig: clean.dockerfile.unified, at 42fadc3): passes.Book.m4bis the only path removed under /media.Risk and rollback
.DS_Store,Thumbs.db,desktop.ini,._*,metadata.json,metadata.abs) and junk folders (@eaDir,.@__thumb), then the emptied folder and empty parents. A user file named exactly like a junk entry, alone in a separate ebook folder, goes with it.request-creator.service.ts). Same as main; left for a follow-up.cover.jpgthe user customized, in a folder with nothing else of theirs, is removed with the audio.Limits
filePathnull: its download answers 404 until the book is imported again.._Book.m4b) counts as audio, so a folder holding one fails the hash check and is kept whole (as on main).Screenshots
Closes #526
🤖 Generated with Claude Code
The admin toast after deleting an audiobook request whose folder also holds another request's ebook, the cover and a user file (1280 px, light):
Evidence: https://gist.github.com/kikootwo/5eafe6cc6276611667ed2b992bddd8b9
Closes #526
🤖 Generated with Claude Code