Skip to content

Pruning

Removing a pattern from a device’s whitelist stops Syncthing from syncing those files but leaves the copies on the device. warpzone prune deletes them, safely. It runs on the device against the local Syncthing, and only ever looks at the folders warpzone manages and whitelists (wz-library, wz-songs). The one exception is what an emulator kept outside the library for a game the hub deleted on purpose (see Emulator data of deleted games).

warpzone does not parse ignore patterns. Syncthing already decides what is ignored, and records the verdict in the folder database. Verified against Syncthing v2.1.7 with two real instances (hub send-only, device receive-only):

  • After a full scan, GET /rest/db/file?folder=&file= on a file that was synced and is now ignored answers local.ignored: true, local.invalid: true, local.localFlags: 2, local.size: 0, while global still holds the hub’s record (ignored: false, real size, hub modification time). Directories behave the same. This is the signal. GET /rest/db/ignores has no “does this path match” call (it returns the patterns and an expanded list), which is why the matcher is not reimplemented.
  • The flags are refreshed only by a full scan. Right after POST /rest/db/ignores, files still read ignored: false (only a directory was flagged); and after re-whitelisting, files still read ignored: true until the next scan. So prune always runs POST /rest/db/scan first and waits for it (GET /rest/db/status: state leaves scanning/scan-waiting; stateChanged moving or a grace period of a few seconds covers scans that start and end between two polls). It therefore does not depend on rescan_seconds.
  • A file Syncthing never recorded (ignored since before it ever scanned it, for example something the user created, or a file copied in by hand) answers 404. After the full scan this means “not synced and not ours”: it is counted and left alone, never listed as a leftover.
  • A device-only file that was scanned before it became ignored is mirrored into global with invalid/ignored set, so global existing does not prove the hub has it. The hub’s copy counts only when global is not deleted, ignored nor invalid.
  • Not usable: GET /rest/db/browse lists the global tree, ignored or not, with no flag; GET /rest/db/need and GET /rest/db/localchanged came back empty for ignored files (they list what is outstanding or locally changed, not what is ignored).

prune walks the folder on disk (never following symlinks) and asks GET /rest/db/file once per file; the answer is about 1 ms on loopback, so 100k files take on the order of a minute and a half.

A file is deleted only if rules 1, 2, 3 and 6 hold and either rules 4a and 5 or rule 4b holds. Rules 3 to 6 are checked again for each file, right before it is deleted. Rules 1 and 2 are checked once per run, before the rescan, and for rule 4b also before each delete, because comparing a large file can take minutes: the first failure stops the deletes of rule 4b in that folder and the run reports it. Rule 4a keeps its single check per run.

  1. The folder is a warpzone whitelisted folder, receive-only, not paused, at the path the config says, and its marker directory (.stfolder) exists (an unmounted SD card must not look like an empty library).
  2. The folder’s ignores are the final #include patterns-<device>.stignore (the bootstrap ignores, *, would mark the whole library as ignored), they load without error, and the whitelist files are in sync with the hub (Syncthing reports their local and global versions equal, provision_folders.in_sync).
  3. Syncthing flags it local.ignored after the full scan.
  4. The hub’s state allows it, in one of two ways:
    • 4a. The hub has it. global is not deleted, ignored or invalid, and has the same kind (file or symlink).

    • 4b. The hub deleted it on purpose. global is deleted and is neither ignored nor invalid, and all of these hold (see Files the hub deleted on purpose):

      • the folder is wz-library and the file is a regular file, never a symlink and never a directory;
      • the console’s patterns-_deleted.stignore is in sync with the hub, read after the full scan: Syncthing reports that file’s local and global versions equal (the same notion as the whitelist files in rule 2, and global is not deleted, ignored or invalid either), and its size on disk equals its local record and its modification time is within 2 s of the local record’s modified;
      • the record parses, and an entry that has not expired lists the same path with the same size and a modification time within 2 s of the file’s (FAT stores 2 s steps);
      • when that entry has a sha1, the file’s SHA-1 equals it. A path can be in several entries; it is authorised when any one of them matches the file, so one with no sha1 that matches the size and time is enough.

      A run that may delete (--apply) compares the files that need a SHA-1 while it inspects the folder, before the guards and the size cap are computed; a copy whose content differs, or that cannot be read, is kept and listed with its reason, and is not a failure. Right before each delete the rest is checked again: the record file is still the version that was read (the same Syncthing local version, the same size and modification time on disk), global is still deleted, the file is still a regular file with no link on the way to it, and its device, inode, size and modification time are those it had when it was compared (a file that changed since is kept, not compared again).

  5. Under 4a, for regular files, the size equals the hub’s and the modification time is within 2 s of the hub’s (FAT stores 2 s steps). A file edited on the device after it was synced is reported, not deleted. Under 4b the hub has no copy to compare with, so the record’s size and modification time (rule 4b) take its place.
  6. It is not .stfolder, .stignore, .stversions or patterns-*.stignore at the folder root, nor a Syncthing temp file, and it lives in a directory whose real path is inside the folder root (for rule 4b, with no link in the path at all). Symlinks are unlinked, never followed. The subtree of any other Syncthing folder nested in the root (the save folders under ~/Emulation) is skipped.

Files that are ignored but fail 4a and 4b, or 5, are listed as kept, with the reason; the expected ones are only counted (see Files the console generates). After deleting, directories that held deleted files, that Syncthing flags ignored, and that are now empty are removed (rmdir, so a non-empty one is never touched). A full scan does not always flag a directory that just became ignored (seen on Linux), so each candidate is scanned on its own (POST /rest/db/scan?folder=...&sub=...) before its record is trusted.

The default is report only. --apply deletes. A run that would delete more than --max-gib (default 50) refuses to delete anything, exits 1, and needs --force or a higher cap; the cap counts the total over all folders. A run with --unattended has no size cap and the half-of-device guard instead (see Unattended runs).

Output is a per-folder summary (leftover files and bytes, grouped by the first two path levels; --list prints every file), one line <folder>: N file(s) the hub deleted on purpose, <size> when there are such files (they are listed with the leftovers), the unusual files kept and why, one line with the count of files the console generates by itself, and the count of files Syncthing does not know. Exit codes are those of the other commands: 0 ok (a report with leftovers is still 0), 1 for a runtime error, a folder that was skipped as unsafe, a refused cap or files that could not be deleted, 2 for usage or config errors.

--notify-url URL (http or https), with or without --unattended, POSTs one JSON object per folder that has leftovers or files the hub deleted on purpose: {"device", "folder", "files", "bytes", "applied"}, counting both. When applied, files and bytes are what was actually deleted. It is meant for a Home Assistant webhook. The URL may embed a secret, so it is never printed, and a failed notification is a warning, not a failure of the run. No API key or path appears in the payload. The sender is notify.py (notify, which raises NotifyError), shared with inbox --notify-url (see The run record); prune catches the error and prints it as a warning.

EmuDeck and some emulators write files into the library that the hub never has, and the profile’s [library] ignore list ignores them, so on every run they are ignored files the hub cannot restore. Listing hundreds of them hides the few kept files that matter. prune.is_generated (pure, no I/O) recognises them by path, relative to the folder root, with exact, case-sensitive matching per path segment:

  • roms/<system>/metadata.txt and roms/<system>/systeminfo.txt (one level below roms).
  • Any readme.txt at any depth under bios/.
  • Everything under bios/PPSSPP, bios/HdPacks, bios/Mupen64plus, bios/fbneo, bios/kronos, bios/same_cdi, bios/ryujinx, bios/shadps4 and bios/yuzu.
  • RetroArch’s Flycast files directly in bios/dc: vmu_save_*.bin, dc_nvmem.bin and vulkan_pipeline.cache, plus everything under bios/dc/data.
  • bios/mame2003-plus/hiscore.dat and everything under bios/mame2003-plus/artwork.

The list mirrors the profile’s ignores, so a new ignore for a file an emulator writes by itself is added to both. A file counts here only when its verdict is “the hub does not have it”: a matching file the hub does have in a different state is still listed as kept. These files are still kept, never deleted; they are reported as one line, <folder>: N file(s) emulators and EmuDeck generate themselves, in the normal and in the unattended output. The kept ... ignored file(s) the hub cannot restore line then counts only the other files. The --notify-url payload is unchanged.

A game turned off for a console (its whitelist line removed or an exclude added, so its files are ignored there and wait for prune) and then deleted from the hub in the library UI never receives the deletion, because the path is ignored on the console, and rule 4a cannot hold: the hub has no copy, so the file would be kept as “the hub does not have it” for ever. Rule 4b closes that gap with the hub’s own record of deleted games, patterns-_deleted.stignore in the root of the library folder, which Syncthing delivers to every console like the whitelists.

How the console uses it (prune.read_record, the only I/O that reads it, for wz-library only and after the full scan):

  • No file is the same as an empty record. A file that is not in sync with the hub, whose size on disk is not what Syncthing recorded, that is a link, that changes while it is read or that trash_record.parse does not accept (it returns None, never raises) authorises nothing.
  • trash_record.index is applied with the current time, so an entry past its expires (180 days after the deletion) authorises nothing, even though it may still be in the file.
  • The record alone never proves that the hub no longer has a file: it can briefly list a path that exists again, and the record and the hub’s file index reach the console in no guaranteed order. So the console checks the hub’s own state for that path, Syncthing’s global record, which must be deleted. Until both have arrived the file is kept, as before, and the next run decides.
  • prune.classify (pure) decides 4b without hashing and without I/O, from the db/file answer, the file on disk and the record’s files for that path (prune.record_matches: same size, modification time within 2 s). prune.sha1_ok (pure) compares a record file’s sha1, when it has one, with a digest.
  • Hashing is in chunks of 1 MiB and only for a copy whose matching entries all have a sha1 (a matching entry with none authorises it without hashing). A report-only run never hashes, so it lists such a copy as deletable and says on the line that the content is not compared yet; the run with --apply compares it during the inspection (prune.inspect_folder), so a copy that differs, or cannot be read, is already kept when the guard and --max-gib are computed. A run that a guard or the cap then refuses has therefore hashed these files. A file whose size or modification time changes while it is hashed is kept. At the delete, apply_folder does not hash again: it compares the file’s identity with the one it had when it was compared, and then checks record_unchanged, global and the folder (rules 1 and 2) once more before the unlink.
  • A copy that differs would be hashed again by every run of the timer. The run therefore leaves ~/.local/state/warpzone/prune-compared.json (next to prune-guard.json, mode 0600, replaced atomically): for each such copy the SHA-256 of its path (no file name), its device, inode, size and modification time, and the SHA-1s it was compared with. While those are unchanged the run skips the hash. A missing or unreadable file only means hashing again, a failed write is a warning, and an --apply run that settles without such a copy removes the file.

What is deleted and what is not:

  • A file removed from the hub by hand, with rm or in the hub’s file manager, is never enough: the hub’s own deletion record says only that the hub lacks the file, which a wrong path, a failed mount or a mistake looks the same as. Only the library UI writes the record, so a copy with no entry in it stays and is reported as before.
  • A copy whose size or modification time differs from the entry’s, or whose content differs from its sha1, was edited on the console or is another file at the same path. It stays, listed as kept (its content differs from the deleted game, or cannot be read to compare with the deleted game when it cannot be opened).
  • A symlink or a directory at a recorded path stays.
  • A copy whose entry expired, or whose trash entry was purged before the hub ran a version that writes the record, is not in the record and needs a manual delete.
  • The other whitelisted folders (wz-songs) never use the record.

The unattended runs apply 4b too. These files count as leftovers for the half-of-device guard and for --max-gib; see Unattended runs.

An emulator writes data of its own for a game outside the library, and it stays when the game goes. RPCS3 compiles each game it runs into ~/.cache/rpcs3/cache/<serial>/ (tens of MB) and installs the disc’s game data into dev_hdd0/game/<serial>/ of its storage (often more than a GB), with an empty marker dev_hdd0/game/$locks/<serial>_v<version> (the folder name starts with U+FF04, a fullwidth dollar sign). When the hub deletes the game on purpose, prune removes them too, in the same run that removes the game’s file. Only what a profile declares is removed ([leftovers.<emulator>], see Emulator data of deleted games in Profiles), and the shipped profiles declare RPCS3 only. The pure decisions and the disk side are in prune_leftovers.py; prune.py gives it the Syncthing answers.

Which games. A game file P (a path of the record, relative to the library) has its emulator data removed only when all of these hold (prune_leftovers.gone, pure):

  • the record of deleted games lists P in an entry that has not expired, read as for rule 4b (prune.read_record: in sync with the hub, the same size, parsed whole);
  • P matches one of the declaration’s games patterns (roms/ps3/*.iso for RPCS3; matched segment by segment, so * never crosses a /);
  • Syncthing’s global for P is deleted and is neither ignored nor invalid: as for rule 4b, the record alone never proves the hub no longer has the game;
  • the console no longer holds P: nothing is at that path once the run’s own deletes are done. A copy that is kept (its content differs from the record, or it could not be deleted) keeps its emulator data too. A report counts the copies its --apply would delete as gone.

This covers both ways a deleted game leaves a console: a console that still had the game whitelisted receives the deletion from Syncthing, and one that had turned it off deletes the copy by rule 4b in the same run. A game that only left a whitelist is not in the record and the hub still has it, so its emulator data stays: it may come back. A game removed from the hub by hand is not in the record either.

Where the id comes from. From the emulator’s own index, never from the game file (the console’s copy is usually gone by then) and never from the hub. The declaration names the index (index, RPCS3’s ~/.config/rpcs3/games.yml, which maps each serial RPCS3 has seen to the path it ran it from) and its format (index_format). An id belongs to P when the index maps it to <library root>/P, after os.path.normpath, with the root as configured or as resolved. Why this and not an id the hub writes into the record:

  • The index is what named the emulator’s folders, so the id is exactly the one they carry.
  • It needs nothing from the hub: a record written by any hub version works, and the record’s format does not change. The hub could only read the serial from the image in the trash (the catalog keeps none for PS3, which has no DAT), and an older record would have no id at all.
  • A game the emulator never ran is not in its index, and left nothing to remove.

A path the index does not list, an index that is missing or cannot be read, and a path the index lists under more than one id (prune_leftovers.ids_for) give nothing. The one format is yaml-flat, the settings layer’s handler (settings_formats): one key: value per line; a key whose value spans lines, is nested or is given twice is left out, and a quoted value is unquoted strictly (a double-quoted one with the escapes \\ \" \/ \t \n \r \0 \xNN \uNNNN \UNNNNNNNN only, anything else leaves the key out). Before it is put into a path, an id must fully match the declaration’s match ([A-Z]{4}\d{5} for RPCS3) and also [A-Za-z0-9_-]+, whatever match says, so it can never hold a separator, a dot or a wildcard.

What is removed. Each paths template has {id} once, in its last component. The rest is the base folder (~/.cache/rpcs3/cache), with no placeholder or wildcard, and the last component with the id in place is a pattern (fnmatch.fnmatchcase; {id}_v* for the marker). The candidates are the entries of a listing of the base whose name matches, so no name is ever built and opened. prune_leftovers.plan (pure; it gets the listings, the real paths of the folders and what Syncthing tracks) keeps a candidate, listed with its reason, when:

  • the base is reached through a symbolic link (its real path is not the declared one, ~ expanded), or the candidate is a link or neither a folder nor a regular file;
  • it is, holds or lies inside a protected folder: every folder of the local Syncthing (the save folders, the BIOS placements) and every save folder and BIOS placement the profile declares, current and previous paths, whether the console syncs them or not, compared by real path. So a save or a trophy is never touched, even when a template points at it.
  • it lies inside a whitelisted folder that this run inspected and holds a path Syncthing tracks there. The library folder is ~/Emulation, which holds EmuDeck’s storage/: a candidate there is allowed only when no file below it got a verdict other than “not known to Syncthing” or “the hub does not have it” in the walk of rule 3 (FolderReport.tracked), and when it does not hold the folder’s root. A whitelisted folder that was not inspected is protected whole.
  • a game the console still holds carries the same id. The declaration’s optional held_id names a reader (ps3-param-sfo for RPCS3: the TITLE_ID of PS3_GAME/PARAM.SFO, read from the volume descriptor, two directories and the SFO, never the whole image, with the inbox’s own code, inbox_console_iso.ps3_title_id). prune_leftovers.held_ids reads every file of the library that the declaration’s games match, except the copies this run deletes itself (rule 4b). This is what keeps the data of a game the hub replaced with another file of the same serial (A.iso deleted, A (Rev 1).iso filed) while the index still names the deleted file because RPCS3 has not run the new one yet. A file that cannot be read for its id (not a disc image, a link, a folder on the way that cannot be listed) keeps every id of that emulator: in doubt, nothing is removed.

An item that cannot be measured (it went away or cannot be read between the listing and its lstat) is kept and listed with the error.

A folder is removed with everything in it and never through a link: shutil.rmtree, which works through file descriptors where it can (shutil.rmtree.avoids_symlink_attacks); where it cannot, nothing is removed. Its size is the sum of the regular files below it, walked without following links.

When. In the run that handles the record: with --apply (by hand or from the timer) after the folders’ own deletes, as a report otherwise. Not when the library was not inspected (skipped, or busy for the timer), and not when the size cap or the half-of-device guard refused the run. Nothing is removed while one of the declaration’s process runs: the run says so and the next one tries again, which is not a failure; with no /proc to read, it warns and removes nothing. A process counts when its comm, the base name of its argv[0] or the base name of the program it runs (readlink /proc/<pid>/exe, without a (deleted) suffix; procs.running with exe) is one of the names. The last is what finds RPCS3 started from EmuDeck’s AppImage: the AppImage runtime is rpcs3.AppImage, but the emulator it starts is AppRun.wrapped running /tmp/.mount_<random>/usr/bin/rpcs3, and the runtime may be gone while the emulator runs. A /proc/<pid>/exe that cannot be read (another user’s process) only drops that clue. No state is kept. Once removed, a path is not found again, so each item goes once; the check repeats while the record entry lives (180 days) and costs one read of the index and one listing per base.

Right before each removal these are checked again, and a failure keeps the item and counts as not deleted: the library folder still passes rules 1 and 2, the record file is the version that was read (record_unchanged), global for P is still deleted, nothing is at P, the index read again still maps the id to P, the base is still reached through no link, and the item is still the same folder or file (device, inode, kind) and not a link. The process check and the ids of the games the console holds are read once per emulator, before its first removal; an id held then or at the inspection keeps its items (a game with that id that arrived meanwhile counts).

Output and counting. Per emulator, <emulator>: N item(s) of M game(s) the hub deleted on purpose, <size>, then each item (~ for the home folder) with its size, and the kept ones with their reason; with --apply, <emulator>: deleted N item(s), <size>. An unattended run lists no item, only the counts (a warning about an item that could not be removed names it, as it does for a file). The bytes count for --max-gib (the attended cap covers the whole run), but not for the half-of-device guard, which compares what a folder would lose with what it holds and these are outside every folder, and not for the --notify-url payload, which stays per folder. A removal that fails or an item that changed is exit 1, like a file that could not be deleted; an item kept by the rules above is listed and is not a failure.

Limits.

  • For a disc game, dev_hdd0/game/<serial>/ also holds an update or a DLC installed from a .pkg. It goes with the game: the game was deleted on purpose. A PS3 game that is not a disc image (a .pkg installed into dev_hdd0/game/) is not in scope: inbox never places one, so the library and the record never hold one.
  • A game deleted and filed again under another name keeps its data while the console holds the new file (see held_id above). Filed again under the same name, the hub drops the path from the record and has the file, so nothing is removed either. Only a game filed again after the console removed the data starts without it: RPCS3 rebuilds the cache and the installed data, and an update or DLC installed there must be installed again.
  • Saves and trophies (dev_hdd0/home/<user>/savedata and trophy) are save folders and stay.

A game turned off in the library UI must leave the console without anyone running a command there, so each console that lists library runs prune <device> --apply --unattended from a timer. This is the one exception to the rule that a destructive command acts only when a person runs it with --apply (see Writing commands). Every safety rule above still holds, with the re-checks described there. What changes:

  • No size cap. --max-gib does not apply: one PS4 game is larger than the 50 GiB default. --unattended requires --apply and refuses --max-gib and --force (usage error).
  • The half-of-device guard. For each folder, the bytes the run would delete (the leftovers and the files the hub deleted on purpose) are compared with the bytes of the regular files the walk found in that folder (what the device holds there, nested Syncthing folders excluded). When any folder would lose more than half (2 * delete > held), the run deletes nothing in any folder, prints why and exits 1. prune.guard (pure) decides it from the per-folder totals. It is what stops a whitelist that was emptied by mistake, or a library folder that briefly looks wrong, from wiping a console. A removal it refuses stays until the user runs prune --apply by hand, where the size cap and --force apply as usual; a device that holds a single game can only lose it that way.
  • A refusal leaves a trace. Nobody reads the journal of the timer, so when guard names a folder the run writes ~/.local/state/warpzone/prune-guard.json (next to the updater’s state, mode 0600, replaced atomically through safefile.write_file): version, time (seconds since the epoch) and, per folder id, the bytes it would have deleted and the bytes the device holds. No file or folder names. prune.render_guard_state and prune.parse_guard_state are the pure ends of it; record_guard_refusal and clear_guard_refusal touch the disk. Any --apply run that goes on to settle clears the file: no folder was skipped or busy, and it either deleted or had nothing to delete (a manual prune --apply after the review does it). A report-only run, a busy or skipped run, and a refusal by the attended size cap leave it as it was. prune_device takes the state folder as state_base (the CLI passes ~/.local/state/warpzone; None records nothing), and a failed write is a warning, not a failure of the run. doctor reads the file (see Doctor). health does not: it decides whether a version works, and a refusal says nothing about that.
  • Busy is not a failure. A full scan that gets no answer in time (“rescan queued”, see Syncthing API surface) means the device is still pulling: the run deletes nothing and exits 0, and the next tick tries again. Every other unsafe folder (no marker, bootstrap ignores, whitelists not in sync) still exits 1, so the unit shows as failed.
  • Output. Only the summary lines; --list is ignored. --notify-url works as for any run.

The timer is installed by provision device for a device that lists library, unless its config sets auto_prune = false: ~/.config/systemd/user/warpzone-prune.service (Type=oneshot, ExecStart=<python> -m warpzone prune <device> --apply --unattended, naming --config only when it is not the default) and warpzone-prune.timer (OnBootSec=15min, OnUnitActiveSec=1h), written and started like the artwork timer (rewritten only when their text differs, the same import check of the Python, the user manager running without a login session). With auto_prune = false the units are removed if they exist.