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