|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::{fs, os::unix::fs::symlink, path::Path}; |
| 3 | + |
| 4 | +use crate::{ |
| 5 | + chunks::{HashKind, hash::hash, load_tree}, |
| 6 | + repo::{PackageManifest, get_package, read_manifest}, |
| 7 | +}; |
| 8 | + |
| 9 | +fn hash_package(package_manifest: &PackageManifest, hash_kind: HashKind) -> Result<String> { |
| 10 | + let hash_str = serde_yaml::to_string(package_manifest)?; |
| 11 | + |
| 12 | + Ok(hash(hash_kind, hash_str.as_bytes())) |
| 13 | +} |
| 14 | + |
| 15 | +/// Installs the latest version of a package, assumes all chunks are available. |
| 16 | +/// It is recommended you call `autoclean_versions` after. |
| 17 | +/// |
| 18 | +/// # Errors |
| 19 | +/// |
| 20 | +/// - Filesystem errors (Out of space, Permissions) |
| 21 | +/// - Invalid Repository/Package manifest |
| 22 | +pub fn install_version(repo_path: &Path, package_id: &str, chunk_store_path: &Path) -> Result<()> { |
| 23 | + let repo_manifest = read_manifest(repo_path)?; |
| 24 | + |
| 25 | + let package_manifest = get_package(&repo_manifest, package_id) |
| 26 | + .with_context(|| "Failed to get package from Repository.")?; |
| 27 | + let package_hash = hash_package(&package_manifest, repo_manifest.hash_kind)?; |
| 28 | + let installed_path = &repo_path |
| 29 | + .join("versions") |
| 30 | + .join(format!("{}-{}", package_manifest.id, package_hash)); |
| 31 | + |
| 32 | + load_tree(installed_path, chunk_store_path, &package_manifest.chunks) |
| 33 | + .with_context(|| "Failed to rebuild the tree.")?; |
| 34 | + |
| 35 | + fs::write( |
| 36 | + installed_path.join("install.meta"), |
| 37 | + serde_yaml::to_string(&package_manifest)?, |
| 38 | + )?; |
| 39 | + |
| 40 | + Ok(()) |
| 41 | +} |
| 42 | + |
| 43 | +pub fn switch_version(repo_path: &Path, hash: String, package_id: &str) -> Result<()> { |
| 44 | + let target_parent_path = repo_path.join("installed"); |
| 45 | + let target_path = target_parent_path.join(package_id); |
| 46 | + symlink(format!("../versions/{package_id}-{hash}"), target_path)?; |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +/// Gets all versions for the `package_id` |
| 52 | +pub fn get_versions(repo_path: &Path, package_id: &str) -> Result<Vec<String>> { |
| 53 | + let mut versions = Vec::new(); |
| 54 | + |
| 55 | + for entry in repo_path.join("versions").read_dir()? { |
| 56 | + if let Ok(entry) = entry |
| 57 | + && let Ok(file_name) = entry.file_name().into_string() |
| 58 | + { |
| 59 | + let split: Vec<&str> = file_name.split('-').collect(); |
| 60 | + |
| 61 | + if let Some((version_hash, file_name_split)) = split.split_last() |
| 62 | + && file_name_split.join("-") == package_id |
| 63 | + { |
| 64 | + versions.push((*version_hash).to_string()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + Ok(versions) |
| 70 | +} |
| 71 | + |
| 72 | +/// Removes a version of a package. |
| 73 | +pub fn remove_version(repo_path: &Path, hash: String, package_id: &str) -> Result<()> { |
| 74 | + let path = repo_path.join(format!("versions/{}-{}", package_id, hash)); |
| 75 | + fs::remove_dir_all(path)?; |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
0 commit comments