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