cowfile
A copy-on-write overlay layer for immutable binary data.
Overview
cowfile provides CowFile, a type that wraps immutable binary data with a sparse copy-on-write
overlay. Modifications are tracked as byte-range patches without ever mutating the original data.
A final merged output is produced only when explicitly requested.
This is useful for binary analysis and transformation pipelines where multiple passes modify a binary without needing to copy the entire file between each pass.
Features
- Zero-copy base layer: Memory-mapped files or owned byte vectors as the immutable base
- Sparse overlay: Only modified byte ranges are stored, not the entire file
- Two-tier commit model: Pending modifications can be committed (consolidated) independently
- Thread-safe:
Send + Syncwith internalRwLocksynchronization - Dual output: Produce final output as
Vec<u8>or write directly to a file
Quick Start
use CowFile;
// Create from owned bytes
let pf = from_vec;
// Apply modifications (multiple passes)
pf.write.unwrap;
pf.write.unwrap;
// Commit consolidates pending changes
pf.commit.unwrap;
// More modifications in a second pass
pf.write.unwrap;
// Produce final output with all modifications applied
let output = pf.to_vec.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
From a file (memory-mapped)
use CowFile;
let pf = from_path.unwrap;
pf.write.unwrap; // Patch MZ header
pf.to_file.unwrap;
Architecture
Base Layer (immutable) Overlay (copy-on-write)
+---------------------+ +-------------------------+
| Vec<u8> or Mmap | | committed: BTreeMap |
| (never modified) | <--- | pending: BTreeMap |
+---------------------+ +-------------------------+
|
read: base + committed + pending
commit: pending -> committed
to_vec/to_file: materialize all
License
Licensed under the Apache License, Version 2.0. See LICENSE-APACHE for details.