383dfb5893d9b31a4d8dd33b6274946584fc896a
[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 #define opkg_assert(expr) if (!(expr)) { \
37 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
38 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
39
40 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
41
42 /** Private Functions ***/
43
44 static int
45 opkg_configure_packages(char *pkg_name)
46 {
47 pkg_vec_t *all;
48 int i;
49 pkg_t *pkg;
50 int r, err = 0;
51
52 all = pkg_vec_alloc();
53 pkg_hash_fetch_available(all);
54
55 for (i = 0; i < all->len; i++) {
56 pkg = all->pkgs[i];
57
58 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
59 continue;
60
61 if (pkg->state_status == SS_UNPACKED) {
62 r = opkg_configure(pkg);
63 if (r == 0) {
64 pkg->state_status = SS_INSTALLED;
65 pkg->parent->state_status = SS_INSTALLED;
66 pkg->state_flag &= ~SF_PREFER;
67 } else {
68 if (!err)
69 err = r;
70 }
71 }
72 }
73
74 pkg_vec_free(all);
75 return err;
76 }
77
78 struct _curl_cb_data {
79 opkg_progress_callback_t cb;
80 opkg_progress_data_t *progress_data;
81 void *user_data;
82 int start_range;
83 int finish_range;
84 };
85
86 int
87 curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
88 double d, /* dlnow */
89 double ultotal, double ulnow)
90 {
91 int p = (t) ? d * 100 / t : 0;
92 static int prev = -1;
93 int progress = 0;
94
95 /* prevent the same value being sent twice (can occur due to rounding) */
96 if (p == prev)
97 return 0;
98 prev = p;
99
100 if (t < 1)
101 return 0;
102
103 progress = cb_data->start_range +
104 (d / t * ((cb_data->finish_range - cb_data->start_range)));
105 cb_data->progress_data->percentage = progress;
106
107 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
108
109 return 0;
110 }
111
112
113 /*** Public API ***/
114
115 int
116 opkg_new()
117 {
118 args_t args;
119
120 args_init(&args);
121
122 if (opkg_conf_init(&args))
123 goto err0;
124
125 args_deinit(&args);
126
127 if (pkg_hash_load_feeds())
128 goto err1;
129
130 if (pkg_hash_load_status_files())
131 goto err1;
132
133 return 0;
134
135 err1:
136 pkg_hash_deinit();
137 err0:
138 opkg_conf_deinit();
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 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 pdata.pkg = NULL;
530 progress(pdata, 100);
531 return 0;
532 }
533
534 int
535 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
536 void *user_data)
537 {
538 char *tmp;
539 int err, result = 0;
540 char *lists_dir;
541 pkg_src_list_elt_t *iter;
542 pkg_src_t *src;
543 int sources_list_count, sources_done;
544 opkg_progress_data_t pdata;
545
546 pdata.action = OPKG_DOWNLOAD;
547 pdata.pkg = NULL;
548 progress(pdata, 0);
549
550 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
551 ? conf->default_dest->lists_dir : conf->lists_dir);
552
553 if (!file_is_dir(lists_dir)) {
554 if (file_exists(lists_dir)) {
555 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
556 free(lists_dir);
557 return 1;
558 }
559
560 err = file_mkdir_hier(lists_dir, 0755);
561 if (err) {
562 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
563 lists_dir);
564 free(lists_dir);
565 return 1;
566 }
567 }
568
569 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
570 if (mkdtemp(tmp) == NULL) {
571 opkg_perror(ERROR, "Coundn't create temporary directory %s",
572 tmp);
573 free(lists_dir);
574 free(tmp);
575 return 1;
576 }
577
578 /* count the number of sources so we can give some progress updates */
579 sources_list_count = 0;
580 sources_done = 0;
581 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
582 sources_list_count++;
583 }
584
585 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
586 char *url, *list_file_name = NULL;
587
588 src = (pkg_src_t *) iter->data;
589
590 if (src->extra_data) /* debian style? */
591 sprintf_alloc(&url, "%s/%s/%s", src->value,
592 src->extra_data,
593 src->gzip ? "Packages.gz" : "Packages");
594 else
595 sprintf_alloc(&url, "%s/%s", src->value,
596 src->gzip ? "Packages.gz" : "Packages");
597
598 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
599 if (src->gzip) {
600 FILE *in, *out;
601 struct _curl_cb_data cb_data;
602 char *tmp_file_name = NULL;
603
604 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
605 src->name);
606
607 opkg_msg(INFO, "Downloading %s to %s...\n", url,
608 tmp_file_name);
609
610 cb_data.cb = progress_callback;
611 cb_data.progress_data = &pdata;
612 cb_data.user_data = user_data;
613 cb_data.start_range =
614 100 * sources_done / sources_list_count;
615 cb_data.finish_range =
616 100 * (sources_done + 1) / sources_list_count;
617
618 err = opkg_download(url, tmp_file_name,
619 (curl_progress_func) curl_progress_cb,
620 &cb_data);
621
622 if (err == 0) {
623 opkg_msg(INFO, "Inflating %s...\n",
624 tmp_file_name);
625 in = fopen(tmp_file_name, "r");
626 out = fopen(list_file_name, "w");
627 if (in && out)
628 unzip(in, out);
629 else
630 err = 1;
631 if (in)
632 fclose(in);
633 if (out)
634 fclose(out);
635 unlink(tmp_file_name);
636 }
637 free(tmp_file_name);
638 } else
639 err = opkg_download(url, list_file_name, NULL, NULL);
640
641 if (err) {
642 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
643 result = -1;
644 }
645 free(url);
646
647 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
648 if (conf->check_signature) {
649 char *sig_file_name;
650 /* download detached signitures to verify the package lists */
651 /* get the url for the sig file */
652 if (src->extra_data) /* debian style? */
653 sprintf_alloc(&url, "%s/%s/%s", src->value,
654 src->extra_data, "Packages.sig");
655 else
656 sprintf_alloc(&url, "%s/%s", src->value,
657 "Packages.sig");
658
659 /* create filename for signature */
660 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
661 src->name);
662
663 /* make sure there is no existing signature file */
664 unlink(sig_file_name);
665
666 err = opkg_download(url, sig_file_name, NULL, NULL);
667 if (err) {
668 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
669 } else {
670 int err;
671 err = opkg_verify_file(list_file_name,
672 sig_file_name);
673 if (err == 0) {
674 opkg_msg(INFO, "Signature check "
675 "passed for %s",
676 list_file_name);
677 } else {
678 opkg_msg(ERROR, "Signature check "
679 "failed for %s",
680 list_file_name);
681 }
682 }
683 free(sig_file_name);
684 free(list_file_name);
685 free(url);
686 }
687 #else
688 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
689 " has not been enabled in this build\n",
690 list_file_name);
691 #endif
692
693 sources_done++;
694 progress(pdata, 100 * sources_done / sources_list_count);
695 }
696
697 rmdir(tmp);
698 free(tmp);
699 free(lists_dir);
700
701 /* Now re-read the package lists to update package hash tables. */
702 opkg_re_read_config_files();
703
704 return result;
705 }
706
707
708 int
709 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
710 {
711 pkg_vec_t *all;
712 int i;
713
714 opkg_assert(callback);
715
716 all = pkg_vec_alloc();
717 pkg_hash_fetch_available(all);
718 for (i = 0; i < all->len; i++) {
719 pkg_t *pkg;
720
721 pkg = all->pkgs[i];
722
723 callback(pkg, user_data);
724 }
725
726 pkg_vec_free(all);
727
728 return 0;
729 }
730
731 int
732 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
733 {
734 struct active_list *head;
735 struct active_list *node;
736 pkg_t *old = NULL, *new = NULL;
737
738 opkg_assert(callback);
739
740 /* ensure all data is valid */
741 pkg_info_preinstall_check();
742
743 head = prepare_upgrade_list();
744 for (node = active_list_next(head, head); node;
745 active_list_next(head, node)) {
746 old = list_entry(node, pkg_t, list);
747 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
748 if (new == NULL)
749 continue;
750 callback(new, user_data);
751 }
752 active_list_head_delete(head);
753 return 0;
754 }
755
756 pkg_t *
757 opkg_find_package(const char *name, const char *ver, const char *arch,
758 const char *repo)
759 {
760 pkg_t *pkg = NULL;
761 pkg_vec_t *all;
762 int i;
763 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
764
765 all = pkg_vec_alloc();
766 pkg_hash_fetch_available(all);
767 for (i = 0; i < all->len; i++) {
768 char *pkgv;
769
770 pkg = all->pkgs[i];
771
772 /* check name */
773 if (sstrcmp(pkg->name, name))
774 continue;
775
776 /* check version */
777 pkgv = pkg_version_str_alloc(pkg);
778 if (sstrcmp(pkgv, ver)) {
779 free(pkgv);
780 continue;
781 }
782 free(pkgv);
783
784 /* check architecture */
785 if (arch) {
786 if (sstrcmp(pkg->architecture, arch))
787 continue;
788 }
789
790 /* check repository */
791 if (repo) {
792 if (sstrcmp(pkg->src->name, repo))
793 continue;
794 }
795
796 /* match found */
797 break;
798 }
799
800 pkg_vec_free(all);
801
802 return pkg;
803 }
804
805 #ifdef HAVE_CURL
806 #include <curl/curl.h>
807 #endif
808 /**
809 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
810 * @return return how many repositories cannot access. 0 means all okay.
811 */
812 int
813 opkg_repository_accessibility_check(void)
814 {
815 pkg_src_list_elt_t *iter;
816 str_list_elt_t *iter1;
817 str_list_t *src;
818 int repositories = 0;
819 int ret = 0;
820 int err;
821 char *repo_ptr;
822 char *stmp;
823 char *host, *end;
824
825 src = str_list_alloc();
826
827 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
828 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
829 end = index(host, '/');
830 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
831 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
832 end - ((pkg_src_t *) iter->data)->value);
833 else
834 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
835
836 for (iter1 = str_list_first(src); iter1;
837 iter1 = str_list_next(src, iter1)) {
838 if (strstr(iter1->data, stmp))
839 break;
840 }
841 if (iter1)
842 continue;
843
844 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
845 free(stmp);
846
847 str_list_append(src, repo_ptr);
848 free(repo_ptr);
849 repositories++;
850 }
851 while (repositories > 0) {
852 iter1 = str_list_pop(src);
853 repositories--;
854
855 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
856 #ifdef HAVE_CURL
857 if (!(err == CURLE_OK ||
858 err == CURLE_HTTP_RETURNED_ERROR ||
859 err == CURLE_FILE_COULDNT_READ_FILE ||
860 err == CURLE_REMOTE_FILE_NOT_FOUND ||
861 err == CURLE_TFTP_NOTFOUND)) {
862 #else
863 if (!(err == 0)) {
864 #endif
865 ret++;
866 }
867 str_list_elt_deinit(iter1);
868 }
869 free(src);
870 return ret;
871 }