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