1 /* opkg.c - the opkg package management system
3 Thomas Wood <thomas@openedhand.com>
5 Copyright (C) 2008 OpenMoko Inc
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.
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.
25 #include "opkg_conf.h"
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31 #include "opkg_upgrade.h"
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
36 #include <libbb/libbb.h>
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 (); }
42 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
44 /** Private Functions ***/
47 opkg_configure_packages(char *pkg_name)
54 all = pkg_vec_alloc();
55 pkg_hash_fetch_available(all);
57 for (i = 0; i < all->len; i++) {
60 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
63 if (pkg->state_status == SS_UNPACKED) {
64 r = opkg_configure(pkg);
66 pkg->state_status = SS_INSTALLED;
67 pkg->parent->state_status = SS_INSTALLED;
68 pkg->state_flag &= ~SF_PREFER;
80 struct _curl_cb_data {
81 opkg_progress_callback_t cb;
82 opkg_progress_data_t *progress_data;
89 curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
91 double ultotal, double ulnow)
93 int p = (t) ? d * 100 / t : 0;
97 /* prevent the same value being sent twice (can occur due to rounding) */
105 progress = cb_data->start_range +
106 (d / t * ((cb_data->finish_range - cb_data->start_range)));
107 cb_data->progress_data->percentage = progress;
109 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
120 if (opkg_conf_init())
123 if (opkg_conf_load())
126 if (pkg_hash_load_feeds())
129 if (pkg_hash_load_status_files())
152 opkg_re_read_config_files(void)
157 if (pkg_hash_load_feeds())
160 if (pkg_hash_load_status_files())
171 opkg_get_option(char *option, void **value)
174 extern opkg_option_t options[];
176 /* look up the option
177 * TODO: this would be much better as a hash table
179 while (options[i].name) {
180 if (strcmp(options[i].name, option) != 0) {
187 switch (options[i].type) {
188 case OPKG_OPT_TYPE_BOOL:
189 *((int *) value) = *((int *) options[i].value);
192 case OPKG_OPT_TYPE_INT:
193 *((int *) value) = *((int *) options[i].value);
196 case OPKG_OPT_TYPE_STRING:
197 *((char **) value) = xstrdup(options[i].value);
204 opkg_set_option(char *option, void *value)
206 int i = 0, found = 0;
207 extern opkg_option_t options[];
209 opkg_assert(option != NULL);
210 opkg_assert(value != NULL);
212 /* look up the option
213 * TODO: this would be much better as a hash table
215 while (options[i].name) {
216 if (strcmp(options[i].name, option) == 0) {
224 opkg_msg(ERROR, "Invalid option: %s\n", option);
229 switch (options[i].type) {
230 case OPKG_OPT_TYPE_BOOL:
231 if (*((int *) value) == 0)
232 *((int *) options[i].value) = 0;
234 *((int *) options[i].value) = 1;
237 case OPKG_OPT_TYPE_INT:
238 *((int *) options[i].value) = *((int *) value);
241 case OPKG_OPT_TYPE_STRING:
242 *((char **) options[i].value) = xstrdup(value);
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.
254 opkg_install_package(const char *package_name,
255 opkg_progress_callback_t progress_callback,
259 char *stripped_filename;
260 opkg_progress_data_t pdata;
262 pkg_vec_t *deps, *all;
264 char **unresolved = NULL;
266 opkg_assert(package_name != NULL);
269 pkg_info_preinstall_check();
272 /* check to ensure package is not already installed */
273 old = pkg_hash_fetch_installed_by_name(package_name);
275 opkg_msg(ERROR, "Package %s is already installed\n",
280 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
282 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
286 new->state_flag |= SF_USER;
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,
299 char **tmp = unresolved;
300 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
301 " for %s:\n", package_name);
303 opkg_msg(ERROR, "\t%s", *tmp);
309 opkg_message(ERROR, "\n");
313 /* insert the package we are installing so that we download it */
314 pkg_vec_insert(deps, new);
316 /* download package and dependencies */
317 for (i = 0; i < deps->len; i++) {
319 struct _curl_cb_data cb_data;
323 if (pkg->local_filename)
327 pdata.action = OPKG_DOWNLOAD;
329 if (pkg->src == NULL) {
330 opkg_msg(ERROR, "Package %s not available from any "
331 "configured src\n", package_name);
335 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
337 /* Get the filename part, without any directory */
338 stripped_filename = strrchr(pkg->filename, '/');
339 if (!stripped_filename)
340 stripped_filename = pkg->filename;
342 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
345 cb_data.cb = progress_callback;
346 cb_data.progress_data = &pdata;
347 cb_data.user_data = user_data;
348 /* 75% of "install" progress is for downloading */
349 cb_data.start_range = 75 * i / deps->len;
350 cb_data.finish_range = 75 * (i + 1) / deps->len;
352 err = opkg_download(url, pkg->local_filename,
353 (curl_progress_func) curl_progress_cb,
365 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
366 all = pkg_vec_alloc();
367 pkg_hash_fetch_available(all);
368 for (i = 0; i < all->len; i++) {
369 all->pkgs[i]->parent->dependencies_checked = 0;
374 /* 75% of "install" progress is for downloading */
376 pdata.action = OPKG_INSTALL;
379 /* unpack the package */
380 err = opkg_install_pkg(new, 0);
388 /* run configure scripts, etc. */
389 err = opkg_configure_packages(NULL);
394 /* write out status files and file lists */
395 opkg_conf_write_status_files();
396 pkg_write_changed_filelists();
398 progress(pdata, 100);
403 opkg_remove_package(const char *package_name,
404 opkg_progress_callback_t progress_callback, void *user_data)
408 pkg_t *pkg_to_remove;
409 opkg_progress_data_t pdata;
411 opkg_assert(package_name != NULL);
413 pkg_info_preinstall_check();
415 pkg = pkg_hash_fetch_installed_by_name(package_name);
417 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
418 opkg_msg(ERROR, "Package %s not installed\n", package_name);
422 pdata.action = OPKG_REMOVE;
426 if (conf->restrict_to_default_dest) {
427 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
430 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
436 err = opkg_remove_pkg(pkg_to_remove, 0);
438 /* write out status files and file lists */
439 opkg_conf_write_status_files();
440 pkg_write_changed_filelists();
443 progress(pdata, 100);
444 return (err) ? -1 : 0;
448 opkg_upgrade_package(const char *package_name,
449 opkg_progress_callback_t progress_callback,
454 opkg_progress_data_t pdata;
456 opkg_assert(package_name != NULL);
458 pkg_info_preinstall_check();
460 if (conf->restrict_to_default_dest) {
461 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
464 pkg = pkg_hash_fetch_installed_by_name(package_name);
468 opkg_msg(ERROR, "Package %s not installed\n", package_name);
472 pdata.action = OPKG_INSTALL;
476 err = opkg_upgrade_pkg(pkg);
482 err = opkg_configure_packages(NULL);
487 /* write out status files and file lists */
488 opkg_conf_write_status_files();
489 pkg_write_changed_filelists();
491 progress(pdata, 100);
496 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
498 pkg_vec_t *installed;
502 opkg_progress_data_t pdata;
504 pdata.action = OPKG_INSTALL;
509 installed = pkg_vec_alloc();
510 pkg_info_preinstall_check();
512 pkg_hash_fetch_all_installed(installed);
513 for (i = 0; i < installed->len; i++) {
514 pkg = installed->pkgs[i];
517 progress(pdata, 99 * i / installed->len);
519 err += opkg_upgrade_pkg(pkg);
521 pkg_vec_free(installed);
526 err = opkg_configure_packages(NULL);
530 /* write out status files and file lists */
531 opkg_conf_write_status_files();
532 pkg_write_changed_filelists();
535 progress(pdata, 100);
540 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
546 pkg_src_list_elt_t *iter;
548 int sources_list_count, sources_done;
549 opkg_progress_data_t pdata;
551 pdata.action = OPKG_DOWNLOAD;
555 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
556 ? conf->default_dest->lists_dir : conf->lists_dir);
558 if (!file_is_dir(lists_dir)) {
559 if (file_exists(lists_dir)) {
560 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
565 err = file_mkdir_hier(lists_dir, 0755);
567 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
574 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
575 if (mkdtemp(tmp) == NULL) {
576 opkg_perror(ERROR, "Coundn't create temporary directory %s",
583 /* count the number of sources so we can give some progress updates */
584 sources_list_count = 0;
586 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
587 sources_list_count++;
590 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
591 char *url, *list_file_name = NULL;
593 src = (pkg_src_t *) iter->data;
595 if (src->extra_data) /* debian style? */
596 sprintf_alloc(&url, "%s/%s/%s", src->value,
598 src->gzip ? "Packages.gz" : "Packages");
600 sprintf_alloc(&url, "%s/%s", src->value,
601 src->gzip ? "Packages.gz" : "Packages");
603 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
606 struct _curl_cb_data cb_data;
607 char *tmp_file_name = NULL;
609 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
612 opkg_msg(INFO, "Downloading %s to %s...\n", url,
615 cb_data.cb = progress_callback;
616 cb_data.progress_data = &pdata;
617 cb_data.user_data = user_data;
618 cb_data.start_range =
619 100 * sources_done / sources_list_count;
620 cb_data.finish_range =
621 100 * (sources_done + 1) / sources_list_count;
623 err = opkg_download(url, tmp_file_name,
624 (curl_progress_func) curl_progress_cb,
628 opkg_msg(INFO, "Inflating %s...\n",
630 in = fopen(tmp_file_name, "r");
631 out = fopen(list_file_name, "w");
640 unlink(tmp_file_name);
644 err = opkg_download(url, list_file_name, NULL, NULL);
647 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
652 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
653 if (conf->check_signature) {
655 /* download detached signitures to verify the package lists */
656 /* get the url for the sig file */
657 if (src->extra_data) /* debian style? */
658 sprintf_alloc(&url, "%s/%s/%s", src->value,
659 src->extra_data, "Packages.sig");
661 sprintf_alloc(&url, "%s/%s", src->value,
664 /* create filename for signature */
665 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
668 /* make sure there is no existing signature file */
669 unlink(sig_file_name);
671 err = opkg_download(url, sig_file_name, NULL, NULL);
673 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
676 err = opkg_verify_file(list_file_name,
679 opkg_msg(INFO, "Signature check "
683 opkg_msg(ERROR, "Signature check "
689 free(list_file_name);
693 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
694 " has not been enabled in this build\n",
699 progress(pdata, 100 * sources_done / sources_list_count);
706 /* Now re-read the package lists to update package hash tables. */
707 opkg_re_read_config_files();
713 pkg_compare_names_and_version(const void *a0, const void *b0)
715 const pkg_t *a = *(const pkg_t **)a0;
716 const pkg_t *b = *(const pkg_t **)b0;
719 ret = strcmp(a->name, b->name);
722 ret = pkg_compare_versions(a, b);
728 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
733 opkg_assert(callback);
735 all = pkg_vec_alloc();
736 pkg_hash_fetch_available(all);
738 pkg_vec_sort(all, pkg_compare_names_and_version);
740 for (i = 0; i < all->len; i++) {
745 callback(pkg, user_data);
754 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
756 struct active_list *head;
757 struct active_list *node;
758 pkg_t *old = NULL, *new = NULL;
760 opkg_assert(callback);
762 /* ensure all data is valid */
763 pkg_info_preinstall_check();
765 head = prepare_upgrade_list();
766 for (node = active_list_next(head, head); node;
767 node = active_list_next(head, node)) {
768 old = list_entry(node, pkg_t, list);
769 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
772 callback(new, user_data);
774 active_list_head_delete(head);
779 opkg_find_package(const char *name, const char *ver, const char *arch,
785 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
787 all = pkg_vec_alloc();
788 pkg_hash_fetch_available(all);
789 for (i = 0; i < all->len; i++) {
795 if (sstrcmp(pkg->name, name))
799 pkgv = pkg_version_str_alloc(pkg);
800 if (sstrcmp(pkgv, ver)) {
806 /* check architecture */
808 if (sstrcmp(pkg->architecture, arch))
812 /* check repository */
814 if (sstrcmp(pkg->src->name, repo))
828 * @brief Check the accessibility of repositories.
829 * @return return how many repositories cannot access. 0 means all okay.
832 opkg_repository_accessibility_check(void)
834 pkg_src_list_elt_t *iter;
835 str_list_elt_t *iter1;
837 int repositories = 0;
843 src = str_list_alloc();
845 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
846 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
847 end = index(host, '/');
848 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
849 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
850 end - ((pkg_src_t *) iter->data)->value);
852 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
854 for (iter1 = str_list_first(src); iter1;
855 iter1 = str_list_next(src, iter1)) {
856 if (strstr(iter1->data, stmp))
862 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
865 str_list_append(src, repo_ptr);
870 while (repositories > 0) {
871 iter1 = str_list_pop(src);
874 if (opkg_download(iter1->data, "/dev/null", NULL, NULL))
876 str_list_elt_deinit(iter1);