use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use sha2::{Sha256, Digest};
use serde::Deserialize;
use crate::db;
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ArchiveFormat {
Raw,
TarGz,
Zip,
}
impl std::fmt::Display for ArchiveFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ArchiveFormat::Raw => write!(f, "raw"),
ArchiveFormat::TarGz => write!(f, "tar.gz"),
ArchiveFormat::Zip => write!(f, "zip"),
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct Provider {
#[serde(rename = "type")]
pub provider_type: String,
#[serde(default)]
pub url: Option<String>,
#[serde(default)]
pub path: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct PlatformEntry {
pub path: String,
pub hash: String,
pub size: u64,
pub format: ArchiveFormat,
pub providers: Vec<Provider>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ExecutableManifest {
#[serde(default)]
pub name: Option<String>,
#[serde(default)]
pub version: Option<String>,
pub platforms: std::collections::HashMap<String, PlatformEntry>,
}
pub fn detect_platform() -> String {
let os = std::env::consts::OS;
let arch = std::env::consts::ARCH;
match (os, arch) {
("macos", "aarch64") => "darwin-arm64".to_string(),
("macos", "x86_64") => "darwin-x86_64".to_string(),
("linux", "x86_64") => "linux-x86_64".to_string(),
("windows", "x86_64") => "windows-amd64".to_string(),
_ => format!("{}-{}", os, arch),
}
}
pub fn compute_file_hash(path: &Path) -> Result<String, String> {
let mut file = fs::File::open(path)
.map_err(|e| format!("Failed to open file {:?}: {}", path, e))?;
let mut hasher = Sha256::new();
let mut buffer = [0u8; 8192];
loop {
let n = file.read(&mut buffer)
.map_err(|e| format!("Failed to read file: {}", e))?;
if n == 0 {
break;
}
hasher.update(&buffer[..n]);
}
Ok(format!("{:x}", hasher.finalize()))
}
pub fn verify_hash(path: &Path, expected_hash: &str) -> Result<(), String> {
let actual = compute_file_hash(path)?;
if actual == expected_hash {
Ok(())
} else {
Err(format!(
"Hash mismatch for {:?}: expected {}, got {}",
path, expected_hash, actual
))
}
}
pub fn cache_binary(source: &Path, hash: &str) -> Result<PathBuf, String> {
let cache_dir = dirs_cache_dir()?;
let cache_path = cache_dir.join(hash);
if cache_path.exists() {
if verify_hash(&cache_path, hash).is_ok() {
return Ok(cache_path);
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&cache_path, fs::Permissions::from_mode(0o755));
}
let _ = fs::remove_file(&cache_path);
}
fs::create_dir_all(&cache_dir)
.map_err(|e| format!("Failed to create cache dir: {}", e))?;
let nonce = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_nanos())
.unwrap_or(0);
let tmp_path = cache_dir.join(format!("{}.tmp.{}.{}", hash, std::process::id(), nonce));
fs::copy(source, &tmp_path)
.map_err(|e| format!("Failed to copy to cache tmp file: {}", e))?;
if let Err(_e) = fs::rename(&tmp_path, &cache_path) {
let _ = fs::remove_file(&tmp_path);
if verify_hash(&cache_path, hash).is_err() {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&cache_path, fs::Permissions::from_mode(0o755));
}
let _ = fs::remove_file(&cache_path);
let _ = fs::copy(source, &cache_path);
}
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let perms = fs::Permissions::from_mode(0o555);
let _ = fs::set_permissions(&cache_path, perms);
}
Ok(cache_path)
}
fn dirs_cache_dir() -> Result<PathBuf, String> {
if let Ok(home) = std::env::var("HOME") {
Ok(PathBuf::from(home).join(".clm").join("cache").join("bin"))
} else if let Ok(user_profile) = std::env::var("USERPROFILE") {
Ok(PathBuf::from(user_profile).join(".clm").join("cache").join("bin"))
} else {
Ok(PathBuf::from("/tmp").join("clm-cache").join("bin"))
}
}
fn fetch_from_provider(
provider: &Provider,
clm_root: &str,
dest: &Path,
) -> Result<PathBuf, String> {
match provider.provider_type.as_str() {
"local" => {
let local_path = provider.path.as_ref().ok_or("Local provider missing path")?;
let candidate = if Path::new(local_path).is_absolute() {
PathBuf::from(local_path)
} else {
PathBuf::from(clm_root).join(local_path)
};
if candidate.exists() && candidate.is_file() {
if dest != candidate {
fs::copy(&candidate, dest)
.map_err(|e| format!("Failed to copy from local provider: {}", e))?;
}
Ok(dest.to_path_buf())
} else {
Err(format!("Local provider path not found: {}", candidate.display()))
}
}
"http" | "github-release" => {
Err(format!("Provider type '{}' not yet implemented (requires HTTP dependency)", provider.provider_type))
}
_ => Err(format!("Unknown provider type: {}", provider.provider_type)),
}
}
#[allow(dead_code)]
fn fetch_from_providers(
providers: &[Provider],
clm_root: &str,
dest: &Path,
) -> Result<PathBuf, String> {
let mut errors = Vec::new();
for provider in providers {
match fetch_from_provider(provider, clm_root, dest) {
Ok(path) => return Ok(path),
Err(e) => errors.push(format!("{}: {}", provider.provider_type, e)),
}
}
Err(format!("All providers failed: {}", errors.join("; ")))
}
pub fn lookup_manifest(handle: &str, clm_root: &str) -> Result<ExecutableManifest, String> {
let content = db::resolve_handle_content("content_memory", clm_root, handle)
.map_err(|e| format!("DB error looking up handle '{}': {}", handle, e))?
.ok_or_else(|| format!("Handle '{}' not found in content_memory", handle))?;
let manifest: ExecutableManifest = serde_json::from_str(&content)
.map_err(|e| format!("Failed to parse manifest JSON: {}", e))?;
Ok(manifest)
}
pub fn resolve_binary(handle: &str, clm_root: &str) -> Result<PathBuf, String> {
let manifest = lookup_manifest(handle, clm_root)?;
let platform = detect_platform();
let entry = manifest.platforms.get(&platform)
.ok_or_else(|| format!("Platform '{}' not found in manifest for handle '{}'", platform, handle))?;
let temp_dir = std::env::temp_dir().join("clm-manifest-resolve");
let _ = fs::create_dir_all(&temp_dir);
let temp_dest = temp_dir.join(format!("{}_{}", handle.replace(':', "_"), &entry.hash[..12]));
let mut errors = Vec::new();
let fetched = {
let mut result: Option<PathBuf> = None;
for provider in &entry.providers {
match fetch_from_provider(provider, clm_root, &temp_dest) {
Ok(path) => {
if !entry.hash.chars().all(|c| c == '0') {
match verify_hash(&path, &entry.hash) {
Ok(()) => {
result = Some(path);
break;
}
Err(e) => {
errors.push(format!("{}: hash mismatch ({})", provider.provider_type, e));
continue;
}
}
} else {
result = Some(path);
break;
}
}
Err(e) => errors.push(format!("{}: {}", provider.provider_type, e)),
}
}
match result {
Some(p) => p,
None => return Err(format!("All providers failed: {}", errors.join("; "))),
}
};
let cached = cache_binary(&fetched, &entry.hash)?;
if cached != fetched {
let _ = fs::remove_file(&fetched);
}
Ok(cached)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_detect_platform() {
let platform = detect_platform();
assert!(!platform.is_empty());
assert!(platform.contains('-'));
}
#[test]
fn test_verify_hash_match() {
let dir = std::env::temp_dir().join("clm_manifest_test_hash_match");
let _ = fs::create_dir_all(&dir);
let test_file = dir.join("test_binary.bin");
fs::write(&test_file, b"hello world").unwrap();
let mut hasher = Sha256::new();
hasher.update(b"hello world");
let expected = format!("{:x}", hasher.finalize());
assert!(verify_hash(&test_file, &expected).is_ok());
let _ = fs::remove_file(&test_file);
let _ = fs::remove_dir(&dir);
}
#[test]
fn test_verify_hash_mismatch() {
let dir = std::env::temp_dir().join("clm_manifest_test_hash_mismatch");
let _ = fs::create_dir_all(&dir);
let test_file = dir.join("test_binary_mismatch.bin");
fs::write(&test_file, b"hello world").unwrap();
let wrong_hash = "0".repeat(64);
let result = verify_hash(&test_file, &wrong_hash);
assert!(result.is_err());
assert!(result.unwrap_err().contains("Hash mismatch"));
let _ = fs::remove_file(&test_file);
let _ = fs::remove_dir(&dir);
}
#[test]
fn test_compute_file_hash() {
let dir = std::env::temp_dir().join("clm_manifest_test_compute_hash");
let _ = fs::create_dir_all(&dir);
let test_file = dir.join("compute_hash_test.bin");
fs::write(&test_file, b"test content for hashing").unwrap();
let hash = compute_file_hash(&test_file).unwrap();
assert_eq!(hash.len(), 64);
assert!(hash.chars().all(|c| c.is_ascii_hexdigit()));
let _ = fs::remove_file(&test_file);
let _ = fs::remove_dir(&dir);
}
#[test]
fn test_local_provider_found() {
let dir = std::env::temp_dir().join("clm_manifest_test_local_provider");
let _ = fs::create_dir_all(&dir);
let source = dir.join("source.bin");
fs::write(&source, b"binary content").unwrap();
let dest = dir.join("dest.bin");
let provider = Provider {
provider_type: "local".to_string(),
url: None,
path: Some(source.to_string_lossy().to_string()),
};
let result = fetch_from_provider(&provider, "/tmp", &dest);
assert!(result.is_ok());
assert!(dest.exists());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn test_local_provider_not_found() {
let provider = Provider {
provider_type: "local".to_string(),
url: None,
path: Some("/nonexistent/path/to/binary".to_string()),
};
let result = fetch_from_provider(&provider, "/tmp", Path::new("/tmp/dest.bin"));
assert!(result.is_err());
}
#[test]
fn test_provider_chain_fallback() {
let dir = std::env::temp_dir().join("clm_manifest_test_chain");
let _ = fs::create_dir_all(&dir);
let source = dir.join("source_chain.bin");
fs::write(&source, b"chain content").unwrap();
let dest = dir.join("dest_chain.bin");
let providers = vec![
Provider {
provider_type: "local".to_string(),
url: None,
path: Some("/nonexistent/path".to_string()),
},
Provider {
provider_type: "local".to_string(),
url: None,
path: Some(source.to_string_lossy().to_string()),
},
];
let result = fetch_from_providers(&providers, "/tmp", &dest);
assert!(result.is_ok());
assert!(dest.exists());
let _ = fs::remove_dir_all(&dir);
}
#[test]
fn test_cache_hit() {
let dir = std::env::temp_dir().join("clm_manifest_test_cache_hit");
let _ = fs::create_dir_all(&dir);
let source = dir.join("source_cache.bin");
fs::write(&source, b"cacheable content").unwrap();
let mut hasher = Sha256::new();
hasher.update(b"cacheable content");
let hash = format!("{:x}", hasher.finalize());
let cached1 = cache_binary(&source, &hash).unwrap();
assert!(cached1.exists());
let cached2 = cache_binary(&source, &hash).unwrap();
assert_eq!(cached1, cached2);
let _ = fs::remove_dir_all(&dir);
let cache_dir = dirs_cache_dir().unwrap();
let _ = fs::remove_file(cache_dir.join(&hash));
}
#[test]
fn test_cache_miss_then_hit() {
let dir = std::env::temp_dir().join("clm_manifest_test_cache_miss");
let _ = fs::create_dir_all(&dir);
let source = dir.join("source_miss.bin");
fs::write(&source, b"miss then hit content").unwrap();
let mut hasher = Sha256::new();
hasher.update(b"miss then hit content");
let hash = format!("{:x}", hasher.finalize());
let cache_dir = dirs_cache_dir().unwrap();
let cache_path = cache_dir.join(&hash);
let _ = fs::remove_file(&cache_path);
let cached = cache_binary(&source, &hash).unwrap();
assert!(cached.exists());
let _ = fs::remove_dir_all(&dir);
let _ = fs::remove_file(&cache_path);
}
#[test]
fn test_manifest_deserialization() {
let json = r#"{
"name": "test_binary",
"version": "1.0.0",
"platforms": {
"darwin-arm64": {
"path": "/usr/local/bin/test",
"hash": "abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
"size": 1024,
"format": "raw",
"providers": [
{"type": "local", "path": "/usr/local/bin/test"}
]
}
}
}"#;
let manifest: ExecutableManifest = serde_json::from_str(json).unwrap();
assert_eq!(manifest.name.as_deref(), Some("test_binary"));
assert_eq!(manifest.platforms.len(), 1);
let entry = manifest.platforms.get("darwin-arm64").unwrap();
assert_eq!(entry.format, ArchiveFormat::Raw);
assert_eq!(entry.providers.len(), 1);
}
}