Repository navigation
Remove deleted clipboard images from disk and from the clipboard - #18809
ThePrimeagen wants to merge 3 commits into
Conversation
Image history entries store their bytes in clipboard-images/, but deleting, clearing, or evicting an entry only rewrote clipboard-history.json, leaving the file on disk and, for the current copy, the live Wayland clipboard serving it. Add ClipboardHistory.removedImagePaths to compute image paths dropped between two histories, and Clipboard.pruneRemovedImages to unlink those paths under clipboard-images/ and run wl-copy --clear when the removed entry was the current one or all history was cleared. Call it from addClipboardEntry (eviction at the 500 limit, without clearing the new current clipboard), removeDisplayIndex (clearing only when history index 0 was deleted, so an older image deletion leaves the newer clipboard untouched), and confirmClearHistory.
[Medium impact] The changes stay within the desktop clipboard-history workflow. The PR still needs capture and cleanup to stop overlapping before it is safe to merge. Findings
SummaryRemoves image files when users delete clipboard entries, clear history, or reach the history limit.
|
| if (path.indexOf(root.imageDir + "/") === 0) | ||
| Quickshell.execDetached(["rm", "-f", path]) |
There was a problem hiding this comment.
Cleanup deletes a returning image
Image cleanup can delete a file that a capture is about to add back. Image names come from their bytes, and capture.sh discards its temporary copy when that file already exists. At the history limit, a preceding capture can evict that image and start rm while its re-capture prepares its JSON output. The re-capture then adds a path whose file was deleted, breaking its preview and later copy. Coordinate capture and cleanup so checking, publishing, and deleting the shared file cannot overlap.
There was a problem hiding this comment.
Fixed in 7fdbec7: removed files are no longer deleted right away. They wait in pendingImageRemovals, and when a five-second one-shot timer (restarted on each removal) fires, only the files removedImagePaths(pending, root.history) still reports are deleted. A re-capture that found A's file reports A within milliseconds, so A is back in history by then and its file is kept. One limit: a shell that exits within those five seconds leaves those files behind.
There was a problem hiding this comment.
Since 6774b80, the deferred delete runs in a detached process and checks the saved history. Why this closes the race: after capture.sh finds A's file, all it has left is rm of its temp copy, one date and one jq. The shell adds the line the moment it reads it and saves history at once. So A is back in the saved history long before the five-second prune reads it, and A's file is kept. The decision to delete is made against the history as it stands then, not the history at the moment B evicted A.
There was a problem hiding this comment.
You're right about the interleaving I flagged: after a re-capture is handled, saveHistory() persists A, and the detached prune checks that current history after five seconds, so it keeps A's file. My previous comment described unconditional deletion and no longer applies to this version; I consider the finding addressed.
Strictly, the five-second grace period isn't an atomic guarantee: a capture could find the existing file and then be stalled for more than five seconds before its JSON is handled and history saved. Given the remaining capture work is just temp cleanup, date, and jq, that's a narrow pathological delay rather than the ordinary race I raised.
Deleting the newest history entry ran wl-copy --clear on the guess that it was still on the clipboard. A password manager's copy is never recorded, so the older image stayed newest and deleting it wiped the password. Each image file is named for the sha256 of its bytes, so the clipboard is now cleared only when its image hashes to the deleted file's name. A capture that finds an image's file already on disk drops its own copy and reports the file a moment later, so an eviction in between could delete the file it was about to add back. Removed files now wait five seconds and are deleted only if the history still does not hold them. Co-authored-by: Cursor <cursoragent@cursor.com>
Removed image files waited five seconds in the shell's memory, so a shell restart in that window left them on disk for good. A detached process now waits instead and deletes each file the saved history still does not hold. Clearing history read and hashed the clipboard once per removed image. It now reads it once and compares the hash against every removed file. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Closing in favour of #14266, an earlier contributor PR for the same clipboard image cleanup. The deletion here races a re-capture of the same image after its sleep, deletes every candidate when the jq read fails, and adds a separate clearing of the live clipboard that races too. Reviewed by Claude Opus 5.5 with Codex Medium as a second opinion; its independence is not guaranteed. |
Deleting an image in clipboard history (Super+Ctrl+V, then Delete or Shift+Delete) only rewrites
clipboard-history.jsoninClipboard.qml: the image's file stays in~/.local/state/omarchy/clipboard-images, and the current copy stays on the clipboard. Images dropped at the 500-entry history limit leave their files behind too.Screenshots
Reproduce
qrencode -o /tmp/a.png hello && wl-copy --type image/png < /tmp/a.pngto copy an image.ls ~/.local/state/omarchy/clipboard-images; wl-paste --list-types: the image's file is still there, and the clipboard still offersimage/png.More cases
Shift+Delete, clearing the whole history
Copying an image when history already holds 500 entries: the file count grows on the nightly and stays put with the change
Related
clipboard-imagesafter Delete, Shift+Delete and the history limithistoryLimitto 50