Skip to main content

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_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_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///     None,   // min chunk (auto-detected)
100///     None,   // avg chunk (auto-detected)
101///     None,   // max chunk (auto-detected)
102///     None,   // workers (auto)
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    min_chunk: Option<u32>,
116    avg_chunk: Option<u32>,
117    max_chunk: Option<u32>,
118    workers: Option<usize>,
119    silent: bool,
120) -> Result<()> {
121    // Get password if encryption is enabled
122    let password = if encrypt {
123        Some(rpassword::prompt_password("Enter encryption password: ")?)
124    } else {
125        None
126    };
127
128    // Calculate total size for progress bar
129    let total_size = {
130        let mut size = 0u64;
131        if let Some(ref path) = disk {
132            size += std::fs::metadata(path)?.len();
133        }
134        if let Some(ref path) = memory {
135            size += std::fs::metadata(path)?.len();
136        }
137        size
138    };
139
140    // Create progress bar
141    let pb = if !silent {
142        let pb = create_progress_bar(total_size);
143        let pb = Arc::new(Mutex::new(pb));
144        Some(pb)
145    } else {
146        None
147    };
148    let pb_clone = pb.clone();
149
150    if train_dict && !silent {
151        println!("Training compression dictionary...");
152    }
153
154    // Create pack configuration
155    let config = PackConfig {
156        disk,
157        memory,
158        output: output.clone(),
159        compression,
160        encrypt,
161        password,
162        train_dict,
163        block_size,
164        min_chunk,
165        avg_chunk,
166        max_chunk,
167        parallel: workers != Some(1),
168        num_workers: workers.unwrap_or(0),
169        ..Default::default()
170    };
171
172    // Run the packing operation with progress callback
173    pack_snapshot(
174        config,
175        Some(move |current, _total| {
176            if let Some(ref pb) = pb_clone {
177                if let Ok(pb) = pb.lock() {
178                    pb.set_position(current);
179                }
180            }
181        }),
182    )?;
183
184    if let Some(ref pb) = pb {
185        if let Ok(pb) = pb.lock() {
186            pb.finish_with_message("Done");
187        }
188    }
189
190    if !silent {
191        println!("Archive created: {:?}", output);
192    }
193    Ok(())
194}