hexz_cli/cmd/data/pack.rs
1//! Pack data into a Hexz archive.
2//!
3//! This command creates a `.st` archive from disk images and/or memory dumps,
4//! delegating to the core packing logic in [`hexz_core::ops::pack`].
5//!
6//! # Overview
7//!
8//! The `pack` command is the primary way to create Hexz archives from raw data.
9//! It supports multiple compression algorithms, encryption, deduplication, and
10//! both fixed-size and content-defined chunking strategies.
11//!
12//! # Key Features
13//!
14//! - **Compression**: LZ4 (speed) or Zstandard (compression ratio)
15//! - **Deduplication**: SHA-256 based block deduplication (enabled by default)
16//! - **Encryption**: AES-256-GCM with password-based key derivation
17//! - **CDC**: Content-defined chunking for better deduplication
18//! - **Dictionary Training**: Improves Zstd compression by 10-30%
19//!
20//! # Performance Tuning
21//!
22//! **For maximum speed:**
23//! ```bash
24//! hexz data pack --disk image.img --output fast.st --compression lz4
25//! ```
26//!
27//! **For maximum compression:**
28//! ```bash
29//! hexz data pack --disk image.img --output small.st \
30//! --compression zstd --train-dict --cdc
31//! ```
32//!
33//! **For balanced performance:**
34//! ```bash
35//! hexz data pack --disk image.img --output balanced.st \
36//! --compression lz4 --block-size 131072
37//! ```
38
39use crate::ui::progress::create_progress_bar;
40use anyhow::Result;
41use hexz_core::ops::pack::{PackConfig, pack_snapshot};
42use std::path::PathBuf;
43use std::sync::{Arc, Mutex};
44
45/// Execute the pack command to create a Hexz snapshot archive.
46///
47/// This command creates a `.st` snapshot file from disk and/or memory dump files.
48/// It supports compression (LZ4 or Zstd), optional encryption, deduplication,
49/// content-defined chunking (CDC), and dictionary training for improved compression.
50///
51/// # Workflow
52///
53/// The packing process follows these steps:
54///
55/// 1. **Password prompt** (if encryption enabled): Prompts for password and derives encryption key
56/// 2. **Dictionary training** (if enabled): Samples blocks and trains a Zstd compression dictionary
57/// 3. **Chunking**: Splits input file(s) into blocks using fixed-size or CDC chunking
58/// 4. **Compression**: Compresses each block using the selected algorithm and optional dictionary
59/// 5. **Deduplication**: Hashes compressed blocks and eliminates duplicates (enabled by default)
60/// 6. **Index building**: Constructs the master index with page entries and block metadata
61/// 7. **Header writing**: Serializes header with format version, offsets, and feature flags
62///
63/// # Arguments
64///
65/// * `disk` - Optional path to disk image file (raw or qcow2)
66/// * `memory` - Optional path to memory dump file
67/// * `output` - Output path for the `.st` snapshot file
68/// * `compression` - Compression algorithm: "lz4" (fast) or "zstd" (balanced)
69/// * `encrypt` - Enable AES-256-GCM encryption (prompts for password)
70/// * `train_dict` - Train a Zstd dictionary for improved compression ratios
71/// * `block_size` - Block size in bytes (default: 64 KiB)
72/// * `cdc` - Enable content-defined chunking for variable-sized blocks
73/// * `min_chunk` - Minimum chunk size for CDC (default: 16 KiB)
74/// * `avg_chunk` - Average chunk size for CDC (default: 64 KiB)
75/// * `max_chunk` - Maximum chunk size for CDC (default: 128 KiB)
76/// * `silent` - Suppress progress output
77///
78/// # Performance Characteristics
79///
80/// - **LZ4**: ~500 MB/s compression throughput
81/// - **Zstd level 3**: ~200 MB/s compression throughput
82/// - **Deduplication overhead**: ~5-10% additional time for hashing
83/// - **Dictionary training**: 2-5 seconds for typical datasets
84///
85/// # Example
86///
87/// ```no_run
88/// # use std::path::PathBuf;
89/// # use hexz_cli::cmd::data::pack;
90/// // Pack a disk image with Zstd compression and dictionary training
91/// pack::run(
92/// Some(PathBuf::from("disk.img")),
93/// None,
94/// PathBuf::from("snapshot.hxz"),
95/// "zstd".to_string(),
96/// false, // no encryption
97/// true, // train dictionary
98/// 65536, // 64 KiB blocks
99/// false, // fixed-size blocks
100/// 16384,
101/// 65536,
102/// 131072,
103/// false, // show progress
104/// );
105/// ```
106#[allow(clippy::too_many_arguments)]
107pub fn run(
108 disk: Option<PathBuf>,
109 memory: Option<PathBuf>,
110 output: PathBuf,
111 compression: String,
112 encrypt: bool,
113 train_dict: bool,
114 block_size: u32,
115 cdc: bool,
116 min_chunk: u32,
117 avg_chunk: u32,
118 max_chunk: u32,
119 silent: bool,
120) -> Result<()> {
121 // Validate CDC chunk size ordering
122 if cdc && !(min_chunk < avg_chunk && avg_chunk < max_chunk) {
123 anyhow::bail!(
124 "CDC chunk sizes must satisfy min_chunk < avg_chunk < max_chunk, got {} < {} < {}",
125 min_chunk,
126 avg_chunk,
127 max_chunk
128 );
129 }
130
131 // Get password if encryption is enabled
132 let password = if encrypt {
133 Some(rpassword::prompt_password("Enter encryption password: ")?)
134 } else {
135 None
136 };
137
138 // Calculate total size for progress bar
139 let total_size = {
140 let mut size = 0u64;
141 if let Some(ref path) = disk {
142 size += std::fs::metadata(path)?.len();
143 }
144 if let Some(ref path) = memory {
145 size += std::fs::metadata(path)?.len();
146 }
147 size
148 };
149
150 // Create progress bar
151 let pb = if !silent {
152 let pb = create_progress_bar(total_size);
153 let pb = Arc::new(Mutex::new(pb));
154 Some(pb)
155 } else {
156 None
157 };
158 let pb_clone = pb.clone();
159
160 if train_dict && !silent {
161 println!("Training compression dictionary...");
162 }
163
164 // Create pack configuration
165 let config = PackConfig {
166 disk,
167 memory,
168 output: output.clone(),
169 compression,
170 encrypt,
171 password,
172 train_dict,
173 block_size,
174 cdc_enabled: cdc,
175 min_chunk,
176 avg_chunk,
177 max_chunk,
178 };
179
180 // Run the packing operation with progress callback
181 pack_snapshot(
182 config,
183 Some(move |current, _total| {
184 if let Some(ref pb) = pb_clone {
185 if let Ok(pb) = pb.lock() {
186 pb.set_position(current);
187 }
188 }
189 }),
190 )?;
191
192 if let Some(ref pb) = pb {
193 if let Ok(pb) = pb.lock() {
194 pb.finish_with_message("Done");
195 }
196 }
197
198 if !silent {
199 println!("Archive created: {:?}", output);
200 }
201 Ok(())
202}