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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
use std::borrow::Cow;
use std::cmp::Ordering;
use std::ffi::{c_char, c_int};
use std::ptr;
use pkgcraft::dep::{Blocker, Cpv, Dep, DepFields, Intersects, SlotOperator, Version};
use pkgcraft::eapi::Eapi;
use pkgcraft::restrict::{Restrict, Restriction};
use pkgcraft::utils::hash;
use crate::eapi::eapi_or_default;
use crate::macros::*;
use crate::panic::ffi_catch_panic;
use crate::utils::str_to_raw;
/// Parse a string into a package dependency using a specific EAPI. Pass NULL for the eapi argument
/// in order to parse using the latest EAPI with extensions (e.g. support for repo deps).
///
/// Returns NULL on error.
///
/// # Safety
/// The eapi argument may be NULL to use the default EAPI.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_new(s: *const c_char, eapi: *const Eapi) -> *mut Dep {
ffi_catch_panic! {
let s = try_str_from_ptr!(s);
let eapi = eapi_or_default!(eapi);
let dep = unwrap_or_panic!(eapi.dep(s));
Box::into_raw(Box::new(dep))
}
}
/// Determine if a string is a valid package dependency.
///
/// Returns NULL on error.
///
/// # Safety
/// The eapi argument may be NULL to use the default EAPI.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_valid(s: *const c_char, eapi: *const Eapi) -> *const c_char {
ffi_catch_panic! {
let val = try_str_from_ptr!(s);
let eapi = option_from_ptr!(eapi);
unwrap_or_panic!(Dep::valid(val, eapi));
s
}
}
/// Return a given package dependency without the specified fields.
///
/// # Safety
/// The arguments must be a non-null Dep pointer and a DepFields bitflag.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_without(d: *mut Dep, fields: u32) -> *mut Dep {
let dep = try_ref_from_ptr!(d);
let fields = DepFields::from_bits_truncate(fields);
if let Cow::Owned(d) = dep.without(fields) {
Box::into_raw(Box::new(d))
} else {
d
}
}
/// Parse a string into an unversioned package dependency.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_new_cpn(s: *const c_char) -> *mut Dep {
ffi_catch_panic! {
let s = try_str_from_ptr!(s);
let dep = unwrap_or_panic!(Dep::new_cpn(s));
Box::into_raw(Box::new(dep))
}
}
/// Parse a string into a Blocker's raw value.
///
/// Returns a value of 0 for nonexistence.
///
/// # Safety
/// The argument must be a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_blocker_from_str(s: *const c_char) -> u32 {
let s = try_str_from_ptr!(s);
s.parse::<Blocker>().map(|x| x as u32).unwrap_or_default()
}
/// Return the string for a Blocker.
#[no_mangle]
pub extern "C" fn pkgcraft_dep_blocker_str(b: Blocker) -> *mut c_char {
try_ptr_from_str!(b.as_ref())
}
/// Parse a string into a SlotOperator's raw value.
///
/// Returns a value of 0 for nonexistence.
///
/// # Safety
/// The argument must be a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_slot_op_from_str(s: *const c_char) -> u32 {
let s = try_str_from_ptr!(s);
s.parse::<SlotOperator>()
.map(|x| x as u32)
.unwrap_or_default()
}
/// Return the string for a SlotOperator.
#[no_mangle]
pub extern "C" fn pkgcraft_dep_slot_op_str(op: SlotOperator) -> *mut c_char {
try_ptr_from_str!(op.as_ref())
}
/// Compare two package dependencies returning -1, 0, or 1 if the first is less than, equal to, or
/// greater than the second, respectively.
///
/// # Safety
/// The arguments must be non-null Dep pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_cmp(d1: *mut Dep, d2: *mut Dep) -> c_int {
let d1 = try_ref_from_ptr!(d1);
let d2 = try_ref_from_ptr!(d2);
match d1.cmp(d2) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
/// Determine if two package dependencies intersect.
///
/// # Safety
/// The arguments must be non-null Dep pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_intersects(d1: *mut Dep, d2: *mut Dep) -> bool {
let d1 = try_ref_from_ptr!(d1);
let d2 = try_ref_from_ptr!(d2);
d1.intersects(d2)
}
/// Determine if a package dependency intersects with a Cpv.
///
/// # Safety
/// The arguments must be non-null Cpv and Dep pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_intersects_cpv(d: *mut Dep, c: *mut Cpv) -> bool {
let d = try_ref_from_ptr!(d);
let c = try_ref_from_ptr!(c);
d.intersects(c)
}
/// Get the category of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "cat".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_category(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.category())
}
/// Get the package name of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "pkg".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_package(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.package())
}
/// Get a package dependency's raw blocker value.
/// For example, the package dependency "!cat/pkg" has a weak blocker.
///
/// Returns a value of 0 for nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_blocker(d: *mut Dep) -> u32 {
let dep = try_ref_from_ptr!(d);
dep.blocker().map(|x| x as u32).unwrap_or_default()
}
/// Get the version of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "1-r2".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_version(d: *mut Dep) -> *mut Version {
let dep = try_ref_from_ptr!(d);
match dep.version() {
Some(v) => Box::into_raw(Box::new(v.clone())),
None => ptr::null_mut(),
}
}
/// Get the slot of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2:3" returns "3".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_slot(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.slot() {
Some(s) => try_ptr_from_str!(s),
None => ptr::null_mut(),
}
}
/// Get the subslot of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2:3/4" returns "4".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_subslot(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.subslot() {
Some(s) => try_ptr_from_str!(s),
None => ptr::null_mut(),
}
}
/// Get a package dependency's raw slot operator value.
/// For example, the package dependency "=cat/pkg-1-r2:0=" has an equal slot operator.
///
/// Returns a value of 0 for nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_slot_op(d: *mut Dep) -> u32 {
let dep = try_ref_from_ptr!(d);
dep.slot_op().map(|x| x as u32).unwrap_or_default()
}
/// Get the USE dependencies of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2[a,b,c]" has USE dependencies of "a, b, c".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_use_deps(d: *mut Dep, len: *mut usize) -> *mut *mut c_char {
// TODO: switch from usize to std::os::raw::c_size_t when it's stable.
let dep = try_ref_from_ptr!(d);
match dep.use_deps() {
Some(use_deps) => iter_to_array!(use_deps.iter(), len, str_to_raw),
None => ptr::null_mut(),
}
}
/// Get the repo of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2:3/4::repo" returns "repo".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_repo(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.repo() {
Some(s) => try_ptr_from_str!(s),
None => ptr::null_mut(),
}
}
/// Get the package and revision of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "pkg-1".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_p(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.p())
}
/// Get the package, version, and revision of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "pkg-1-r2".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_pf(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.pf())
}
/// Get the revision of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "r2".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_pr(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.pr().as_str() {
"" => ptr::null_mut(),
s => try_ptr_from_str!(s),
}
}
/// Get the version of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "1".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_pv(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.pv().as_str() {
"" => ptr::null_mut(),
s => try_ptr_from_str!(s),
}
}
/// Get the version and revision of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "1-r2".
///
/// Returns NULL on nonexistence.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_pvr(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
match dep.pvr().as_str() {
"" => ptr::null_mut(),
s => try_ptr_from_str!(s),
}
}
/// Get the category and package of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "cat/pkg".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_cpn(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.cpn())
}
/// Get the category, package, and version of a package dependency.
/// For example, the package dependency "=cat/pkg-1-r2" returns "cat/pkg-1-r2".
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_cpv(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.cpv())
}
/// Return the string for a package dependency.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_str(d: *mut Dep) -> *mut c_char {
let dep = try_ref_from_ptr!(d);
try_ptr_from_str!(dep.to_string())
}
/// Return the hash value for a package dependency.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_hash(d: *mut Dep) -> u64 {
let dep = try_ref_from_ptr!(d);
hash(dep)
}
/// Return the restriction for a package dependency.
///
/// # Safety
/// The argument must be a non-null Dep pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_restrict(d: *mut Dep) -> *mut Restrict {
let dep = try_ref_from_ptr!(d);
Box::into_raw(Box::new(dep.into()))
}
/// Determine if a restriction matches a package dependency.
///
/// # Safety
/// The arguments must be valid Restrict and Dep pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_restrict_matches(d: *mut Dep, r: *mut Restrict) -> bool {
let dep = try_ref_from_ptr!(d);
let restrict = try_ref_from_ptr!(r);
restrict.matches(dep)
}
/// Free a package dependency.
///
/// # Safety
/// The argument must be a Dep pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_free(d: *mut Dep) {
if !d.is_null() {
unsafe { drop(Box::from_raw(d)) };
}
}