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.
22 #include "opkg_conf.h"
24 #include "opkg_install.h"
25 #include "opkg_configure.h"
26 #include "opkg_download.h"
27 #include "opkg_remove.h"
28 #include "opkg_upgrade.h"
30 #include "sprintf_alloc.h"
31 #include "file_util.h"
33 #include <libbb/libbb.h>
35 #define opkg_assert(expr) if (!(expr)) { \
36 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
37 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
39 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
41 /** Private Functions ***/
44 opkg_configure_packages(char *pkg_name)
51 all = pkg_vec_alloc();
52 pkg_hash_fetch_available(all);
54 for (i = 0; i < all->len; i++) {
57 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
60 if (pkg->state_status == SS_UNPACKED) {
61 r = opkg_configure(pkg);
63 pkg->state_status = SS_INSTALLED;
64 pkg->parent->state_status = SS_INSTALLED;
65 pkg->state_flag &= ~SF_PREFER;
77 struct _curl_cb_data {
78 opkg_progress_callback_t cb;
79 opkg_progress_data_t *progress_data;
86 curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
88 double ultotal, double ulnow)
90 int p = (t) ? d * 100 / t : 0;
94 /* prevent the same value being sent twice (can occur due to rounding) */
102 progress = cb_data->start_range +
103 (d / t * ((cb_data->finish_range - cb_data->start_range)));
104 cb_data->progress_data->percentage = progress;
106 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
117 if (opkg_conf_init())
120 if (pkg_hash_load_feeds())
123 if (pkg_hash_load_status_files())
145 opkg_re_read_config_files(void)
150 if (pkg_hash_load_feeds())
153 if (pkg_hash_load_status_files())
164 opkg_get_option(char *option, void **value)
167 extern opkg_option_t options[];
169 /* look up the option
170 * TODO: this would be much better as a hash table
172 while (options[i].name) {
173 if (strcmp(options[i].name, option) != 0) {
180 switch (options[i].type) {
181 case OPKG_OPT_TYPE_BOOL:
182 *((int *) value) = *((int *) options[i].value);
185 case OPKG_OPT_TYPE_INT:
186 *((int *) value) = *((int *) options[i].value);
189 case OPKG_OPT_TYPE_STRING:
190 *((char **) value) = xstrdup(options[i].value);
197 opkg_set_option(char *option, void *value)
199 int i = 0, found = 0;
200 extern opkg_option_t options[];
202 opkg_assert(option != NULL);
203 opkg_assert(value != NULL);
205 /* look up the option
206 * TODO: this would be much better as a hash table
208 while (options[i].name) {
209 if (strcmp(options[i].name, option) == 0) {
217 opkg_msg(ERROR, "Invalid option: %s\n", option);
222 switch (options[i].type) {
223 case OPKG_OPT_TYPE_BOOL:
224 if (*((int *) value) == 0)
225 *((int *) options[i].value) = 0;
227 *((int *) options[i].value) = 1;
230 case OPKG_OPT_TYPE_INT:
231 *((int *) options[i].value) = *((int *) value);
234 case OPKG_OPT_TYPE_STRING:
235 *((char **) options[i].value) = xstrdup(value);
242 * @brief libopkg API: Install package
243 * @param package_name The name of package in which is going to install
244 * @param progress_callback The callback function that report the status to caller.
247 opkg_install_package(const char *package_name,
248 opkg_progress_callback_t progress_callback,
252 char *stripped_filename;
253 opkg_progress_data_t pdata;
255 pkg_vec_t *deps, *all;
257 char **unresolved = NULL;
259 opkg_assert(package_name != NULL);
262 pkg_info_preinstall_check();
265 /* check to ensure package is not already installed */
266 old = pkg_hash_fetch_installed_by_name(package_name);
268 opkg_msg(ERROR, "Package %s is already installed\n",
273 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
275 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
279 new->state_flag |= SF_USER;
286 /* find dependancies and download them */
287 deps = pkg_vec_alloc();
288 /* this function does not return the original package, so we insert it later */
289 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
292 char **tmp = unresolved;
293 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
294 " for %s:\n", package_name);
296 opkg_msg(ERROR, "\t%s", *tmp);
305 /* insert the package we are installing so that we download it */
306 pkg_vec_insert(deps, new);
308 /* download package and dependencies */
309 for (i = 0; i < deps->len; i++) {
311 struct _curl_cb_data cb_data;
315 if (pkg->local_filename)
319 pdata.action = OPKG_DOWNLOAD;
321 if (pkg->src == NULL) {
322 opkg_msg(ERROR, "Package %s not available from any "
323 "configured src\n", package_name);
327 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
329 /* Get the filename part, without any directory */
330 stripped_filename = strrchr(pkg->filename, '/');
331 if (!stripped_filename)
332 stripped_filename = pkg->filename;
334 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
337 cb_data.cb = progress_callback;
338 cb_data.progress_data = &pdata;
339 cb_data.user_data = user_data;
340 /* 75% of "install" progress is for downloading */
341 cb_data.start_range = 75 * i / deps->len;
342 cb_data.finish_range = 75 * (i + 1) / deps->len;
344 err = opkg_download(url, pkg->local_filename,
345 (curl_progress_func) curl_progress_cb,
357 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
358 all = pkg_vec_alloc();
359 pkg_hash_fetch_available(all);
360 for (i = 0; i < all->len; i++) {
361 all->pkgs[i]->parent->dependencies_checked = 0;
366 /* 75% of "install" progress is for downloading */
368 pdata.action = OPKG_INSTALL;
371 /* unpack the package */
372 err = opkg_install_pkg(new, 0);
380 /* run configure scripts, etc. */
381 err = opkg_configure_packages(NULL);
386 /* write out status files and file lists */
387 opkg_conf_write_status_files();
388 pkg_write_changed_filelists();
390 progress(pdata, 100);
395 opkg_remove_package(const char *package_name,
396 opkg_progress_callback_t progress_callback, void *user_data)
400 pkg_t *pkg_to_remove;
401 opkg_progress_data_t pdata;
403 opkg_assert(package_name != NULL);
405 pkg_info_preinstall_check();
407 pkg = pkg_hash_fetch_installed_by_name(package_name);
409 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
410 opkg_msg(ERROR, "Package %s not installed\n", package_name);
414 pdata.action = OPKG_REMOVE;
418 if (conf->restrict_to_default_dest) {
419 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
422 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
428 err = opkg_remove_pkg(pkg_to_remove, 0);
430 /* write out status files and file lists */
431 opkg_conf_write_status_files();
432 pkg_write_changed_filelists();
435 progress(pdata, 100);
436 return (err) ? -1 : 0;
440 opkg_upgrade_package(const char *package_name,
441 opkg_progress_callback_t progress_callback,
446 opkg_progress_data_t pdata;
448 opkg_assert(package_name != NULL);
450 pkg_info_preinstall_check();
452 if (conf->restrict_to_default_dest) {
453 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
456 pkg = pkg_hash_fetch_installed_by_name(package_name);
460 opkg_msg(ERROR, "Package %s not installed\n", package_name);
464 pdata.action = OPKG_INSTALL;
468 err = opkg_upgrade_pkg(pkg);
474 err = opkg_configure_packages(NULL);
479 /* write out status files and file lists */
480 opkg_conf_write_status_files();
481 pkg_write_changed_filelists();
483 progress(pdata, 100);
488 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
490 pkg_vec_t *installed;
494 opkg_progress_data_t pdata;
496 pdata.action = OPKG_INSTALL;
501 installed = pkg_vec_alloc();
502 pkg_info_preinstall_check();
504 pkg_hash_fetch_all_installed(installed);
505 for (i = 0; i < installed->len; i++) {
506 pkg = installed->pkgs[i];
509 progress(pdata, 99 * i / installed->len);
511 err += opkg_upgrade_pkg(pkg);
513 pkg_vec_free(installed);
518 err = opkg_configure_packages(NULL);
523 progress(pdata, 100);
528 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
534 pkg_src_list_elt_t *iter;
536 int sources_list_count, sources_done;
537 opkg_progress_data_t pdata;
539 pdata.action = OPKG_DOWNLOAD;
543 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
544 ? conf->default_dest->lists_dir : conf->lists_dir);
546 if (!file_is_dir(lists_dir)) {
547 if (file_exists(lists_dir)) {
548 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
553 err = file_mkdir_hier(lists_dir, 0755);
555 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
562 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
563 if (mkdtemp(tmp) == NULL) {
564 opkg_perror(ERROR, "Coundn't create temporary directory %s",
571 /* count the number of sources so we can give some progress updates */
572 sources_list_count = 0;
574 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
575 sources_list_count++;
578 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
579 char *url, *list_file_name = NULL;
581 src = (pkg_src_t *) iter->data;
583 if (src->extra_data) /* debian style? */
584 sprintf_alloc(&url, "%s/%s/%s", src->value,
586 src->gzip ? "Packages.gz" : "Packages");
588 sprintf_alloc(&url, "%s/%s", src->value,
589 src->gzip ? "Packages.gz" : "Packages");
591 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
594 struct _curl_cb_data cb_data;
595 char *tmp_file_name = NULL;
597 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
600 opkg_msg(INFO, "Downloading %s to %s...\n", url,
603 cb_data.cb = progress_callback;
604 cb_data.progress_data = &pdata;
605 cb_data.user_data = user_data;
606 cb_data.start_range =
607 100 * sources_done / sources_list_count;
608 cb_data.finish_range =
609 100 * (sources_done + 1) / sources_list_count;
611 err = opkg_download(url, tmp_file_name,
612 (curl_progress_func) curl_progress_cb,
616 opkg_msg(INFO, "Inflating %s...\n",
618 in = fopen(tmp_file_name, "r");
619 out = fopen(list_file_name, "w");
628 unlink(tmp_file_name);
632 err = opkg_download(url, list_file_name, NULL, NULL);
635 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
640 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
641 if (conf->check_signature) {
643 /* download detached signitures to verify the package lists */
644 /* get the url for the sig file */
645 if (src->extra_data) /* debian style? */
646 sprintf_alloc(&url, "%s/%s/%s", src->value,
647 src->extra_data, "Packages.sig");
649 sprintf_alloc(&url, "%s/%s", src->value,
652 /* create filename for signature */
653 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
656 /* make sure there is no existing signature file */
657 unlink(sig_file_name);
659 err = opkg_download(url, sig_file_name, NULL, NULL);
661 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
664 err = opkg_verify_file(list_file_name,
667 opkg_msg(INFO, "Signature check "
671 opkg_msg(ERROR, "Signature check "
677 free(list_file_name);
681 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
682 " has not been enabled in this build\n",
687 progress(pdata, 100 * sources_done / sources_list_count);
694 /* Now re-read the package lists to update package hash tables. */
695 opkg_re_read_config_files();
701 pkg_compare_names_and_version(const void *a0, const void *b0)
703 const pkg_t *a = *(const pkg_t **)a0;
704 const pkg_t *b = *(const pkg_t **)b0;
707 ret = strcmp(a->name, b->name);
710 ret = pkg_compare_versions(a, b);
716 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
721 opkg_assert(callback);
723 all = pkg_vec_alloc();
724 pkg_hash_fetch_available(all);
726 pkg_vec_sort(all, pkg_compare_names_and_version);
728 for (i = 0; i < all->len; i++) {
733 callback(pkg, user_data);
742 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
744 struct active_list *head;
745 struct active_list *node;
746 pkg_t *old = NULL, *new = NULL;
748 opkg_assert(callback);
750 /* ensure all data is valid */
751 pkg_info_preinstall_check();
753 head = prepare_upgrade_list();
754 for (node = active_list_next(head, head); node;
755 active_list_next(head, node)) {
756 old = list_entry(node, pkg_t, list);
757 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
760 callback(new, user_data);
762 active_list_head_delete(head);
767 opkg_find_package(const char *name, const char *ver, const char *arch,
773 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
775 all = pkg_vec_alloc();
776 pkg_hash_fetch_available(all);
777 for (i = 0; i < all->len; i++) {
783 if (sstrcmp(pkg->name, name))
787 pkgv = pkg_version_str_alloc(pkg);
788 if (sstrcmp(pkgv, ver)) {
794 /* check architecture */
796 if (sstrcmp(pkg->architecture, arch))
800 /* check repository */
802 if (sstrcmp(pkg->src->name, repo))
816 #include <curl/curl.h>
819 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
820 * @return return how many repositories cannot access. 0 means all okay.
823 opkg_repository_accessibility_check(void)
825 pkg_src_list_elt_t *iter;
826 str_list_elt_t *iter1;
828 int repositories = 0;
835 src = str_list_alloc();
837 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
838 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
839 end = index(host, '/');
840 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
841 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
842 end - ((pkg_src_t *) iter->data)->value);
844 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
846 for (iter1 = str_list_first(src); iter1;
847 iter1 = str_list_next(src, iter1)) {
848 if (strstr(iter1->data, stmp))
854 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
857 str_list_append(src, repo_ptr);
861 while (repositories > 0) {
862 iter1 = str_list_pop(src);
865 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
867 if (!(err == CURLE_OK ||
868 err == CURLE_HTTP_RETURNED_ERROR ||
869 err == CURLE_FILE_COULDNT_READ_FILE ||
870 err == CURLE_REMOTE_FILE_NOT_FOUND ||
871 err == CURLE_TFTP_NOTFOUND)) {
877 str_list_elt_deinit(iter1);