s365b.storage.repository¶
The backup repository format.
Layout on the backend:
config plaintext JSON: format version, repo ID, KDF
parameters, wrapped repository key
packs/<aa>/<pack-id> encrypted blobs, concatenated
index/<index-id> encrypted JSON: which blobs are in which pack
How content is stored:
Content is split into chunks of at most
chunk_sizebytes (fixed-size chunking; the format allows switching to content-defined chunking later because chunks are addressed by hash, not position).Each chunk’s ID is
BLAKE3(id_key, plaintext). A chunk whose ID is already in the index is not stored again (deduplication).New chunks are zstd-compressed (kept raw if compression does not help), encrypted with AES-256-GCM using the blob ID as associated data, and buffered.
When the buffer reaches
pack_size(or onRepository.flush()), it is written as one pack file plus one index file.
Blob on-disk format: nonce(12) || AESGCM(flag(1) || payload) where the flag
is 0 for raw and 1 for zstd.
Every index file is self-contained, so the full index is the union of all
index files and can always be rebuilt by listing index/.
Attributes¶
Exceptions¶
Raised for repository-level problems (missing, corrupt, wrong passphrase). |
Classes¶
Where a blob lives inside a pack. |
|
Reference to a piece of content stored in the repository. |
|
Outcome of |
|
Basic repository statistics. |
|
An open backup repository. |
Functions¶
|
Every blob a catalog content entry uses, its extra copies included. |
|
Return a named extra copy of an item version's content, if stored. |
Module Contents¶
- exception s365b.storage.repository.RepositoryError[source]¶
Bases:
ExceptionRaised for repository-level problems (missing, corrupt, wrong passphrase).
- class s365b.storage.repository.BlobLocation[source]¶
Where a blob lives inside a pack.
- Variables:
pack – Pack key.
offset – Byte offset in the pack.
length – Stored (encrypted) length.
raw_length – Plaintext length.
- class s365b.storage.repository.ContentRef[source]¶
Reference to a piece of content stored in the repository.
This is what the catalog saves for each item version.
- Variables:
blobs – Blob IDs in order; concatenated they form the content.
size – Total plaintext size in bytes.
- to_json() dict[str, Any][source]¶
Serialise for storage in the catalog.
- Returns:
A JSON-compatible dict.
- classmethod from_json(data: dict[str, Any]) ContentRef[source]¶
Load from
to_json()output.- Parameters:
data – The stored dict.
- Returns:
The reference.
- s365b.storage.repository.content_blobs(content: dict[str, Any] | None) list[str][source]¶
Every blob a catalog content entry uses, its extra copies included.
- Parameters:
content – An item version’s stored content (
ContentRefJSON, with an optional"extra"map of namedContentRefJSON).- Returns:
Blob IDs.
- s365b.storage.repository.extra_ref(content: dict[str, Any] | None, name: str) ContentRef | None[source]¶
Return a named extra copy of an item version’s content, if stored.
- Parameters:
content – An item version’s stored content.
name – Copy name, e.g.
"fts".
- Returns:
Its reference, or
None.
- class s365b.storage.repository.CompactStats[source]¶
Outcome of
Repository.compact().- Variables:
packs_deleted – Packs that held only unreferenced blobs.
packs_rewritten – Packs rewritten without their unreferenced blobs.
blobs_deleted – Unreferenced blobs removed.
bytes_freed – Stored (compressed, encrypted) bytes removed.
- class s365b.storage.repository.RepositoryStats[source]¶
Basic repository statistics.
- Variables:
blobs – Number of unique blobs.
packs – Number of pack files.
stored_bytes – Bytes stored (compressed and encrypted).
raw_bytes – Plaintext bytes of unique blobs.
- class s365b.storage.repository.Repository(backend: s365b.storage.backends.StorageBackend, keys: s365b.storage.crypto.RepoKeys, config: dict[str, Any])[source]¶
An open backup repository.
Create one with
init()(new repository) oropen(). A repository object is safe to share between tasks in one process; writes from several processes are safe too, at worst storing a blob twice.- Parameters:
backend – Where the files live.
keys – Unwrapped repository keys.
config – Parsed config file.
- classmethod init(backend: s365b.storage.backends.StorageBackend, passphrase: str, *, chunk_size: int = DEFAULT_CHUNK_SIZE, pack_size: int = DEFAULT_PACK_SIZE, kdf: s365b.storage.crypto.KdfParams | None = None) Repository[source]¶
- Async:
Create a new, empty repository.
- Parameters:
backend – Empty storage location.
passphrase – Passphrase protecting the repository key. Losing it means losing access to every backup in the repository.
chunk_size – Maximum chunk size in bytes.
pack_size – Target pack file size in bytes.
kdf – Argon2id parameters (defaults are used if omitted; tests pass cheaper ones).
- Returns:
The open repository.
- Raises:
RepositoryError – If a repository already exists there.
- classmethod open(backend: s365b.storage.backends.StorageBackend, passphrase: str) Repository[source]¶
- Async:
Open an existing repository and load its index.
- Parameters:
backend – Storage location.
passphrase – Repository passphrase.
- Returns:
The open repository.
- Raises:
RepositoryError – If there is no repository, the format is unsupported, or the passphrase is wrong.
- async reload_index() None[source]¶
Re-read all index files, picking up blobs written by other processes.
- async put_blob(data: bytes) str[source]¶
Store one blob (deduplicated).
- Parameters:
data – Plaintext, at most
chunk_sizebytes.- Returns:
The blob ID.
- async put_content(source: bytes | collections.abc.Iterable[bytes] | collections.abc.AsyncIterable[bytes]) ContentRef[source]¶
Store content of any size, splitting it into chunks.
- Parameters:
source – The whole content, or an (async) iterable of byte pieces of any size, e.g. a streamed HTTP download.
- Returns:
Reference to save in the catalog.
- async flush() None[source]¶
Write buffered blobs as a pack and index file.
Call this at the end of every job; content is not durable until it has been flushed.
- async read_generation() int[source]¶
Return the catalog generation recorded in the repository (0 if none).
Every catalog commit raises the generation in the catalog database and then writes it here, so the repository’s number is never ahead of the database that wrote to it. When it is, the database is older than the repository (a fresh install, or a restored dump) and does not know all of its data: space must not be reclaimed (see
s365b.core.generation).- Returns:
The highest generation written.
- async write_generation(generation: int) None[source]¶
Record a catalog generation in the repository.
Each number is a file of its own, and lower ones are removed after: writers finishing out of order cannot lower the highest.
- Parameters:
generation – The catalog’s new generation.
- async compact(live: set[str], *, min_garbage: float = 0.2) CompactStats[source]¶
Delete blobs nothing refers to, reclaiming their space.
Packs holding only unreferenced blobs are deleted. Packs where at least
min_garbageof the bytes are unreferenced are rewritten with only the referenced blobs (copied sealed, never decrypted). New packs are written before old ones are deleted, so a crash leaves duplicates, never gaps.The caller must make sure nothing writes to the repository while this runs (a backup could otherwise reuse a blob being deleted).
- Parameters:
live – IDs of every blob the catalog still refers to.
min_garbage – Share of unreferenced bytes at which a pack is rewritten.
- Returns:
What was done.
- async remove_blobs(blob_ids: set[str]) CompactStats[source]¶
Delete these blobs and nothing else.
Packs holding them are rewritten without them. Blobs the catalog may not know about are kept, unlike
compact()with a live set.- Parameters:
blob_ids – The blobs to delete.
- Returns:
What was done.
- has_blob(blob_id: str) bool[source]¶
Return whether a blob is stored or buffered.
- Parameters:
blob_id – Blob ID.
- Returns:
True if present.
- async get_blob(blob_id: str) bytes[source]¶
Read and verify one blob.
- Parameters:
blob_id – Blob ID.
- Returns:
The plaintext.
- Raises:
RepositoryError – If the blob is missing, corrupt or tampered with.
- async get_content(ref: ContentRef) collections.abc.AsyncIterator[bytes][source]¶
Stream stored content chunk by chunk.
- Parameters:
ref – Reference from
put_content().- Yields:
Plaintext chunks in order.
- async check(*, read_data: bool = False) list[str][source]¶
Verify the repository.
- Parameters:
read_data – Also read and verify every blob (slow). Otherwise only checks that every indexed pack exists.
- Returns:
A list of problems; empty if the repository is healthy.
- async unreadable_blobs() set[str][source]¶
Read every blob; return the IDs of those that fail verification.
- Returns:
IDs of missing, corrupt or tampered blobs.
- stats() RepositoryStats[source]¶
Return statistics about flushed data.
- Returns:
Repository statistics.