|
| 1 | +use anyhow::Result; |
| 2 | +use async_trait::async_trait; |
| 3 | +use async_walkdir::WalkDir; |
| 4 | +use futures::StreamExt; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use sha2::{Digest, Sha256}; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use tokio::fs; |
| 9 | + |
| 10 | +use crate::cmd::Command; |
| 11 | + |
| 12 | +const DEFAULT_METADATA_FILE: &str = "cryptpilot.metadata.json"; |
| 13 | + |
| 14 | +pub struct FormatCommand { |
| 15 | + pub options: crate::cli::FormatOptions, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, Deserialize, Debug)] |
| 19 | +struct FileInfo { |
| 20 | + path: String, |
| 21 | + sha256: String, |
| 22 | +} |
| 23 | + |
| 24 | +#[async_trait] |
| 25 | +impl Command for FormatCommand { |
| 26 | + async fn run(&self) -> Result<()> { |
| 27 | + tracing::info!("Starting verity format command"); |
| 28 | + tracing::info!("Data directory: {:?}", self.options.data_dir); |
| 29 | + |
| 30 | + // Collect all file paths |
| 31 | + let mut files = Vec::new(); |
| 32 | + self.collect_files(&self.options.data_dir, &mut files) |
| 33 | + .await?; |
| 34 | + |
| 35 | + tracing::info!("Found {} files in data directory", files.len()); |
| 36 | + |
| 37 | + // Sort file paths to ensure deterministic output |
| 38 | + files.sort(); |
| 39 | + |
| 40 | + // Calculate hash for each file |
| 41 | + let mut file_infos = Vec::new(); |
| 42 | + for file_path in files { |
| 43 | + tracing::debug!("Processing file: {:?}", file_path); |
| 44 | + let content = fs::read(&file_path).await?; |
| 45 | + let hash = hex::encode(Sha256::digest(&content)); |
| 46 | + |
| 47 | + let relative_path = file_path |
| 48 | + .strip_prefix(&self.options.data_dir)? |
| 49 | + .to_path_buf(); |
| 50 | + let path_str = relative_path.to_string_lossy().to_string(); |
| 51 | + |
| 52 | + file_infos.push(FileInfo { |
| 53 | + path: path_str, |
| 54 | + sha256: hash, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + // Generate JSON metadata |
| 59 | + let json = serde_json::to_string_pretty(&file_infos)?; |
| 60 | + tracing::debug!("Generated metadata JSON with {} entries", file_infos.len()); |
| 61 | + |
| 62 | + // Determine the actual metadata file path |
| 63 | + let metadata_path = if let Some(ref metadata) = self.options.metadata { |
| 64 | + if metadata.is_absolute() { |
| 65 | + metadata.clone() |
| 66 | + } else { |
| 67 | + self.options.data_dir.join(metadata) |
| 68 | + } |
| 69 | + } else { |
| 70 | + self.options.data_dir.join(DEFAULT_METADATA_FILE) |
| 71 | + }; |
| 72 | + |
| 73 | + tracing::info!("Writing metadata to: {:?}", metadata_path); |
| 74 | + |
| 75 | + // Write JSON metadata to file |
| 76 | + fs::write(&metadata_path, &json).await?; |
| 77 | + |
| 78 | + // Calculate overall directory root hash |
| 79 | + let mut hasher = Sha256::new(); |
| 80 | + hasher.update(&json); |
| 81 | + let root_hash = hex::encode(hasher.finalize()); |
| 82 | + |
| 83 | + tracing::info!("Root hash calculated: {}", root_hash); |
| 84 | + |
| 85 | + // Write root hash to specified output or stdout |
| 86 | + if self.options.hash_output.as_os_str() == "-" { |
| 87 | + println!("{}", root_hash); |
| 88 | + } else { |
| 89 | + tracing::info!("Writing root hash to: {:?}", self.options.hash_output); |
| 90 | + fs::write(&self.options.hash_output, &root_hash).await?; |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +impl FormatCommand { |
| 98 | + async fn collect_files(&self, dir: &Path, files: &mut Vec<PathBuf>) -> Result<()> { |
| 99 | + let mut entries = WalkDir::new(dir); |
| 100 | + |
| 101 | + while let Some(Ok(entry)) = entries.next().await { |
| 102 | + if entry.path().file_name() == Some(std::ffi::OsStr::new(DEFAULT_METADATA_FILE)) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + if entry.file_type().await.map_or(false, |ft| ft.is_file()) { |
| 107 | + files.push(entry.path()); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | +} |
0 commit comments