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