diff --git a/src/lib.rs b/src/lib.rs index a042ee3..d0f3043 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,9 @@ use librpm::config; // use librpm pub mod rpm; +// use librpmio; +pub mod rpmio; + // 使用 Once 对配置进行一次性地初始化 static CONFIGURE: Once = Once::new(); diff --git a/src/rpmio.rs b/src/rpmio.rs new file mode 100644 index 0000000..ea7fb50 --- /dev/null +++ b/src/rpmio.rs @@ -0,0 +1,29 @@ +use std::ffi::CString; + +/// rpmvercmp 可对比两个软件包的版本 +/// 若 V1 大于 V2,则结果 > 0 +/// 若 V1 等于 V2,则结果 == 0 +/// 若 V1 小于 V2,则结果 < 0 +pub fn rpmvercmp(v1: &str, v2: &str) -> i32 { + let v1 = CString::new(v1).expect("?"); + let v2 = CString::new(v2).expect("?"); + + unsafe { librpm_sys::rpmvercmp(v1.as_ptr(), v2.as_ptr()) } +} + +#[test] +fn test_rpmvercmp() { + let v1 = "5.10.0-60.ule3.x86_64"; + let v2 = "5.10.0-96.ule3.x86_64"; + let v3 = "5.10.0-108.ule3.x86_64"; + + let lt = rpmvercmp(v1, v2); + let eq = rpmvercmp(v2, v2); + let gt = rpmvercmp(v3, v2); + + if lt < eq && eq < gt && eq == 0 { + assert!(true); + } else { + assert!(false); + } +}