]> git.ipfire.org Git - pakfire.git/blame - src/libpakfire/package.c
files: Statically allocate name, user and group
[pakfire.git] / src / libpakfire / package.c
CommitLineData
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 46struct _PakfirePackage {
cbed1552 47 Pakfire pakfire;
7847efcd 48 Id id;
7847efcd 49 int nrefs;
e9d9eadc
MT
50
51 PakfireRepo repo;
7847efcd
MT
52};
53
f1c7996e 54static Pool* pakfire_package_get_solv_pool(PakfirePackage pkg) {
44b27b6f 55 return pakfire_get_solv_pool(pkg->pakfire);
f1c7996e
MT
56}
57
cbed1552 58static 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 64PAKFIRE_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 80PAKFIRE_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 102static 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
112PAKFIRE_EXPORT PakfirePackage pakfire_package_ref(PakfirePackage pkg) {
113 pkg->nrefs++;
114
115 return pkg;
116}
117
118PAKFIRE_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
129PAKFIRE_EXPORT Pakfire pakfire_package_get_pakfire(PakfirePackage pkg) {
130 return pakfire_ref(pkg->pakfire);
131}
132
221cc3ce 133static 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
139static Repo* pakfire_package_solv_repo(PakfirePackage pkg) {
140 Solvable* s = get_solvable(pkg);
141
142 return s->repo;
143}
144
145static 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 152PAKFIRE_EXPORT int pakfire_package_identical(PakfirePackage pkg1, PakfirePackage pkg2) {
221cc3ce
MT
153 return pkg1->id == pkg2->id;
154}
155
9f953e68 156PAKFIRE_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 196PAKFIRE_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 205PAKFIRE_EXPORT Id pakfire_package_id(PakfirePackage pkg) {
221cc3ce
MT
206 return pkg->id;
207}
208
9f953e68 209PAKFIRE_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 218PAKFIRE_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 225PAKFIRE_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 232PAKFIRE_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 239PAKFIRE_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
246PAKFIRE_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 265static 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 294PAKFIRE_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 312PAKFIRE_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 320PAKFIRE_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 328PAKFIRE_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 335PAKFIRE_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
342static 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
350static 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
365static 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
374static 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 383PAKFIRE_EXPORT const char* pakfire_package_get_uuid(PakfirePackage pkg) {
221cc3ce
MT
384 return pakfire_package_get_string(pkg, SOLVABLE_PKGID);
385}
386
9f953e68 387PAKFIRE_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 391PAKFIRE_EXPORT const char* pakfire_package_get_checksum(PakfirePackage pkg) {
221cc3ce
MT
392 return pakfire_package_get_string(pkg, SOLVABLE_CHECKSUM);
393}
394
9f953e68 395PAKFIRE_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 399PAKFIRE_EXPORT const char* pakfire_package_get_summary(PakfirePackage pkg) {
221cc3ce
MT
400 return pakfire_package_get_string(pkg, SOLVABLE_SUMMARY);
401}
402
9f953e68 403PAKFIRE_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 407PAKFIRE_EXPORT const char* pakfire_package_get_description(PakfirePackage pkg) {
221cc3ce
MT
408 return pakfire_package_get_string(pkg, SOLVABLE_DESCRIPTION);
409}
410
9f953e68 411PAKFIRE_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 415PAKFIRE_EXPORT const char* pakfire_package_get_license(PakfirePackage pkg) {
221cc3ce
MT
416 return pakfire_package_get_string(pkg, SOLVABLE_LICENSE);
417}
418
9f953e68 419PAKFIRE_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 423PAKFIRE_EXPORT const char* pakfire_package_get_url(PakfirePackage pkg) {
221cc3ce
MT
424 return pakfire_package_get_string(pkg, SOLVABLE_URL);
425}
426
9f953e68 427PAKFIRE_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
431PAKFIRE_EXPORT const char* pakfire_package_get_groups(PakfirePackage pkg) {
432 return pakfire_package_get_string(pkg, SOLVABLE_GROUP);
221cc3ce
MT
433}
434
7ccc9d74
MT
435PAKFIRE_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 444PAKFIRE_EXPORT const char* pakfire_package_get_vendor(PakfirePackage pkg) {
221cc3ce
MT
445 return pakfire_package_get_string(pkg, SOLVABLE_VENDOR);
446}
447
9f953e68 448PAKFIRE_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 452PAKFIRE_EXPORT const char* pakfire_package_get_maintainer(PakfirePackage pkg) {
221cc3ce
MT
453 return pakfire_package_get_string(pkg, SOLVABLE_PACKAGER);
454}
455
9f953e68 456PAKFIRE_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 460PAKFIRE_EXPORT const char* pakfire_package_get_filename(PakfirePackage pkg) {
221cc3ce
MT
461 return pakfire_package_get_string(pkg, SOLVABLE_MEDIAFILE);
462}
463
9f953e68 464PAKFIRE_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 468PAKFIRE_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
475static 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
482static 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 488PAKFIRE_EXPORT unsigned long long pakfire_package_get_downloadsize(PakfirePackage pkg) {
221cc3ce
MT
489 return pakfire_package_get_num(pkg, SOLVABLE_DOWNLOADSIZE);
490}
491
9f953e68 492PAKFIRE_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 496PAKFIRE_EXPORT unsigned long long pakfire_package_get_installsize(PakfirePackage pkg) {
221cc3ce
MT
497 return pakfire_package_get_num(pkg, SOLVABLE_INSTALLSIZE);
498}
499
9f953e68 500PAKFIRE_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 504PAKFIRE_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 511PAKFIRE_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
515PAKFIRE_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 519PAKFIRE_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
523PAKFIRE_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
af647c55 527PAKFIRE_EXPORT time_t pakfire_package_get_install_time(PakfirePackage pkg) {
221cc3ce
MT
528 return pakfire_package_get_num(pkg, SOLVABLE_INSTALLTIME);
529}
530
af647c55
MT
531PAKFIRE_EXPORT void pakfire_package_set_install_time(PakfirePackage pkg, time_t install_time) {
532 pakfire_package_set_num(pkg, SOLVABLE_INSTALLTIME, install_time);
533}
534
a9659851
MT
535static PakfireRelationList pakfire_package_get_relationlist(
536 PakfirePackage pkg, Id type, Id marker) {
221cc3ce
MT
537 Queue q;
538 queue_init(&q);
539
540 Solvable* s = get_solvable(pkg);
a9659851 541 solvable_lookup_deparray(s, type, &q, marker);
221cc3ce 542
d6ea32df
MT
543 PakfireRelationList list;
544 int r = pakfire_relationlist_from_queue(&list, pkg->pakfire, q);
545
221cc3ce
MT
546 queue_free(&q);
547
d6ea32df
MT
548 if (r)
549 return NULL;
550
551 return list;
221cc3ce
MT
552}
553
a9659851
MT
554static void pakfire_package_set_relationlist(PakfirePackage pkg, Id type,
555 PakfireRelationList relationlist, Id marker) {
221cc3ce 556 Solvable* s = get_solvable(pkg);
97eb9a58 557 Queue* q = pakfire_relationlist_get_queue(relationlist);
221cc3ce 558
97eb9a58
MT
559#if 0
560 // This implemention should be the fastest, but unfortunately does not work.
a9659851 561 solvable_set_deparray(s, type, q, marker);
97eb9a58
MT
562#else
563 for (int i = 0; i < q->count; i++)
a9659851 564 solvable_add_deparray(s, type, q->elements[i], marker);
221cc3ce 565#endif
221cc3ce
MT
566}
567
a9659851
MT
568static void pakfire_package_add_relation(PakfirePackage pkg, Id type,
569 PakfireRelation relation, Id marker) {
221cc3ce
MT
570 Solvable* s = get_solvable(pkg);
571
a9659851 572 solvable_add_deparray(s, type, pakfire_relation_get_id(relation), marker);
221cc3ce
MT
573}
574
9f953e68 575PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_provides(PakfirePackage pkg) {
063ca10f 576 return pakfire_package_get_relationlist(pkg, SOLVABLE_PROVIDES, -SOLVABLE_FILEMARKER);
221cc3ce
MT
577}
578
9f953e68 579PAKFIRE_EXPORT void pakfire_package_set_provides(PakfirePackage pkg, PakfireRelationList relationlist) {
063ca10f 580 pakfire_package_set_relationlist(pkg, SOLVABLE_PROVIDES, relationlist, -SOLVABLE_FILEMARKER);
221cc3ce
MT
581}
582
9f953e68 583PAKFIRE_EXPORT void pakfire_package_add_provides(PakfirePackage pkg, PakfireRelation relation) {
063ca10f 584 pakfire_package_add_relation(pkg, SOLVABLE_PROVIDES, relation, -SOLVABLE_FILEMARKER);
221cc3ce
MT
585}
586
9f953e68 587PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_prerequires(PakfirePackage pkg) {
063ca10f 588 return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, SOLVABLE_PREREQMARKER);
221cc3ce
MT
589}
590
dec8207e 591PAKFIRE_EXPORT void pakfire_package_set_prerequires(PakfirePackage pkg, PakfireRelationList relationlist) {
063ca10f 592 pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, SOLVABLE_PREREQMARKER);
dec8207e
MT
593}
594
9f953e68 595PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_requires(PakfirePackage pkg) {
063ca10f 596 return pakfire_package_get_relationlist(pkg, SOLVABLE_REQUIRES, -SOLVABLE_PREREQMARKER);
221cc3ce
MT
597}
598
9f953e68 599PAKFIRE_EXPORT void pakfire_package_set_requires(PakfirePackage pkg, PakfireRelationList relationlist) {
063ca10f 600 pakfire_package_set_relationlist(pkg, SOLVABLE_REQUIRES, relationlist, -SOLVABLE_PREREQMARKER);
221cc3ce
MT
601}
602
9f953e68 603PAKFIRE_EXPORT void pakfire_package_add_requires(PakfirePackage pkg, PakfireRelation relation) {
063ca10f 604 pakfire_package_add_relation(pkg, SOLVABLE_REQUIRES, relation, -SOLVABLE_PREREQMARKER);
221cc3ce
MT
605}
606
9f953e68 607PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_conflicts(PakfirePackage pkg) {
a9659851 608 return pakfire_package_get_relationlist(pkg, SOLVABLE_CONFLICTS, 0);
221cc3ce
MT
609}
610
9f953e68 611PAKFIRE_EXPORT void pakfire_package_set_conflicts(PakfirePackage pkg, PakfireRelationList relationlist) {
a9659851 612 pakfire_package_set_relationlist(pkg, SOLVABLE_CONFLICTS, relationlist, 0);
221cc3ce
MT
613}
614
9f953e68 615PAKFIRE_EXPORT void pakfire_package_add_conflicts(PakfirePackage pkg, PakfireRelation relation) {
a9659851 616 pakfire_package_add_relation(pkg, SOLVABLE_CONFLICTS, relation, 0);
221cc3ce
MT
617}
618
9f953e68 619PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_obsoletes(PakfirePackage pkg) {
a9659851 620 return pakfire_package_get_relationlist(pkg, SOLVABLE_OBSOLETES, 0);
221cc3ce
MT
621}
622
9f953e68 623PAKFIRE_EXPORT void pakfire_package_set_obsoletes(PakfirePackage pkg, PakfireRelationList relationlist) {
a9659851 624 pakfire_package_set_relationlist(pkg, SOLVABLE_OBSOLETES, relationlist, 0);
221cc3ce
MT
625}
626
9f953e68 627PAKFIRE_EXPORT void pakfire_package_add_obsoletes(PakfirePackage pkg, PakfireRelation relation) {
a9659851 628 pakfire_package_add_relation(pkg, SOLVABLE_OBSOLETES, relation, 0);
221cc3ce
MT
629}
630
9f953e68 631PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_recommends(PakfirePackage pkg) {
a9659851 632 return pakfire_package_get_relationlist(pkg, SOLVABLE_RECOMMENDS, 0);
221cc3ce
MT
633}
634
9f953e68 635PAKFIRE_EXPORT void pakfire_package_set_recommends(PakfirePackage pkg, PakfireRelationList relationlist) {
a9659851 636 pakfire_package_set_relationlist(pkg, SOLVABLE_RECOMMENDS, relationlist, 0);
221cc3ce
MT
637}
638
9f953e68 639PAKFIRE_EXPORT void pakfire_package_add_recommends(PakfirePackage pkg, PakfireRelation relation) {
a9659851 640 pakfire_package_add_relation(pkg, SOLVABLE_RECOMMENDS, relation, 0);
221cc3ce
MT
641}
642
9f953e68 643PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_suggests(PakfirePackage pkg) {
a9659851 644 return pakfire_package_get_relationlist(pkg, SOLVABLE_SUGGESTS, 0);
221cc3ce
MT
645}
646
9f953e68 647PAKFIRE_EXPORT void pakfire_package_set_suggests(PakfirePackage pkg, PakfireRelationList relationlist) {
a9659851 648 pakfire_package_set_relationlist(pkg, SOLVABLE_SUGGESTS, relationlist, 0);
221cc3ce
MT
649}
650
9f953e68 651PAKFIRE_EXPORT void pakfire_package_add_suggests(PakfirePackage pkg, PakfireRelation relation) {
a9659851 652 pakfire_package_add_relation(pkg, SOLVABLE_SUGGESTS, relation, 0);
221cc3ce
MT
653}
654
8fe2e4ba
MT
655PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_supplements(PakfirePackage pkg) {
656 return pakfire_package_get_relationlist(pkg, SOLVABLE_SUPPLEMENTS, 0);
657}
658
659PAKFIRE_EXPORT void pakfire_package_set_supplements(PakfirePackage pkg, PakfireRelationList relationlist) {
660 pakfire_package_set_relationlist(pkg, SOLVABLE_SUPPLEMENTS, relationlist, 0);
661}
662
663PAKFIRE_EXPORT PakfireRelationList pakfire_package_get_enhances(PakfirePackage pkg) {
664 return pakfire_package_get_relationlist(pkg, SOLVABLE_ENHANCES, 0);
665}
666
667PAKFIRE_EXPORT void pakfire_package_set_enhances(PakfirePackage pkg, PakfireRelationList relationlist) {
668 pakfire_package_set_relationlist(pkg, SOLVABLE_ENHANCES, relationlist, 0);
669}
670
9f953e68 671PAKFIRE_EXPORT PakfireRepo pakfire_package_get_repo(PakfirePackage pkg) {
e9d9eadc
MT
672 if (!pkg->repo) {
673 Solvable* s = get_solvable(pkg);
674
675 pkg->repo = pakfire_repo_create_from_repo(pkg->pakfire, s->repo);
676 }
221cc3ce 677
e9d9eadc 678 return pakfire_repo_ref(pkg->repo);
221cc3ce
MT
679}
680
9f953e68 681PAKFIRE_EXPORT void pakfire_package_set_repo(PakfirePackage pkg, PakfireRepo repo) {
e9d9eadc
MT
682 // Free old repository
683 if (pkg->repo)
684 pakfire_repo_unref(pkg->repo);
221cc3ce 685
e9d9eadc 686 Solvable* s = get_solvable(pkg);
3ff6aee6 687 s->repo = pakfire_repo_get_repo(repo);
e9d9eadc
MT
688
689 pkg->repo = pakfire_repo_ref(repo);
221cc3ce
MT
690}
691
9f953e68 692PAKFIRE_EXPORT char* pakfire_package_get_location(PakfirePackage pkg) {
221cc3ce
MT
693 pakfire_package_internalize_repo(pkg);
694
695 Solvable* s = get_solvable(pkg);
696
697 const char* location = solvable_get_location(s, NULL);
5d901566 698 return strdup(location);
221cc3ce
MT
699}
700
701static void pakfire_package_dump_add_line(char** str, const char* key, const char* val) {
702 if (val)
703 asprintf(str, "%s%-15s: %s\n", *str, key ? key : "", val);
704}
705
706static void pakfire_package_dump_add_lines(char** str, const char* key, const char* val) {
707 const char* string = val;
708
709 while (*string) {
710 char line[STRING_SIZE];
711 int counter = 0;
712
713 while (*string) {
714 if (*string == '\n') {
715 string++;
716 break;
717 }
718
719 line[counter++] = *string++;
720 }
721 line[counter] = '\0';
722
723 if (*line) {
724 pakfire_package_dump_add_line(str, key, line);
725 key = NULL;
726 }
727 }
728}
729
730static void pakfire_package_dump_add_line_date(char** str, const char* key, unsigned long long date) {
731 // Convert from integer to tm struct.
732 struct tm* timer = gmtime((time_t *)&date);
733
734 char val[STRING_SIZE];
735 strftime(val, STRING_SIZE, "%a, %d %b %Y %T %z", timer);
736
737 pakfire_package_dump_add_line(str, key, val);
738}
739
740static void pakfire_package_dump_add_line_relations(char** str, const char* key, PakfireRelationList deps) {
7ac9a26b
MT
741 pakfire_relationlist_sort(deps);
742
b76d7b47
MT
743 size_t size = pakfire_relationlist_size(deps);
744 for (unsigned int i = 0; i < size; i++) {
745 PakfireRelation relation = pakfire_relationlist_get(deps, i);
221cc3ce
MT
746
747 if (relation) {
748 char* dep = pakfire_relation_str(relation);
1a497776 749 pakfire_relation_unref(relation);
221cc3ce 750
221cc3ce
MT
751 if (dep) {
752 pakfire_package_dump_add_line(str, (i == 0) ? key : "", dep);
f0d6233d 753 free(dep);
221cc3ce
MT
754 }
755 }
756 }
757}
758
759static void pakfire_package_dump_add_line_size(char** str, const char* key, unsigned long long size) {
760 char* val = pakfire_format_size(size);
761
762 if (val) {
763 pakfire_package_dump_add_line(str, key, val);
f0d6233d 764 free(val);
221cc3ce
MT
765 }
766}
767
9f953e68 768PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) {
221cc3ce
MT
769 char* string = "";
770
771 // Name
772 const char* name = pakfire_package_get_name(pkg);
773 pakfire_package_dump_add_line(&string, _("Name"), name);
774
775 // Version
776 const char* version = pakfire_package_get_version(pkg);
777 pakfire_package_dump_add_line(&string, _("Version"), version);
778
779 // Release
780 const char* release = pakfire_package_get_release(pkg);
781 pakfire_package_dump_add_line(&string, _("Release"), release);
782
783 // Size
784 unsigned long long size = pakfire_package_get_size(pkg);
785 pakfire_package_dump_add_line_size(&string, _("Size"), size);
786
787 // Installed size
788 if (pakfire_package_is_installed(pkg)) {
789 unsigned long long installsize = pakfire_package_get_installsize(pkg);
790 pakfire_package_dump_add_line_size(&string, _("Installed size"), installsize);
791
792 // Downloadsize
793 } else {
794 unsigned long long downloadsize = pakfire_package_get_downloadsize(pkg);
795 pakfire_package_dump_add_line_size(&string, _("Download size"), downloadsize);
796 }
797
798 PakfireRepo repo = pakfire_package_get_repo(pkg);
799 if (repo) {
800 const char* repo_name = pakfire_repo_get_name(repo);
801 pakfire_package_dump_add_line(&string, _("Repo"), repo_name);
802
3ff6aee6 803 pakfire_repo_unref(repo);
221cc3ce
MT
804 }
805
806 // Summary
807 const char* summary = pakfire_package_get_summary(pkg);
808 pakfire_package_dump_add_line(&string, _("Summary"), summary);
809
810 // Description
811 const char* description = pakfire_package_get_description(pkg);
1b99c676
MT
812 if (description)
813 pakfire_package_dump_add_lines(&string, _("Description"), description);
221cc3ce
MT
814
815 // Groups
7ccc9d74 816 const char* groups = pakfire_package_get_groups(pkg);
2141b578 817 if (groups) {
7ccc9d74 818 pakfire_package_dump_add_lines(&string, _("Groups"), groups);
2141b578 819 }
221cc3ce
MT
820
821 // URL
822 const char* url = pakfire_package_get_url(pkg);
823 pakfire_package_dump_add_line(&string, _("URL"), url);
824
825 // License
826 const char* license = pakfire_package_get_license(pkg);
827 pakfire_package_dump_add_line(&string, _("License"), license);
828
829 if (flags & PAKFIRE_PKG_DUMP_LONG) {
af647c55
MT
830 // Install Time
831 time_t install_time = pakfire_package_get_install_time(pkg);
832 if (install_time) {
833 pakfire_package_dump_add_line_date(&string, _("Install Time"), install_time);
834 }
835
221cc3ce
MT
836 // Maintainer
837 const char* maintainer = pakfire_package_get_maintainer(pkg);
838 pakfire_package_dump_add_line(&string, _("Maintainer"), maintainer);
839
840 // Vendor
841 const char* vendor = pakfire_package_get_vendor(pkg);
842 pakfire_package_dump_add_line(&string, _("Vendor"), vendor);
843
844 // UUID
845 const char* uuid = pakfire_package_get_uuid(pkg);
846 pakfire_package_dump_add_line(&string, _("UUID"), uuid);
847
848 // Build time
2ffc704d
MT
849 time_t build_time = pakfire_package_get_build_time(pkg);
850 pakfire_package_dump_add_line_date(&string, _("Build Time"), build_time);
221cc3ce
MT
851
852 // Build host
2ffc704d
MT
853 const char* build_host = pakfire_package_get_build_host(pkg);
854 pakfire_package_dump_add_line(&string, _("Build Host"), build_host);
221cc3ce 855
2f873877
MT
856 // Dependencies
857
858 const struct relation {
859 const char* name;
860 PakfireRelationList (*get)(PakfirePackage pkg);
861 } relations[] = {
862 { _("Provides"), pakfire_package_get_provides, },
863 { _("Pre-Requires"), pakfire_package_get_prerequires, },
864 { _("Requires"), pakfire_package_get_requires, },
865 { _("Conflicts"), pakfire_package_get_conflicts, },
866 { _("Obsoletes"), pakfire_package_get_obsoletes, },
867 { _("Recommends"), pakfire_package_get_recommends, },
868 { _("Suggests"), pakfire_package_get_suggests, },
8fe2e4ba
MT
869 { _("Supplements"), pakfire_package_get_supplements, },
870 { _("Enhances"), pakfire_package_get_enhances, },
2f873877
MT
871 { NULL, NULL},
872 };
873
874 for (const struct relation* relation = relations; relation->name; relation++) {
875 PakfireRelationList list = relation->get(pkg);
876 if (list) {
877 pakfire_package_dump_add_line_relations(&string, relation->name, list);
878 pakfire_relationlist_unref(list);
879 }
221cc3ce
MT
880 }
881 }
882
883 if (flags & PAKFIRE_PKG_DUMP_FILELIST) {
5e9463ec
MT
884 PakfireFilelist filelist = pakfire_package_get_filelist(pkg);
885
886 const char* prefix = _("Filelist");
887
888 for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) {
889 PakfireFile file = pakfire_filelist_get(filelist, i);
221cc3ce 890
221cc3ce
MT
891 const char* name = pakfire_file_get_name(file);
892 pakfire_package_dump_add_line(&string, prefix, name);
893
5e9463ec 894 pakfire_file_unref(file);
221cc3ce 895
5e9463ec 896 // Only prefix the first line
221cc3ce
MT
897 prefix = NULL;
898 }
5e9463ec
MT
899
900 pakfire_filelist_unref(filelist);
221cc3ce
MT
901 }
902
903 return string;
904}
905
9f953e68 906PAKFIRE_EXPORT int pakfire_package_is_cached(PakfirePackage pkg) {
3f7bcbc4 907 char* path = pakfire_package_get_cache_path(pkg);
cbed1552 908
3f7bcbc4 909 // Check if the file is readable
12656820 910 int r = pakfire_access(pkg->pakfire, path, NULL, R_OK);
f0d6233d 911 free(path);
221cc3ce 912
cf27dd9c 913 return (r == 0);
221cc3ce
MT
914}
915
9f953e68 916PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) {
3f7bcbc4 917 char buffer[STRING_SIZE] = "";
221cc3ce 918
3f7bcbc4
MT
919 const char* filename = pakfire_package_get_filename(pkg);
920 const char* checksum = pakfire_package_get_checksum(pkg);
221cc3ce 921
3f7bcbc4 922 if (strlen(checksum) < 3)
221cc3ce
MT
923 return NULL;
924
3f7bcbc4
MT
925 snprintf(buffer, sizeof(buffer), "%c%c/%s/%s", checksum[0], checksum[1],
926 checksum + 2, filename);
221cc3ce 927
3f7bcbc4 928 return pakfire_get_cache_path(pkg->pakfire, buffer);
221cc3ce
MT
929}
930
cbed1552 931PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) {
cbed1552 932 // Otherwise open the archive from the cache
3f7bcbc4 933 char* path = pakfire_package_get_cache_path(pkg);
641cab18
MT
934 if (!path)
935 return NULL;
cbed1552 936
641cab18
MT
937 // Open archive
938 PakfireArchive archive = pakfire_archive_open(pkg->pakfire, path);
f0d6233d 939 free(path);
cbed1552
MT
940
941 return archive;
942}
943
f4560c3f 944static int pakfire_package_fetch_legacy_filelist(PakfirePackage pkg, PakfireFilelist filelist) {
221cc3ce
MT
945 pakfire_package_internalize_repo(pkg);
946
221cc3ce
MT
947 PakfireRepo repo = pakfire_package_get_repo(pkg);
948 Solvable* s = get_solvable(pkg);
f1c7996e 949 Pool* p = pakfire_package_get_solv_pool(pkg);
3ff6aee6 950 Repo* r = pakfire_repo_get_repo(repo);
5e9463ec 951 pakfire_repo_unref(repo);
221cc3ce
MT
952
953 int found_marker = 0;
954
955 Id id, *ids;
956 ids = r->idarraydata + s->provides;
957 while((id = *ids++) != 0) {
958 const char* filename = pool_dep2str(p, id);
959
960 if (found_marker) {
5e9463ec 961 PakfireFile file;
221cc3ce 962
5e9463ec
MT
963 int r = pakfire_file_create(&file);
964 if (r)
965 return r;
966
967 // Set name
221cc3ce 968 pakfire_file_set_name(file, filename);
5e9463ec 969
f4560c3f 970 r = pakfire_filelist_append(filelist, file);
5e9463ec
MT
971 if (r)
972 return r;
973
974 pakfire_file_unref(file);
221cc3ce
MT
975 continue;
976 }
977
978 if (strcmp(filename, PAKFIRE_SOLVABLE_FILEMARKER) == 0)
979 ++found_marker;
980 }
981
5e9463ec 982 return 0;
221cc3ce
MT
983}
984
f4560c3f 985static int pakfire_package_fetch_filelist(PakfirePackage pkg, PakfireFilelist filelist) {
5e9463ec
MT
986 int r;
987
221cc3ce
MT
988 pakfire_package_internalize_repo(pkg);
989
f1c7996e 990 Pool* pool = pakfire_package_get_solv_pool(pkg);
221cc3ce
MT
991 Repo* repo = pakfire_package_solv_repo(pkg);
992 Id handle = pakfire_package_get_handle(pkg);
993
994 Dataiterator di;
995 dataiterator_init(&di, pool, repo, handle,
996 SOLVABLE_FILELIST, NULL, SEARCH_FILES | SEARCH_COMPLETE_FILELIST);
5e9463ec 997
221cc3ce 998 while (dataiterator_step(&di)) {
5e9463ec 999 PakfireFile file;
221cc3ce 1000
5e9463ec
MT
1001 r = pakfire_file_create(&file);
1002 if (r)
1003 return r;
221cc3ce 1004
5e9463ec 1005 pakfire_file_set_name(file, di.kv.str);
221cc3ce 1006
5e9463ec 1007 // Append to filelist
f4560c3f 1008 pakfire_filelist_append(filelist, file);
5e9463ec 1009 pakfire_file_unref(file);
221cc3ce 1010 }
5e9463ec 1011 dataiterator_free(&di);
221cc3ce
MT
1012
1013 // If the file list is empty, we fall back to read files
1014 // in the older format.
f4560c3f
MT
1015 if (pakfire_filelist_is_empty(filelist)) {
1016 r = pakfire_package_fetch_legacy_filelist(pkg, filelist);
5e9463ec
MT
1017 if (r)
1018 return r;
1019 }
1020
1021 // Sort the list
f4560c3f 1022 pakfire_filelist_sort(filelist);
221cc3ce 1023
5e9463ec 1024 return 0;
221cc3ce
MT
1025}
1026
5e9463ec 1027PAKFIRE_EXPORT PakfireFilelist pakfire_package_get_filelist(PakfirePackage pkg) {
f4560c3f 1028 PakfireFilelist filelist;
5e9463ec 1029
f4560c3f
MT
1030 int r = pakfire_filelist_create(&filelist);
1031 if (r)
1032 return NULL;
5e9463ec 1033
f4560c3f
MT
1034 r = pakfire_package_fetch_filelist(pkg, filelist);
1035 if (r) {
1036 pakfire_filelist_unref(filelist);
1037 return NULL;
221cc3ce
MT
1038 }
1039
f4560c3f 1040 return filelist;
221cc3ce
MT
1041}
1042
d995f7fc 1043static int pakfire_package_append_file(PakfirePackage pkg, const char* path) {
5e9463ec 1044 // Fetch repodata
221cc3ce 1045 PakfireRepo repo = pakfire_package_get_repo(pkg);
3ff6aee6 1046 Repodata* repodata = pakfire_repo_get_repodata(repo);
5e9463ec 1047 pakfire_repo_unref(repo);
221cc3ce
MT
1048
1049 Id handle = pakfire_package_get_handle(pkg);
1050
d995f7fc
MT
1051 char* basename = pakfire_basename(path);
1052 char* dirname = pakfire_dirname(path);
1053
1054 // Convert directory into ID
1055 Id did = repodata_str2dir(repodata, dirname, 1);
1056 if (!did)
1057 did = repodata_str2dir(repodata, "/", 1);
1058
1059 // Add data to list
1060 repodata_add_dirstr(repodata, handle,
1061 SOLVABLE_FILELIST, did, basename);
1062
1063 free(basename);
1064 free(dirname);
1065
1066 return 0;
1067}
1068
1069PAKFIRE_EXPORT int pakfire_package_set_filelist(PakfirePackage pkg, PakfireFilelist filelist) {
1070 int r;
1071
5e9463ec
MT
1072 for (unsigned int i = 0; i < pakfire_filelist_size(filelist); i++) {
1073 PakfireFile file = pakfire_filelist_get(filelist, i);
221cc3ce 1074
d995f7fc
MT
1075 const char* name = pakfire_file_get_name(file);
1076 if (name) {
1077 r = pakfire_package_append_file(pkg, name);
1078 if (r) {
1079 pakfire_file_unref(file);
1080 return r;
1081 }
1082 }
1083
1084 pakfire_file_unref(file);
1085
1086 }
1087
1088 return 0;
1089}
221cc3ce 1090
d995f7fc
MT
1091int pakfire_package_set_filelist_from_string(PakfirePackage pkg, const char* files) {
1092 // Total length of the input string
1093 size_t size = strlen(files);
221cc3ce 1094
d995f7fc
MT
1095 // Copy files to the stack to be able to modify it
1096 char* buffer = alloca(size + 1);
3ff6aee6 1097
d995f7fc
MT
1098 // Copy the content
1099 memcpy(buffer, files, size);
1100
1101 // Replace all line breaks with NUL
1102 for (unsigned int i = 0; i < size; i++) {
1103 if (buffer[i] == '\n')
1104 buffer[i] = '\0';
1105 }
1106
1107 size_t p = 0;
1108 while (p < size) {
1109 const char* name = &buffer[p];
1110
1111 int r = pakfire_package_append_file(pkg, name);
1112 if (r)
1113 return r;
1114
1115 p += strlen(name) + 1;
5e9463ec 1116 }
221cc3ce 1117
5e9463ec 1118 return 0;
221cc3ce 1119}