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