| 1 |
use std::cell::OnceCell; |
| 2 |
use std::collections::HashMap; |
| 3 |
use std::fmt; |
| 4 |
|
| 5 |
use cxx::UniquePtr; |
| 6 |
|
| 7 |
use crate::raw::{DepIterator, VerIterator}; |
| 8 |
use crate::{Cache, Package, Version}; |
| 9 |
|
| 10 |
/// DepFlags defined in depcache.h |
| 11 |
#[allow(non_upper_case_globals, non_snake_case)] |
| 12 |
pub mod DepFlags { |
| 13 |
pub const DepNow: u8 = 1; |
| 14 |
pub const DepInstall: u8 = 2; |
| 15 |
pub const DepCVer: u8 = 4; |
| 16 |
pub const DepGNow: u8 = 8; |
| 17 |
pub const DepGInstall: u8 = 16; |
| 18 |
pub const DepGVer: u8 = 32; |
| 19 |
} |
| 20 |
|
| 21 |
/// The different types of Dependencies. |
| 22 |
#[derive(Debug, Eq, PartialEq, Hash)] |
| 23 |
pub enum DepType { |
| 24 |
Depends = 1, |
| 25 |
PreDepends = 2, |
| 26 |
Suggests = 3, |
| 27 |
Recommends = 4, |
| 28 |
Conflicts = 5, |
| 29 |
Replaces = 6, |
| 30 |
Obsoletes = 7, |
| 31 |
DpkgBreaks = 8, |
| 32 |
Enhances = 9, |
| 33 |
} |
| 34 |
|
| 35 |
impl From<u8> for DepType { |
| 36 |
fn from(value: u8) -> Self { |
| 37 |
match value { |
| 38 |
1 => DepType::Depends, |
| 39 |
2 => DepType::PreDepends, |
| 40 |
3 => DepType::Suggests, |
| 41 |
4 => DepType::Recommends, |
| 42 |
5 => DepType::Conflicts, |
| 43 |
6 => DepType::Replaces, |
| 44 |
7 => DepType::Obsoletes, |
| 45 |
8 => DepType::DpkgBreaks, |
| 46 |
9 => DepType::Enhances, |
| 47 |
_ => panic!("Dependency is malformed?"), |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl AsRef<str> for DepType { |
| 53 |
fn as_ref(&self) -> &str { |
| 54 |
match self { |
| 55 |
DepType::Depends => "Depends", |
| 56 |
DepType::PreDepends => "PreDepends", |
| 57 |
DepType::Suggests => "Suggests", |
| 58 |
DepType::Recommends => "Recommends", |
| 59 |
DepType::Conflicts => "Conflicts", |
| 60 |
DepType::Replaces => "Replaces", |
| 61 |
DepType::Obsoletes => "Obsoletes", |
| 62 |
DepType::DpkgBreaks => "Breaks", |
| 63 |
DepType::Enhances => "Enhances", |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
impl fmt::Display for DepType { |
| 69 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.as_ref()) } |
| 70 |
} |
| 71 |
|
| 72 |
/// A struct representing a Base Dependency. |
| 73 |
pub struct BaseDep<'a> { |
| 74 |
pub ptr: UniquePtr<DepIterator>, |
| 75 |
cache: &'a Cache, |
| 76 |
target: OnceCell<Package<'a>>, |
| 77 |
parent_ver: OnceCell<UniquePtr<VerIterator>>, |
| 78 |
} |
| 79 |
|
| 80 |
impl<'a> BaseDep<'a> { |
| 81 |
pub fn new(ptr: UniquePtr<DepIterator>, cache: &'a Cache) -> BaseDep { |
| 82 |
BaseDep { |
| 83 |
ptr, |
| 84 |
cache, |
| 85 |
target: OnceCell::new(), |
| 86 |
parent_ver: OnceCell::new(), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/// This is the name of the dependency. |
| 91 |
pub fn name(&self) -> &str { self.target_package().name() } |
| 92 |
|
| 93 |
/// Return the target package. |
| 94 |
/// |
| 95 |
/// For Reverse Dependencies this will actually return the parent package |
| 96 |
pub fn target_package(&self) -> &Package<'a> { |
| 97 |
self.target.get_or_init(|| { |
| 98 |
if self.is_reverse() { |
| 99 |
Package::new(self.cache, unsafe { self.parent_pkg() }) |
| 100 |
} else { |
| 101 |
Package::new(self.cache, unsafe { self.target_pkg() }) |
| 102 |
} |
| 103 |
}) |
| 104 |
} |
| 105 |
|
| 106 |
/// The target version &str of the dependency if specified. |
| 107 |
pub fn version(&self) -> Option<&str> { |
| 108 |
if self.is_reverse() { |
| 109 |
Some( |
| 110 |
self.parent_ver |
| 111 |
.get_or_init(|| unsafe { self.parent_ver() }) |
| 112 |
.version(), |
| 113 |
) |
| 114 |
} else { |
| 115 |
self.target_ver().ok() |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/// The Dependency Type. Depends, Recommends, etc. |
| 120 |
pub fn dep_type(&self) -> DepType { DepType::from(self.ptr.dep_type()) } |
| 121 |
|
| 122 |
/// Comparison type of the dependency version, if specified. |
| 123 |
pub fn comp_type(&self) -> Option<&str> { self.ptr.comp_type().ok() } |
| 124 |
|
| 125 |
// Iterate all Versions that are able to satisfy this dependency |
| 126 |
pub fn all_targets(&self) -> Vec<Version> { |
| 127 |
unsafe { |
| 128 |
self.ptr |
| 129 |
.all_targets() |
| 130 |
.iter() |
| 131 |
.map(|v| Version::new(v.unique(), self.cache)) |
| 132 |
.collect() |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<'a> fmt::Display for BaseDep<'a> { |
| 138 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 139 |
if let (Some(comp), Some(version)) = (self.comp_type(), self.version()) { |
| 140 |
write!(f, "({} {comp} {version})", self.name()) |
| 141 |
} else { |
| 142 |
write!(f, "({})", self.name()) |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl<'a> fmt::Debug for BaseDep<'a> { |
| 148 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 149 |
f.debug_struct("BaseDep") |
| 150 |
.field("parent", unsafe { &self.parent_pkg().name() }) |
| 151 |
.field("name", &self.name()) |
| 152 |
.field("comp", &self.comp_type()) |
| 153 |
.field("version", &self.version()) |
| 154 |
.field("dep_type", &self.dep_type()) |
| 155 |
.field("is_reverse", &self.is_reverse()) |
| 156 |
.finish() |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/// A struct representing a single Dependency record. |
| 161 |
/// |
| 162 |
/// This can contain multiple Base Dependencies that can |
| 163 |
/// satisfy the same Dependency. |
| 164 |
#[derive(fmt::Debug)] |
| 165 |
pub struct Dependency<'a> { |
| 166 |
pub(crate) ptr: Vec<BaseDep<'a>>, |
| 167 |
} |
| 168 |
|
| 169 |
impl<'a> Dependency<'a> { |
| 170 |
/// Return the Dep Type of this group. Depends, Pre-Depends. |
| 171 |
pub fn dep_type(&self) -> DepType { self[0].dep_type() } |
| 172 |
|
| 173 |
/// Returns True if there are multiple dependencies that can satisfy this |
| 174 |
pub fn is_or(&self) -> bool { self.len() > 1 } |
| 175 |
|
| 176 |
/// Returns a reference to the first BaseDep |
| 177 |
pub fn first(&self) -> &BaseDep<'a> { &self[0] } |
| 178 |
} |
| 179 |
|
| 180 |
impl<'a> fmt::Display for Dependency<'a> { |
| 181 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 182 |
let mut dep_str = String::new(); |
| 183 |
|
| 184 |
for (i, base_dep) in self.iter().enumerate() { |
| 185 |
dep_str += &base_dep.to_string(); |
| 186 |
if i + 1 != self.len() { |
| 187 |
dep_str += " | " |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
write!( |
| 192 |
f, |
| 193 |
"{} {:?} {dep_str}", |
| 194 |
unsafe { self.first().parent_pkg().fullname(false) }, |
| 195 |
self.dep_type(), |
| 196 |
)?; |
| 197 |
Ok(()) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
pub fn create_depends_map( |
| 202 |
cache: &Cache, |
| 203 |
dep: Option<UniquePtr<DepIterator>>, |
| 204 |
) -> HashMap<DepType, Vec<Dependency>> { |
| 205 |
let mut dependencies: HashMap<DepType, Vec<Dependency>> = HashMap::new(); |
| 206 |
|
| 207 |
if let Some(mut dep) = dep { |
| 208 |
while !dep.end() { |
| 209 |
let mut or_deps = vec![]; |
| 210 |
or_deps.push(BaseDep::new(unsafe { dep.unique() }, cache)); |
| 211 |
|
| 212 |
// This means that more than one thing can satisfy a dependency. |
| 213 |
// For reverse dependencies we cannot get the or deps. |
| 214 |
// This can cause a segfault |
| 215 |
// See: https://gitlab.com/volian/rust-apt/-/merge_requests/36 |
| 216 |
if dep.or_dep() && !dep.is_reverse() { |
| 217 |
loop { |
| 218 |
dep.pin_mut().raw_next(); |
| 219 |
or_deps.push(BaseDep::new(unsafe { dep.unique() }, cache)); |
| 220 |
// This is the last of the Or group |
| 221 |
if !dep.or_dep() { |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
let dep_type = DepType::from(dep.dep_type()); |
| 228 |
|
| 229 |
// If the entry already exists in the map append it. |
| 230 |
if let Some(vec) = dependencies.get_mut(&dep_type) { |
| 231 |
vec.push(Dependency { ptr: or_deps }) |
| 232 |
} else { |
| 233 |
// Doesn't exist so we create it |
| 234 |
dependencies.insert(dep_type, vec![Dependency { ptr: or_deps }]); |
| 235 |
} |
| 236 |
dep.pin_mut().raw_next(); |
| 237 |
} |
| 238 |
} |
| 239 |
dependencies |
| 240 |
} |
| 241 |
|
| 242 |
#[cxx::bridge] |
| 243 |
pub(crate) mod raw { |
| 244 |
unsafe extern "C++" { |
| 245 |
include!("rust-apt/apt-pkg-c/package.h"); |
| 246 |
|
| 247 |
type DepIterator; |
| 248 |
|
| 249 |
type PkgIterator = crate::raw::PkgIterator; |
| 250 |
type VerIterator = crate::raw::VerIterator; |
| 251 |
|
| 252 |
/// The Parent PkgIterator for this dependency |
| 253 |
/// |
| 254 |
/// # Safety |
| 255 |
/// |
| 256 |
/// If the inner pointer is null segfaults can occur. |
| 257 |
/// |
| 258 |
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option |
| 259 |
/// is recommended. |
| 260 |
/// |
| 261 |
/// The returned UniquePtr cannot outlive the cache. |
| 262 |
unsafe fn parent_pkg(self: &DepIterator) -> UniquePtr<PkgIterator>; |
| 263 |
|
| 264 |
/// The Parent VerIterator for this dependency |
| 265 |
/// |
| 266 |
/// # Safety |
| 267 |
/// |
| 268 |
/// If the inner pointer is null segfaults can occur. |
| 269 |
/// |
| 270 |
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option |
| 271 |
/// is recommended. |
| 272 |
/// |
| 273 |
/// The returned UniquePtr cannot outlive the cache. |
| 274 |
unsafe fn parent_ver(self: &DepIterator) -> UniquePtr<VerIterator>; |
| 275 |
|
| 276 |
// Dependency Declarations |
| 277 |
/// String representation of the dependency compare type |
| 278 |
/// "","<=",">=","<",">","=","!=" |
| 279 |
/// |
| 280 |
/// This returns Error for no compare type. |
| 281 |
pub fn comp_type(self: &DepIterator) -> Result<&str>; |
| 282 |
|
| 283 |
// Get the dependency type as a u8 |
| 284 |
// You can use `DepType::from(raw_dep.dep_type())` to convert to enum. |
| 285 |
pub fn dep_type(self: &DepIterator) -> u8; |
| 286 |
|
| 287 |
/// Returns true if the dependency type is critical. |
| 288 |
/// |
| 289 |
/// Depends, PreDepends, Conflicts, Obsoletes, Breaks |
| 290 |
/// will return [true]. |
| 291 |
/// |
| 292 |
/// Suggests, Recommends, Replaces and Enhances |
| 293 |
/// will return [false]. |
| 294 |
#[cxx_name = "IsCritical"] |
| 295 |
pub fn is_critical(self: &DepIterator) -> bool; |
| 296 |
|
| 297 |
/// Return True if the dep is reverse, false if normal |
| 298 |
#[cxx_name = "Reverse"] |
| 299 |
pub fn is_reverse(self: &DepIterator) -> bool; |
| 300 |
|
| 301 |
pub fn target_ver(self: &DepIterator) -> Result<&str>; |
| 302 |
|
| 303 |
/// Return the Target Package for the dependency. |
| 304 |
/// |
| 305 |
/// # Safety |
| 306 |
/// |
| 307 |
/// If the inner pointer is null segfaults can occur. |
| 308 |
/// |
| 309 |
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option |
| 310 |
/// is recommended. |
| 311 |
/// |
| 312 |
/// The returned UniquePtr cannot outlive the cache. |
| 313 |
unsafe fn target_pkg(self: &DepIterator) -> UniquePtr<PkgIterator>; |
| 314 |
|
| 315 |
/// Returns a CxxVector of VerIterators. |
| 316 |
/// |
| 317 |
/// # Safety |
| 318 |
/// |
| 319 |
/// These can not be owned and will need to be Cloned with unique. |
| 320 |
/// |
| 321 |
/// If the inner pointer is null segfaults can occur. |
| 322 |
/// |
| 323 |
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option |
| 324 |
/// is recommended. |
| 325 |
/// |
| 326 |
/// The returned UniquePtr cannot outlive the cache. |
| 327 |
unsafe fn all_targets(self: &DepIterator) -> UniquePtr<CxxVector<VerIterator>>; |
| 328 |
|
| 329 |
/// Return true if this dep is Or'd with the next. The last dep in the |
| 330 |
/// or group will return False. |
| 331 |
pub fn or_dep(self: &DepIterator) -> bool; |
| 332 |
|
| •333 |
pub fn index(self: &DepIterator) -> usize; |
| 334 |
/// Clone the pointer. |
| 335 |
/// |
| 336 |
/// # Safety |
| 337 |
/// |
| 338 |
/// If the inner pointer is null segfaults can occur. |
| 339 |
/// |
| 340 |
/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option |
| 341 |
/// is recommended. |
| 342 |
/// |
| 343 |
/// The returned UniquePtr cannot outlive the cache. |
| 344 |
unsafe fn unique(self: &DepIterator) -> UniquePtr<DepIterator>; |
| 345 |
pub fn raw_next(self: Pin<&mut DepIterator>); |
| 346 |
pub fn end(self: &DepIterator) -> bool; |
| 347 |
} |
| 348 |
} |