Return negative error code from opkg_download(), not curl error codes.
[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 pdata.pkg = NULL;
527 progress(pdata, 100);
528 return 0;
529 }
530
531 int
532 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
533 void *user_data)
534 {
535 char *tmp;
536 int err, result = 0;
537 char *lists_dir;
538 pkg_src_list_elt_t *iter;
539 pkg_src_t *src;
540 int sources_list_count, sources_done;
541 opkg_progress_data_t pdata;
542
543 pdata.action = OPKG_DOWNLOAD;
544 pdata.pkg = NULL;
545 progress(pdata, 0);
546
547 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
548 ? conf->default_dest->lists_dir : conf->lists_dir);
549
550 if (!file_is_dir(lists_dir)) {
551 if (file_exists(lists_dir)) {
552 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
553 free(lists_dir);
554 return 1;
555 }
556
557 err = file_mkdir_hier(lists_dir, 0755);
558 if (err) {
559 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
560 lists_dir);
561 free(lists_dir);
562 return 1;
563 }
564 }
565
566 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
567 if (mkdtemp(tmp) == NULL) {
568 opkg_perror(ERROR, "Coundn't create temporary directory %s",
569 tmp);
570 free(lists_dir);
571 free(tmp);
572 return 1;
573 }
574
575 /* count the number of sources so we can give some progress updates */
576 sources_list_count = 0;
577 sources_done = 0;
578 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
579 sources_list_count++;
580 }
581
582 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
583 char *url, *list_file_name = NULL;
584
585 src = (pkg_src_t *) iter->data;
586
587 if (src->extra_data) /* debian style? */
588 sprintf_alloc(&url, "%s/%s/%s", src->value,
589 src->extra_data,
590 src->gzip ? "Packages.gz" : "Packages");
591 else
592 sprintf_alloc(&url, "%s/%s", src->value,
593 src->gzip ? "Packages.gz" : "Packages");
594
595 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
596 if (src->gzip) {
597 FILE *in, *out;
598 struct _curl_cb_data cb_data;
599 char *tmp_file_name = NULL;
600
601 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
602 src->name);
603
604 opkg_msg(INFO, "Downloading %s to %s...\n", url,
605 tmp_file_name);
606
607 cb_data.cb = progress_callback;
608 cb_data.progress_data = &pdata;
609 cb_data.user_data = user_data;
610 cb_data.start_range =
611 100 * sources_done / sources_list_count;
612 cb_data.finish_range =
613 100 * (sources_done + 1) / sources_list_count;
614
615 err = opkg_download(url, tmp_file_name,
616 (curl_progress_func) curl_progress_cb,
617 &cb_data);
618
619 if (err == 0) {
620 opkg_msg(INFO, "Inflating %s...\n",
621 tmp_file_name);
622 in = fopen(tmp_file_name, "r");
623 out = fopen(list_file_name, "w");
624 if (in && out)
625 unzip(in, out);
626 else
627 err = 1;
628 if (in)
629 fclose(in);
630 if (out)
631 fclose(out);
632 unlink(tmp_file_name);
633 }
634 free(tmp_file_name);
635 } else
636 err = opkg_download(url, list_file_name, NULL, NULL);
637
638 if (err) {
639 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
640 result = -1;
641 }
642 free(url);
643
644 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
645 if (conf->check_signature) {
646 char *sig_file_name;
647 /* download detached signitures to verify the package lists */
648 /* get the url for the sig file */
649 if (src->extra_data) /* debian style? */
650 sprintf_alloc(&url, "%s/%s/%s", src->value,
651 src->extra_data, "Packages.sig");
652 else
653 sprintf_alloc(&url, "%s/%s", src->value,
654 "Packages.sig");
655
656 /* create filename for signature */
657 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
658 src->name);
659
660 /* make sure there is no existing signature file */
661 unlink(sig_file_name);
662
663 err = opkg_download(url, sig_file_name, NULL, NULL);
664 if (err) {
665 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
666 } else {
667 int err;
668 err = opkg_verify_file(list_file_name,
669 sig_file_name);
670 if (err == 0) {
671 opkg_msg(INFO, "Signature check "
672 "passed for %s",
673 list_file_name);
674 } else {
675 opkg_msg(ERROR, "Signature check "
676 "failed for %s",
677 list_file_name);
678 }
679 }
680 free(sig_file_name);
681 free(list_file_name);
682 free(url);
683 }
684 #else
685 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
686 " has not been enabled in this build\n",
687 list_file_name);
688 #endif
689
690 sources_done++;
691 progress(pdata, 100 * sources_done / sources_list_count);
692 }
693
694 rmdir(tmp);
695 free(tmp);
696 free(lists_dir);
697
698 /* Now re-read the package lists to update package hash tables. */
699 opkg_re_read_config_files();
700
701 return result;
702 }
703
704 static int
705 pkg_compare_names_and_version(const void *a0, const void *b0)
706 {
707 const pkg_t *a = *(const pkg_t **)a0;
708 const pkg_t *b = *(const pkg_t **)b0;
709 int ret;
710
711 ret = strcmp(a->name, b->name);
712
713 if (ret == 0)
714 ret = pkg_compare_versions(a, b);
715
716 return ret;
717 }
718
719 int
720 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
721 {
722 pkg_vec_t *all;
723 int i;
724
725 opkg_assert(callback);
726
727 all = pkg_vec_alloc();
728 pkg_hash_fetch_available(all);
729
730 pkg_vec_sort(all, pkg_compare_names_and_version);
731
732 for (i = 0; i < all->len; i++) {
733 pkg_t *pkg;
734
735 pkg = all->pkgs[i];
736
737 callback(pkg, user_data);
738 }
739
740 pkg_vec_free(all);
741
742 return 0;
743 }
744
745 int
746 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
747 {
748 struct active_list *head;
749 struct active_list *node;
750 pkg_t *old = NULL, *new = NULL;
751
752 opkg_assert(callback);
753
754 /* ensure all data is valid */
755 pkg_info_preinstall_check();
756
757 head = prepare_upgrade_list();
758 for (node = active_list_next(head, head); node;
759 active_list_next(head, node)) {
760 old = list_entry(node, pkg_t, list);
761 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
762 if (new == NULL)
763 continue;
764 callback(new, user_data);
765 }
766 active_list_head_delete(head);
767 return 0;
768 }
769
770 pkg_t *
771 opkg_find_package(const char *name, const char *ver, const char *arch,
772 const char *repo)
773 {
774 pkg_t *pkg = NULL;
775 pkg_vec_t *all;
776 int i;
777 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
778
779 all = pkg_vec_alloc();
780 pkg_hash_fetch_available(all);
781 for (i = 0; i < all->len; i++) {
782 char *pkgv;
783
784 pkg = all->pkgs[i];
785
786 /* check name */
787 if (sstrcmp(pkg->name, name))
788 continue;
789
790 /* check version */
791 pkgv = pkg_version_str_alloc(pkg);
792 if (sstrcmp(pkgv, ver)) {
793 free(pkgv);
794 continue;
795 }
796 free(pkgv);
797
798 /* check architecture */
799 if (arch) {
800 if (sstrcmp(pkg->architecture, arch))
801 continue;
802 }
803
804 /* check repository */
805 if (repo) {
806 if (sstrcmp(pkg->src->name, repo))
807 continue;
808 }
809
810 /* match found */
811 break;
812 }
813
814 pkg_vec_free(all);
815
816 return pkg;
817 }
818
819 /**
820 * @brief Check the accessibility of repositories.
821 * @return return how many repositories cannot access. 0 means all okay.
822 */
823 int
824 opkg_repository_accessibility_check(void)
825 {
826 pkg_src_list_elt_t *iter;
827 str_list_elt_t *iter1;
828 str_list_t *src;
829 int repositories = 0;
830 int ret = 0;
831 char *repo_ptr;
832 char *stmp;
833 char *host, *end;
834
835 src = str_list_alloc();
836
837 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
838 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
839 end = index(host, '/');
840 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
841 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
842 end - ((pkg_src_t *) iter->data)->value);
843 else
844 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
845
846 for (iter1 = str_list_first(src); iter1;
847 iter1 = str_list_next(src, iter1)) {
848 if (strstr(iter1->data, stmp))
849 break;
850 }
851 if (iter1)
852 continue;
853
854 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
855 free(stmp);
856
857 str_list_append(src, repo_ptr);
858 free(repo_ptr);
859 repositories++;
860 }
861
862 while (repositories > 0) {
863 iter1 = str_list_pop(src);
864 repositories--;
865
866 if (opkg_download(iter1->data, "/dev/null", NULL, NULL))
867 ret++;
868 str_list_elt_deinit(iter1);
869 }
870
871 free(src);
872
873 return ret;
874 }