diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..9a04567 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,18 @@ +[target.aarch64-unknown-linux-musl] +linker = "aarch64-hitchhiker-linux-musleabihf-gcc" + +[target.riscv64gc-unknown-linux-musl] +rustflags = [ + "-C", "target-feature=-crt-static" +] +linker = "riscv64-hitchhiker-linux-musl-gcc" +ar = "riscv64-hitchhiker-linux-musl-ar" + +[target.i686-unknown-linux-musl] +linker = "i686-hitchhiker-linux-musl-gcc" + +[target.i586-unknown-linux-musl] +linker = "i586-hitchhiker-linux-musl-gcc" + +[target.x86_64-unknown-linux-musl] +linker = "x86_64-hitchhiker-linux-musl-gcc" diff --git a/src/db/mod.rs b/src/db/mod.rs index 2fbbfed..cbf2db4 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,10 +1,11 @@ -use std::{fmt, path::PathBuf}; +use ron::ser::PrettyConfig; use { crate::{Package, Repository, Version}, serde::{Deserialize, Serialize}, - std::{collections::HashMap, error::Error}, + std::{collections::HashMap, error::Error, fmt, fs::{self, File}, path::PathBuf}, url::Url, + zstd::{Decoder, Encoder}, }; #[derive(Clone, Debug)] @@ -92,15 +93,35 @@ impl Database { Ok(updates) } - pub fn from_file(prefix: Option) -> Result> { - unimplemented!(); + pub fn from_file(prefix: Option) -> Result> { + let db = crate::get_db(prefix); + if let Some(dir) = db.parent() { + if !dir.exists() { + fs::create_dir_all(dir)?; + } + } + let fd = File::open(db)?; + let reader = Decoder::new(fd)?; + let res = ron::de::from_reader(reader)?; + Ok(res) } - pub fn save_to_file(prefix: Option) -> Result<(), Box> { - unimplemented!(); + pub fn save_to_file(&self, prefix: Option) -> Result<(), Box> { + let db = crate::get_db(prefix); + if let Some(dir) = db.parent() { + if !dir.exists() { + fs::create_dir_all(dir)?; + } + } + let fd = File::create(db)?; + let mut writer = Encoder::new(fd, 0)?; + let cfg = PrettyConfig::new().struct_names(true); + ron::ser::to_writer_pretty(&mut writer, self, cfg)?; + writer.finish()?; + Ok(()) } - pub fn rebuild(prefix: Option) -> Result<(), Box> { + pub fn rebuild(prefix: Option) -> Result> { unimplemented!(); } } diff --git a/src/lib.rs b/src/lib.rs index 45db295..5d21335 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,8 +22,9 @@ pub use { version::*, }; -pub const DB: [&str; 4] = ["var", "db", "hpk", "db.ron.zstd"]; -pub const CACHE: [&str; 3] = ["var", "cache", "hpk"]; +const DB: [&str; 4] = ["var", "db", "hpk", "db.ron.zstd"]; +const DBDIR: [&str; 3] = ["var", "db", "hpk"]; +const CACHE: [&str; 3] = ["var", "cache", "hpk"]; /// Gets the database file path. Takes an optional prefix variable for working /// with an alternate filesystem root. @@ -38,19 +39,36 @@ pub const CACHE: [&str; 3] = ["var", "cache", "hpk"]; /// let db = hpk::get_db(Some(chroot)); /// ``` pub fn get_db(prefix: Option) -> PathBuf { - match prefix { - Some(mut p) => { - DB.iter().for_each(|seg| p.push(seg)); - p - }, - None => { - let mut p = PathBuf::from("/"); - DB.iter().for_each(|seg| p.push(seg)); - p - }, - } + let mut path = match prefix { + Some(p) => p, + None => PathBuf::from("/"), + }; + DB.iter().for_each(|seg| path.push(seg)); + path } +/// Gets the database directory path. Takes anoptional prefix variable for +/// working with an alternate filesystem root. +/// ## Examples +/// Get the database path for the currently running system: +/// ```Rust +/// let db = hpk::get_dbdir(None); +/// ``` +/// Get the database path for a chroot mounted at /mnt/aarch64: +/// ```Rust +/// let chroot = PathBuf::from("/mnt/aarch64"); +/// let db = hpk::get_dbdir(Some(chroot)); +/// ``` +pub fn get_dbdir(prefix: Option) -> PathBuf { + let mut path = match prefix { + Some(p) => p, + None => PathBuf::from("/"), + }; + DBDIR.iter().for_each(|seg| path.push(seg)); + path +} + + /// Gets the cache directory path. Takes an optional prefix variable for working /// with an alternate filesystem root. /// ## Examples @@ -64,17 +82,12 @@ pub fn get_db(prefix: Option) -> PathBuf { /// let db = hpk::get_cache(Some(chroot)); /// ``` pub fn get_cache(prefix: Option) -> PathBuf { - match prefix { - Some(mut p) => { - CACHE.iter().for_each(|seg| p.push(seg)); - p - } - None => { - let mut p = PathBuf::from("/"); - CACHE.iter().for_each(|seg| p.push(seg)); - p - }, - } + let mut path = match prefix { + Some(p) => p, + None => PathBuf::from("/"), + }; + CACHE.iter().for_each(|seg| path.push(seg)); + path } #[cfg(test)] diff --git a/src/version/mod.rs b/src/version/mod.rs index 21b3fa6..3ee2d71 100644 --- a/src/version/mod.rs +++ b/src/version/mod.rs @@ -210,3 +210,22 @@ impl PartialOrd for SemVer { } } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn cmp_semver_rapid_gt() { + let sem = "1.42.1".parse::().unwrap(); + let rpd = "1.42".parse::().unwrap(); + assert!(sem > rpd); + } + + #[test] + fn cmp_semver_rapid_eq() { + let sem = "1.42.0".parse::().unwrap(); + let rpd = "1.42".parse::().unwrap(); + assert!(sem == rpd); + } +}