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
use std::cmp::Ordering;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int};
use std::ptr;
use std::sync::Arc;

use pkgcraft::repo::ebuild::Repo as EbuildRepo;
use pkgcraft::repo::{self, Contains, Repository};
use pkgcraft::{pkg, restrict, utils::hash, Error};

use crate::macros::*;

pub mod ebuild;

#[repr(C)]
pub enum RepoFormat {
    Ebuild,
    Fake,
    Empty,
}

impl From<&repo::Repo> for RepoFormat {
    fn from(repo: &repo::Repo) -> Self {
        match repo {
            repo::Repo::Ebuild(_) => Self::Ebuild,
            repo::Repo::Fake(_) => Self::Fake,
            repo::Repo::Unsynced(_) => Self::Empty,
        }
    }
}

/// Return a repo's id.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_id(r: *mut repo::Repo) -> *mut c_char {
    let repo = null_ptr_check!(r.as_ref());
    CString::new(repo.id()).unwrap().into_raw()
}

/// Return a repo's path.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_path(r: *mut repo::Repo) -> *mut c_char {
    let repo = null_ptr_check!(r.as_ref());
    CString::new(repo.path().as_str()).unwrap().into_raw()
}

/// Return a repo's length.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_len(r: *mut repo::Repo) -> usize {
    let repo = null_ptr_check!(r.as_ref());
    repo.len()
}

/// Return a repo's categories.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_categories(
    r: *mut repo::Repo,
    len: *mut usize,
) -> *mut *mut c_char {
    let repo = null_ptr_check!(r.as_ref());
    obj_to_array_char_p!(repo.categories(), len)
}

/// Return a repo's packages for a category.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null Repo pointer and category.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_packages(
    r: *mut repo::Repo,
    cat: *const c_char,
    len: *mut usize,
) -> *mut *mut c_char {
    let repo = null_ptr_check!(r.as_ref());
    let cat = null_ptr_check!(cat.as_ref());
    let cat = unsafe { unwrap_or_return!(CStr::from_ptr(cat).to_str(), ptr::null_mut()) };
    obj_to_array_char_p!(repo.packages(cat), len)
}

/// Return a repo's versions for a package.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null Repo pointer, category, and package.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_versions(
    r: *mut repo::Repo,
    cat: *const c_char,
    pkg: *const c_char,
    len: *mut usize,
) -> *mut *mut c_char {
    let repo = null_ptr_check!(r.as_ref());
    let cat = null_ptr_check!(cat.as_ref());
    let pkg = null_ptr_check!(pkg.as_ref());
    let cat = unsafe { unwrap_or_return!(CStr::from_ptr(cat).to_str(), ptr::null_mut()) };
    let pkg = unsafe { unwrap_or_return!(CStr::from_ptr(pkg).to_str(), ptr::null_mut()) };
    obj_to_array_char_p!(repo.versions(cat, pkg), len)
}

/// Compare two repos returning -1, 0, or 1 if the first repo is less than, equal to, or greater
/// than the second repo, respectively.
///
/// # Safety
/// The arguments must be non-null Repo pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_cmp(r1: *mut repo::Repo, r2: *mut repo::Repo) -> c_int {
    let repo1 = null_ptr_check!(r1.as_ref());
    let repo2 = null_ptr_check!(r2.as_ref());

    match repo1.cmp(repo2) {
        Ordering::Less => -1,
        Ordering::Equal => 0,
        Ordering::Greater => 1,
    }
}

/// Convert a Repo into an EbuildRepo.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_as_ebuild(r: *mut repo::Repo) -> *const EbuildRepo {
    let repo = null_ptr_check!(r.as_ref());
    let result = repo
        .as_ebuild()
        .ok_or_else(|| Error::InvalidValue("invalid repo format".to_string()));
    let repo = unwrap_or_return!(result, ptr::null());
    Arc::as_ptr(repo)
}

/// Return the hash value for a repo.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_hash(r: *mut repo::Repo) -> u64 {
    let repo = null_ptr_check!(r.as_ref());
    hash(repo)
}

/// Determine if a path is in a repo.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_contains_path(
    r: *mut repo::Repo,
    path: *const c_char,
) -> bool {
    let repo = null_ptr_check!(r.as_ref());
    let path = null_ptr_check!(path.as_ref());
    let path = unsafe { unwrap_or_return!(CStr::from_ptr(path).to_str(), false) };
    repo.contains(path)
}

/// Free a repo.
///
/// # Safety
/// The argument must be a Repo pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_free(r: *mut repo::Repo) {
    if !r.is_null() {
        unsafe { drop(Box::from_raw(r)) };
    }
}

/// Return a package iterator for a repo.
///
/// # Safety
/// The argument must be a non-null Repo pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_iter<'a>(r: *mut repo::Repo) -> *mut repo::PkgIter<'a> {
    let repo = null_ptr_check!(r.as_ref());
    Box::into_raw(Box::new(repo.iter()))
}

/// Return the next package from a package iterator.
///
/// Returns NULL when the iterator is empty.
///
/// # Safety
/// The argument must be a non-null PkgIter pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_iter_next(i: *mut repo::PkgIter) -> *mut pkg::Pkg {
    let iter = null_ptr_check!(i.as_mut());
    match iter.next() {
        None => ptr::null_mut(),
        Some(p) => Box::into_raw(Box::new(p)),
    }
}

/// Free a repo iterator.
///
/// # Safety
/// The argument must be a non-null PkgIter pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_iter_free(i: *mut repo::PkgIter) {
    if !i.is_null() {
        unsafe { drop(Box::from_raw(i)) };
    }
}

/// Return a restriction package iterator for a repo.
///
/// # Safety
/// The repo argument must be a non-null Repo pointer and the restrict argument must be a non-null
/// Restrict pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_restrict_iter<'a>(
    repo: *mut repo::Repo,
    restrict: *mut restrict::Restrict,
) -> *mut repo::RestrictPkgIter<'a> {
    let repo = null_ptr_check!(repo.as_ref());
    let restrict = null_ptr_check!(restrict.as_ref());
    Box::into_raw(Box::new(repo.iter_restrict(restrict.clone())))
}

/// Return the next package from a restriction package iterator.
///
/// Returns NULL when the iterator is empty.
///
/// # Safety
/// The argument must be a non-null RestrictPkgIter pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_restrict_iter_next(
    i: *mut repo::RestrictPkgIter,
) -> *mut pkg::Pkg {
    let iter = null_ptr_check!(i.as_mut());
    match iter.next() {
        None => ptr::null_mut(),
        Some(p) => Box::into_raw(Box::new(p)),
    }
}

/// Free a repo iterator.
///
/// # Safety
/// The argument must be a non-null RestrictPkgIter pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_repo_restrict_iter_free(i: *mut repo::RestrictPkgIter) {
    if !i.is_null() {
        unsafe { drop(Box::from_raw(i)) };
    }
}