253080fae5f40461b0cfa94b08d9942a9d6cdcd8
[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;
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 pkg_hash_fetch_unsatisfied_dependencies(new, deps, &unresolved);
275 if (unresolved) {
276 char **tmp = unresolved;
277 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
278 " for %s:\n", package_name);
279 while (*tmp) {
280 opkg_msg(ERROR, "\t%s", *tmp);
281 free(*tmp);
282 tmp++;
283 }
284 free(unresolved);
285 pkg_vec_free(deps);
286 opkg_message(ERROR, "\n");
287 return -1;
288 }
289
290 /* insert the package we are installing so that we download it */
291 pkg_vec_insert(deps, new);
292
293 /* download package and dependencies */
294 for (i = 0; i < deps->len; i++) {
295 pkg_t *pkg;
296 struct _curl_cb_data cb_data;
297 char *url;
298
299 pkg = deps->pkgs[i];
300 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
301 continue;
302
303 pdata.pkg = pkg;
304 pdata.action = OPKG_DOWNLOAD;
305
306 if (pkg->src == NULL) {
307 opkg_msg(ERROR, "Package %s not available from any "
308 "configured src\n", package_name);
309 return -1;
310 }
311
312 filename = pkg_get_string(pkg, PKG_FILENAME);
313
314 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
315
316 /* Get the filename part, without any directory */
317 stripped_filename = strrchr(filename, '/');
318 if (!stripped_filename)
319 stripped_filename = (char *)filename;
320
321 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
322 stripped_filename);
323
324 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
325
326 cb_data.cb = progress_callback;
327 cb_data.progress_data = &pdata;
328 cb_data.user_data = user_data;
329 /* 75% of "install" progress is for downloading */
330 cb_data.start_range = 75 * i / deps->len;
331 cb_data.finish_range = 75 * (i + 1) / deps->len;
332
333 err = opkg_download(url, local_filename,
334 (curl_progress_func) curl_progress_cb,
335 &cb_data, 0);
336 free(url);
337
338 if (err) {
339 pkg_vec_free(deps);
340 return -1;
341 }
342
343 }
344 pkg_vec_free(deps);
345
346 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
347 all = pkg_vec_alloc();
348 pkg_hash_fetch_available(all);
349 for (i = 0; i < all->len; i++) {
350 all->pkgs[i]->parent->dependencies_checked = 0;
351 }
352 pkg_vec_free(all);
353
354 /* 75% of "install" progress is for downloading */
355 pdata.pkg = new;
356 pdata.action = OPKG_INSTALL;
357 progress(pdata, 75);
358
359 /* unpack the package */
360 err = opkg_install_pkg(new, 0);
361
362 if (err) {
363 return -1;
364 }
365
366 progress(pdata, 75);
367
368 /* run configure scripts, etc. */
369 err = opkg_configure_packages(NULL);
370 if (err) {
371 return -1;
372 }
373
374 /* write out status files and file lists */
375 opkg_conf_write_status_files();
376 pkg_write_changed_filelists();
377
378 progress(pdata, 100);
379 return 0;
380 }
381
382 int
383 opkg_remove_package(const char *package_name,
384 opkg_progress_callback_t progress_callback, void *user_data)
385 {
386 int err;
387 pkg_t *pkg = NULL;
388 pkg_t *pkg_to_remove;
389 opkg_progress_data_t pdata;
390
391 opkg_assert(package_name != NULL);
392
393 pkg_info_preinstall_check();
394
395 pkg = pkg_hash_fetch_installed_by_name(package_name);
396
397 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
398 opkg_msg(ERROR, "Package %s not installed\n", package_name);
399 return -1;
400 }
401
402 pdata.action = OPKG_REMOVE;
403 pdata.pkg = pkg;
404 progress(pdata, 0);
405
406 if (conf->restrict_to_default_dest) {
407 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
408 conf->
409 default_dest);
410 } else {
411 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
412 }
413
414 progress(pdata, 75);
415
416 err = opkg_remove_pkg(pkg_to_remove, 0);
417
418 /* write out status files and file lists */
419 opkg_conf_write_status_files();
420 pkg_write_changed_filelists();
421
422 progress(pdata, 100);
423 return (err) ? -1 : 0;
424 }
425
426 int
427 opkg_upgrade_package(const char *package_name,
428 opkg_progress_callback_t progress_callback,
429 void *user_data)
430 {
431 int err;
432 pkg_t *pkg;
433 opkg_progress_data_t pdata;
434
435 opkg_assert(package_name != NULL);
436
437 pkg_info_preinstall_check();
438
439 if (conf->restrict_to_default_dest) {
440 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
441 conf->default_dest);
442 } else {
443 pkg = pkg_hash_fetch_installed_by_name(package_name);
444 }
445
446 if (!pkg) {
447 opkg_msg(ERROR, "Package %s not installed\n", package_name);
448 return -1;
449 }
450
451 pdata.action = OPKG_INSTALL;
452 pdata.pkg = pkg;
453 progress(pdata, 0);
454
455 err = opkg_upgrade_pkg(pkg);
456 if (err) {
457 return -1;
458 }
459 progress(pdata, 75);
460
461 err = opkg_configure_packages(NULL);
462 if (err) {
463 return -1;
464 }
465
466 /* write out status files and file lists */
467 opkg_conf_write_status_files();
468 pkg_write_changed_filelists();
469
470 progress(pdata, 100);
471 return 0;
472 }
473
474 int
475 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
476 {
477 pkg_vec_t *installed;
478 int err = 0;
479 int i;
480 pkg_t *pkg;
481 opkg_progress_data_t pdata;
482
483 pdata.action = OPKG_INSTALL;
484 pdata.pkg = NULL;
485
486 progress(pdata, 0);
487
488 installed = pkg_vec_alloc();
489 pkg_info_preinstall_check();
490
491 pkg_hash_fetch_all_installed(installed);
492 for (i = 0; i < installed->len; i++) {
493 pkg = installed->pkgs[i];
494
495 pdata.pkg = pkg;
496 progress(pdata, 99 * i / installed->len);
497
498 err += opkg_upgrade_pkg(pkg);
499 }
500 pkg_vec_free(installed);
501
502 if (err)
503 return 1;
504
505 err = opkg_configure_packages(NULL);
506 if (err)
507 return 1;
508
509 /* write out status files and file lists */
510 opkg_conf_write_status_files();
511 pkg_write_changed_filelists();
512
513 pdata.pkg = NULL;
514 progress(pdata, 100);
515 return 0;
516 }
517
518 int
519 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
520 void *user_data)
521 {
522 char *tmp;
523 int err, result = 0;
524 char *lists_dir;
525 pkg_src_list_elt_t *iter;
526 pkg_src_t *src;
527 int sources_list_count, sources_done;
528 opkg_progress_data_t pdata;
529
530 pdata.action = OPKG_DOWNLOAD;
531 pdata.pkg = NULL;
532 progress(pdata, 0);
533
534 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
535 ? conf->default_dest->lists_dir : conf->lists_dir);
536
537 if (!file_is_dir(lists_dir)) {
538 if (file_exists(lists_dir)) {
539 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
540 free(lists_dir);
541 return 1;
542 }
543
544 err = file_mkdir_hier(lists_dir, 0755);
545 if (err) {
546 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
547 lists_dir);
548 free(lists_dir);
549 return 1;
550 }
551 }
552
553 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
554 if (mkdtemp(tmp) == NULL) {
555 opkg_perror(ERROR, "Coundn't create temporary directory %s",
556 tmp);
557 free(lists_dir);
558 free(tmp);
559 return 1;
560 }
561
562 /* count the number of sources so we can give some progress updates */
563 sources_list_count = 0;
564 sources_done = 0;
565 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
566 sources_list_count++;
567 }
568
569 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
570 char *url, *list_file_name = NULL;
571
572 src = (pkg_src_t *) iter->data;
573
574 if (src->extra_data) /* debian style? */
575 sprintf_alloc(&url, "%s/%s/%s", src->value,
576 src->extra_data,
577 src->gzip ? "Packages.gz" : "Packages");
578 else
579 sprintf_alloc(&url, "%s/%s", src->value,
580 src->gzip ? "Packages.gz" : "Packages");
581
582 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
583
584 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
585 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
586 result = -1;
587 }
588 free(url);
589
590 #if defined(HAVE_USIGN)
591 if (conf->check_signature) {
592 char *sig_file_name;
593 /* download detached signitures to verify the package lists */
594 /* get the url for the sig file */
595 if (src->extra_data) /* debian style? */
596 sprintf_alloc(&url, "%s/%s/%s", src->value,
597 src->extra_data, "Packages.sig");
598 else
599 sprintf_alloc(&url, "%s/%s", src->value,
600 "Packages.sig");
601
602 /* create filename for signature */
603 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
604 src->name);
605
606 /* make sure there is no existing signature file */
607 unlink(sig_file_name);
608
609 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
610 if (err) {
611 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
612 } else {
613 int err;
614 err = opkg_verify_file(list_file_name,
615 sig_file_name);
616 if (err == 0) {
617 opkg_msg(INFO, "Signature check "
618 "passed for %s",
619 list_file_name);
620 } else {
621 opkg_msg(ERROR, "Signature check "
622 "failed for %s",
623 list_file_name);
624 }
625 }
626 free(sig_file_name);
627 free(url);
628 }
629 #else
630 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
631 " has not been enabled in this build\n",
632 list_file_name);
633 #endif
634 free(list_file_name);
635
636 sources_done++;
637 progress(pdata, 100 * sources_done / sources_list_count);
638 }
639
640 rmdir(tmp);
641 free(tmp);
642 free(lists_dir);
643
644 /* Now re-read the package lists to update package hash tables. */
645 opkg_re_read_config_files();
646
647 return result;
648 }
649
650 static int pkg_compare_names_and_version(const void *a0, const void *b0)
651 {
652 const pkg_t *a = *(const pkg_t **)a0;
653 const pkg_t *b = *(const pkg_t **)b0;
654 int ret;
655
656 ret = strcmp(a->name, b->name);
657
658 if (ret == 0)
659 ret = pkg_compare_versions(a, b);
660
661 return ret;
662 }
663
664 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
665 {
666 pkg_vec_t *all;
667 int i;
668
669 opkg_assert(callback);
670
671 all = pkg_vec_alloc();
672 pkg_hash_fetch_available(all);
673
674 pkg_vec_sort(all, pkg_compare_names_and_version);
675
676 for (i = 0; i < all->len; i++) {
677 pkg_t *pkg;
678
679 pkg = all->pkgs[i];
680
681 callback(pkg, user_data);
682 }
683
684 pkg_vec_free(all);
685
686 return 0;
687 }
688
689 int
690 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
691 {
692 struct active_list *head;
693 struct active_list *node;
694 pkg_t *old = NULL, *new = NULL;
695
696 opkg_assert(callback);
697
698 /* ensure all data is valid */
699 pkg_info_preinstall_check();
700
701 head = prepare_upgrade_list();
702 for (node = active_list_next(head, head); node;
703 node = active_list_next(head, node)) {
704 old = node->pkg;
705 new =
706 pkg_hash_fetch_best_installation_candidate_by_name(old->
707 name);
708 if (new == NULL)
709 continue;
710 callback(new, user_data);
711 }
712 active_list_head_delete(head);
713 return 0;
714 }
715
716 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
717 const char *repo)
718 {
719 int pkg_found = 0;
720 pkg_t *pkg = NULL;
721 pkg_vec_t *all;
722 int i;
723 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
724
725 all = pkg_vec_alloc();
726 pkg_hash_fetch_available(all);
727 for (i = 0; i < all->len; i++) {
728 char *pkgv;
729
730 pkg = all->pkgs[i];
731
732 /* check name */
733 if (sstrcmp(pkg->name, name))
734 continue;
735
736 /* check version */
737 pkgv = pkg_version_str_alloc(pkg);
738 if (sstrcmp(pkgv, ver)) {
739 free(pkgv);
740 continue;
741 }
742 free(pkgv);
743
744 /* check architecture */
745 if (arch) {
746 if (sstrcmp(pkg_get_architecture(pkg), arch))
747 continue;
748 }
749
750 /* check repository */
751 if (repo) {
752 if (sstrcmp(pkg->src->name, repo))
753 continue;
754 }
755
756 /* match found */
757 pkg_found = 1;
758 break;
759 }
760
761 pkg_vec_free(all);
762
763 return pkg_found ? pkg : NULL;
764 }
765
766 /**
767 * @brief Check the accessibility of repositories.
768 * @return return how many repositories cannot access. 0 means all okay.
769 */
770 int opkg_repository_accessibility_check(void)
771 {
772 pkg_src_list_elt_t *iter;
773 str_list_elt_t *iter1;
774 str_list_t *src;
775 int repositories = 0;
776 int ret = 0;
777 char *repo_ptr;
778 char *stmp;
779 char *host, *end;
780
781 src = str_list_alloc();
782
783 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
784 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
785 end = index(host, '/');
786 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
787 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
788 end -
789 ((pkg_src_t *) iter->data)->value);
790 else
791 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
792
793 for (iter1 = str_list_first(src); iter1;
794 iter1 = str_list_next(src, iter1)) {
795 if (strstr(iter1->data, stmp))
796 break;
797 }
798 if (iter1)
799 continue;
800
801 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
802 free(stmp);
803
804 str_list_append(src, repo_ptr);
805 free(repo_ptr);
806 repositories++;
807 }
808
809 while (repositories > 0) {
810 iter1 = str_list_pop(src);
811 repositories--;
812
813 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
814 ret++;
815 str_list_elt_deinit(iter1);
816 }
817
818 free(src);
819
820 return ret;
821 }