]>
Commit | Line | Data |
---|---|---|
221cc3ce MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2013 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
4a81ff85 | 21 | #include <linux/limits.h> |
221cc3ce MT |
22 | #include <stdlib.h> |
23 | #include <string.h> | |
24 | #include <time.h> | |
25 | ||
26 | #include <solv/evr.h> | |
27 | #include <solv/pool.h> | |
28 | #include <solv/pooltypes.h> | |
29 | #include <solv/repo.h> | |
30 | #include <solv/solvable.h> | |
31 | ||
cbed1552 | 32 | #include <pakfire/archive.h> |
221cc3ce MT |
33 | #include <pakfire/constants.h> |
34 | #include <pakfire/file.h> | |
5e9463ec | 35 | #include <pakfire/filelist.h> |
221cc3ce | 36 | #include <pakfire/i18n.h> |
6cdfb9dd | 37 | #include <pakfire/logging.h> |
221cc3ce | 38 | #include <pakfire/package.h> |
cbed1552 | 39 | #include <pakfire/pakfire.h> |
9f953e68 | 40 | #include <pakfire/private.h> |
221cc3ce MT |
41 | #include <pakfire/relation.h> |
42 | #include <pakfire/relationlist.h> | |
43 | #include <pakfire/repo.h> | |
221cc3ce MT |
44 | #include <pakfire/util.h> |
45 | ||
7847efcd | 46 | struct _PakfirePackage { |
cbed1552 | 47 | Pakfire pakfire; |
7847efcd | 48 | Id id; |
7847efcd | 49 | int nrefs; |
e9d9eadc MT |
50 | |
51 | PakfireRepo repo; | |
7847efcd MT |
52 | }; |
53 | ||
f1c7996e | 54 | static Pool* pakfire_package_get_solv_pool(PakfirePackage pkg) { |
44b27b6f | 55 | return pakfire_get_solv_pool(pkg->pakfire); |
f1c7996e MT |
56 | } |
57 | ||
cbed1552 | 58 | static void pakfire_package_add_self_provides(Pakfire pakfire, PakfirePackage pkg, const char* name, const char* evr) { |
1a497776 | 59 | PakfireRelation relation = pakfire_relation_create(pakfire, name, PAKFIRE_EQ, evr); |
221cc3ce | 60 | pakfire_package_add_provides(pkg, relation); |
1a497776 | 61 | pakfire_relation_unref(relation); |
221cc3ce MT |
62 | } |
63 | ||
bd9c3c28 | 64 | PAKFIRE_EXPORT PakfirePackage pakfire_package_create_from_solvable(Pakfire pakfire, Id id) { |
90312f5c | 65 | PakfirePackage pkg = calloc(1, sizeof(*pkg)); |
e9d9eadc MT |
66 | if (!pkg) |
67 | return NULL; | |
6cdfb9dd | 68 | |
e9d9eadc | 69 | DEBUG(pakfire, "Allocated Package at %p\n", pkg); |
221cc3ce | 70 | |
e9d9eadc MT |
71 | pkg->pakfire = pakfire_ref(pakfire); |
72 | pkg->id = id; | |
73 | ||
74 | // Initialize reference counter | |
75 | pkg->nrefs = 1; | |
221cc3ce MT |
76 | |
77 | return pkg; | |
78 | } | |
79 | ||
f71cc0c7 | 80 | PAKFIRE_EXPORT PakfirePackage pakfire_package_create(Pakfire pakfire, PakfireRepo repo, const char* name, const char* evr, const char* arch) { |
e9d9eadc MT |
81 | Id id = pakfire_repo_add_solvable(repo); |
82 | if (!id) | |
83 | return NULL; | |
84 | ||
85 | PakfirePackage pkg = pakfire_package_create_from_solvable(pakfire, id); | |
86 | if (!pkg) | |
87 | return NULL; | |
221cc3ce | 88 | |
e9d9eadc MT |
89 | pkg->repo = pakfire_repo_ref(repo); |
90 | ||
91 | // Set the given attributes | |
221cc3ce MT |
92 | pakfire_package_set_name(pkg, name); |
93 | pakfire_package_set_evr(pkg, evr); | |
94 | pakfire_package_set_arch(pkg, arch); | |
95 | ||
e9d9eadc | 96 | // Add self-provides |
cbed1552 | 97 | pakfire_package_add_self_provides(pakfire, pkg, name, evr); |
221cc3ce MT |
98 | |
99 | return pkg; | |
100 | } | |
101 | ||
a4e3894f | 102 | static void pakfire_package_free(PakfirePackage pkg) { |
12656820 | 103 | DEBUG(pkg->pakfire, "Releasing Package at %p\n", pkg); |
12656820 | 104 | |
e9d9eadc MT |
105 | if (pkg->repo) |
106 | pakfire_repo_unref(pkg->repo); | |
107 | ||
5e9463ec | 108 | pakfire_unref(pkg->pakfire); |
f0d6233d | 109 | free(pkg); |
221cc3ce MT |
110 | } |
111 | ||
a4e3894f MT |
112 | PAKFIRE_EXPORT PakfirePackage pakfire_package_ref(PakfirePackage pkg) { |
113 | pkg->nrefs++; | |
114 | ||
115 | return pkg; | |
116 | } | |
117 | ||
118 | PAKFIRE_EXPORT PakfirePackage pakfire_package_unref(PakfirePackage pkg) { | |
119 | if (!pkg) | |
120 | return NULL; | |
121 | ||
122 | if (--pkg->nrefs > 0) | |
123 | return pkg; | |
124 | ||
125 | pakfire_package_free(pkg); | |
126 | return NULL; | |
127 | } | |
128 | ||
178a4506 MT |
129 | PAKFIRE_EXPORT Pakfire pakfire_package_get_pakfire(PakfirePackage pkg) { |
130 | return pakfire_ref(pkg->pakfire); | |
131 | } | |
132 | ||
221cc3ce | 133 | static Solvable* get_solvable(PakfirePackage pkg) { |
f1c7996e | 134 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce | 135 | |
f1c7996e | 136 | return pool_id2solvable(pool, pkg->id); |
221cc3ce MT |
137 | } |
138 | ||
139 | static Repo* pakfire_package_solv_repo(PakfirePackage pkg) { | |
140 | Solvable* s = get_solvable(pkg); | |
141 | ||
142 | return s->repo; | |
143 | } | |
144 | ||
145 | static Id pakfire_package_get_handle(PakfirePackage pkg) { | |
f1c7996e | 146 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
147 | Solvable* s = get_solvable(pkg); |
148 | ||
149 | return s - pool->solvables; | |
150 | } | |
151 | ||
9f953e68 | 152 | PAKFIRE_EXPORT int pakfire_package_identical(PakfirePackage pkg1, PakfirePackage pkg2) { |
221cc3ce MT |
153 | return pkg1->id == pkg2->id; |
154 | } | |
155 | ||
9f953e68 | 156 | PAKFIRE_EXPORT int pakfire_package_cmp(PakfirePackage pkg1, PakfirePackage pkg2) { |
f1c7996e | 157 | Pool* pool = pakfire_package_get_solv_pool(pkg1); |
221cc3ce MT |
158 | |
159 | Solvable* s1 = get_solvable(pkg1); | |
160 | Solvable* s2 = get_solvable(pkg2); | |
161 | ||
162 | // Check names | |
163 | const char* str1 = pool_id2str(pool, s1->name); | |
164 | const char* str2 = pool_id2str(pool, s2->name); | |
165 | ||
166 | int ret = strcmp(str1, str2); | |
167 | if (ret) | |
168 | return ret; | |
169 | ||
170 | // Check the version string | |
171 | ret = pakfire_package_evr_cmp(pkg1, pkg2); | |
172 | if (ret) | |
173 | return ret; | |
174 | ||
175 | // Check repositories | |
176 | PakfireRepo repo1 = pakfire_package_get_repo(pkg1); | |
177 | PakfireRepo repo2 = pakfire_package_get_repo(pkg2); | |
178 | ||
179 | if (repo1 && repo2) { | |
180 | ret = pakfire_repo_cmp(repo1, repo2); | |
3ff6aee6 | 181 | } |
221cc3ce | 182 | |
3ff6aee6 MT |
183 | pakfire_repo_unref(repo1); |
184 | pakfire_repo_unref(repo2); | |
221cc3ce | 185 | |
3ff6aee6 MT |
186 | if (ret) |
187 | return ret; | |
221cc3ce MT |
188 | |
189 | // Check package architectures | |
190 | str1 = pool_id2str(pool, s1->arch); | |
191 | str2 = pool_id2str(pool, s2->arch); | |
192 | ||
193 | return strcmp(str1, str2); | |
194 | } | |
195 | ||
9f953e68 | 196 | PAKFIRE_EXPORT int pakfire_package_evr_cmp(PakfirePackage pkg1, PakfirePackage pkg2) { |
f1c7996e | 197 | Pool* pool = pakfire_package_get_solv_pool(pkg1); |
221cc3ce MT |
198 | |
199 | Solvable* s1 = get_solvable(pkg1); | |
200 | Solvable* s2 = get_solvable(pkg2); | |
201 | ||
202 | return pool_evrcmp(pool, s1->evr, s2->evr, EVRCMP_COMPARE); | |
203 | } | |
204 | ||
9f953e68 | 205 | PAKFIRE_EXPORT Id pakfire_package_id(PakfirePackage pkg) { |
221cc3ce MT |
206 | return pkg->id; |
207 | } | |
208 | ||
9f953e68 | 209 | PAKFIRE_EXPORT char* pakfire_package_get_nevra(PakfirePackage pkg) { |
f1c7996e | 210 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
211 | Solvable* s = get_solvable(pkg); |
212 | ||
213 | const char* nevra = pool_solvable2str(pool, s); | |
214 | ||
5d901566 | 215 | return strdup(nevra); |
221cc3ce MT |
216 | } |
217 | ||
9f953e68 | 218 | PAKFIRE_EXPORT const char* pakfire_package_get_name(PakfirePackage pkg) { |
f1c7996e | 219 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
220 | Solvable* s = get_solvable(pkg); |
221 | ||
222 | return pool_id2str(pool, s->name); | |
223 | } | |
224 | ||
9f953e68 | 225 | PAKFIRE_EXPORT void pakfire_package_set_name(PakfirePackage pkg, const char* name) { |
f1c7996e | 226 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
227 | Solvable* s = get_solvable(pkg); |
228 | ||
229 | s->name = pool_str2id(pool, name, 1); | |
230 | } | |
231 | ||
9f953e68 | 232 | PAKFIRE_EXPORT const char* pakfire_package_get_evr(PakfirePackage pkg) { |
f1c7996e | 233 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
234 | Solvable* s = get_solvable(pkg); |
235 | ||
236 | return pool_id2str(pool, s->evr); | |
237 | } | |
238 | ||
9f953e68 | 239 | PAKFIRE_EXPORT void pakfire_package_set_evr(PakfirePackage pkg, const char* evr) { |
f1c7996e | 240 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
241 | Solvable* s = get_solvable(pkg); |
242 | ||
243 | s->evr = pool_str2id(pool, evr, 1); | |
244 | } | |
245 | ||
312fd26f MT |
246 | PAKFIRE_EXPORT char* pakfire_package_join_evr(const char* e, const char* v, const char* r) { |
247 | size_t l = strlen(v) + strlen(r) + 2; | |
248 | ||
249 | // Do not include epoch when it is zero | |
250 | if (strncmp(e, "0", strlen("0")) == 0) { | |
251 | e = NULL; | |
252 | } else { | |
253 | l += strlen(e); | |
254 | } | |
255 | ||
547759ae | 256 | char* buffer = malloc(l + 1); |
312fd26f | 257 | if (e) |
b4e07a2b | 258 | snprintf(buffer, l + 1, "%s:%s-%s", e, v, r); |
312fd26f | 259 | else |
b4e07a2b | 260 | snprintf(buffer, l + 1, "%s-%s", v, r); |
312fd26f MT |
261 | |
262 | return buffer; | |
263 | } | |
264 | ||
221cc3ce | 265 | static void split_evr(Pool* pool, const char* evr_c, char** epoch, char** version, char** release) { |
221cc3ce MT |
266 | char *e, *v, *r; |
267 | ||
464c8ecc MT |
268 | char* evr = pool_alloctmpspace(pool, strlen(evr_c) + 1); |
269 | strcpy(evr, evr_c); | |
270 | ||
221cc3ce MT |
271 | for (e = evr + 1; *e != ':' && *e != '-'; ++e) |
272 | ; | |
273 | ||
274 | if (*e == '-') { | |
275 | *e = '\0'; | |
276 | v = evr; | |
277 | r = e + 1; | |
278 | e = NULL; | |
279 | } else { /* *e == ':' */ | |
280 | *e = '\0'; | |
281 | v = e + 1; | |
282 | e = evr; | |
283 | for (r = v + 1; *r != '-'; ++r) | |
284 | ; | |
285 | *r = '\0'; | |
286 | r++; | |
287 | } | |
288 | ||
289 | *epoch = e; | |
290 | *version = v; | |
291 | *release = r; | |
292 | } | |
293 | ||
9f953e68 | 294 | PAKFIRE_EXPORT unsigned long pakfire_package_get_epoch(PakfirePackage pkg) { |
f1c7996e | 295 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
296 | char *e, *v, *r, *endptr; |
297 | ||
298 | unsigned long epoch = 0; | |
299 | ||
300 | split_evr(pool, pakfire_package_get_evr(pkg), &e, &v, &r); | |
301 | ||
302 | if (e) { | |
303 | long int converted = strtol(e, &endptr, 10); | |
221cc3ce MT |
304 | epoch = converted; |
305 | } | |
306 | ||
307 | return epoch; | |
308 | } | |
309 | ||
9f953e68 | 310 | PAKFIRE_EXPORT const char* pakfire_package_get_version(PakfirePackage pkg) { |
f1c7996e | 311 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
312 | char *e, *v, *r; |
313 | ||
314 | split_evr(pool, pakfire_package_get_evr(pkg), &e, &v, &r); | |
5d901566 | 315 | return strdup(v); |
221cc3ce MT |
316 | } |
317 | ||
9f953e68 | 318 | PAKFIRE_EXPORT const char* pakfire_package_get_release(PakfirePackage pkg) { |
f1c7996e | 319 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
320 | char *e, *v, *r; |
321 | ||
322 | split_evr(pool, pakfire_package_get_evr(pkg), &e, &v, &r); | |
5d901566 | 323 | return strdup(r); |
221cc3ce MT |
324 | } |
325 | ||
9f953e68 | 326 | PAKFIRE_EXPORT const char* pakfire_package_get_arch(PakfirePackage pkg) { |
f1c7996e | 327 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
328 | Solvable* s = get_solvable(pkg); |
329 | ||
330 | return pool_id2str(pool, s->arch); | |
331 | } | |
332 | ||
9f953e68 | 333 | PAKFIRE_EXPORT void pakfire_package_set_arch(PakfirePackage pkg, const char* arch) { |
f1c7996e | 334 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
335 | Solvable* s = get_solvable(pkg); |
336 | ||
337 | s->arch = pool_str2id(pool, arch, 1); | |
338 | } | |
339 | ||
340 | static void pakfire_package_internalize_repo(PakfirePackage pkg) { | |
341 | PakfireRepo repo = pakfire_package_get_repo(pkg); | |
342 | if (repo) { | |
343 | pakfire_repo_internalize(repo); | |
3ff6aee6 | 344 | pakfire_repo_unref(repo); |
221cc3ce MT |
345 | } |
346 | } | |
347 | ||
348 | static const char* pakfire_package_get_string(PakfirePackage pkg, int key) { | |
349 | pakfire_package_internalize_repo(pkg); | |
350 | ||
351 | Solvable* s = get_solvable(pkg); | |
352 | const char* str = solvable_lookup_str(s, key); | |
353 | ||
354 | if (!str) | |
355 | return NULL; | |
356 | ||
357 | if (strlen(str) == 0) | |
358 | return NULL; | |
359 | ||
360 | return str; | |
361 | } | |
362 | ||
363 | static void pakfire_package_set_string(PakfirePackage pkg, int key, const char* value) { | |
364 | Solvable* s = get_solvable(pkg); | |
365 | ||
366 | if (!value) | |
367 | value = ""; | |
368 | ||
7ccc9d74 MT |
369 | solvable_set_poolstr(s, key, value); |
370 | } | |
371 | ||
372 | static void pakfire_package_add_string_array(PakfirePackage pkg, int key, const char* value) { | |
373 | Solvable* s = get_solvable(pkg); | |
374 | ||
375 | if (!value) | |
376 | return; | |
377 | ||
378 | solvable_add_poolstr_array(s, key, value); | |
221cc3ce MT |
379 | } |
380 | ||
9f953e68 | 381 | PAKFIRE_EXPORT const char* pakfire_package_get_uuid(PakfirePackage pkg) { |
221cc3ce MT |
382 | return pakfire_package_get_string(pkg, SOLVABLE_PKGID); |
383 | } | |
384 | ||
9f953e68 | 385 | PAKFIRE_EXPORT void pakfire_package_set_uuid(PakfirePackage pkg, const char* uuid) { |
221cc3ce MT |
386 | pakfire_package_set_string(pkg, SOLVABLE_PKGID, uuid); |
387 | } | |
388 | ||
9f953e68 | 389 | PAKFIRE_EXPORT const char* pakfire_package_get_checksum(PakfirePackage pkg) { |
221cc3ce MT |
390 | return pakfire_package_get_string(pkg, SOLVABLE_CHECKSUM); |
391 | } | |
392 | ||
9f953e68 | 393 | PAKFIRE_EXPORT void pakfire_package_set_checksum(PakfirePackage pkg, const char* checksum) { |
221cc3ce MT |
394 | pakfire_package_set_string(pkg, SOLVABLE_CHECKSUM, checksum); |
395 | } | |
396 | ||
9f953e68 | 397 | PAKFIRE_EXPORT const char* pakfire_package_get_summary(PakfirePackage pkg) { |
221cc3ce MT |
398 | return pakfire_package_get_string(pkg, SOLVABLE_SUMMARY); |
399 | } | |
400 | ||
9f953e68 | 401 | PAKFIRE_EXPORT void pakfire_package_set_summary(PakfirePackage pkg, const char* summary) { |
221cc3ce MT |
402 | pakfire_package_set_string(pkg, SOLVABLE_SUMMARY, summary); |
403 | } | |
404 | ||
9f953e68 | 405 | PAKFIRE_EXPORT const char* pakfire_package_get_description(PakfirePackage pkg) { |
221cc3ce MT |
406 | return pakfire_package_get_string(pkg, SOLVABLE_DESCRIPTION); |
407 | } | |
408 | ||
9f953e68 | 409 | PAKFIRE_EXPORT void pakfire_package_set_description(PakfirePackage pkg, const char* description) { |
221cc3ce MT |
410 | pakfire_package_set_string(pkg, SOLVABLE_DESCRIPTION, description); |
411 | } | |
412 | ||
9f953e68 | 413 | PAKFIRE_EXPORT const char* pakfire_package_get_license(PakfirePackage pkg) { |
221cc3ce MT |
414 | return pakfire_package_get_string(pkg, SOLVABLE_LICENSE); |
415 | } | |
416 | ||
9f953e68 | 417 | PAKFIRE_EXPORT void pakfire_package_set_license(PakfirePackage pkg, const char* license) { |
221cc3ce MT |
418 | pakfire_package_set_string(pkg, SOLVABLE_LICENSE, license); |
419 | } | |
420 | ||
9f953e68 | 421 | PAKFIRE_EXPORT const char* pakfire_package_get_url(PakfirePackage pkg) { |
221cc3ce MT |
422 | return pakfire_package_get_string(pkg, SOLVABLE_URL); |
423 | } | |
424 | ||
9f953e68 | 425 | PAKFIRE_EXPORT void pakfire_package_set_url(PakfirePackage pkg, const char* url) { |
221cc3ce MT |
426 | pakfire_package_set_string(pkg, SOLVABLE_URL, url); |
427 | } | |
428 | ||
7ccc9d74 MT |
429 | PAKFIRE_EXPORT const char* pakfire_package_get_groups(PakfirePackage pkg) { |
430 | return pakfire_package_get_string(pkg, SOLVABLE_GROUP); | |
221cc3ce MT |
431 | } |
432 | ||
7ccc9d74 MT |
433 | PAKFIRE_EXPORT void pakfire_package_set_groups(PakfirePackage pkg, const char* groups) { |
434 | char** list = pakfire_split_string(groups, ' '); | |
221cc3ce | 435 | |
7ccc9d74 MT |
436 | while (list && *list) { |
437 | pakfire_package_add_string_array(pkg, SOLVABLE_GROUP, *list); | |
438 | list++; | |
2141b578 | 439 | } |
8ad0a954 MT |
440 | } |
441 | ||
9f953e68 | 442 | PAKFIRE_EXPORT const char* pakfire_package_get_vendor(PakfirePackage pkg) { |
221cc3ce MT |
443 | return pakfire_package_get_string(pkg, SOLVABLE_VENDOR); |
444 | } | |
445 | ||
9f953e68 | 446 | PAKFIRE_EXPORT void pakfire_package_set_vendor(PakfirePackage pkg, const char* vendor) { |
221cc3ce MT |
447 | pakfire_package_set_string(pkg, SOLVABLE_VENDOR, vendor); |
448 | } | |
449 | ||
9f953e68 | 450 | PAKFIRE_EXPORT const char* pakfire_package_get_maintainer(PakfirePackage pkg) { |
221cc3ce MT |
451 | return pakfire_package_get_string(pkg, SOLVABLE_PACKAGER); |
452 | } | |
453 | ||
9f953e68 | 454 | PAKFIRE_EXPORT void pakfire_package_set_maintainer(PakfirePackage pkg, const char* maintainer) { |
221cc3ce MT |
455 | pakfire_package_set_string(pkg, SOLVABLE_PACKAGER, maintainer); |
456 | } | |
457 | ||
9f953e68 | 458 | PAKFIRE_EXPORT const char* pakfire_package_get_filename(PakfirePackage pkg) { |
221cc3ce MT |
459 | return pakfire_package_get_string(pkg, SOLVABLE_MEDIAFILE); |
460 | } | |
461 | ||
9f953e68 | 462 | PAKFIRE_EXPORT void pakfire_package_set_filename(PakfirePackage pkg, const char* filename) { |
221cc3ce MT |
463 | pakfire_package_set_string(pkg, SOLVABLE_MEDIAFILE, filename); |
464 | } | |
465 | ||
9f953e68 | 466 | PAKFIRE_EXPORT int pakfire_package_is_installed(PakfirePackage pkg) { |
f1c7996e | 467 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
468 | Solvable* s = get_solvable(pkg); |
469 | ||
470 | return pool->installed == s->repo; | |
471 | } | |
472 | ||
473 | static unsigned long long pakfire_package_get_num(PakfirePackage pkg, Id type) { | |
474 | pakfire_package_internalize_repo(pkg); | |
475 | ||
476 | Solvable* s = get_solvable(pkg); | |
477 | return solvable_lookup_num(s, type, 0); | |
478 | } | |
479 | ||
480 | static void pakfire_package_set_num(PakfirePackage pkg, Id type, unsigned long long value) { | |
481 | Solvable* s = get_solvable(pkg); | |
482 | ||
483 | solvable_set_num(s, type, value); | |
484 | } | |
485 | ||
9f953e68 | 486 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_downloadsize(PakfirePackage pkg) { |
221cc3ce MT |
487 | return pakfire_package_get_num(pkg, SOLVABLE_DOWNLOADSIZE); |
488 | } | |
489 | ||
9f953e68 | 490 | PAKFIRE_EXPORT void pakfire_package_set_downloadsize(PakfirePackage pkg, unsigned long long downloadsize) { |
221cc3ce MT |
491 | return pakfire_package_set_num(pkg, SOLVABLE_DOWNLOADSIZE, downloadsize); |
492 | } | |
493 | ||
9f953e68 | 494 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_installsize(PakfirePackage pkg) { |
221cc3ce MT |
495 | return pakfire_package_get_num(pkg, SOLVABLE_INSTALLSIZE); |
496 | } | |
497 | ||
9f953e68 | 498 | PAKFIRE_EXPORT void pakfire_package_set_installsize(PakfirePackage pkg, unsigned long long installsize) { |
221cc3ce MT |
499 | return pakfire_package_set_num(pkg, SOLVABLE_INSTALLSIZE, installsize); |
500 | } | |
501 | ||
9f953e68 | 502 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_size(PakfirePackage pkg) { |
221cc3ce MT |
503 | if (pakfire_package_is_installed(pkg)) |
504 | return pakfire_package_get_installsize(pkg); | |
505 | ||
506 | return pakfire_package_get_downloadsize(pkg); | |
507 | } | |
508 | ||
2ffc704d | 509 | PAKFIRE_EXPORT const char* pakfire_package_get_build_host(PakfirePackage pkg) { |
221cc3ce MT |
510 | return pakfire_package_get_string(pkg, SOLVABLE_BUILDHOST); |
511 | } | |
512 | ||
2ffc704d MT |
513 | PAKFIRE_EXPORT void pakfire_package_set_build_host(PakfirePackage pkg, const char* build_host) { |
514 | pakfire_package_set_string(pkg, SOLVABLE_BUILDHOST, build_host); | |
221cc3ce MT |
515 | } |
516 | ||
2ffc704d | 517 | PAKFIRE_EXPORT time_t pakfire_package_get_build_time(PakfirePackage pkg) { |
221cc3ce MT |
518 | return pakfire_package_get_num(pkg, SOLVABLE_BUILDTIME); |
519 | } | |
520 | ||
2ffc704d MT |
521 | PAKFIRE_EXPORT void pakfire_package_set_build_time(PakfirePackage pkg, time_t build_time) { |
522 | pakfire_package_set_num(pkg, SOLVABLE_BUILDTIME, build_time); | |
221cc3ce MT |
523 | } |
524 | ||
af647c55 | 525 | PAKFIRE_EXPORT time_t pakfire_package_get_install_time(PakfirePackage pkg) { |
221cc3ce MT |
526 | return pakfire_package_get_num(pkg, SOLVABLE_INSTALLTIME); |
527 | } | |
528 | ||
af647c55 MT |
529 | PAKFIRE_EXPORT void pakfire_package_set_install_time(PakfirePackage pkg, time_t install_time) { |
530 | pakfire_package_set_num(pkg, SOLVABLE_INSTALLTIME, install_time); | |
531 | } | |
532 | ||
a9659851 MT |
533 | static PakfireRelationList pakfire_package_get_relationlist( |
534 | PakfirePackage pkg, Id type, Id marker) { | |
221cc3ce MT |
535 | Queue q; |
536 | queue_init(&q); | |
537 | ||
538 | Solvable* s = get_solvable(pkg); | |
a9659851 | 539 | solvable_lookup_deparray(s, type, &q, marker); |
221cc3ce | 540 | |
d6ea32df MT |
541 | PakfireRelationList list; |
542 | int r = pakfire_relationlist_from_queue(&list, pkg->pakfire, q); | |
543 | ||
221cc3ce MT |
544 | queue_free(&q); |
545 | ||
d6ea32df MT |
546 | if (r) |
547 | return NULL; | |
548 | ||
549 | return list; | |
221cc3ce MT |
550 | } |
551 | ||
a9659851 MT |
552 | static void pakfire_package_set_relationlist(PakfirePackage pkg, Id type, |
553 | PakfireRelationList relationlist, Id marker) { | |
221cc3ce | 554 | Solvable* s = get_solvable(pkg); |
97eb9a58 | 555 | Queue* q = pakfire_relationlist_get_queue(relationlist); |
221cc3ce | 556 | |
97eb9a58 MT |
557 | #if 0 |
558 | // This implemention should be the fastest, but unfortunately does not work. | |
a9659851 | 559 | solvable_set_deparray(s, type, q, marker); |
97eb9a58 MT |
560 | #else |
561 | for (int i = 0; i < q->count; i++) | |
a9659851 | 562 | solvable_add_deparray(s, type, q->elements[i], marker); |
221cc3ce | 563 | #endif |
221cc3ce MT |
564 | } |
565 | ||
a9659851 MT |
566 | static void pakfire_package_add_relation(PakfirePackage pkg, Id type, |
567 | PakfireRelation relation, Id marker) { | |
221cc3ce MT |
568 | Solvable* s = get_solvable(pkg); |
569 | ||
a9659851 | 570 | solvable_add_deparray(s, type, pakfire_relation_get_id(relation), marker); |
221cc3ce MT |
571 | } |
572 | ||
9f953e68 | 573 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_provides(PakfirePackage pkg) { |
063ca10f | 574 | return pakfire_package_get_relationlist(pkg, SOLVABLE_PROVIDES, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
575 | } |
576 | ||
9f953e68 | 577 | PAKFIRE_EXPORT void pakfire_package_set_provides(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 578 | pakfire_package_set_relationlist(pkg, SOLVABLE_PROVIDES, relationlist, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
579 | } |
580 | ||
9f953e68 | 581 | PAKFIRE_EXPORT void pakfire_package_add_provides(PakfirePackage pkg, PakfireRelation relation) { |
063ca10f | 582 | pakfire_package_add_relation(pkg, SOLVABLE_PROVIDES, relation, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
583 | } |
584 | ||
9f953e68 | 585 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_prerequires(PakfirePackage pkg) { |
063ca10f | 586 | return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, SOLVABLE_PREREQMARKER); |
221cc3ce MT |
587 | } |
588 | ||
dec8207e | 589 | PAKFIRE_EXPORT void pakfire_package_set_prerequires(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 590 | pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, SOLVABLE_PREREQMARKER); |
dec8207e MT |
591 | } |
592 | ||
9f953e68 | 593 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_requires(PakfirePackage pkg) { |
063ca10f | 594 | return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
595 | } |
596 | ||
9f953e68 | 597 | PAKFIRE_EXPORT void pakfire_package_set_requires(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 598 | pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
599 | } |
600 | ||
9f953e68 | 601 | PAKFIRE_EXPORT void pakfire_package_add_requires(PakfirePackage pkg, PakfireRelation relation) { |
063ca10f | 602 | pakfire_package_add_relation(pkg, SOLVABLE_REQUIRES, relation, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
603 | } |
604 | ||
9f953e68 | 605 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_conflicts(PakfirePackage pkg) { |
a9659851 | 606 | return pakfire_package_get_relationlist(pkg, SOLVABLE_CONFLICTS, 0); |
221cc3ce MT |
607 | } |
608 | ||
9f953e68 | 609 | PAKFIRE_EXPORT void pakfire_package_set_conflicts(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 610 | pakfire_package_set_relationlist(pkg, SOLVABLE_CONFLICTS, relationlist, 0); |
221cc3ce MT |
611 | } |
612 | ||
9f953e68 | 613 | PAKFIRE_EXPORT void pakfire_package_add_conflicts(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 614 | pakfire_package_add_relation(pkg, SOLVABLE_CONFLICTS, relation, 0); |
221cc3ce MT |
615 | } |
616 | ||
9f953e68 | 617 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_obsoletes(PakfirePackage pkg) { |
a9659851 | 618 | return pakfire_package_get_relationlist(pkg, SOLVABLE_OBSOLETES, 0); |
221cc3ce MT |
619 | } |
620 | ||
9f953e68 | 621 | PAKFIRE_EXPORT void pakfire_package_set_obsoletes(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 622 | pakfire_package_set_relationlist(pkg, SOLVABLE_OBSOLETES, relationlist, 0); |
221cc3ce MT |
623 | } |
624 | ||
9f953e68 | 625 | PAKFIRE_EXPORT void pakfire_package_add_obsoletes(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 626 | pakfire_package_add_relation(pkg, SOLVABLE_OBSOLETES, relation, 0); |
221cc3ce MT |
627 | } |
628 | ||
9f953e68 | 629 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_recommends(PakfirePackage pkg) { |
a9659851 | 630 | return pakfire_package_get_relationlist(pkg, SOLVABLE_RECOMMENDS, 0); |
221cc3ce MT |
631 | } |
632 | ||
9f953e68 | 633 | PAKFIRE_EXPORT void pakfire_package_set_recommends(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 634 | pakfire_package_set_relationlist(pkg, SOLVABLE_RECOMMENDS, relationlist, 0); |
221cc3ce MT |
635 | } |
636 | ||
9f953e68 | 637 | PAKFIRE_EXPORT void pakfire_package_add_recommends(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 638 | pakfire_package_add_relation(pkg, SOLVABLE_RECOMMENDS, relation, 0); |
221cc3ce MT |
639 | } |
640 | ||
9f953e68 | 641 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_suggests(PakfirePackage pkg) { |
a9659851 | 642 | return pakfire_package_get_relationlist(pkg, SOLVABLE_SUGGESTS, 0); |
221cc3ce MT |
643 | } |
644 | ||
9f953e68 | 645 | PAKFIRE_EXPORT void pakfire_package_set_suggests(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 646 | pakfire_package_set_relationlist(pkg, SOLVABLE_SUGGESTS, relationlist, 0); |
221cc3ce MT |
647 | } |
648 | ||
9f953e68 | 649 | PAKFIRE_EXPORT void pakfire_package_add_suggests(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 650 | pakfire_package_add_relation(pkg, SOLVABLE_SUGGESTS, relation, 0); |
221cc3ce MT |
651 | } |
652 | ||
8fe2e4ba MT |
653 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_supplements(PakfirePackage pkg) { |
654 | return pakfire_package_get_relationlist(pkg, SOLVABLE_SUPPLEMENTS, 0); | |
655 | } | |
656 | ||
657 | PAKFIRE_EXPORT void pakfire_package_set_supplements(PakfirePackage pkg, PakfireRelationList relationlist) { | |
658 | pakfire_package_set_relationlist(pkg, SOLVABLE_SUPPLEMENTS, relationlist, 0); | |
659 | } | |
660 | ||
661 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_enhances(PakfirePackage pkg) { | |
662 | return pakfire_package_get_relationlist(pkg, SOLVABLE_ENHANCES, 0); | |
663 | } | |
664 | ||
665 | PAKFIRE_EXPORT void pakfire_package_set_enhances(PakfirePackage pkg, PakfireRelationList relationlist) { | |
666 | pakfire_package_set_relationlist(pkg, SOLVABLE_ENHANCES, relationlist, 0); | |
667 | } | |
668 | ||
4a81ff85 MT |
669 | /* |
670 | Fast-path function to check if a package is in a certain repository | |
671 | */ | |
672 | int pakfire_package_is_in_repo(PakfirePackage pkg, Repo* repo) { | |
673 | Solvable* s = get_solvable(pkg); | |
674 | ||
675 | return s->repo == repo; | |
676 | } | |
677 | ||
9f953e68 | 678 | PAKFIRE_EXPORT PakfireRepo pakfire_package_get_repo(PakfirePackage pkg) { |
e9d9eadc MT |
679 | if (!pkg->repo) { |
680 | Solvable* s = get_solvable(pkg); | |
681 | ||
682 | pkg->repo = pakfire_repo_create_from_repo(pkg->pakfire, s->repo); | |
683 | } | |
221cc3ce | 684 | |
e9d9eadc | 685 | return pakfire_repo_ref(pkg->repo); |
221cc3ce MT |
686 | } |
687 | ||
9f953e68 | 688 | PAKFIRE_EXPORT void pakfire_package_set_repo(PakfirePackage pkg, PakfireRepo repo) { |
e9d9eadc MT |
689 | // Free old repository |
690 | if (pkg->repo) | |
691 | pakfire_repo_unref(pkg->repo); | |
221cc3ce | 692 | |
e9d9eadc | 693 | Solvable* s = get_solvable(pkg); |
3ff6aee6 | 694 | s->repo = pakfire_repo_get_repo(repo); |
e9d9eadc MT |
695 | |
696 | pkg->repo = pakfire_repo_ref(repo); | |
221cc3ce MT |
697 | } |
698 | ||
9f953e68 | 699 | PAKFIRE_EXPORT char* pakfire_package_get_location(PakfirePackage pkg) { |
221cc3ce MT |
700 | pakfire_package_internalize_repo(pkg); |
701 | ||
702 | Solvable* s = get_solvable(pkg); | |
703 | ||
704 | const char* location = solvable_get_location(s, NULL); | |
5d901566 | 705 | return strdup(location); |
221cc3ce MT |
706 | } |
707 | ||
708 | static void pakfire_package_dump_add_line(char** str, const char* key, const char* val) { | |
709 | if (val) | |
710 | asprintf(str, "%s%-15s: %s\n", *str, key ? key : "", val); | |
711 | } | |
712 | ||
713 | static void pakfire_package_dump_add_lines(char** str, const char* key, const char* val) { | |
7500066c MT |
714 | char** lines = pakfire_split_string(val, '\n'); |
715 | if (!lines) | |
716 | return; | |
221cc3ce | 717 | |
7500066c MT |
718 | while (*lines) { |
719 | pakfire_package_dump_add_line(str, key, *lines); | |
720 | lines++; | |
221cc3ce MT |
721 | } |
722 | } | |
723 | ||
724 | static void pakfire_package_dump_add_line_date(char** str, const char* key, unsigned long long date) { | |
725 | // Convert from integer to tm struct. | |
726 | struct tm* timer = gmtime((time_t *)&date); | |
727 | ||
7500066c MT |
728 | char val[128]; |
729 | strftime(val, sizeof(val) - 1, "%a, %d %b %Y %T %z", timer); | |
221cc3ce MT |
730 | |
731 | pakfire_package_dump_add_line(str, key, val); | |
732 | } | |
733 | ||
734 | static void pakfire_package_dump_add_line_relations(char** str, const char* key, PakfireRelationList deps) { | |
7ac9a26b MT |
735 | pakfire_relationlist_sort(deps); |
736 | ||
b76d7b47 MT |
737 | size_t size = pakfire_relationlist_size(deps); |
738 | for (unsigned int i = 0; i < size; i++) { | |
739 | PakfireRelation relation = pakfire_relationlist_get(deps, i); | |
221cc3ce MT |
740 | |
741 | if (relation) { | |
742 | char* dep = pakfire_relation_str(relation); | |
1a497776 | 743 | pakfire_relation_unref(relation); |
221cc3ce | 744 | |
221cc3ce MT |
745 | if (dep) { |
746 | pakfire_package_dump_add_line(str, (i == 0) ? key : "", dep); | |
f0d6233d | 747 | free(dep); |
221cc3ce MT |
748 | } |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | static void pakfire_package_dump_add_line_size(char** str, const char* key, unsigned long long size) { | |
754 | char* val = pakfire_format_size(size); | |
755 | ||
756 | if (val) { | |
757 | pakfire_package_dump_add_line(str, key, val); | |
f0d6233d | 758 | free(val); |
221cc3ce MT |
759 | } |
760 | } | |
761 | ||
9f953e68 | 762 | PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) { |
221cc3ce MT |
763 | char* string = ""; |
764 | ||
765 | // Name | |
766 | const char* name = pakfire_package_get_name(pkg); | |
767 | pakfire_package_dump_add_line(&string, _("Name"), name); | |
768 | ||
769 | // Version | |
770 | const char* version = pakfire_package_get_version(pkg); | |
771 | pakfire_package_dump_add_line(&string, _("Version"), version); | |
772 | ||
773 | // Release | |
774 | const char* release = pakfire_package_get_release(pkg); | |
775 | pakfire_package_dump_add_line(&string, _("Release"), release); | |
776 | ||
777 | // Size | |
778 | unsigned long long size = pakfire_package_get_size(pkg); | |
779 | pakfire_package_dump_add_line_size(&string, _("Size"), size); | |
780 | ||
781 | // Installed size | |
782 | if (pakfire_package_is_installed(pkg)) { | |
783 | unsigned long long installsize = pakfire_package_get_installsize(pkg); | |
784 | pakfire_package_dump_add_line_size(&string, _("Installed size"), installsize); | |
785 | ||
786 | // Downloadsize | |
787 | } else { | |
788 | unsigned long long downloadsize = pakfire_package_get_downloadsize(pkg); | |
789 | pakfire_package_dump_add_line_size(&string, _("Download size"), downloadsize); | |
790 | } | |
791 | ||
792 | PakfireRepo repo = pakfire_package_get_repo(pkg); | |
793 | if (repo) { | |
794 | const char* repo_name = pakfire_repo_get_name(repo); | |
795 | pakfire_package_dump_add_line(&string, _("Repo"), repo_name); | |
796 | ||
3ff6aee6 | 797 | pakfire_repo_unref(repo); |
221cc3ce MT |
798 | } |
799 | ||
800 | // Summary | |
801 | const char* summary = pakfire_package_get_summary(pkg); | |
802 | pakfire_package_dump_add_line(&string, _("Summary"), summary); | |
803 | ||
804 | // Description | |
805 | const char* description = pakfire_package_get_description(pkg); | |
1b99c676 MT |
806 | if (description) |
807 | pakfire_package_dump_add_lines(&string, _("Description"), description); | |
221cc3ce MT |
808 | |
809 | // Groups | |
7ccc9d74 | 810 | const char* groups = pakfire_package_get_groups(pkg); |
2141b578 | 811 | if (groups) { |
7ccc9d74 | 812 | pakfire_package_dump_add_lines(&string, _("Groups"), groups); |
2141b578 | 813 | } |
221cc3ce MT |
814 | |
815 | // URL | |
816 | const char* url = pakfire_package_get_url(pkg); | |
817 | pakfire_package_dump_add_line(&string, _("URL"), url); | |
818 | ||
819 | // License | |
820 | const char* license = pakfire_package_get_license(pkg); | |
821 | pakfire_package_dump_add_line(&string, _("License"), license); | |
822 | ||
823 | if (flags & PAKFIRE_PKG_DUMP_LONG) { | |
af647c55 MT |
824 | // Install Time |
825 | time_t install_time = pakfire_package_get_install_time(pkg); | |
826 | if (install_time) { | |
827 | pakfire_package_dump_add_line_date(&string, _("Install Time"), install_time); | |
828 | } | |
829 | ||
221cc3ce MT |
830 | // Maintainer |
831 | const char* maintainer = pakfire_package_get_maintainer(pkg); | |
832 | pakfire_package_dump_add_line(&string, _("Maintainer"), maintainer); | |
833 | ||
834 | // Vendor | |
835 | const char* vendor = pakfire_package_get_vendor(pkg); | |
836 | pakfire_package_dump_add_line(&string, _("Vendor"), vendor); | |
837 | ||
838 | // UUID | |
839 | const char* uuid = pakfire_package_get_uuid(pkg); | |
840 | pakfire_package_dump_add_line(&string, _("UUID"), uuid); | |
841 | ||
842 | // Build time | |
2ffc704d MT |
843 | time_t build_time = pakfire_package_get_build_time(pkg); |
844 | pakfire_package_dump_add_line_date(&string, _("Build Time"), build_time); | |
221cc3ce MT |
845 | |
846 | // Build host | |
2ffc704d MT |
847 | const char* build_host = pakfire_package_get_build_host(pkg); |
848 | pakfire_package_dump_add_line(&string, _("Build Host"), build_host); | |
221cc3ce | 849 | |
2f873877 MT |
850 | // Dependencies |
851 | ||
852 | const struct relation { | |
853 | const char* name; | |
854 | PakfireRelationList (*get)(PakfirePackage pkg); | |
855 | } relations[] = { | |
856 | { _("Provides"), pakfire_package_get_provides, }, | |
857 | { _("Pre-Requires"), pakfire_package_get_prerequires, }, | |
858 | { _("Requires"), pakfire_package_get_requires, }, | |
859 | { _("Conflicts"), pakfire_package_get_conflicts, }, | |
860 | { _("Obsoletes"), pakfire_package_get_obsoletes, }, | |
861 | { _("Recommends"), pakfire_package_get_recommends, }, | |
862 | { _("Suggests"), pakfire_package_get_suggests, }, | |
8fe2e4ba MT |
863 | { _("Supplements"), pakfire_package_get_supplements, }, |
864 | { _("Enhances"), pakfire_package_get_enhances, }, | |
2f873877 MT |
865 | { NULL, NULL}, |
866 | }; | |
867 | ||
868 | for (const struct relation* relation = relations; relation->name; relation++) { | |
869 | PakfireRelationList list = relation->get(pkg); | |
870 | if (list) { | |
871 | pakfire_package_dump_add_line_relations(&string, relation->name, list); | |
872 | pakfire_relationlist_unref(list); | |
873 | } | |
221cc3ce MT |
874 | } |
875 | } | |
876 | ||
877 | if (flags & PAKFIRE_PKG_DUMP_FILELIST) { | |
5e9463ec MT |
878 | PakfireFilelist filelist = pakfire_package_get_filelist(pkg); |
879 | ||
880 | const char* prefix = _("Filelist"); | |
881 | ||
882 | for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) { | |
883 | PakfireFile file = pakfire_filelist_get(filelist, i); | |
221cc3ce | 884 | |
32485f6c MT |
885 | const char* path = pakfire_file_get_path(file); |
886 | pakfire_package_dump_add_line(&string, prefix, path); | |
221cc3ce | 887 | |
5e9463ec | 888 | pakfire_file_unref(file); |
221cc3ce | 889 | |
5e9463ec | 890 | // Only prefix the first line |
221cc3ce MT |
891 | prefix = NULL; |
892 | } | |
5e9463ec MT |
893 | |
894 | pakfire_filelist_unref(filelist); | |
221cc3ce MT |
895 | } |
896 | ||
897 | return string; | |
898 | } | |
899 | ||
9f953e68 | 900 | PAKFIRE_EXPORT int pakfire_package_is_cached(PakfirePackage pkg) { |
3f7bcbc4 | 901 | char* path = pakfire_package_get_cache_path(pkg); |
cbed1552 | 902 | |
3f7bcbc4 | 903 | // Check if the file is readable |
9f812185 | 904 | int r = access(path, R_OK); |
f0d6233d | 905 | free(path); |
221cc3ce | 906 | |
cf27dd9c | 907 | return (r == 0); |
221cc3ce MT |
908 | } |
909 | ||
9f953e68 | 910 | PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) { |
4a81ff85 | 911 | char path[PATH_MAX]; |
221cc3ce | 912 | |
3f7bcbc4 MT |
913 | const char* filename = pakfire_package_get_filename(pkg); |
914 | const char* checksum = pakfire_package_get_checksum(pkg); | |
221cc3ce | 915 | |
3f7bcbc4 | 916 | if (strlen(checksum) < 3) |
221cc3ce MT |
917 | return NULL; |
918 | ||
6d0f0836 MT |
919 | pakfire_make_cache_path(pkg->pakfire, path, sizeof(path) - 1, |
920 | "%c%c/%s/%s", checksum[0], checksum[1], checksum + 2, filename); | |
221cc3ce | 921 | |
6d0f0836 | 922 | return strdup(path); |
221cc3ce MT |
923 | } |
924 | ||
cbed1552 | 925 | PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) { |
cbed1552 | 926 | // Otherwise open the archive from the cache |
3f7bcbc4 | 927 | char* path = pakfire_package_get_cache_path(pkg); |
641cab18 MT |
928 | if (!path) |
929 | return NULL; | |
cbed1552 | 930 | |
cf3e773a MT |
931 | PakfireArchive archive = NULL; |
932 | ||
641cab18 | 933 | // Open archive |
cf3e773a | 934 | int r = pakfire_archive_open(&archive, pkg->pakfire, path); |
f0d6233d | 935 | free(path); |
cbed1552 | 936 | |
cf3e773a MT |
937 | if (r == 0) |
938 | return archive; | |
939 | ||
940 | return NULL; | |
cbed1552 MT |
941 | } |
942 | ||
f4560c3f | 943 | static int pakfire_package_fetch_legacy_filelist(PakfirePackage pkg, PakfireFilelist filelist) { |
221cc3ce MT |
944 | pakfire_package_internalize_repo(pkg); |
945 | ||
221cc3ce MT |
946 | PakfireRepo repo = pakfire_package_get_repo(pkg); |
947 | Solvable* s = get_solvable(pkg); | |
f1c7996e | 948 | Pool* p = pakfire_package_get_solv_pool(pkg); |
3ff6aee6 | 949 | Repo* r = pakfire_repo_get_repo(repo); |
5e9463ec | 950 | pakfire_repo_unref(repo); |
221cc3ce MT |
951 | |
952 | int found_marker = 0; | |
953 | ||
954 | Id id, *ids; | |
955 | ids = r->idarraydata + s->provides; | |
956 | while((id = *ids++) != 0) { | |
32485f6c | 957 | const char* path = pool_dep2str(p, id); |
221cc3ce MT |
958 | |
959 | if (found_marker) { | |
5e9463ec | 960 | PakfireFile file; |
221cc3ce | 961 | |
883b3be9 | 962 | int r = pakfire_file_create(&file, pkg->pakfire); |
5e9463ec MT |
963 | if (r) |
964 | return r; | |
965 | ||
32485f6c MT |
966 | // Set path |
967 | pakfire_file_set_path(file, path); | |
5e9463ec | 968 | |
f4560c3f | 969 | r = pakfire_filelist_append(filelist, file); |
5e9463ec MT |
970 | if (r) |
971 | return r; | |
972 | ||
973 | pakfire_file_unref(file); | |
221cc3ce MT |
974 | continue; |
975 | } | |
976 | ||
32485f6c | 977 | if (strcmp(path, PAKFIRE_SOLVABLE_FILEMARKER) == 0) |
221cc3ce MT |
978 | ++found_marker; |
979 | } | |
980 | ||
5e9463ec | 981 | return 0; |
221cc3ce MT |
982 | } |
983 | ||
f4560c3f | 984 | static int pakfire_package_fetch_filelist(PakfirePackage pkg, PakfireFilelist filelist) { |
5e9463ec MT |
985 | int r; |
986 | ||
221cc3ce MT |
987 | pakfire_package_internalize_repo(pkg); |
988 | ||
f1c7996e | 989 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
990 | Repo* repo = pakfire_package_solv_repo(pkg); |
991 | Id handle = pakfire_package_get_handle(pkg); | |
992 | ||
993 | Dataiterator di; | |
994 | dataiterator_init(&di, pool, repo, handle, | |
995 | SOLVABLE_FILELIST, NULL, SEARCH_FILES | SEARCH_COMPLETE_FILELIST); | |
5e9463ec | 996 | |
221cc3ce | 997 | while (dataiterator_step(&di)) { |
5e9463ec | 998 | PakfireFile file; |
221cc3ce | 999 | |
883b3be9 | 1000 | r = pakfire_file_create(&file, pkg->pakfire); |
5e9463ec MT |
1001 | if (r) |
1002 | return r; | |
221cc3ce | 1003 | |
32485f6c | 1004 | pakfire_file_set_path(file, di.kv.str); |
221cc3ce | 1005 | |
5e9463ec | 1006 | // Append to filelist |
f4560c3f | 1007 | pakfire_filelist_append(filelist, file); |
5e9463ec | 1008 | pakfire_file_unref(file); |
221cc3ce | 1009 | } |
5e9463ec | 1010 | dataiterator_free(&di); |
221cc3ce MT |
1011 | |
1012 | // If the file list is empty, we fall back to read files | |
1013 | // in the older format. | |
f4560c3f MT |
1014 | if (pakfire_filelist_is_empty(filelist)) { |
1015 | r = pakfire_package_fetch_legacy_filelist(pkg, filelist); | |
5e9463ec MT |
1016 | if (r) |
1017 | return r; | |
1018 | } | |
1019 | ||
1020 | // Sort the list | |
f4560c3f | 1021 | pakfire_filelist_sort(filelist); |
221cc3ce | 1022 | |
5e9463ec | 1023 | return 0; |
221cc3ce MT |
1024 | } |
1025 | ||
5e9463ec | 1026 | PAKFIRE_EXPORT PakfireFilelist pakfire_package_get_filelist(PakfirePackage pkg) { |
f4560c3f | 1027 | PakfireFilelist filelist; |
5e9463ec | 1028 | |
883b3be9 | 1029 | int r = pakfire_filelist_create(&filelist, pkg->pakfire); |
f4560c3f MT |
1030 | if (r) |
1031 | return NULL; | |
5e9463ec | 1032 | |
f4560c3f MT |
1033 | r = pakfire_package_fetch_filelist(pkg, filelist); |
1034 | if (r) { | |
1035 | pakfire_filelist_unref(filelist); | |
1036 | return NULL; | |
221cc3ce MT |
1037 | } |
1038 | ||
f4560c3f | 1039 | return filelist; |
221cc3ce MT |
1040 | } |
1041 | ||
d995f7fc | 1042 | static int pakfire_package_append_file(PakfirePackage pkg, const char* path) { |
5e9463ec | 1043 | // Fetch repodata |
221cc3ce | 1044 | PakfireRepo repo = pakfire_package_get_repo(pkg); |
3ff6aee6 | 1045 | Repodata* repodata = pakfire_repo_get_repodata(repo); |
5e9463ec | 1046 | pakfire_repo_unref(repo); |
221cc3ce MT |
1047 | |
1048 | Id handle = pakfire_package_get_handle(pkg); | |
1049 | ||
d995f7fc MT |
1050 | char* basename = pakfire_basename(path); |
1051 | char* dirname = pakfire_dirname(path); | |
1052 | ||
1053 | // Convert directory into ID | |
1054 | Id did = repodata_str2dir(repodata, dirname, 1); | |
1055 | if (!did) | |
1056 | did = repodata_str2dir(repodata, "/", 1); | |
1057 | ||
1058 | // Add data to list | |
1059 | repodata_add_dirstr(repodata, handle, | |
1060 | SOLVABLE_FILELIST, did, basename); | |
1061 | ||
1062 | free(basename); | |
1063 | free(dirname); | |
1064 | ||
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | PAKFIRE_EXPORT int pakfire_package_set_filelist(PakfirePackage pkg, PakfireFilelist filelist) { | |
1069 | int r; | |
1070 | ||
5e9463ec MT |
1071 | for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) { |
1072 | PakfireFile file = pakfire_filelist_get(filelist, i); | |
221cc3ce | 1073 | |
32485f6c MT |
1074 | const char* path = pakfire_file_get_path(file); |
1075 | if (path) { | |
1076 | r = pakfire_package_append_file(pkg, path); | |
d995f7fc MT |
1077 | if (r) { |
1078 | pakfire_file_unref(file); | |
1079 | return r; | |
1080 | } | |
1081 | } | |
1082 | ||
1083 | pakfire_file_unref(file); | |
1084 | ||
1085 | } | |
1086 | ||
1087 | return 0; | |
1088 | } | |
221cc3ce | 1089 | |
d995f7fc MT |
1090 | int pakfire_package_set_filelist_from_string(PakfirePackage pkg, const char* files) { |
1091 | // Total length of the input string | |
1092 | size_t size = strlen(files); | |
221cc3ce | 1093 | |
d995f7fc MT |
1094 | // Copy files to the stack to be able to modify it |
1095 | char* buffer = alloca(size + 1); | |
3ff6aee6 | 1096 | |
d995f7fc MT |
1097 | // Copy the content |
1098 | memcpy(buffer, files, size); | |
1099 | ||
1100 | // Replace all line breaks with NUL | |
1101 | for (unsigned int i = 0; i < size; i++) { | |
1102 | if (buffer[i] == '\n') | |
1103 | buffer[i] = '\0'; | |
1104 | } | |
1105 | ||
1106 | size_t p = 0; | |
1107 | while (p < size) { | |
1108 | const char* name = &buffer[p]; | |
1109 | ||
1110 | int r = pakfire_package_append_file(pkg, name); | |
1111 | if (r) | |
1112 | return r; | |
1113 | ||
1114 | p += strlen(name) + 1; | |
5e9463ec | 1115 | } |
221cc3ce | 1116 | |
5e9463ec | 1117 | return 0; |
221cc3ce | 1118 | } |