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())
151 opkg_re_read_config_files(void)
156 if (pkg_hash_load_feeds())
159 if (pkg_hash_load_status_files())
170 opkg_get_option(char *option, void **value)
173 extern opkg_option_t options[];
175 /* look up the option
176 * TODO: this would be much better as a hash table
178 while (options[i].name) {
179 if (strcmp(options[i].name, option) != 0) {
186 switch (options[i].type) {
187 case OPKG_OPT_TYPE_BOOL:
188 *((int *) value) = *((int *) options[i].value);
191 case OPKG_OPT_TYPE_INT:
192 *((int *) value) = *((int *) options[i].value);
195 case OPKG_OPT_TYPE_STRING:
196 *((char **) value) = xstrdup(options[i].value);
203 opkg_set_option(char *option, void *value)
205 int i = 0, found = 0;
206 extern opkg_option_t options[];
208 opkg_assert(option != NULL);
209 opkg_assert(value != NULL);
211 /* look up the option
212 * TODO: this would be much better as a hash table
214 while (options[i].name) {
215 if (strcmp(options[i].name, option) == 0) {
223 opkg_msg(ERROR, "Invalid option: %s\n", option);
228 switch (options[i].type) {
229 case OPKG_OPT_TYPE_BOOL:
230 if (*((int *) value) == 0)
231 *((int *) options[i].value) = 0;
233 *((int *) options[i].value) = 1;
236 case OPKG_OPT_TYPE_INT:
237 *((int *) options[i].value) = *((int *) value);
240 case OPKG_OPT_TYPE_STRING:
241 *((char **) options[i].value) = xstrdup(value);
248 * @brief libopkg API: Install package
249 * @param package_name The name of package in which is going to install
250 * @param progress_callback The callback function that report the status to caller.
253 opkg_install_package(const char *package_name,
254 opkg_progress_callback_t progress_callback,
258 char *stripped_filename;
259 opkg_progress_data_t pdata;
261 pkg_vec_t *deps, *all;
263 char **unresolved = NULL;
265 opkg_assert(package_name != NULL);
268 pkg_info_preinstall_check();
271 /* check to ensure package is not already installed */
272 old = pkg_hash_fetch_installed_by_name(package_name);
274 opkg_msg(ERROR, "Package %s is already installed\n",
279 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
281 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
285 new->state_flag |= SF_USER;
292 /* find dependancies and download them */
293 deps = pkg_vec_alloc();
294 /* this function does not return the original package, so we insert it later */
295 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
298 char **tmp = unresolved;
299 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
300 " for %s:\n", package_name);
302 opkg_msg(ERROR, "\t%s", *tmp);
308 opkg_message(ERROR, "\n");
312 /* insert the package we are installing so that we download it */
313 pkg_vec_insert(deps, new);
315 /* download package and dependencies */
316 for (i = 0; i < deps->len; i++) {
318 struct _curl_cb_data cb_data;
322 if (pkg->local_filename)
326 pdata.action = OPKG_DOWNLOAD;
328 if (pkg->src == NULL) {
329 opkg_msg(ERROR, "Package %s not available from any "
330 "configured src\n", package_name);
334 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
336 /* Get the filename part, without any directory */
337 stripped_filename = strrchr(pkg->filename, '/');
338 if (!stripped_filename)
339 stripped_filename = pkg->filename;
341 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
344 cb_data.cb = progress_callback;
345 cb_data.progress_data = &pdata;
346 cb_data.user_data = user_data;
347 /* 75% of "install" progress is for downloading */
348 cb_data.start_range = 75 * i / deps->len;
349 cb_data.finish_range = 75 * (i + 1) / deps->len;
351 err = opkg_download(url, pkg->local_filename,
352 (curl_progress_func) curl_progress_cb,
364 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
365 all = pkg_vec_alloc();
366 pkg_hash_fetch_available(all);
367 for (i = 0; i < all->len; i++) {
368 all->pkgs[i]->parent->dependencies_checked = 0;
373 /* 75% of "install" progress is for downloading */
375 pdata.action = OPKG_INSTALL;
378 /* unpack the package */
379 err = opkg_install_pkg(new, 0);
387 /* run configure scripts, etc. */
388 err = opkg_configure_packages(NULL);
393 /* write out status files and file lists */
394 opkg_conf_write_status_files();
395 pkg_write_changed_filelists();
397 progress(pdata, 100);
402 opkg_remove_package(const char *package_name,
403 opkg_progress_callback_t progress_callback, void *user_data)
407 pkg_t *pkg_to_remove;
408 opkg_progress_data_t pdata;
410 opkg_assert(package_name != NULL);
412 pkg_info_preinstall_check();
414 pkg = pkg_hash_fetch_installed_by_name(package_name);
416 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
417 opkg_msg(ERROR, "Package %s not installed\n", package_name);
421 pdata.action = OPKG_REMOVE;
425 if (conf->restrict_to_default_dest) {
426 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
429 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
435 err = opkg_remove_pkg(pkg_to_remove, 0);
437 /* write out status files and file lists */
438 opkg_conf_write_status_files();
439 pkg_write_changed_filelists();
442 progress(pdata, 100);
443 return (err) ? -1 : 0;
447 opkg_upgrade_package(const char *package_name,
448 opkg_progress_callback_t progress_callback,
453 opkg_progress_data_t pdata;
455 opkg_assert(package_name != NULL);
457 pkg_info_preinstall_check();
459 if (conf->restrict_to_default_dest) {
460 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
463 pkg = pkg_hash_fetch_installed_by_name(package_name);
467 opkg_msg(ERROR, "Package %s not installed\n", package_name);
471 pdata.action = OPKG_INSTALL;
475 err = opkg_upgrade_pkg(pkg);
481 err = opkg_configure_packages(NULL);
486 /* write out status files and file lists */
487 opkg_conf_write_status_files();
488 pkg_write_changed_filelists();
490 progress(pdata, 100);
495 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
497 pkg_vec_t *installed;
501 opkg_progress_data_t pdata;
503 pdata.action = OPKG_INSTALL;
508 installed = pkg_vec_alloc();
509 pkg_info_preinstall_check();
511 pkg_hash_fetch_all_installed(installed);
512 for (i = 0; i < installed->len; i++) {
513 pkg = installed->pkgs[i];
516 progress(pdata, 99 * i / installed->len);
518 err += opkg_upgrade_pkg(pkg);
520 pkg_vec_free(installed);
525 err = opkg_configure_packages(NULL);
529 /* write out status files and file lists */
530 opkg_conf_write_status_files();
531 pkg_write_changed_filelists();
534 progress(pdata, 100);
539 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
545 pkg_src_list_elt_t *iter;
547 int sources_list_count, sources_done;
548 opkg_progress_data_t pdata;
550 pdata.action = OPKG_DOWNLOAD;
554 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
555 ? conf->default_dest->lists_dir : conf->lists_dir);
557 if (!file_is_dir(lists_dir)) {
558 if (file_exists(lists_dir)) {
559 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
564 err = file_mkdir_hier(lists_dir, 0755);
566 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
573 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
574 if (mkdtemp(tmp) == NULL) {
575 opkg_perror(ERROR, "Coundn't create temporary directory %s",
582 /* count the number of sources so we can give some progress updates */
583 sources_list_count = 0;
585 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
586 sources_list_count++;
589 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
590 char *url, *list_file_name = NULL;
592 src = (pkg_src_t *) iter->data;
594 if (src->extra_data) /* debian style? */
595 sprintf_alloc(&url, "%s/%s/%s", src->value,
597 src->gzip ? "Packages.gz" : "Packages");
599 sprintf_alloc(&url, "%s/%s", src->value,
600 src->gzip ? "Packages.gz" : "Packages");
602 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
605 struct _curl_cb_data cb_data;
606 char *tmp_file_name = NULL;
608 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
611 opkg_msg(INFO, "Downloading %s to %s...\n", url,
614 cb_data.cb = progress_callback;
615 cb_data.progress_data = &pdata;
616 cb_data.user_data = user_data;
617 cb_data.start_range =
618 100 * sources_done / sources_list_count;
619 cb_data.finish_range =
620 100 * (sources_done + 1) / sources_list_count;
622 err = opkg_download(url, tmp_file_name,
623 (curl_progress_func) curl_progress_cb,
627 opkg_msg(INFO, "Inflating %s...\n",
629 in = fopen(tmp_file_name, "r");
630 out = fopen(list_file_name, "w");
639 unlink(tmp_file_name);
643 err = opkg_download(url, list_file_name, NULL, NULL);
646 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
651 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
652 if (conf->check_signature) {
654 /* download detached signitures to verify the package lists */
655 /* get the url for the sig file */
656 if (src->extra_data) /* debian style? */
657 sprintf_alloc(&url, "%s/%s/%s", src->value,
658 src->extra_data, "Packages.sig");
660 sprintf_alloc(&url, "%s/%s", src->value,
663 /* create filename for signature */
664 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
667 /* make sure there is no existing signature file */
668 unlink(sig_file_name);
670 err = opkg_download(url, sig_file_name, NULL, NULL);
672 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
675 err = opkg_verify_file(list_file_name,
678 opkg_msg(INFO, "Signature check "
682 opkg_msg(ERROR, "Signature check "
688 free(list_file_name);
692 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
693 " has not been enabled in this build\n",
698 progress(pdata, 100 * sources_done / sources_list_count);
705 /* Now re-read the package lists to update package hash tables. */
706 opkg_re_read_config_files();
712 pkg_compare_names_and_version(const void *a0, const void *b0)
714 const pkg_t *a = *(const pkg_t **)a0;
715 const pkg_t *b = *(const pkg_t **)b0;
718 ret = strcmp(a->name, b->name);
721 ret = pkg_compare_versions(a, b);
727 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
732 opkg_assert(callback);
734 all = pkg_vec_alloc();
735 pkg_hash_fetch_available(all);
737 pkg_vec_sort(all, pkg_compare_names_and_version);
739 for (i = 0; i < all->len; i++) {
744 callback(pkg, user_data);
753 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
755 struct active_list *head;
756 struct active_list *node;
757 pkg_t *old = NULL, *new = NULL;
759 opkg_assert(callback);
761 /* ensure all data is valid */
762 pkg_info_preinstall_check();
764 head = prepare_upgrade_list();
765 for (node = active_list_next(head, head); node;
766 node = active_list_next(head, node)) {
767 old = list_entry(node, pkg_t, list);
768 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
771 callback(new, user_data);
773 active_list_head_delete(head);
778 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))
825 return pkg_found ? pkg : NULL;
829 * @brief Check the accessibility of repositories.
830 * @return return how many repositories cannot access. 0 means all okay.
833 opkg_repository_accessibility_check(void)
835 pkg_src_list_elt_t *iter;
836 str_list_elt_t *iter1;
838 int repositories = 0;
844 src = str_list_alloc();
846 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
847 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
848 end = index(host, '/');
849 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
850 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
851 end - ((pkg_src_t *) iter->data)->value);
853 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
855 for (iter1 = str_list_first(src); iter1;
856 iter1 = str_list_next(src, iter1)) {
857 if (strstr(iter1->data, stmp))
863 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
866 str_list_append(src, repo_ptr);
871 while (repositories > 0) {
872 iter1 = str_list_pop(src);
875 if (opkg_download(iter1->data, "/dev/null", NULL, NULL))
877 str_list_elt_deinit(iter1);