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 ***/
46 static int opkg_configure_packages(char *pkg_name)
53 all = pkg_vec_alloc();
54 pkg_hash_fetch_available(all);
56 for (i = 0; i < all->len; i++) {
59 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
62 if (pkg->state_status == SS_UNPACKED) {
63 r = opkg_configure(pkg);
65 pkg->state_status = SS_INSTALLED;
66 pkg->parent->state_status = SS_INSTALLED;
67 pkg->state_flag &= ~SF_PREFER;
79 struct _curl_cb_data {
80 opkg_progress_callback_t cb;
81 opkg_progress_data_t *progress_data;
87 int curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
89 double ultotal, double ulnow)
91 int p = (t) ? d * 100 / t : 0;
95 /* prevent the same value being sent twice (can occur due to rounding) */
103 progress = cb_data->start_range +
104 (d / t * ((cb_data->finish_range - cb_data->start_range)));
105 cb_data->progress_data->percentage = progress;
107 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
112 static struct opkg_conf saved_conf;
119 if (opkg_conf_init())
122 if (opkg_conf_load())
125 if (pkg_hash_load_feeds())
128 if (pkg_hash_load_status_files())
148 int opkg_re_read_config_files(void)
155 void opkg_get_option(char *option, void **value)
158 extern opkg_option_t options[];
160 /* look up the option
161 * TODO: this would be much better as a hash table
163 while (options[i].name) {
164 if (strcmp(options[i].name, option) != 0) {
171 switch (options[i].type) {
172 case OPKG_OPT_TYPE_BOOL:
173 *((int *)value) = *((int *)options[i].value);
176 case OPKG_OPT_TYPE_INT:
177 *((int *)value) = *((int *)options[i].value);
180 case OPKG_OPT_TYPE_STRING:
181 *((char **)value) = xstrdup(options[i].value);
187 void opkg_set_option(char *option, void *value)
189 int i = 0, found = 0;
190 extern opkg_option_t options[];
192 opkg_assert(option != NULL);
193 opkg_assert(value != NULL);
195 /* look up the option
196 * TODO: this would be much better as a hash table
198 while (options[i].name) {
199 if (strcmp(options[i].name, option) == 0) {
207 opkg_msg(ERROR, "Invalid option: %s\n", option);
212 switch (options[i].type) {
213 case OPKG_OPT_TYPE_BOOL:
214 if (*((int *)value) == 0)
215 *((int *)options[i].value) = 0;
217 *((int *)options[i].value) = 1;
220 case OPKG_OPT_TYPE_INT:
221 *((int *)options[i].value) = *((int *)value);
224 case OPKG_OPT_TYPE_STRING:
225 *((char **)options[i].value) = xstrdup(value);
232 * @brief libopkg API: Install package
233 * @param package_name The name of package in which is going to install
234 * @param progress_callback The callback function that report the status to caller.
237 opkg_install_package(const char *package_name,
238 opkg_progress_callback_t progress_callback,
242 char *stripped_filename;
243 opkg_progress_data_t pdata;
245 pkg_vec_t *deps, *all;
247 char **unresolved = NULL;
249 opkg_assert(package_name != NULL);
252 pkg_info_preinstall_check();
254 /* check to ensure package is not already installed */
255 old = pkg_hash_fetch_installed_by_name(package_name);
257 opkg_msg(ERROR, "Package %s is already installed\n",
262 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
264 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
268 new->state_flag |= SF_USER;
275 /* find dependancies and download them */
276 deps = pkg_vec_alloc();
277 /* this function does not return the original package, so we insert it later */
278 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
281 char **tmp = unresolved;
282 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
283 " for %s:\n", package_name);
285 opkg_msg(ERROR, "\t%s", *tmp);
291 opkg_message(ERROR, "\n");
295 /* insert the package we are installing so that we download it */
296 pkg_vec_insert(deps, new);
298 /* download package and dependencies */
299 for (i = 0; i < deps->len; i++) {
301 struct _curl_cb_data cb_data;
305 if (pkg->local_filename)
309 pdata.action = OPKG_DOWNLOAD;
311 if (pkg->src == NULL) {
312 opkg_msg(ERROR, "Package %s not available from any "
313 "configured src\n", package_name);
317 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
319 /* Get the filename part, without any directory */
320 stripped_filename = strrchr(pkg->filename, '/');
321 if (!stripped_filename)
322 stripped_filename = pkg->filename;
324 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
327 cb_data.cb = progress_callback;
328 cb_data.progress_data = &pdata;
329 cb_data.user_data = user_data;
330 /* 75% of "install" progress is for downloading */
331 cb_data.start_range = 75 * i / deps->len;
332 cb_data.finish_range = 75 * (i + 1) / deps->len;
334 err = opkg_download(url, pkg->local_filename,
335 (curl_progress_func) curl_progress_cb,
347 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
348 all = pkg_vec_alloc();
349 pkg_hash_fetch_available(all);
350 for (i = 0; i < all->len; i++) {
351 all->pkgs[i]->parent->dependencies_checked = 0;
355 /* 75% of "install" progress is for downloading */
357 pdata.action = OPKG_INSTALL;
360 /* unpack the package */
361 err = opkg_install_pkg(new, 0);
369 /* run configure scripts, etc. */
370 err = opkg_configure_packages(NULL);
375 /* write out status files and file lists */
376 opkg_conf_write_status_files();
377 pkg_write_changed_filelists();
379 progress(pdata, 100);
384 opkg_remove_package(const char *package_name,
385 opkg_progress_callback_t progress_callback, void *user_data)
389 pkg_t *pkg_to_remove;
390 opkg_progress_data_t pdata;
392 opkg_assert(package_name != NULL);
394 pkg_info_preinstall_check();
396 pkg = pkg_hash_fetch_installed_by_name(package_name);
398 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
399 opkg_msg(ERROR, "Package %s not installed\n", package_name);
403 pdata.action = OPKG_REMOVE;
407 if (conf->restrict_to_default_dest) {
408 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
412 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
417 err = opkg_remove_pkg(pkg_to_remove, 0);
419 /* write out status files and file lists */
420 opkg_conf_write_status_files();
421 pkg_write_changed_filelists();
423 progress(pdata, 100);
424 return (err) ? -1 : 0;
428 opkg_upgrade_package(const char *package_name,
429 opkg_progress_callback_t progress_callback,
434 opkg_progress_data_t pdata;
436 opkg_assert(package_name != NULL);
438 pkg_info_preinstall_check();
440 if (conf->restrict_to_default_dest) {
441 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
444 pkg = pkg_hash_fetch_installed_by_name(package_name);
448 opkg_msg(ERROR, "Package %s not installed\n", package_name);
452 pdata.action = OPKG_INSTALL;
456 err = opkg_upgrade_pkg(pkg);
462 err = opkg_configure_packages(NULL);
467 /* write out status files and file lists */
468 opkg_conf_write_status_files();
469 pkg_write_changed_filelists();
471 progress(pdata, 100);
476 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
478 pkg_vec_t *installed;
482 opkg_progress_data_t pdata;
484 pdata.action = OPKG_INSTALL;
489 installed = pkg_vec_alloc();
490 pkg_info_preinstall_check();
492 pkg_hash_fetch_all_installed(installed);
493 for (i = 0; i < installed->len; i++) {
494 pkg = installed->pkgs[i];
497 progress(pdata, 99 * i / installed->len);
499 err += opkg_upgrade_pkg(pkg);
501 pkg_vec_free(installed);
506 err = opkg_configure_packages(NULL);
510 /* write out status files and file lists */
511 opkg_conf_write_status_files();
512 pkg_write_changed_filelists();
515 progress(pdata, 100);
520 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
526 pkg_src_list_elt_t *iter;
528 int sources_list_count, sources_done;
529 opkg_progress_data_t pdata;
531 pdata.action = OPKG_DOWNLOAD;
535 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
536 ? conf->default_dest->lists_dir : conf->lists_dir);
538 if (!file_is_dir(lists_dir)) {
539 if (file_exists(lists_dir)) {
540 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
545 err = file_mkdir_hier(lists_dir, 0755);
547 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
554 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
555 if (mkdtemp(tmp) == NULL) {
556 opkg_perror(ERROR, "Coundn't create temporary directory %s",
563 /* count the number of sources so we can give some progress updates */
564 sources_list_count = 0;
566 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
567 sources_list_count++;
570 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
571 char *url, *list_file_name = NULL;
573 src = (pkg_src_t *) iter->data;
575 if (src->extra_data) /* debian style? */
576 sprintf_alloc(&url, "%s/%s/%s", src->value,
578 src->gzip ? "Packages.gz" : "Packages");
580 sprintf_alloc(&url, "%s/%s", src->value,
581 src->gzip ? "Packages.gz" : "Packages");
583 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
585 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
586 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
591 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
592 if (conf->check_signature) {
594 /* download detached signitures to verify the package lists */
595 /* get the url for the sig file */
596 if (src->extra_data) /* debian style? */
597 sprintf_alloc(&url, "%s/%s/%s", src->value,
598 src->extra_data, "Packages.sig");
600 sprintf_alloc(&url, "%s/%s", src->value,
603 /* create filename for signature */
604 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
607 /* make sure there is no existing signature file */
608 unlink(sig_file_name);
610 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
612 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
615 err = opkg_verify_file(list_file_name,
618 opkg_msg(INFO, "Signature check "
622 opkg_msg(ERROR, "Signature check "
631 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
632 " has not been enabled in this build\n",
635 free(list_file_name);
638 progress(pdata, 100 * sources_done / sources_list_count);
645 /* Now re-read the package lists to update package hash tables. */
646 opkg_re_read_config_files();
651 static int pkg_compare_names_and_version(const void *a0, const void *b0)
653 const pkg_t *a = *(const pkg_t **)a0;
654 const pkg_t *b = *(const pkg_t **)b0;
657 ret = strcmp(a->name, b->name);
660 ret = pkg_compare_versions(a, b);
665 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
670 opkg_assert(callback);
672 all = pkg_vec_alloc();
673 pkg_hash_fetch_available(all);
675 pkg_vec_sort(all, pkg_compare_names_and_version);
677 for (i = 0; i < all->len; i++) {
682 callback(pkg, user_data);
691 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
693 struct active_list *head;
694 struct active_list *node;
695 pkg_t *old = NULL, *new = NULL;
697 opkg_assert(callback);
699 /* ensure all data is valid */
700 pkg_info_preinstall_check();
702 head = prepare_upgrade_list();
703 for (node = active_list_next(head, head); node;
704 node = active_list_next(head, node)) {
705 old = list_entry(node, pkg_t, list);
707 pkg_hash_fetch_best_installation_candidate_by_name(old->
711 callback(new, user_data);
713 active_list_head_delete(head);
717 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
724 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
726 all = pkg_vec_alloc();
727 pkg_hash_fetch_available(all);
728 for (i = 0; i < all->len; i++) {
734 if (sstrcmp(pkg->name, name))
738 pkgv = pkg_version_str_alloc(pkg);
739 if (sstrcmp(pkgv, ver)) {
745 /* check architecture */
747 if (sstrcmp(pkg->architecture, arch))
751 /* check repository */
753 if (sstrcmp(pkg->src->name, repo))
764 return pkg_found ? pkg : NULL;
768 * @brief Check the accessibility of repositories.
769 * @return return how many repositories cannot access. 0 means all okay.
771 int opkg_repository_accessibility_check(void)
773 pkg_src_list_elt_t *iter;
774 str_list_elt_t *iter1;
776 int repositories = 0;
782 src = str_list_alloc();
784 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
785 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
786 end = index(host, '/');
787 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
788 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
790 ((pkg_src_t *) iter->data)->value);
792 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
794 for (iter1 = str_list_first(src); iter1;
795 iter1 = str_list_next(src, iter1)) {
796 if (strstr(iter1->data, stmp))
802 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
805 str_list_append(src, repo_ptr);
810 while (repositories > 0) {
811 iter1 = str_list_pop(src);
814 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
816 str_list_elt_deinit(iter1);