DRSProjects

DocsParbit

Parbit documentation

How it works

Per-file parity, moves, safety

Parbit is a thin, careful layer over par2, the Reed–Solomon recovery format known from Usenet. par2 does the maths; Parbit decides which files need parity, keeps it in sync with your edits, and makes checks and repairs safe.

Layout on disk

The hidden folder .parbit/ mirrors parbit/: every protected file gets a folder of its own, named like the file.

parbit/photos/2019/beach.jpg
.parbit/photos/2019/beach.jpg/
    data.par2            index: file name, size, block checksums
    data.vol000+40.par2  recovery blocks
    .parbit-meta         what Parbit knows about the file

.parbit-meta is a small text file:

4183922 1563447820.000000000      size in bytes, modification time (Unix seconds, 9 decimals)
r=20                              redundancy used
alias=0                           1 if par2 had to see the file under a scratch name
name=photos/beach.jpg             only for moved files: the name inside the parity set

Because each file has its own set, a change costs only that file's parity, and one damaged parity set never affects another file.

Detecting changes

update and status walk parbit/ once and compare each file's size and modification time with its .parbit-meta:

Size Time Content Interpretation
same same — unchanged (not even read)
different — — you edited it → new parity
same different intact touch, copy tool, time zone → only the time is recorded
same different damaged, shift of whole quarter hours damage seen after a time-zone shift → reported, old parity kept
same different damaged, other shift a same-size edit → new parity
same same damaged bit rot: invisible to update, found by verify

The last row is the reason to run verify: real bit rot changes neither size nor time. Only reading the file and comparing it with its parity reveals it.

Moved and renamed files

When a file disappears and a new one appears with the same size and modification time (and no other file shares that pair, so the match is unambiguous), Parbit treats it as a move. update moves the parity folder and records the original name as name=. No data is read and no parity is computed.

Keeping the original parity matters: re-protecting a moved file would turn any damage it already had into "correct" parity.

par2 identifies files by the name they had when the set was created. To check a moved file, Parbit renames it back to that name for the duration of the check (an instant rename on the same drive) and returns it right after. Before renaming, it writes the two names to .parbit.journal: if the run is interrupted, the next run (on either system) puts the file back. If the old name is now used by another file, par2 is given a scratch hard link (or copy) under the old name instead.

Time-zone shifts

exFAT and FAT drives store local time. Moving such a drive between computers with different time zones, or across a daylight-saving change on some systems, can shift every timestamp by whole hours (or half/quarter hours in some zones). To tell this apart from editing, Parbit checks files whose size is unchanged but time differs:

  • content intact → the new time is recorded; nothing is recomputed;
  • content damaged and the shift is a multiple of 15 minutes (up to 26 hours) with an identical sub-second part → reported as damage, parity kept so repair can fix it.

Block size and redundancy

par2 splits a file into blocks and creates recovery blocks. Any damaged block can be replaced by any recovery block. par2's default of ~2000 blocks per file gives tiny blocks, and much more parity than data, for small files. Parbit sizes blocks per file:

File size Block size
up to ~40 KB a tenth of the file (at least 10 blocks), rounded up to 4 bytes
~40 KB to ~8 MB 4 KiB
above ~8 MB size / 2000

The number of recovery blocks is REDUNDANCY% of the data blocks, rounded up, at least one. With 4 KiB blocks, a bad 4 KiB sector costs exactly one block. A file is repairable when no more blocks are damaged than there are recovery blocks, which is roughly REDUNDANCY% of the file, scattered anywhere.

Repairs

par2 repairs by writing a new file and renaming the damaged one aside. That would give the file a new identity: default permissions, lost ACLs, broken hard links. So Parbit:

  1. makes a scratch hard link to the file (a copy where hard links aren't supported, such as exFAT), under the name par2 expects;
  2. lets par2 repair the scratch entry;
  3. writes the repaired content into the original file, in place;
  4. restores the modification time and records it, so the next update sees nothing to do.

If writing back fails (disk full, for example), the repaired copy is saved as .parbit-quarantine/<time>/<name>.repaired and the parity is untouched, so nothing is lost. With KEEP_DAMAGED=yes the damaged version is copied there first.

Safety

  • Your data is only written to repair it. Everything else Parbit writes is parity and its own small files. The only other operation on your files is the temporary rename described above.
  • No half-written parity. New parity is built in .parbit.tmp/ and moved into place when complete. Leftovers of an interrupted run are cleaned up by the next one.
  • No damage baked in. Parity is never recomputed for a file that might be damaged: moves keep the old parity, time changes are checked first, and --rebuild verifies before rebuilding.
  • Mass-deletion guard. If at least DELETE_GUARD% of protected files are missing (with 5 or more protected), their parity is kept until you confirm with update -y. This covers the classic mistake of running on an unmounted drive's empty mount point.
  • One run at a time. A lock file prevents concurrent runs; scheduled runs simply skip if one is in progress.
  • Wildcards in names. par2 expands * and ? in file names. Files whose path contains them are handed to par2 through a scratch hard link with a neutral name (alias=1 in the meta file).

Limits

  • Not a backup. Parity sits on the same drive as the data.
  • Damage between edits. If a file gets damaged and then edited before you verify, the edit is taken as the new truth, as with any checksum-based tool. Verify regularly.
  • A file changed while being protected gets parity of a mix of old and new content. The next update notices the new timestamp and redoes it.
  • Very small files get relatively more parity: at least one recovery block, plus par2's fixed overhead of a few KB per set.
  • Names with a line break are not protected (they are listed with a warning).

Edit this page on GitHub