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 (pkg_hash_load_feeds())
126 if (pkg_hash_load_status_files())
148 opkg_re_read_config_files(void)
153 if (pkg_hash_load_feeds())
156 if (pkg_hash_load_status_files())
167 opkg_get_option(char *option, void **value)
170 extern opkg_option_t options[];
172 /* look up the option
173 * TODO: this would be much better as a hash table
175 while (options[i].name) {
176 if (strcmp(options[i].name, option) != 0) {
183 switch (options[i].type) {
184 case OPKG_OPT_TYPE_BOOL:
185 *((int *) value) = *((int *) options[i].value);
188 case OPKG_OPT_TYPE_INT:
189 *((int *) value) = *((int *) options[i].value);
192 case OPKG_OPT_TYPE_STRING:
193 *((char **) value) = xstrdup(options[i].value);
200 opkg_set_option(char *option, void *value)
202 int i = 0, found = 0;
203 extern opkg_option_t options[];
205 opkg_assert(option != NULL);
206 opkg_assert(value != NULL);
208 /* look up the option
209 * TODO: this would be much better as a hash table
211 while (options[i].name) {
212 if (strcmp(options[i].name, option) == 0) {
220 opkg_msg(ERROR, "Invalid option: %s\n", option);
225 switch (options[i].type) {
226 case OPKG_OPT_TYPE_BOOL:
227 if (*((int *) value) == 0)
228 *((int *) options[i].value) = 0;
230 *((int *) options[i].value) = 1;
233 case OPKG_OPT_TYPE_INT:
234 *((int *) options[i].value) = *((int *) value);
237 case OPKG_OPT_TYPE_STRING:
238 *((char **) options[i].value) = xstrdup(value);
245 * @brief libopkg API: Install package
246 * @param package_name The name of package in which is going to install
247 * @param progress_callback The callback function that report the status to caller.
250 opkg_install_package(const char *package_name,
251 opkg_progress_callback_t progress_callback,
255 char *stripped_filename;
256 opkg_progress_data_t pdata;
258 pkg_vec_t *deps, *all;
260 char **unresolved = NULL;
262 opkg_assert(package_name != NULL);
265 pkg_info_preinstall_check();
268 /* check to ensure package is not already installed */
269 old = pkg_hash_fetch_installed_by_name(package_name);
271 opkg_msg(ERROR, "Package %s is already installed\n",
276 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
278 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
282 new->state_flag |= SF_USER;
289 /* find dependancies and download them */
290 deps = pkg_vec_alloc();
291 /* this function does not return the original package, so we insert it later */
292 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
295 char **tmp = unresolved;
296 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
297 " for %s:\n", package_name);
299 opkg_msg(ERROR, "\t%s", *tmp);
305 opkg_message(ERROR, "\n");
309 /* insert the package we are installing so that we download it */
310 pkg_vec_insert(deps, new);
312 /* download package and dependencies */
313 for (i = 0; i < deps->len; i++) {
315 struct _curl_cb_data cb_data;
319 if (pkg->local_filename)
323 pdata.action = OPKG_DOWNLOAD;
325 if (pkg->src == NULL) {
326 opkg_msg(ERROR, "Package %s not available from any "
327 "configured src\n", package_name);
331 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
333 /* Get the filename part, without any directory */
334 stripped_filename = strrchr(pkg->filename, '/');
335 if (!stripped_filename)
336 stripped_filename = pkg->filename;
338 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
341 cb_data.cb = progress_callback;
342 cb_data.progress_data = &pdata;
343 cb_data.user_data = user_data;
344 /* 75% of "install" progress is for downloading */
345 cb_data.start_range = 75 * i / deps->len;
346 cb_data.finish_range = 75 * (i + 1) / deps->len;
348 err = opkg_download(url, pkg->local_filename,
349 (curl_progress_func) curl_progress_cb,
361 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
362 all = pkg_vec_alloc();
363 pkg_hash_fetch_available(all);
364 for (i = 0; i < all->len; i++) {
365 all->pkgs[i]->parent->dependencies_checked = 0;
370 /* 75% of "install" progress is for downloading */
372 pdata.action = OPKG_INSTALL;
375 /* unpack the package */
376 err = opkg_install_pkg(new, 0);
384 /* run configure scripts, etc. */
385 err = opkg_configure_packages(NULL);
390 /* write out status files and file lists */
391 opkg_conf_write_status_files();
392 pkg_write_changed_filelists();
394 progress(pdata, 100);
399 opkg_remove_package(const char *package_name,
400 opkg_progress_callback_t progress_callback, void *user_data)
404 pkg_t *pkg_to_remove;
405 opkg_progress_data_t pdata;
407 opkg_assert(package_name != NULL);
409 pkg_info_preinstall_check();
411 pkg = pkg_hash_fetch_installed_by_name(package_name);
413 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
414 opkg_msg(ERROR, "Package %s not installed\n", package_name);
418 pdata.action = OPKG_REMOVE;
422 if (conf->restrict_to_default_dest) {
423 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
426 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
432 err = opkg_remove_pkg(pkg_to_remove, 0);
434 /* write out status files and file lists */
435 opkg_conf_write_status_files();
436 pkg_write_changed_filelists();
439 progress(pdata, 100);
440 return (err) ? -1 : 0;
444 opkg_upgrade_package(const char *package_name,
445 opkg_progress_callback_t progress_callback,
450 opkg_progress_data_t pdata;
452 opkg_assert(package_name != NULL);
454 pkg_info_preinstall_check();
456 if (conf->restrict_to_default_dest) {
457 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
460 pkg = pkg_hash_fetch_installed_by_name(package_name);
464 opkg_msg(ERROR, "Package %s not installed\n", package_name);
468 pdata.action = OPKG_INSTALL;
472 err = opkg_upgrade_pkg(pkg);
478 err = opkg_configure_packages(NULL);
483 /* write out status files and file lists */
484 opkg_conf_write_status_files();
485 pkg_write_changed_filelists();
487 progress(pdata, 100);
492 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
494 pkg_vec_t *installed;
498 opkg_progress_data_t pdata;
500 pdata.action = OPKG_INSTALL;
505 installed = pkg_vec_alloc();
506 pkg_info_preinstall_check();
508 pkg_hash_fetch_all_installed(installed);
509 for (i = 0; i < installed->len; i++) {
510 pkg = installed->pkgs[i];
513 progress(pdata, 99 * i / installed->len);
515 err += opkg_upgrade_pkg(pkg);
517 pkg_vec_free(installed);
522 err = opkg_configure_packages(NULL);
526 /* write out status files and file lists */
527 opkg_conf_write_status_files();
528 pkg_write_changed_filelists();
531 progress(pdata, 100);
536 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
542 pkg_src_list_elt_t *iter;
544 int sources_list_count, sources_done;
545 opkg_progress_data_t pdata;
547 pdata.action = OPKG_DOWNLOAD;
551 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
552 ? conf->default_dest->lists_dir : conf->lists_dir);
554 if (!file_is_dir(lists_dir)) {
555 if (file_exists(lists_dir)) {
556 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
561 err = file_mkdir_hier(lists_dir, 0755);
563 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
570 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
571 if (mkdtemp(tmp) == NULL) {
572 opkg_perror(ERROR, "Coundn't create temporary directory %s",
579 /* count the number of sources so we can give some progress updates */
580 sources_list_count = 0;
582 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
583 sources_list_count++;
586 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
587 char *url, *list_file_name = NULL;
589 src = (pkg_src_t *) iter->data;
591 if (src->extra_data) /* debian style? */
592 sprintf_alloc(&url, "%s/%s/%s", src->value,
594 src->gzip ? "Packages.gz" : "Packages");
596 sprintf_alloc(&url, "%s/%s", src->value,
597 src->gzip ? "Packages.gz" : "Packages");
599 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
602 struct _curl_cb_data cb_data;
603 char *tmp_file_name = NULL;
605 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
608 opkg_msg(INFO, "Downloading %s to %s...\n", url,
611 cb_data.cb = progress_callback;
612 cb_data.progress_data = &pdata;
613 cb_data.user_data = user_data;
614 cb_data.start_range =
615 100 * sources_done / sources_list_count;
616 cb_data.finish_range =
617 100 * (sources_done + 1) / sources_list_count;
619 err = opkg_download(url, tmp_file_name,
620 (curl_progress_func) curl_progress_cb,
624 opkg_msg(INFO, "Inflating %s...\n",
626 in = fopen(tmp_file_name, "r");
627 out = fopen(list_file_name, "w");
636 unlink(tmp_file_name);
640 err = opkg_download(url, list_file_name, NULL, NULL);
643 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
648 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
649 if (conf->check_signature) {
651 /* download detached signitures to verify the package lists */
652 /* get the url for the sig file */
653 if (src->extra_data) /* debian style? */
654 sprintf_alloc(&url, "%s/%s/%s", src->value,
655 src->extra_data, "Packages.sig");
657 sprintf_alloc(&url, "%s/%s", src->value,
660 /* create filename for signature */
661 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
664 /* make sure there is no existing signature file */
665 unlink(sig_file_name);
667 err = opkg_download(url, sig_file_name, NULL, NULL);
669 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
672 err = opkg_verify_file(list_file_name,
675 opkg_msg(INFO, "Signature check "
679 opkg_msg(ERROR, "Signature check "
685 free(list_file_name);
689 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
690 " has not been enabled in this build\n",
695 progress(pdata, 100 * sources_done / sources_list_count);
702 /* Now re-read the package lists to update package hash tables. */
703 opkg_re_read_config_files();
709 pkg_compare_names_and_version(const void *a0, const void *b0)
711 const pkg_t *a = *(const pkg_t **)a0;
712 const pkg_t *b = *(const pkg_t **)b0;
715 ret = strcmp(a->name, b->name);
718 ret = pkg_compare_versions(a, b);
724 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
729 opkg_assert(callback);
731 all = pkg_vec_alloc();
732 pkg_hash_fetch_available(all);
734 pkg_vec_sort(all, pkg_compare_names_and_version);
736 for (i = 0; i < all->len; i++) {
741 callback(pkg, user_data);
750 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
752 struct active_list *head;
753 struct active_list *node;
754 pkg_t *old = NULL, *new = NULL;
756 opkg_assert(callback);
758 /* ensure all data is valid */
759 pkg_info_preinstall_check();
761 head = prepare_upgrade_list();
762 for (node = active_list_next(head, head); node;
763 node = active_list_next(head, node)) {
764 old = list_entry(node, pkg_t, list);
765 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
768 callback(new, user_data);
770 active_list_head_delete(head);
775 opkg_find_package(const char *name, const char *ver, const char *arch,
781 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
783 all = pkg_vec_alloc();
784 pkg_hash_fetch_available(all);
785 for (i = 0; i < all->len; i++) {
791 if (sstrcmp(pkg->name, name))
795 pkgv = pkg_version_str_alloc(pkg);
796 if (sstrcmp(pkgv, ver)) {
802 /* check architecture */
804 if (sstrcmp(pkg->architecture, arch))
808 /* check repository */
810 if (sstrcmp(pkg->src->name, repo))
824 * @brief Check the accessibility of repositories.
825 * @return return how many repositories cannot access. 0 means all okay.
828 opkg_repository_accessibility_check(void)
830 pkg_src_list_elt_t *iter;
831 str_list_elt_t *iter1;
833 int repositories = 0;
839 src = str_list_alloc();
841 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
842 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
843 end = index(host, '/');
844 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
845 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
846 end - ((pkg_src_t *) iter->data)->value);
848 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
850 for (iter1 = str_list_first(src); iter1;
851 iter1 = str_list_next(src, iter1)) {
852 if (strstr(iter1->data, stmp))
858 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
861 str_list_append(src, repo_ptr);
866 while (repositories > 0) {
867 iter1 = str_list_pop(src);
870 if (opkg_download(iter1->data, "/dev/null", NULL, NULL))
872 str_list_elt_deinit(iter1);