Skip to main content

Module salt_cache

Module salt_cache 

Source
Expand description

Persistent salt+file_id cache for deterministic re-encryption.

During decrypt, the file’s salt and file_id are recorded. During encrypt, the cached values are reused so that decrypt→encrypt on the same plaintext produces byte-identical output.

§Architecture

§Read Path (encrypt) — Zero-copy via mmap + rkyv

SaltCacheReader memory-maps the cache file and uses rkyv’s zero-copy deserialization to access the archived HashMap<String, CachedEntry> directly. No heap allocation or full deserialization is required for lookups.

§Write Path (decrypt) — mpsc + rkyv

SaltCacheSender is a Sync handle that wraps an mpsc::Sender. Rayon worker threads send (path, entry) pairs through the channel. After all parallel work completes, SaltCacheSaver collects the entries, merges with any existing on-disk cache, and serializes the result via rkyv.

§Key Format

Cache keys are repo-relative path bytes with forward slashes (b'/'), computed by the caller via crate::crypt::cache_key. Using raw bytes (Vec<u8>) avoids UTF-8 validation overhead and string allocation.

§Persistence

Serialized via rkyv to <repo>/.git/git-simple-encrypt-salt-cache. The binary format is opaque and not meant for human consumption. Writes are performed atomically to prevent corruption.

§Lifecycle

  • Decrypt: Create sender → workers send entries → saver persists (atomically)
  • Encrypt: Create reader (mmap, read-only) → workers look up cached values. No write is performed during encryption.
  • On error: Cache is saved with whatever entries were captured before the failure, preserving partial progress.
  • Stale entries: Entries for files that no longer exist are harmless (looked up by key, simply not found) and do not affect correctness.

Structs§

ArchivedCachedEntry
An archived CachedEntry
CacheRef
Borrowed reference to a salt-cache writer + the repo-relative key for a single file.
CachedEntry
A cached header entry for deterministic re-encryption.
CachedEntryResolver
The resolver for an archived CachedEntry
SaltCacheReader
Read-only salt cache backed by memory-mapped file + rkyv zero-copy access.
SaltCacheSaver
Receiver that collects and persists cache entries to disk.
SaltCacheSender
Thread-safe sender for cache entries, safe to share across rayon workers.

Functions§

create_writer
Create a paired sender/saver for collecting cache entries.