libopkg: introduce SF_NEED_DETAIL flag
[project/opkg-lede.git] / libopkg / opkg.c
1 /* opkg.c - the opkg package management system
2
3 Thomas Wood <thomas@openedhand.com>
4
5 Copyright (C) 2008 OpenMoko Inc
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include "config.h"
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <fnmatch.h>
23
24 #include "opkg.h"
25 #include "opkg_conf.h"
26
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31 #include "opkg_upgrade.h"
32
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
35
36 #include <libbb/libbb.h>
37
38 #define opkg_assert(expr) if (!(expr)) { \
39 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
40 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
41
42 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
43
44 /** Private Functions ***/
45
46 static int opkg_configure_packages(char *pkg_name)
47 {
48 pkg_vec_t *all;
49 int i;
50 pkg_t *pkg;
51 int r, err = 0;
52
53 all = pkg_vec_alloc();
54 pkg_hash_fetch_available(all);
55
56 for (i = 0; i < all->len; i++) {
57 pkg = all->pkgs[i];
58
59 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
60 continue;
61
62 if (pkg->state_status == SS_UNPACKED) {
63 r = opkg_configure(pkg);
64 if (r == 0) {
65 pkg->state_status = SS_INSTALLED;
66 pkg->parent->state_status = SS_INSTALLED;
67 pkg->state_flag &= ~SF_PREFER;
68 } else {
69 if (!err)
70 err = r;
71 }
72 }
73 }
74
75 pkg_vec_free(all);
76 return err;
77 }
78
79 struct _curl_cb_data {
80 opkg_progress_callback_t cb;
81 opkg_progress_data_t *progress_data;
82 void *user_data;
83 int start_range;
84 int finish_range;
85 };
86
87 int curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
88 double d, /* dlnow */
89 double ultotal, double ulnow)
90 {
91 int p = (t) ? d * 100 / t : 0;
92 static int prev = -1;
93 int progress = 0;
94
95 /* prevent the same value being sent twice (can occur due to rounding) */
96 if (p == prev)
97 return 0;
98 prev = p;
99
100 if (t < 1)
101 return 0;
102
103 progress = cb_data->start_range +
104 (d / t * ((cb_data->finish_range - cb_data->start_range)));
105 cb_data->progress_data->percentage = progress;
106
107 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
108
109 return 0;
110 }
111
112 static struct opkg_conf saved_conf;
113 /*** Public API ***/
114
115 int opkg_new()
116 {
117 saved_conf = *conf;
118
119 if (opkg_conf_init())
120 goto err0;
121
122 if (opkg_conf_load())
123 goto err0;
124
125 if (pkg_hash_load_feeds())
126 goto err1;
127
128 if (pkg_hash_load_status_files())
129 goto err1;
130
131 return 0;
132
133 err1:
134 pkg_hash_deinit();
135 err0:
136 opkg_conf_deinit();
137 return -1;
138 }
139
140 void opkg_free(void)
141 {
142 #ifdef HAVE_CURL
143 opkg_curl_cleanup();
144 #endif
145 opkg_conf_deinit();
146 }
147
148 int opkg_re_read_config_files(void)
149 {
150 opkg_free();
151 *conf = saved_conf;
152 return opkg_new();
153 }
154
155 void opkg_get_option(char *option, void **value)
156 {
157 int i = 0;
158 extern opkg_option_t options[];
159
160 /* look up the option
161 * TODO: this would be much better as a hash table
162 */
163 while (options[i].name) {
164 if (strcmp(options[i].name, option) != 0) {
165 i++;
166 continue;
167 }
168 }
169
170 /* get the option */
171 switch (options[i].type) {
172 case OPKG_OPT_TYPE_BOOL:
173 *((int *)value) = *((int *)options[i].value);
174 return;
175
176 case OPKG_OPT_TYPE_INT:
177 *((int *)value) = *((int *)options[i].value);
178 return;
179
180 case OPKG_OPT_TYPE_STRING:
181 *((char **)value) = xstrdup(options[i].value);
182 return;
183 }
184
185 }
186
187 void opkg_set_option(char *option, void *value)
188 {
189 int i = 0, found = 0;
190 extern opkg_option_t options[];
191
192 opkg_assert(option != NULL);
193 opkg_assert(value != NULL);
194
195 /* look up the option
196 * TODO: this would be much better as a hash table
197 */
198 while (options[i].name) {
199 if (strcmp(options[i].name, option) == 0) {
200 found = 1;
201 break;
202 }
203 i++;
204 }
205
206 if (!found) {
207 opkg_msg(ERROR, "Invalid option: %s\n", option);
208 return;
209 }
210
211 /* set the option */
212 switch (options[i].type) {
213 case OPKG_OPT_TYPE_BOOL:
214 if (*((int *)value) == 0)
215 *((int *)options[i].value) = 0;
216 else
217 *((int *)options[i].value) = 1;
218 return;
219
220 case OPKG_OPT_TYPE_INT:
221 *((int *)options[i].value) = *((int *)value);
222 return;
223
224 case OPKG_OPT_TYPE_STRING:
225 *((char **)options[i].value) = xstrdup(value);
226 return;
227 }
228
229 }
230
231 /**
232 * @brief libopkg API: Install package
233 * @param package_name The name of package in which is going to install
234 * @param progress_callback The callback function that report the status to caller.
235 */
236 int
237 opkg_install_package(const char *package_name,
238 opkg_progress_callback_t progress_callback,
239 void *user_data)
240 {
241 int err;
242 char *stripped_filename, *local_filename;
243 opkg_progress_data_t pdata;
244 pkg_t *old, *new;
245 pkg_vec_t *deps, *all;
246 int i, ndepends;
247 char **unresolved = NULL;
248 const char *filename;
249
250 opkg_assert(package_name != NULL);
251
252 /* ... */
253 pkg_info_preinstall_check();
254
255 /* check to ensure package is not already installed */
256 old = pkg_hash_fetch_installed_by_name(package_name);
257 if (old) {
258 opkg_msg(ERROR, "Package %s is already installed\n",
259 package_name);
260 return -1;
261 }
262
263 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
264 if (!new) {
265 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
266 return -1;
267 }
268
269 new->state_flag |= SF_USER;
270
271 pdata.action = -1;
272 pdata.pkg = new;
273
274 progress(pdata, 0);
275
276 /* find dependancies and download them */
277 deps = pkg_vec_alloc();
278 /* this function does not return the original package, so we insert it later */
279 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
280 &unresolved);
281 if (unresolved) {
282 char **tmp = unresolved;
283 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
284 " for %s:\n", package_name);
285 while (*tmp) {
286 opkg_msg(ERROR, "\t%s", *tmp);
287 free(*tmp);
288 tmp++;
289 }
290 free(unresolved);
291 pkg_vec_free(deps);
292 opkg_message(ERROR, "\n");
293 return -1;
294 }
295
296 /* insert the package we are installing so that we download it */
297 pkg_vec_insert(deps, new);
298
299 /* download package and dependencies */
300 for (i = 0; i < deps->len; i++) {
301 pkg_t *pkg;
302 struct _curl_cb_data cb_data;
303 char *url;
304
305 pkg = deps->pkgs[i];
306 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
307 continue;
308
309 pdata.pkg = pkg;
310 pdata.action = OPKG_DOWNLOAD;
311
312 if (pkg->src == NULL) {
313 opkg_msg(ERROR, "Package %s not available from any "
314 "configured src\n", package_name);
315 return -1;
316 }
317
318 filename = pkg_get_string(pkg, PKG_FILENAME);
319
320 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
321
322 /* Get the filename part, without any directory */
323 stripped_filename = strrchr(filename, '/');
324 if (!stripped_filename)
325 stripped_filename = (char *)filename;
326
327 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
328 stripped_filename);
329
330 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
331
332 cb_data.cb = progress_callback;
333 cb_data.progress_data = &pdata;
334 cb_data.user_data = user_data;
335 /* 75% of "install" progress is for downloading */
336 cb_data.start_range = 75 * i / deps->len;
337 cb_data.finish_range = 75 * (i + 1) / deps->len;
338
339 err = opkg_download(url, local_filename,
340 (curl_progress_func) curl_progress_cb,
341 &cb_data, 0);
342 free(url);
343
344 if (err) {
345 pkg_vec_free(deps);
346 return -1;
347 }
348
349 }
350 pkg_vec_free(deps);
351
352 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
353 all = pkg_vec_alloc();
354 pkg_hash_fetch_available(all);
355 for (i = 0; i < all->len; i++) {
356 all->pkgs[i]->parent->dependencies_checked = 0;
357 }
358 pkg_vec_free(all);
359
360 /* 75% of "install" progress is for downloading */
361 pdata.pkg = new;
362 pdata.action = OPKG_INSTALL;
363 progress(pdata, 75);
364
365 /* unpack the package */
366 err = opkg_install_pkg(new, 0);
367
368 if (err) {
369 return -1;
370 }
371
372 progress(pdata, 75);
373
374 /* run configure scripts, etc. */
375 err = opkg_configure_packages(NULL);
376 if (err) {
377 return -1;
378 }
379
380 /* write out status files and file lists */
381 opkg_conf_write_status_files();
382 pkg_write_changed_filelists();
383
384 progress(pdata, 100);
385 return 0;
386 }
387
388 int
389 opkg_remove_package(const char *package_name,
390 opkg_progress_callback_t progress_callback, void *user_data)
391 {
392 int err;
393 pkg_t *pkg = NULL;
394 pkg_t *pkg_to_remove;
395 opkg_progress_data_t pdata;
396
397 opkg_assert(package_name != NULL);
398
399 pkg_info_preinstall_check();
400
401 pkg = pkg_hash_fetch_installed_by_name(package_name);
402
403 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
404 opkg_msg(ERROR, "Package %s not installed\n", package_name);
405 return -1;
406 }
407
408 pdata.action = OPKG_REMOVE;
409 pdata.pkg = pkg;
410 progress(pdata, 0);
411
412 if (conf->restrict_to_default_dest) {
413 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
414 conf->
415 default_dest);
416 } else {
417 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
418 }
419
420 progress(pdata, 75);
421
422 err = opkg_remove_pkg(pkg_to_remove, 0);
423
424 /* write out status files and file lists */
425 opkg_conf_write_status_files();
426 pkg_write_changed_filelists();
427
428 progress(pdata, 100);
429 return (err) ? -1 : 0;
430 }
431
432 int
433 opkg_upgrade_package(const char *package_name,
434 opkg_progress_callback_t progress_callback,
435 void *user_data)
436 {
437 int err;
438 pkg_t *pkg;
439 opkg_progress_data_t pdata;
440
441 opkg_assert(package_name != NULL);
442
443 pkg_info_preinstall_check();
444
445 if (conf->restrict_to_default_dest) {
446 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
447 conf->default_dest);
448 } else {
449 pkg = pkg_hash_fetch_installed_by_name(package_name);
450 }
451
452 if (!pkg) {
453 opkg_msg(ERROR, "Package %s not installed\n", package_name);
454 return -1;
455 }
456
457 pdata.action = OPKG_INSTALL;
458 pdata.pkg = pkg;
459 progress(pdata, 0);
460
461 err = opkg_upgrade_pkg(pkg);
462 if (err) {
463 return -1;
464 }
465 progress(pdata, 75);
466
467 err = opkg_configure_packages(NULL);
468 if (err) {
469 return -1;
470 }
471
472 /* write out status files and file lists */
473 opkg_conf_write_status_files();
474 pkg_write_changed_filelists();
475
476 progress(pdata, 100);
477 return 0;
478 }
479
480 int
481 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
482 {
483 pkg_vec_t *installed;
484 int err = 0;
485 int i;
486 pkg_t *pkg;
487 opkg_progress_data_t pdata;
488
489 pdata.action = OPKG_INSTALL;
490 pdata.pkg = NULL;
491
492 progress(pdata, 0);
493
494 installed = pkg_vec_alloc();
495 pkg_info_preinstall_check();
496
497 pkg_hash_fetch_all_installed(installed);
498 for (i = 0; i < installed->len; i++) {
499 pkg = installed->pkgs[i];
500
501 pdata.pkg = pkg;
502 progress(pdata, 99 * i / installed->len);
503
504 err += opkg_upgrade_pkg(pkg);
505 }
506 pkg_vec_free(installed);
507
508 if (err)
509 return 1;
510
511 err = opkg_configure_packages(NULL);
512 if (err)
513 return 1;
514
515 /* write out status files and file lists */
516 opkg_conf_write_status_files();
517 pkg_write_changed_filelists();
518
519 pdata.pkg = NULL;
520 progress(pdata, 100);
521 return 0;
522 }
523
524 int
525 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
526 void *user_data)
527 {
528 char *tmp;
529 int err, result = 0;
530 char *lists_dir;
531 pkg_src_list_elt_t *iter;
532 pkg_src_t *src;
533 int sources_list_count, sources_done;
534 opkg_progress_data_t pdata;
535
536 pdata.action = OPKG_DOWNLOAD;
537 pdata.pkg = NULL;
538 progress(pdata, 0);
539
540 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
541 ? conf->default_dest->lists_dir : conf->lists_dir);
542
543 if (!file_is_dir(lists_dir)) {
544 if (file_exists(lists_dir)) {
545 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
546 free(lists_dir);
547 return 1;
548 }
549
550 err = file_mkdir_hier(lists_dir, 0755);
551 if (err) {
552 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
553 lists_dir);
554 free(lists_dir);
555 return 1;
556 }
557 }
558
559 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
560 if (mkdtemp(tmp) == NULL) {
561 opkg_perror(ERROR, "Coundn't create temporary directory %s",
562 tmp);
563 free(lists_dir);
564 free(tmp);
565 return 1;
566 }
567
568 /* count the number of sources so we can give some progress updates */
569 sources_list_count = 0;
570 sources_done = 0;
571 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
572 sources_list_count++;
573 }
574
575 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
576 char *url, *list_file_name = NULL;
577
578 src = (pkg_src_t *) iter->data;
579
580 if (src->extra_data) /* debian style? */
581 sprintf_alloc(&url, "%s/%s/%s", src->value,
582 src->extra_data,
583 src->gzip ? "Packages.gz" : "Packages");
584 else
585 sprintf_alloc(&url, "%s/%s", src->value,
586 src->gzip ? "Packages.gz" : "Packages");
587
588 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
589
590 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
591 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
592 result = -1;
593 }
594 free(url);
595
596 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
597 if (conf->check_signature) {
598 char *sig_file_name;
599 /* download detached signitures to verify the package lists */
600 /* get the url for the sig file */
601 if (src->extra_data) /* debian style? */
602 sprintf_alloc(&url, "%s/%s/%s", src->value,
603 src->extra_data, "Packages.sig");
604 else
605 sprintf_alloc(&url, "%s/%s", src->value,
606 "Packages.sig");
607
608 /* create filename for signature */
609 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
610 src->name);
611
612 /* make sure there is no existing signature file */
613 unlink(sig_file_name);
614
615 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
616 if (err) {
617 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
618 } else {
619 int err;
620 err = opkg_verify_file(list_file_name,
621 sig_file_name);
622 if (err == 0) {
623 opkg_msg(INFO, "Signature check "
624 "passed for %s",
625 list_file_name);
626 } else {
627 opkg_msg(ERROR, "Signature check "
628 "failed for %s",
629 list_file_name);
630 }
631 }
632 free(sig_file_name);
633 free(url);
634 }
635 #else
636 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
637 " has not been enabled in this build\n",
638 list_file_name);
639 #endif
640 free(list_file_name);
641
642 sources_done++;
643 progress(pdata, 100 * sources_done / sources_list_count);
644 }
645
646 rmdir(tmp);
647 free(tmp);
648 free(lists_dir);
649
650 /* Now re-read the package lists to update package hash tables. */
651 opkg_re_read_config_files();
652
653 return result;
654 }
655
656 static int pkg_compare_names_and_version(const void *a0, const void *b0)
657 {
658 const pkg_t *a = *(const pkg_t **)a0;
659 const pkg_t *b = *(const pkg_t **)b0;
660 int ret;
661
662 ret = strcmp(a->name, b->name);
663
664 if (ret == 0)
665 ret = pkg_compare_versions(a, b);
666
667 return ret;
668 }
669
670 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
671 {
672 pkg_vec_t *all;
673 int i;
674
675 opkg_assert(callback);
676
677 all = pkg_vec_alloc();
678 pkg_hash_fetch_available(all);
679
680 pkg_vec_sort(all, pkg_compare_names_and_version);
681
682 for (i = 0; i < all->len; i++) {
683 pkg_t *pkg;
684
685 pkg = all->pkgs[i];
686
687 callback(pkg, user_data);
688 }
689
690 pkg_vec_free(all);
691
692 return 0;
693 }
694
695 int
696 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
697 {
698 struct active_list *head;
699 struct active_list *node;
700 pkg_t *old = NULL, *new = NULL;
701
702 opkg_assert(callback);
703
704 /* ensure all data is valid */
705 pkg_info_preinstall_check();
706
707 head = prepare_upgrade_list();
708 for (node = active_list_next(head, head); node;
709 node = active_list_next(head, node)) {
710 old = node->pkg;
711 new =
712 pkg_hash_fetch_best_installation_candidate_by_name(old->
713 name);
714 if (new == NULL)
715 continue;
716 callback(new, user_data);
717 }
718 active_list_head_delete(head);
719 return 0;
720 }
721
722 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
723 const char *repo)
724 {
725 int pkg_found = 0;
726 pkg_t *pkg = NULL;
727 pkg_vec_t *all;
728 int i;
729 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
730
731 all = pkg_vec_alloc();
732 pkg_hash_fetch_available(all);
733 for (i = 0; i < all->len; i++) {
734 char *pkgv;
735
736 pkg = all->pkgs[i];
737
738 /* check name */
739 if (sstrcmp(pkg->name, name))
740 continue;
741
742 /* check version */
743 pkgv = pkg_version_str_alloc(pkg);
744 if (sstrcmp(pkgv, ver)) {
745 free(pkgv);
746 continue;
747 }
748 free(pkgv);
749
750 /* check architecture */
751 if (arch) {
752 if (sstrcmp(pkg_get_architecture(pkg), arch))
753 continue;
754 }
755
756 /* check repository */
757 if (repo) {
758 if (sstrcmp(pkg->src->name, repo))
759 continue;
760 }
761
762 /* match found */
763 pkg_found = 1;
764 break;
765 }
766
767 pkg_vec_free(all);
768
769 return pkg_found ? pkg : NULL;
770 }
771
772 /**
773 * @brief Check the accessibility of repositories.
774 * @return return how many repositories cannot access. 0 means all okay.
775 */
776 int opkg_repository_accessibility_check(void)
777 {
778 pkg_src_list_elt_t *iter;
779 str_list_elt_t *iter1;
780 str_list_t *src;
781 int repositories = 0;
782 int ret = 0;
783 char *repo_ptr;
784 char *stmp;
785 char *host, *end;
786
787 src = str_list_alloc();
788
789 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
790 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
791 end = index(host, '/');
792 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
793 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
794 end -
795 ((pkg_src_t *) iter->data)->value);
796 else
797 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
798
799 for (iter1 = str_list_first(src); iter1;
800 iter1 = str_list_next(src, iter1)) {
801 if (strstr(iter1->data, stmp))
802 break;
803 }
804 if (iter1)
805 continue;
806
807 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
808 free(stmp);
809
810 str_list_append(src, repo_ptr);
811 free(repo_ptr);
812 repositories++;
813 }
814
815 while (repositories > 0) {
816 iter1 = str_list_pop(src);
817 repositories--;
818
819 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
820 ret++;
821 str_list_elt_deinit(iter1);
822 }
823
824 free(src);
825
826 return ret;
827 }