Skip to content

integrity

integrity #

Verifying the store: dropping every object whose files no longer hash to its name.

  1. Hash each object outside the index. The stat cache re-reads only the files whose stat changed since they were hashed, which a file truncated by a power loss or rewritten in place always has.
  2. In one index transaction, unindex each object that fails (its row, every name pointing at it, every pin on it) and move its directory into a live workspace; after the commit, delete that workspace.

A call whose result was dropped computes again; a state whose current version was dropped starts again from its seed.

verify(scratch) #

Drop every object in scratch whose tree no longer hashes to its name.

Returns:

Type Description
list[ContentHash]

The hashes of the objects dropped.

Source code in capturegraph-lib/capturegraph/scratch/store/integrity.py
def verify(scratch: Scratch) -> list[ContentHash]:
    """Drop every object in ``scratch`` whose tree no longer hashes to its name.

    Returns:
        The hashes of the objects dropped.
    """
    with scratch.database.read() as db:
        indexed = [row.hash for row in objects.all_rows(db)]
    damaged = [hash for hash in indexed if not _intact(scratch, hash)]
    if not damaged:
        return []
    trash = scratch.live_dir()
    with scratch.database.transaction() as db:
        for hash in damaged:
            objects.delete(db, hash)
            with suppress(FileNotFoundError):
                os.rename(scratch.object_dir(hash), trash.path / hash.hex)
            scratch.file_hashes.forget(scratch.object_dir(hash))
    trash.release()
    for hash in damaged:
        logger.warning(f"verify: dropped damaged object {hash}")
    return damaged