What is content-addressable storage?
In traditional package managers, each package installation stores a complete copy of all files. zerobrew instead uses a two-layer architecture:- Store layer: Downloaded bottles are extracted once and stored by their SHA-256 hash
- Cellar layer: Individual package installations reference the shared store
How it works
Download and storage
When zerobrew installs a package:- Download bottle: The pre-built package archive is downloaded
- Verify checksum: SHA-256 hash is verified against Homebrew’s metadata
- Store by hash: Archive is saved as
cache/blobs/<sha256>.tar.gz - Extract to store: Contents extracted to
store/<sha256>/ - Materialization: Files copied from store to cellar
The store directory uses the bottle’s SHA-256 hash as the key, making storage content-addressable. The same content always has the same address.
Reference counting
zerobrew tracks how many installed packages reference each store entry:- If the store entry exists: increment
refcount - If the store entry doesn’t exist: extract bottle and set
refcount = 1
- Decrement
refcount - If
refcountreaches 0: mark as unreferenced (cleaned up duringzb gc)
Garbage collection
Runzb gc to remove unreferenced store entries:
APFS clonefile: zero-copy installation
On macOS with APFS (Apple File System), zerobrew usesclonefile for instant, space-efficient copying.
What is clonefile?
APFS clonefile creates a copy-on-write clone of a file or directory:- Instant: No data is actually copied, just metadata
- Space-efficient: The clone and original share disk blocks until modified
- Transparent: The clone appears as a complete, independent copy
How zerobrew uses clonefile
During materialization (copying from store to cellar), zerobrew attempts:- APFS clonefile (macOS only): Instant copy-on-write clone
- Hardlinks (fallback): Link files instead of copying
- Regular copy (final fallback): Copy file contents
zb_io/src/cellar/materialize.rs:
Clonefile vs hardlinks
APFS clonefile advantages:- Works across directories
- Preserves file independence (modifying clone doesn’t affect original)
- Handles entire directory trees in one syscall
- Preserves extended attributes and metadata
- Works on non-APFS filesystems (ext4, XFS, etc.)
- Widely supported across Unix-like systems
- Always works, but slow and space-inefficient
- Used as final fallback when other methods fail
Platform differences
macOS (APFS)
- Uses clonefile for instant materialization
- Additional patching for Mach-O binaries:
- Rewrites Homebrew path placeholders
- Ad-hoc code signing after modifications
- Strips quarantine extended attributes
Linux
- Uses hardlinks or copy (no clonefile equivalent)
- Patches ELF binaries for Homebrew path placeholders
- No code signing required
Benefits of content-addressable storage
Automatic deduplication: If multiple packages use the same bottle version, files stored once Safe concurrent access: File locks prevent corruption when multiple processes install simultaneously Efficient upgrades: New version creates new store entry; old version remains until garbage collected Reproducibility: Same SHA-256 hash always produces same installation Bandwidth savings: Downloaded bottles cached incache/blobs/ for reuse
Storage usage example
Traditional approach (Homebrew):zb uninstall [email protected] and zb gc:
The “~0 MB” for cloned directories means they consume negligible additional space due to APFS copy-on-write.
Implementation details
Locking strategy
Fromzb_io/src/storage/store.rs, zerobrew uses file locks to prevent race conditions:
Atomic operations
- Downloads write to
.partfiles, renamed atomically on completion - Store extraction uses temporary
.tmp.{pid}directories, renamed atomically - Database updates use SQLite transactions