1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
use crate::utils::*; use crate::{free, Alpm, AlpmList, Db, FreeMethod, Package, Ver}; use alpm_sys::alpm_depmod_t::*; use alpm_sys::*; use std::ffi::{c_void, CString}; use std::fmt; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem::transmute; #[derive(Debug)] pub struct Depend<'a> { pub(crate) inner: *mut alpm_depend_t, pub(crate) drop: bool, pub(crate) phantom: PhantomData<&'a ()>, } impl<'a> Drop for Depend<'a> { fn drop(&mut self) { if self.drop { unsafe { alpm_dep_free(self.inner) } } } } impl<'a> Hash for Depend<'a> { fn hash<H: Hasher>(&self, state: &mut H) { self.name().hash(state); self.depmod().hash(state); self.version().hash(state); } } impl<'a> PartialEq for Depend<'a> { fn eq(&self, other: &Self) -> bool { self.name() == other.name() && self.depmod() == other.depmod() && self.version() == other.version() && self.desc() == other.desc() } } impl<'a> Eq for Depend<'a> {} impl<'a> fmt::Display for Depend<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { unsafe { let cs = alpm_dep_compute_string(self.inner); let s = from_cstr(cs); let err = write!(f, "{}", s); free(cs as *mut c_void); err } } } impl<S: Into<String>> From<S> for Depend<'static> { fn from(s: S) -> Depend<'static> { Depend::new(s) } } impl<'a> Depend<'a> { pub fn new<S: Into<String>>(s: S) -> Depend<'static> { let s = CString::new(s.into()).unwrap(); let dep = unsafe { alpm_dep_from_string(s.as_ptr()) }; Depend { inner: dep, drop: true, phantom: PhantomData, } } pub fn name(&self) -> &str { unsafe { from_cstr((*self.inner).name) } } pub fn version(&self) -> Option<&Ver> { unsafe { (*self.inner).version.as_ref().map(|p| Ver::from_ptr(p)) } } unsafe fn version_unchecked(&self) -> &Ver { Ver::from_ptr((*self.inner).version) } pub fn desc(&self) -> &str { unsafe { from_cstr((*self.inner).desc) } } pub fn name_hash(&self) -> u64 { unsafe { (*self.inner).name_hash as u64 } } pub fn depmod(&self) -> DepMod { unsafe { transmute::<alpm_depmod_t, DepMod>((*self.inner).mod_) } } pub fn depmodver(&self) -> DepModVer { unsafe { match self.depmod() { DepMod::Any => DepModVer::Any, DepMod::Eq => DepModVer::Eq(self.version_unchecked()), DepMod::Ge => DepModVer::Ge(self.version_unchecked()), DepMod::Le => DepModVer::Le(self.version_unchecked()), DepMod::Gt => DepModVer::Gt(self.version_unchecked()), DepMod::Lt => DepModVer::Lt(self.version_unchecked()), } } } } #[derive(Debug, Eq, PartialEq, Copy, Clone, Ord, PartialOrd, Hash)] pub enum DepModVer<'a> { Any, Eq(&'a Ver), Ge(&'a Ver), Le(&'a Ver), Gt(&'a Ver), Lt(&'a Ver), } impl From<DepModVer<'_>> for DepMod { fn from(d: DepModVer) -> Self { match d { DepModVer::Any => DepMod::Any, DepModVer::Eq(_) => DepMod::Eq, DepModVer::Ge(_) => DepMod::Ge, DepModVer::Le(_) => DepMod::Le, DepModVer::Gt(_) => DepMod::Gt, DepModVer::Lt(_) => DepMod::Lt, } } } impl DepModVer<'_> { pub fn depmod(self) -> DepMod { self.into() } } #[repr(u32)] #[derive(Debug, Eq, PartialEq, Copy, Clone, Ord, PartialOrd, Hash)] pub enum DepMod { Any = ALPM_DEP_MOD_ANY as u32, Eq = ALPM_DEP_MOD_EQ as u32, Ge = ALPM_DEP_MOD_GE as u32, Le = ALPM_DEP_MOD_LE as u32, Gt = ALPM_DEP_MOD_GT as u32, Lt = ALPM_DEP_MOD_LT as u32, } #[derive(Debug)] pub struct DepMissing { pub(crate) inner: *mut alpm_depmissing_t, } impl Drop for DepMissing { fn drop(&mut self) { unsafe { alpm_depmissing_free(self.inner) } } } impl DepMissing { pub fn target<'a>(&self) -> &'a str { let target = unsafe { (*self.inner).target }; unsafe { from_cstr(target) } } pub fn depend(&self) -> Depend { let depend = unsafe { (*self.inner).depend }; Depend { inner: depend, phantom: PhantomData, drop: false, } } pub fn causing_pkg<'a>(&self) -> Option<&'a str> { let causing_pkg = unsafe { (*self.inner).causingpkg }; if causing_pkg.is_null() { None } else { unsafe { Some(from_cstr(causing_pkg)) } } } } impl<'a> AlpmList<'a, Db<'a>> { pub fn find_satisfier<S: Into<String>>(&self, dep: S) -> Option<Package<'a>> { let dep = CString::new(dep.into()).unwrap(); let pkg = unsafe { alpm_find_dbs_satisfier(self.handle.handle, self.list, dep.as_ptr()) }; self.handle.check_null(pkg).ok()?; unsafe { Some(Package::new(self.handle, pkg)) } } } impl<'a> AlpmList<'a, Package<'a>> { pub fn find_satisfier<S: Into<String>>(&self, dep: S) -> Option<Package<'a>> { let dep = CString::new(dep.into()).unwrap(); let pkg = unsafe { alpm_find_satisfier(self.list, dep.as_ptr()) }; self.handle.check_null(pkg).ok()?; unsafe { Some(Package::new(self.handle, pkg)) } } } impl Alpm { pub fn check_deps( &self, pkgs: AlpmList<Package>, rem: AlpmList<Package>, upgrade: AlpmList<Package>, reverse_deps: bool, ) -> AlpmList<DepMissing> { let reverse_deps = if reverse_deps { 1 } else { 0 }; let list = unsafe { alpm_checkdeps(self.handle, pkgs.list, rem.list, upgrade.list, reverse_deps) }; AlpmList::new(self, list, FreeMethod::FreeDepMissing) } } #[cfg(test)] mod tests { use super::*; use crate::SigLevel; #[test] fn test_depend_lifetime() { let handle = Alpm::new("/", "tests/db").unwrap(); let db = handle.register_syncdb("core", SigLevel::NONE).unwrap(); let pkg = db.pkg("linux").unwrap(); let depends = pkg.depends(); let vec = depends.collect::<Vec<_>>(); drop(pkg); drop(db); println!("{:?}", vec); } #[test] fn test_eq() { assert_eq!(Depend::new("foo=1"), Depend::new("foo=1")); assert!(Depend::new("foo=2") != Depend::new("foo=1")); } }