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