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