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);
527 progress(pdata, 100);
532 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
538 pkg_src_list_elt_t *iter;
540 int sources_list_count, sources_done;
541 opkg_progress_data_t pdata;
543 pdata.action = OPKG_DOWNLOAD;
547 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
548 ? conf->default_dest->lists_dir : conf->lists_dir);
550 if (!file_is_dir(lists_dir)) {
551 if (file_exists(lists_dir)) {
552 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
557 err = file_mkdir_hier(lists_dir, 0755);
559 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
566 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
567 if (mkdtemp(tmp) == NULL) {
568 opkg_perror(ERROR, "Coundn't create temporary directory %s",
575 /* count the number of sources so we can give some progress updates */
576 sources_list_count = 0;
578 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
579 sources_list_count++;
582 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
583 char *url, *list_file_name = NULL;
585 src = (pkg_src_t *) iter->data;
587 if (src->extra_data) /* debian style? */
588 sprintf_alloc(&url, "%s/%s/%s", src->value,
590 src->gzip ? "Packages.gz" : "Packages");
592 sprintf_alloc(&url, "%s/%s", src->value,
593 src->gzip ? "Packages.gz" : "Packages");
595 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
598 struct _curl_cb_data cb_data;
599 char *tmp_file_name = NULL;
601 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
604 opkg_msg(INFO, "Downloading %s to %s...\n", url,
607 cb_data.cb = progress_callback;
608 cb_data.progress_data = &pdata;
609 cb_data.user_data = user_data;
610 cb_data.start_range =
611 100 * sources_done / sources_list_count;
612 cb_data.finish_range =
613 100 * (sources_done + 1) / sources_list_count;
615 err = opkg_download(url, tmp_file_name,
616 (curl_progress_func) curl_progress_cb,
620 opkg_msg(INFO, "Inflating %s...\n",
622 in = fopen(tmp_file_name, "r");
623 out = fopen(list_file_name, "w");
632 unlink(tmp_file_name);
636 err = opkg_download(url, list_file_name, NULL, NULL);
639 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
644 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
645 if (conf->check_signature) {
647 /* download detached signitures to verify the package lists */
648 /* get the url for the sig file */
649 if (src->extra_data) /* debian style? */
650 sprintf_alloc(&url, "%s/%s/%s", src->value,
651 src->extra_data, "Packages.sig");
653 sprintf_alloc(&url, "%s/%s", src->value,
656 /* create filename for signature */
657 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
660 /* make sure there is no existing signature file */
661 unlink(sig_file_name);
663 err = opkg_download(url, sig_file_name, NULL, NULL);
665 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
668 err = opkg_verify_file(list_file_name,
671 opkg_msg(INFO, "Signature check "
675 opkg_msg(ERROR, "Signature check "
681 free(list_file_name);
685 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
686 " has not been enabled in this build\n",
691 progress(pdata, 100 * sources_done / sources_list_count);
698 /* Now re-read the package lists to update package hash tables. */
699 opkg_re_read_config_files();
705 pkg_compare_names_and_version(const void *a0, const void *b0)
707 const pkg_t *a = *(const pkg_t **)a0;
708 const pkg_t *b = *(const pkg_t **)b0;
711 ret = strcmp(a->name, b->name);
714 ret = pkg_compare_versions(a, b);
720 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
725 opkg_assert(callback);
727 all = pkg_vec_alloc();
728 pkg_hash_fetch_available(all);
730 pkg_vec_sort(all, pkg_compare_names_and_version);
732 for (i = 0; i < all->len; i++) {
737 callback(pkg, user_data);
746 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
748 struct active_list *head;
749 struct active_list *node;
750 pkg_t *old = NULL, *new = NULL;
752 opkg_assert(callback);
754 /* ensure all data is valid */
755 pkg_info_preinstall_check();
757 head = prepare_upgrade_list();
758 for (node = active_list_next(head, head); node;
759 active_list_next(head, node)) {
760 old = list_entry(node, pkg_t, list);
761 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
764 callback(new, user_data);
766 active_list_head_delete(head);
771 opkg_find_package(const char *name, const char *ver, const char *arch,
777 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
779 all = pkg_vec_alloc();
780 pkg_hash_fetch_available(all);
781 for (i = 0; i < all->len; i++) {
787 if (sstrcmp(pkg->name, name))
791 pkgv = pkg_version_str_alloc(pkg);
792 if (sstrcmp(pkgv, ver)) {
798 /* check architecture */
800 if (sstrcmp(pkg->architecture, arch))
804 /* check repository */
806 if (sstrcmp(pkg->src->name, repo))
820 #include <curl/curl.h>
823 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
824 * @return return how many repositories cannot access. 0 means all okay.
827 opkg_repository_accessibility_check(void)
829 pkg_src_list_elt_t *iter;
830 str_list_elt_t *iter1;
832 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);
865 while (repositories > 0) {
866 iter1 = str_list_pop(src);
869 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
871 if (!(err == CURLE_OK ||
872 err == CURLE_HTTP_RETURNED_ERROR ||
873 err == CURLE_FILE_COULDNT_READ_FILE ||
874 err == CURLE_REMOTE_FILE_NOT_FOUND ||
875 err == CURLE_TFTP_NOTFOUND)) {
881 str_list_elt_deinit(iter1);