]>
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 | ||
21 | #include <assert.h> | |
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); | |
304 | assert(converted > 0); | |
305 | assert(*endptr == '\0'); | |
306 | epoch = converted; | |
307 | } | |
308 | ||
309 | return epoch; | |
310 | } | |
311 | ||
9f953e68 | 312 | PAKFIRE_EXPORT const char* pakfire_package_get_version(PakfirePackage pkg) { |
f1c7996e | 313 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
314 | char *e, *v, *r; |
315 | ||
316 | split_evr(pool, pakfire_package_get_evr(pkg), &e, &v, &r); | |
5d901566 | 317 | return strdup(v); |
221cc3ce MT |
318 | } |
319 | ||
9f953e68 | 320 | PAKFIRE_EXPORT const char* pakfire_package_get_release(PakfirePackage pkg) { |
f1c7996e | 321 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
322 | char *e, *v, *r; |
323 | ||
324 | split_evr(pool, pakfire_package_get_evr(pkg), &e, &v, &r); | |
5d901566 | 325 | return strdup(r); |
221cc3ce MT |
326 | } |
327 | ||
9f953e68 | 328 | PAKFIRE_EXPORT const char* pakfire_package_get_arch(PakfirePackage pkg) { |
f1c7996e | 329 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
330 | Solvable* s = get_solvable(pkg); |
331 | ||
332 | return pool_id2str(pool, s->arch); | |
333 | } | |
334 | ||
9f953e68 | 335 | PAKFIRE_EXPORT void pakfire_package_set_arch(PakfirePackage pkg, const char* arch) { |
f1c7996e | 336 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
337 | Solvable* s = get_solvable(pkg); |
338 | ||
339 | s->arch = pool_str2id(pool, arch, 1); | |
340 | } | |
341 | ||
342 | static void pakfire_package_internalize_repo(PakfirePackage pkg) { | |
343 | PakfireRepo repo = pakfire_package_get_repo(pkg); | |
344 | if (repo) { | |
345 | pakfire_repo_internalize(repo); | |
3ff6aee6 | 346 | pakfire_repo_unref(repo); |
221cc3ce MT |
347 | } |
348 | } | |
349 | ||
350 | static const char* pakfire_package_get_string(PakfirePackage pkg, int key) { | |
351 | pakfire_package_internalize_repo(pkg); | |
352 | ||
353 | Solvable* s = get_solvable(pkg); | |
354 | const char* str = solvable_lookup_str(s, key); | |
355 | ||
356 | if (!str) | |
357 | return NULL; | |
358 | ||
359 | if (strlen(str) == 0) | |
360 | return NULL; | |
361 | ||
362 | return str; | |
363 | } | |
364 | ||
365 | static void pakfire_package_set_string(PakfirePackage pkg, int key, const char* value) { | |
366 | Solvable* s = get_solvable(pkg); | |
367 | ||
368 | if (!value) | |
369 | value = ""; | |
370 | ||
7ccc9d74 MT |
371 | solvable_set_poolstr(s, key, value); |
372 | } | |
373 | ||
374 | static void pakfire_package_add_string_array(PakfirePackage pkg, int key, const char* value) { | |
375 | Solvable* s = get_solvable(pkg); | |
376 | ||
377 | if (!value) | |
378 | return; | |
379 | ||
380 | solvable_add_poolstr_array(s, key, value); | |
221cc3ce MT |
381 | } |
382 | ||
9f953e68 | 383 | PAKFIRE_EXPORT const char* pakfire_package_get_uuid(PakfirePackage pkg) { |
221cc3ce MT |
384 | return pakfire_package_get_string(pkg, SOLVABLE_PKGID); |
385 | } | |
386 | ||
9f953e68 | 387 | PAKFIRE_EXPORT void pakfire_package_set_uuid(PakfirePackage pkg, const char* uuid) { |
221cc3ce MT |
388 | pakfire_package_set_string(pkg, SOLVABLE_PKGID, uuid); |
389 | } | |
390 | ||
9f953e68 | 391 | PAKFIRE_EXPORT const char* pakfire_package_get_checksum(PakfirePackage pkg) { |
221cc3ce MT |
392 | return pakfire_package_get_string(pkg, SOLVABLE_CHECKSUM); |
393 | } | |
394 | ||
9f953e68 | 395 | PAKFIRE_EXPORT void pakfire_package_set_checksum(PakfirePackage pkg, const char* checksum) { |
221cc3ce MT |
396 | pakfire_package_set_string(pkg, SOLVABLE_CHECKSUM, checksum); |
397 | } | |
398 | ||
9f953e68 | 399 | PAKFIRE_EXPORT const char* pakfire_package_get_summary(PakfirePackage pkg) { |
221cc3ce MT |
400 | return pakfire_package_get_string(pkg, SOLVABLE_SUMMARY); |
401 | } | |
402 | ||
9f953e68 | 403 | PAKFIRE_EXPORT void pakfire_package_set_summary(PakfirePackage pkg, const char* summary) { |
221cc3ce MT |
404 | pakfire_package_set_string(pkg, SOLVABLE_SUMMARY, summary); |
405 | } | |
406 | ||
9f953e68 | 407 | PAKFIRE_EXPORT const char* pakfire_package_get_description(PakfirePackage pkg) { |
221cc3ce MT |
408 | return pakfire_package_get_string(pkg, SOLVABLE_DESCRIPTION); |
409 | } | |
410 | ||
9f953e68 | 411 | PAKFIRE_EXPORT void pakfire_package_set_description(PakfirePackage pkg, const char* description) { |
221cc3ce MT |
412 | pakfire_package_set_string(pkg, SOLVABLE_DESCRIPTION, description); |
413 | } | |
414 | ||
9f953e68 | 415 | PAKFIRE_EXPORT const char* pakfire_package_get_license(PakfirePackage pkg) { |
221cc3ce MT |
416 | return pakfire_package_get_string(pkg, SOLVABLE_LICENSE); |
417 | } | |
418 | ||
9f953e68 | 419 | PAKFIRE_EXPORT void pakfire_package_set_license(PakfirePackage pkg, const char* license) { |
221cc3ce MT |
420 | pakfire_package_set_string(pkg, SOLVABLE_LICENSE, license); |
421 | } | |
422 | ||
9f953e68 | 423 | PAKFIRE_EXPORT const char* pakfire_package_get_url(PakfirePackage pkg) { |
221cc3ce MT |
424 | return pakfire_package_get_string(pkg, SOLVABLE_URL); |
425 | } | |
426 | ||
9f953e68 | 427 | PAKFIRE_EXPORT void pakfire_package_set_url(PakfirePackage pkg, const char* url) { |
221cc3ce MT |
428 | pakfire_package_set_string(pkg, SOLVABLE_URL, url); |
429 | } | |
430 | ||
7ccc9d74 MT |
431 | PAKFIRE_EXPORT const char* pakfire_package_get_groups(PakfirePackage pkg) { |
432 | return pakfire_package_get_string(pkg, SOLVABLE_GROUP); | |
221cc3ce MT |
433 | } |
434 | ||
7ccc9d74 MT |
435 | PAKFIRE_EXPORT void pakfire_package_set_groups(PakfirePackage pkg, const char* groups) { |
436 | char** list = pakfire_split_string(groups, ' '); | |
221cc3ce | 437 | |
7ccc9d74 MT |
438 | while (list && *list) { |
439 | pakfire_package_add_string_array(pkg, SOLVABLE_GROUP, *list); | |
440 | list++; | |
2141b578 | 441 | } |
8ad0a954 MT |
442 | } |
443 | ||
9f953e68 | 444 | PAKFIRE_EXPORT const char* pakfire_package_get_vendor(PakfirePackage pkg) { |
221cc3ce MT |
445 | return pakfire_package_get_string(pkg, SOLVABLE_VENDOR); |
446 | } | |
447 | ||
9f953e68 | 448 | PAKFIRE_EXPORT void pakfire_package_set_vendor(PakfirePackage pkg, const char* vendor) { |
221cc3ce MT |
449 | pakfire_package_set_string(pkg, SOLVABLE_VENDOR, vendor); |
450 | } | |
451 | ||
9f953e68 | 452 | PAKFIRE_EXPORT const char* pakfire_package_get_maintainer(PakfirePackage pkg) { |
221cc3ce MT |
453 | return pakfire_package_get_string(pkg, SOLVABLE_PACKAGER); |
454 | } | |
455 | ||
9f953e68 | 456 | PAKFIRE_EXPORT void pakfire_package_set_maintainer(PakfirePackage pkg, const char* maintainer) { |
221cc3ce MT |
457 | pakfire_package_set_string(pkg, SOLVABLE_PACKAGER, maintainer); |
458 | } | |
459 | ||
9f953e68 | 460 | PAKFIRE_EXPORT const char* pakfire_package_get_filename(PakfirePackage pkg) { |
221cc3ce MT |
461 | return pakfire_package_get_string(pkg, SOLVABLE_MEDIAFILE); |
462 | } | |
463 | ||
9f953e68 | 464 | PAKFIRE_EXPORT void pakfire_package_set_filename(PakfirePackage pkg, const char* filename) { |
221cc3ce MT |
465 | pakfire_package_set_string(pkg, SOLVABLE_MEDIAFILE, filename); |
466 | } | |
467 | ||
9f953e68 | 468 | PAKFIRE_EXPORT int pakfire_package_is_installed(PakfirePackage pkg) { |
f1c7996e | 469 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
470 | Solvable* s = get_solvable(pkg); |
471 | ||
472 | return pool->installed == s->repo; | |
473 | } | |
474 | ||
475 | static unsigned long long pakfire_package_get_num(PakfirePackage pkg, Id type) { | |
476 | pakfire_package_internalize_repo(pkg); | |
477 | ||
478 | Solvable* s = get_solvable(pkg); | |
479 | return solvable_lookup_num(s, type, 0); | |
480 | } | |
481 | ||
482 | static void pakfire_package_set_num(PakfirePackage pkg, Id type, unsigned long long value) { | |
483 | Solvable* s = get_solvable(pkg); | |
484 | ||
485 | solvable_set_num(s, type, value); | |
486 | } | |
487 | ||
9f953e68 | 488 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_downloadsize(PakfirePackage pkg) { |
221cc3ce MT |
489 | return pakfire_package_get_num(pkg, SOLVABLE_DOWNLOADSIZE); |
490 | } | |
491 | ||
9f953e68 | 492 | PAKFIRE_EXPORT void pakfire_package_set_downloadsize(PakfirePackage pkg, unsigned long long downloadsize) { |
221cc3ce MT |
493 | return pakfire_package_set_num(pkg, SOLVABLE_DOWNLOADSIZE, downloadsize); |
494 | } | |
495 | ||
9f953e68 | 496 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_installsize(PakfirePackage pkg) { |
221cc3ce MT |
497 | return pakfire_package_get_num(pkg, SOLVABLE_INSTALLSIZE); |
498 | } | |
499 | ||
9f953e68 | 500 | PAKFIRE_EXPORT void pakfire_package_set_installsize(PakfirePackage pkg, unsigned long long installsize) { |
221cc3ce MT |
501 | return pakfire_package_set_num(pkg, SOLVABLE_INSTALLSIZE, installsize); |
502 | } | |
503 | ||
9f953e68 | 504 | PAKFIRE_EXPORT unsigned long long pakfire_package_get_size(PakfirePackage pkg) { |
221cc3ce MT |
505 | if (pakfire_package_is_installed(pkg)) |
506 | return pakfire_package_get_installsize(pkg); | |
507 | ||
508 | return pakfire_package_get_downloadsize(pkg); | |
509 | } | |
510 | ||
2ffc704d | 511 | PAKFIRE_EXPORT const char* pakfire_package_get_build_host(PakfirePackage pkg) { |
221cc3ce MT |
512 | return pakfire_package_get_string(pkg, SOLVABLE_BUILDHOST); |
513 | } | |
514 | ||
2ffc704d MT |
515 | PAKFIRE_EXPORT void pakfire_package_set_build_host(PakfirePackage pkg, const char* build_host) { |
516 | pakfire_package_set_string(pkg, SOLVABLE_BUILDHOST, build_host); | |
221cc3ce MT |
517 | } |
518 | ||
2ffc704d | 519 | PAKFIRE_EXPORT time_t pakfire_package_get_build_time(PakfirePackage pkg) { |
221cc3ce MT |
520 | return pakfire_package_get_num(pkg, SOLVABLE_BUILDTIME); |
521 | } | |
522 | ||
2ffc704d MT |
523 | PAKFIRE_EXPORT void pakfire_package_set_build_time(PakfirePackage pkg, time_t build_time) { |
524 | pakfire_package_set_num(pkg, SOLVABLE_BUILDTIME, build_time); | |
221cc3ce MT |
525 | } |
526 | ||
2ffc704d | 527 | PAKFIRE_EXPORT time_t pakfire_package_get_installtime(PakfirePackage pkg) { |
221cc3ce MT |
528 | return pakfire_package_get_num(pkg, SOLVABLE_INSTALLTIME); |
529 | } | |
530 | ||
a9659851 MT |
531 | static PakfireRelationList pakfire_package_get_relationlist( |
532 | PakfirePackage pkg, Id type, Id marker) { | |
221cc3ce MT |
533 | Queue q; |
534 | queue_init(&q); | |
535 | ||
536 | Solvable* s = get_solvable(pkg); | |
a9659851 | 537 | solvable_lookup_deparray(s, type, &q, marker); |
221cc3ce | 538 | |
d6ea32df MT |
539 | PakfireRelationList list; |
540 | int r = pakfire_relationlist_from_queue(&list, pkg->pakfire, q); | |
541 | ||
221cc3ce MT |
542 | queue_free(&q); |
543 | ||
d6ea32df MT |
544 | if (r) |
545 | return NULL; | |
546 | ||
547 | return list; | |
221cc3ce MT |
548 | } |
549 | ||
a9659851 MT |
550 | static void pakfire_package_set_relationlist(PakfirePackage pkg, Id type, |
551 | PakfireRelationList relationlist, Id marker) { | |
221cc3ce | 552 | Solvable* s = get_solvable(pkg); |
97eb9a58 | 553 | Queue* q = pakfire_relationlist_get_queue(relationlist); |
221cc3ce | 554 | |
97eb9a58 MT |
555 | #if 0 |
556 | // This implemention should be the fastest, but unfortunately does not work. | |
a9659851 | 557 | solvable_set_deparray(s, type, q, marker); |
97eb9a58 MT |
558 | #else |
559 | for (int i = 0; i < q->count; i++) | |
a9659851 | 560 | solvable_add_deparray(s, type, q->elements[i], marker); |
221cc3ce | 561 | #endif |
221cc3ce MT |
562 | } |
563 | ||
a9659851 MT |
564 | static void pakfire_package_add_relation(PakfirePackage pkg, Id type, |
565 | PakfireRelation relation, Id marker) { | |
221cc3ce MT |
566 | Solvable* s = get_solvable(pkg); |
567 | ||
a9659851 | 568 | solvable_add_deparray(s, type, pakfire_relation_get_id(relation), marker); |
221cc3ce MT |
569 | } |
570 | ||
9f953e68 | 571 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_provides(PakfirePackage pkg) { |
063ca10f | 572 | return pakfire_package_get_relationlist(pkg, SOLVABLE_PROVIDES, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
573 | } |
574 | ||
9f953e68 | 575 | PAKFIRE_EXPORT void pakfire_package_set_provides(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 576 | pakfire_package_set_relationlist(pkg, SOLVABLE_PROVIDES, relationlist, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
577 | } |
578 | ||
9f953e68 | 579 | PAKFIRE_EXPORT void pakfire_package_add_provides(PakfirePackage pkg, PakfireRelation relation) { |
063ca10f | 580 | pakfire_package_add_relation(pkg, SOLVABLE_PROVIDES, relation, -SOLVABLE_FILEMARKER); |
221cc3ce MT |
581 | } |
582 | ||
9f953e68 | 583 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_prerequires(PakfirePackage pkg) { |
063ca10f | 584 | return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, SOLVABLE_PREREQMARKER); |
221cc3ce MT |
585 | } |
586 | ||
dec8207e | 587 | PAKFIRE_EXPORT void pakfire_package_set_prerequires(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 588 | pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, SOLVABLE_PREREQMARKER); |
dec8207e MT |
589 | } |
590 | ||
9f953e68 | 591 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_requires(PakfirePackage pkg) { |
063ca10f | 592 | return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
593 | } |
594 | ||
9f953e68 | 595 | PAKFIRE_EXPORT void pakfire_package_set_requires(PakfirePackage pkg, PakfireRelationList relationlist) { |
063ca10f | 596 | pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
597 | } |
598 | ||
9f953e68 | 599 | PAKFIRE_EXPORT void pakfire_package_add_requires(PakfirePackage pkg, PakfireRelation relation) { |
063ca10f | 600 | pakfire_package_add_relation(pkg, SOLVABLE_REQUIRES, relation, -SOLVABLE_PREREQMARKER); |
221cc3ce MT |
601 | } |
602 | ||
9f953e68 | 603 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_conflicts(PakfirePackage pkg) { |
a9659851 | 604 | return pakfire_package_get_relationlist(pkg, SOLVABLE_CONFLICTS, 0); |
221cc3ce MT |
605 | } |
606 | ||
9f953e68 | 607 | PAKFIRE_EXPORT void pakfire_package_set_conflicts(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 608 | pakfire_package_set_relationlist(pkg, SOLVABLE_CONFLICTS, relationlist, 0); |
221cc3ce MT |
609 | } |
610 | ||
9f953e68 | 611 | PAKFIRE_EXPORT void pakfire_package_add_conflicts(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 612 | pakfire_package_add_relation(pkg, SOLVABLE_CONFLICTS, relation, 0); |
221cc3ce MT |
613 | } |
614 | ||
9f953e68 | 615 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_obsoletes(PakfirePackage pkg) { |
a9659851 | 616 | return pakfire_package_get_relationlist(pkg, SOLVABLE_OBSOLETES, 0); |
221cc3ce MT |
617 | } |
618 | ||
9f953e68 | 619 | PAKFIRE_EXPORT void pakfire_package_set_obsoletes(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 620 | pakfire_package_set_relationlist(pkg, SOLVABLE_OBSOLETES, relationlist, 0); |
221cc3ce MT |
621 | } |
622 | ||
9f953e68 | 623 | PAKFIRE_EXPORT void pakfire_package_add_obsoletes(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 624 | pakfire_package_add_relation(pkg, SOLVABLE_OBSOLETES, relation, 0); |
221cc3ce MT |
625 | } |
626 | ||
9f953e68 | 627 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_recommends(PakfirePackage pkg) { |
a9659851 | 628 | return pakfire_package_get_relationlist(pkg, SOLVABLE_RECOMMENDS, 0); |
221cc3ce MT |
629 | } |
630 | ||
9f953e68 | 631 | PAKFIRE_EXPORT void pakfire_package_set_recommends(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 632 | pakfire_package_set_relationlist(pkg, SOLVABLE_RECOMMENDS, relationlist, 0); |
221cc3ce MT |
633 | } |
634 | ||
9f953e68 | 635 | PAKFIRE_EXPORT void pakfire_package_add_recommends(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 636 | pakfire_package_add_relation(pkg, SOLVABLE_RECOMMENDS, relation, 0); |
221cc3ce MT |
637 | } |
638 | ||
9f953e68 | 639 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_suggests(PakfirePackage pkg) { |
a9659851 | 640 | return pakfire_package_get_relationlist(pkg, SOLVABLE_SUGGESTS, 0); |
221cc3ce MT |
641 | } |
642 | ||
9f953e68 | 643 | PAKFIRE_EXPORT void pakfire_package_set_suggests(PakfirePackage pkg, PakfireRelationList relationlist) { |
a9659851 | 644 | pakfire_package_set_relationlist(pkg, SOLVABLE_SUGGESTS, relationlist, 0); |
221cc3ce MT |
645 | } |
646 | ||
9f953e68 | 647 | PAKFIRE_EXPORT void pakfire_package_add_suggests(PakfirePackage pkg, PakfireRelation relation) { |
a9659851 | 648 | pakfire_package_add_relation(pkg, SOLVABLE_SUGGESTS, relation, 0); |
221cc3ce MT |
649 | } |
650 | ||
8fe2e4ba MT |
651 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_supplements(PakfirePackage pkg) { |
652 | return pakfire_package_get_relationlist(pkg, SOLVABLE_SUPPLEMENTS, 0); | |
653 | } | |
654 | ||
655 | PAKFIRE_EXPORT void pakfire_package_set_supplements(PakfirePackage pkg, PakfireRelationList relationlist) { | |
656 | pakfire_package_set_relationlist(pkg, SOLVABLE_SUPPLEMENTS, relationlist, 0); | |
657 | } | |
658 | ||
659 | PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_enhances(PakfirePackage pkg) { | |
660 | return pakfire_package_get_relationlist(pkg, SOLVABLE_ENHANCES, 0); | |
661 | } | |
662 | ||
663 | PAKFIRE_EXPORT void pakfire_package_set_enhances(PakfirePackage pkg, PakfireRelationList relationlist) { | |
664 | pakfire_package_set_relationlist(pkg, SOLVABLE_ENHANCES, relationlist, 0); | |
665 | } | |
666 | ||
9f953e68 | 667 | PAKFIRE_EXPORT PakfireRepo pakfire_package_get_repo(PakfirePackage pkg) { |
e9d9eadc MT |
668 | if (!pkg->repo) { |
669 | Solvable* s = get_solvable(pkg); | |
670 | ||
671 | pkg->repo = pakfire_repo_create_from_repo(pkg->pakfire, s->repo); | |
672 | } | |
221cc3ce | 673 | |
e9d9eadc | 674 | return pakfire_repo_ref(pkg->repo); |
221cc3ce MT |
675 | } |
676 | ||
9f953e68 | 677 | PAKFIRE_EXPORT void pakfire_package_set_repo(PakfirePackage pkg, PakfireRepo repo) { |
e9d9eadc MT |
678 | // Free old repository |
679 | if (pkg->repo) | |
680 | pakfire_repo_unref(pkg->repo); | |
221cc3ce | 681 | |
e9d9eadc | 682 | Solvable* s = get_solvable(pkg); |
3ff6aee6 | 683 | s->repo = pakfire_repo_get_repo(repo); |
e9d9eadc MT |
684 | |
685 | pkg->repo = pakfire_repo_ref(repo); | |
221cc3ce MT |
686 | } |
687 | ||
9f953e68 | 688 | PAKFIRE_EXPORT char* pakfire_package_get_location(PakfirePackage pkg) { |
221cc3ce MT |
689 | pakfire_package_internalize_repo(pkg); |
690 | ||
691 | Solvable* s = get_solvable(pkg); | |
692 | ||
693 | const char* location = solvable_get_location(s, NULL); | |
5d901566 | 694 | return strdup(location); |
221cc3ce MT |
695 | } |
696 | ||
697 | static void pakfire_package_dump_add_line(char** str, const char* key, const char* val) { | |
698 | if (val) | |
699 | asprintf(str, "%s%-15s: %s\n", *str, key ? key : "", val); | |
700 | } | |
701 | ||
702 | static void pakfire_package_dump_add_lines(char** str, const char* key, const char* val) { | |
703 | const char* string = val; | |
704 | ||
705 | while (*string) { | |
706 | char line[STRING_SIZE]; | |
707 | int counter = 0; | |
708 | ||
709 | while (*string) { | |
710 | if (*string == '\n') { | |
711 | string++; | |
712 | break; | |
713 | } | |
714 | ||
715 | line[counter++] = *string++; | |
716 | } | |
717 | line[counter] = '\0'; | |
718 | ||
719 | if (*line) { | |
720 | pakfire_package_dump_add_line(str, key, line); | |
721 | key = NULL; | |
722 | } | |
723 | } | |
724 | } | |
725 | ||
726 | static void pakfire_package_dump_add_line_date(char** str, const char* key, unsigned long long date) { | |
727 | // Convert from integer to tm struct. | |
728 | struct tm* timer = gmtime((time_t *)&date); | |
729 | ||
730 | char val[STRING_SIZE]; | |
731 | strftime(val, STRING_SIZE, "%a, %d %b %Y %T %z", timer); | |
732 | ||
733 | pakfire_package_dump_add_line(str, key, val); | |
734 | } | |
735 | ||
736 | static void pakfire_package_dump_add_line_relations(char** str, const char* key, PakfireRelationList deps) { | |
7ac9a26b MT |
737 | pakfire_relationlist_sort(deps); |
738 | ||
b76d7b47 MT |
739 | size_t size = pakfire_relationlist_size(deps); |
740 | for (unsigned int i = 0; i < size; i++) { | |
741 | PakfireRelation relation = pakfire_relationlist_get(deps, i); | |
221cc3ce MT |
742 | |
743 | if (relation) { | |
744 | char* dep = pakfire_relation_str(relation); | |
1a497776 | 745 | pakfire_relation_unref(relation); |
221cc3ce | 746 | |
221cc3ce MT |
747 | if (dep) { |
748 | pakfire_package_dump_add_line(str, (i == 0) ? key : "", dep); | |
f0d6233d | 749 | free(dep); |
221cc3ce MT |
750 | } |
751 | } | |
752 | } | |
753 | } | |
754 | ||
755 | static void pakfire_package_dump_add_line_size(char** str, const char* key, unsigned long long size) { | |
756 | char* val = pakfire_format_size(size); | |
757 | ||
758 | if (val) { | |
759 | pakfire_package_dump_add_line(str, key, val); | |
f0d6233d | 760 | free(val); |
221cc3ce MT |
761 | } |
762 | } | |
763 | ||
9f953e68 | 764 | PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) { |
221cc3ce MT |
765 | char* string = ""; |
766 | ||
767 | // Name | |
768 | const char* name = pakfire_package_get_name(pkg); | |
769 | pakfire_package_dump_add_line(&string, _("Name"), name); | |
770 | ||
771 | // Version | |
772 | const char* version = pakfire_package_get_version(pkg); | |
773 | pakfire_package_dump_add_line(&string, _("Version"), version); | |
774 | ||
775 | // Release | |
776 | const char* release = pakfire_package_get_release(pkg); | |
777 | pakfire_package_dump_add_line(&string, _("Release"), release); | |
778 | ||
779 | // Size | |
780 | unsigned long long size = pakfire_package_get_size(pkg); | |
781 | pakfire_package_dump_add_line_size(&string, _("Size"), size); | |
782 | ||
783 | // Installed size | |
784 | if (pakfire_package_is_installed(pkg)) { | |
785 | unsigned long long installsize = pakfire_package_get_installsize(pkg); | |
786 | pakfire_package_dump_add_line_size(&string, _("Installed size"), installsize); | |
787 | ||
788 | // Downloadsize | |
789 | } else { | |
790 | unsigned long long downloadsize = pakfire_package_get_downloadsize(pkg); | |
791 | pakfire_package_dump_add_line_size(&string, _("Download size"), downloadsize); | |
792 | } | |
793 | ||
794 | PakfireRepo repo = pakfire_package_get_repo(pkg); | |
795 | if (repo) { | |
796 | const char* repo_name = pakfire_repo_get_name(repo); | |
797 | pakfire_package_dump_add_line(&string, _("Repo"), repo_name); | |
798 | ||
3ff6aee6 | 799 | pakfire_repo_unref(repo); |
221cc3ce MT |
800 | } |
801 | ||
802 | // Summary | |
803 | const char* summary = pakfire_package_get_summary(pkg); | |
804 | pakfire_package_dump_add_line(&string, _("Summary"), summary); | |
805 | ||
806 | // Description | |
807 | const char* description = pakfire_package_get_description(pkg); | |
1b99c676 MT |
808 | if (description) |
809 | pakfire_package_dump_add_lines(&string, _("Description"), description); | |
221cc3ce MT |
810 | |
811 | // Groups | |
7ccc9d74 | 812 | const char* groups = pakfire_package_get_groups(pkg); |
2141b578 | 813 | if (groups) { |
7ccc9d74 | 814 | pakfire_package_dump_add_lines(&string, _("Groups"), groups); |
2141b578 | 815 | } |
221cc3ce MT |
816 | |
817 | // URL | |
818 | const char* url = pakfire_package_get_url(pkg); | |
819 | pakfire_package_dump_add_line(&string, _("URL"), url); | |
820 | ||
821 | // License | |
822 | const char* license = pakfire_package_get_license(pkg); | |
823 | pakfire_package_dump_add_line(&string, _("License"), license); | |
824 | ||
825 | if (flags & PAKFIRE_PKG_DUMP_LONG) { | |
826 | // Maintainer | |
827 | const char* maintainer = pakfire_package_get_maintainer(pkg); | |
828 | pakfire_package_dump_add_line(&string, _("Maintainer"), maintainer); | |
829 | ||
830 | // Vendor | |
831 | const char* vendor = pakfire_package_get_vendor(pkg); | |
832 | pakfire_package_dump_add_line(&string, _("Vendor"), vendor); | |
833 | ||
834 | // UUID | |
835 | const char* uuid = pakfire_package_get_uuid(pkg); | |
836 | pakfire_package_dump_add_line(&string, _("UUID"), uuid); | |
837 | ||
838 | // Build time | |
2ffc704d MT |
839 | time_t build_time = pakfire_package_get_build_time(pkg); |
840 | pakfire_package_dump_add_line_date(&string, _("Build Time"), build_time); | |
221cc3ce MT |
841 | |
842 | // Build host | |
2ffc704d MT |
843 | const char* build_host = pakfire_package_get_build_host(pkg); |
844 | pakfire_package_dump_add_line(&string, _("Build Host"), build_host); | |
221cc3ce | 845 | |
2f873877 MT |
846 | // Dependencies |
847 | ||
848 | const struct relation { | |
849 | const char* name; | |
850 | PakfireRelationList (*get)(PakfirePackage pkg); | |
851 | } relations[] = { | |
852 | { _("Provides"), pakfire_package_get_provides, }, | |
853 | { _("Pre-Requires"), pakfire_package_get_prerequires, }, | |
854 | { _("Requires"), pakfire_package_get_requires, }, | |
855 | { _("Conflicts"), pakfire_package_get_conflicts, }, | |
856 | { _("Obsoletes"), pakfire_package_get_obsoletes, }, | |
857 | { _("Recommends"), pakfire_package_get_recommends, }, | |
858 | { _("Suggests"), pakfire_package_get_suggests, }, | |
8fe2e4ba MT |
859 | { _("Supplements"), pakfire_package_get_supplements, }, |
860 | { _("Enhances"), pakfire_package_get_enhances, }, | |
2f873877 MT |
861 | { NULL, NULL}, |
862 | }; | |
863 | ||
864 | for (const struct relation* relation = relations; relation->name; relation++) { | |
865 | PakfireRelationList list = relation->get(pkg); | |
866 | if (list) { | |
867 | pakfire_package_dump_add_line_relations(&string, relation->name, list); | |
868 | pakfire_relationlist_unref(list); | |
869 | } | |
221cc3ce MT |
870 | } |
871 | } | |
872 | ||
873 | if (flags & PAKFIRE_PKG_DUMP_FILELIST) { | |
5e9463ec MT |
874 | PakfireFilelist filelist = pakfire_package_get_filelist(pkg); |
875 | ||
876 | const char* prefix = _("Filelist"); | |
877 | ||
878 | for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) { | |
879 | PakfireFile file = pakfire_filelist_get(filelist, i); | |
221cc3ce | 880 | |
221cc3ce MT |
881 | const char* name = pakfire_file_get_name(file); |
882 | pakfire_package_dump_add_line(&string, prefix, name); | |
883 | ||
5e9463ec | 884 | pakfire_file_unref(file); |
221cc3ce | 885 | |
5e9463ec | 886 | // Only prefix the first line |
221cc3ce MT |
887 | prefix = NULL; |
888 | } | |
5e9463ec MT |
889 | |
890 | pakfire_filelist_unref(filelist); | |
221cc3ce MT |
891 | } |
892 | ||
893 | return string; | |
894 | } | |
895 | ||
9f953e68 | 896 | PAKFIRE_EXPORT int pakfire_package_is_cached(PakfirePackage pkg) { |
3f7bcbc4 | 897 | char* path = pakfire_package_get_cache_path(pkg); |
cbed1552 | 898 | |
3f7bcbc4 | 899 | // Check if the file is readable |
12656820 | 900 | int r = pakfire_access(pkg->pakfire, path, NULL, R_OK); |
f0d6233d | 901 | free(path); |
221cc3ce | 902 | |
cf27dd9c | 903 | return (r == 0); |
221cc3ce MT |
904 | } |
905 | ||
9f953e68 | 906 | PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) { |
3f7bcbc4 | 907 | char buffer[STRING_SIZE] = ""; |
221cc3ce | 908 | |
3f7bcbc4 MT |
909 | const char* filename = pakfire_package_get_filename(pkg); |
910 | const char* checksum = pakfire_package_get_checksum(pkg); | |
221cc3ce | 911 | |
3f7bcbc4 | 912 | if (strlen(checksum) < 3) |
221cc3ce MT |
913 | return NULL; |
914 | ||
3f7bcbc4 MT |
915 | snprintf(buffer, sizeof(buffer), "%c%c/%s/%s", checksum[0], checksum[1], |
916 | checksum + 2, filename); | |
221cc3ce | 917 | |
3f7bcbc4 | 918 | return pakfire_get_cache_path(pkg->pakfire, buffer); |
221cc3ce MT |
919 | } |
920 | ||
cbed1552 | 921 | PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) { |
cbed1552 | 922 | // Otherwise open the archive from the cache |
3f7bcbc4 | 923 | char* path = pakfire_package_get_cache_path(pkg); |
641cab18 MT |
924 | if (!path) |
925 | return NULL; | |
cbed1552 | 926 | |
641cab18 MT |
927 | // Open archive |
928 | PakfireArchive archive = pakfire_archive_open(pkg->pakfire, path); | |
f0d6233d | 929 | free(path); |
cbed1552 MT |
930 | |
931 | return archive; | |
932 | } | |
933 | ||
f4560c3f | 934 | static int pakfire_package_fetch_legacy_filelist(PakfirePackage pkg, PakfireFilelist filelist) { |
221cc3ce MT |
935 | pakfire_package_internalize_repo(pkg); |
936 | ||
221cc3ce MT |
937 | PakfireRepo repo = pakfire_package_get_repo(pkg); |
938 | Solvable* s = get_solvable(pkg); | |
f1c7996e | 939 | Pool* p = pakfire_package_get_solv_pool(pkg); |
3ff6aee6 | 940 | Repo* r = pakfire_repo_get_repo(repo); |
5e9463ec | 941 | pakfire_repo_unref(repo); |
221cc3ce MT |
942 | |
943 | int found_marker = 0; | |
944 | ||
945 | Id id, *ids; | |
946 | ids = r->idarraydata + s->provides; | |
947 | while((id = *ids++) != 0) { | |
948 | const char* filename = pool_dep2str(p, id); | |
949 | ||
950 | if (found_marker) { | |
5e9463ec | 951 | PakfireFile file; |
221cc3ce | 952 | |
5e9463ec MT |
953 | int r = pakfire_file_create(&file); |
954 | if (r) | |
955 | return r; | |
956 | ||
957 | // Set name | |
221cc3ce | 958 | pakfire_file_set_name(file, filename); |
5e9463ec | 959 | |
f4560c3f | 960 | r = pakfire_filelist_append(filelist, file); |
5e9463ec MT |
961 | if (r) |
962 | return r; | |
963 | ||
964 | pakfire_file_unref(file); | |
221cc3ce MT |
965 | continue; |
966 | } | |
967 | ||
968 | if (strcmp(filename, PAKFIRE_SOLVABLE_FILEMARKER) == 0) | |
969 | ++found_marker; | |
970 | } | |
971 | ||
5e9463ec | 972 | return 0; |
221cc3ce MT |
973 | } |
974 | ||
f4560c3f | 975 | static int pakfire_package_fetch_filelist(PakfirePackage pkg, PakfireFilelist filelist) { |
5e9463ec MT |
976 | int r; |
977 | ||
221cc3ce MT |
978 | pakfire_package_internalize_repo(pkg); |
979 | ||
f1c7996e | 980 | Pool* pool = pakfire_package_get_solv_pool(pkg); |
221cc3ce MT |
981 | Repo* repo = pakfire_package_solv_repo(pkg); |
982 | Id handle = pakfire_package_get_handle(pkg); | |
983 | ||
984 | Dataiterator di; | |
985 | dataiterator_init(&di, pool, repo, handle, | |
986 | SOLVABLE_FILELIST, NULL, SEARCH_FILES | SEARCH_COMPLETE_FILELIST); | |
5e9463ec | 987 | |
221cc3ce | 988 | while (dataiterator_step(&di)) { |
5e9463ec | 989 | PakfireFile file; |
221cc3ce | 990 | |
5e9463ec MT |
991 | r = pakfire_file_create(&file); |
992 | if (r) | |
993 | return r; | |
221cc3ce | 994 | |
5e9463ec | 995 | pakfire_file_set_name(file, di.kv.str); |
221cc3ce | 996 | |
5e9463ec | 997 | // Append to filelist |
f4560c3f | 998 | pakfire_filelist_append(filelist, file); |
5e9463ec | 999 | pakfire_file_unref(file); |
221cc3ce | 1000 | } |
5e9463ec | 1001 | dataiterator_free(&di); |
221cc3ce MT |
1002 | |
1003 | // If the file list is empty, we fall back to read files | |
1004 | // in the older format. | |
f4560c3f MT |
1005 | if (pakfire_filelist_is_empty(filelist)) { |
1006 | r = pakfire_package_fetch_legacy_filelist(pkg, filelist); | |
5e9463ec MT |
1007 | if (r) |
1008 | return r; | |
1009 | } | |
1010 | ||
1011 | // Sort the list | |
f4560c3f | 1012 | pakfire_filelist_sort(filelist); |
221cc3ce | 1013 | |
5e9463ec | 1014 | return 0; |
221cc3ce MT |
1015 | } |
1016 | ||
5e9463ec | 1017 | PAKFIRE_EXPORT PakfireFilelist pakfire_package_get_filelist(PakfirePackage pkg) { |
f4560c3f | 1018 | PakfireFilelist filelist; |
5e9463ec | 1019 | |
f4560c3f MT |
1020 | int r = pakfire_filelist_create(&filelist); |
1021 | if (r) | |
1022 | return NULL; | |
5e9463ec | 1023 | |
f4560c3f MT |
1024 | r = pakfire_package_fetch_filelist(pkg, filelist); |
1025 | if (r) { | |
1026 | pakfire_filelist_unref(filelist); | |
1027 | return NULL; | |
221cc3ce MT |
1028 | } |
1029 | ||
f4560c3f | 1030 | return filelist; |
221cc3ce MT |
1031 | } |
1032 | ||
d995f7fc | 1033 | static int pakfire_package_append_file(PakfirePackage pkg, const char* path) { |
5e9463ec | 1034 | // Fetch repodata |
221cc3ce | 1035 | PakfireRepo repo = pakfire_package_get_repo(pkg); |
3ff6aee6 | 1036 | Repodata* repodata = pakfire_repo_get_repodata(repo); |
5e9463ec | 1037 | pakfire_repo_unref(repo); |
221cc3ce MT |
1038 | |
1039 | Id handle = pakfire_package_get_handle(pkg); | |
1040 | ||
d995f7fc MT |
1041 | char* basename = pakfire_basename(path); |
1042 | char* dirname = pakfire_dirname(path); | |
1043 | ||
1044 | // Convert directory into ID | |
1045 | Id did = repodata_str2dir(repodata, dirname, 1); | |
1046 | if (!did) | |
1047 | did = repodata_str2dir(repodata, "/", 1); | |
1048 | ||
1049 | // Add data to list | |
1050 | repodata_add_dirstr(repodata, handle, | |
1051 | SOLVABLE_FILELIST, did, basename); | |
1052 | ||
1053 | free(basename); | |
1054 | free(dirname); | |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | PAKFIRE_EXPORT int pakfire_package_set_filelist(PakfirePackage pkg, PakfireFilelist filelist) { | |
1060 | int r; | |
1061 | ||
5e9463ec MT |
1062 | for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) { |
1063 | PakfireFile file = pakfire_filelist_get(filelist, i); | |
221cc3ce | 1064 | |
d995f7fc MT |
1065 | const char* name = pakfire_file_get_name(file); |
1066 | if (name) { | |
1067 | r = pakfire_package_append_file(pkg, name); | |
1068 | if (r) { | |
1069 | pakfire_file_unref(file); | |
1070 | return r; | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | pakfire_file_unref(file); | |
1075 | ||
1076 | } | |
1077 | ||
1078 | return 0; | |
1079 | } | |
221cc3ce | 1080 | |
d995f7fc MT |
1081 | int pakfire_package_set_filelist_from_string(PakfirePackage pkg, const char* files) { |
1082 | // Total length of the input string | |
1083 | size_t size = strlen(files); | |
221cc3ce | 1084 | |
d995f7fc MT |
1085 | // Copy files to the stack to be able to modify it |
1086 | char* buffer = alloca(size + 1); | |
3ff6aee6 | 1087 | |
d995f7fc MT |
1088 | // Copy the content |
1089 | memcpy(buffer, files, size); | |
1090 | ||
1091 | // Replace all line breaks with NUL | |
1092 | for (unsigned int i = 0; i < size; i++) { | |
1093 | if (buffer[i] == '\n') | |
1094 | buffer[i] = '\0'; | |
1095 | } | |
1096 | ||
1097 | size_t p = 0; | |
1098 | while (p < size) { | |
1099 | const char* name = &buffer[p]; | |
1100 | ||
1101 | int r = pakfire_package_append_file(pkg, name); | |
1102 | if (r) | |
1103 | return r; | |
1104 | ||
1105 | p += strlen(name) + 1; | |
5e9463ec | 1106 | } |
221cc3ce | 1107 | |
5e9463ec | 1108 | return 0; |
221cc3ce | 1109 | } |