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"
25 #include "opkg_install.h"
26 #include "opkg_configure.h"
27 #include "opkg_download.h"
28 #include "opkg_remove.h"
29 #include "opkg_upgrade.h"
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
34 #include <libbb/libbb.h>
36 #define opkg_assert(expr) if (!(expr)) { \
37 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
38 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
40 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
42 /** Private Functions ***/
45 opkg_configure_packages(char *pkg_name)
52 all = pkg_vec_alloc();
53 pkg_hash_fetch_available(all);
55 for (i = 0; i < all->len; i++) {
58 if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
61 if (pkg->state_status == SS_UNPACKED) {
62 r = opkg_configure(pkg);
64 pkg->state_status = SS_INSTALLED;
65 pkg->parent->state_status = SS_INSTALLED;
66 pkg->state_flag &= ~SF_PREFER;
78 struct _curl_cb_data {
79 opkg_progress_callback_t cb;
80 opkg_progress_data_t *progress_data;
87 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);
122 if (opkg_conf_init(&args))
127 if (pkg_hash_load_feeds())
130 if (pkg_hash_load_status_files())
152 opkg_re_read_config_files(void)
157 if (pkg_hash_load_feeds())
160 if (pkg_hash_load_status_files())
171 opkg_get_option(char *option, void **value)
174 extern opkg_option_t options[];
176 /* look up the option
177 * TODO: this would be much better as a hash table
179 while (options[i].name) {
180 if (strcmp(options[i].name, option) != 0) {
187 switch (options[i].type) {
188 case OPKG_OPT_TYPE_BOOL:
189 *((int *) value) = *((int *) options[i].value);
192 case OPKG_OPT_TYPE_INT:
193 *((int *) value) = *((int *) options[i].value);
196 case OPKG_OPT_TYPE_STRING:
197 *((char **) value) = xstrdup(options[i].value);
204 opkg_set_option(char *option, void *value)
206 int i = 0, found = 0;
207 extern opkg_option_t options[];
209 opkg_assert(option != NULL);
210 opkg_assert(value != NULL);
212 /* look up the option
213 * TODO: this would be much better as a hash table
215 while (options[i].name) {
216 if (strcmp(options[i].name, option) == 0) {
224 opkg_msg(ERROR, "Invalid option: %s\n", option);
229 switch (options[i].type) {
230 case OPKG_OPT_TYPE_BOOL:
231 if (*((int *) value) == 0)
232 *((int *) options[i].value) = 0;
234 *((int *) options[i].value) = 1;
237 case OPKG_OPT_TYPE_INT:
238 *((int *) options[i].value) = *((int *) value);
241 case OPKG_OPT_TYPE_STRING:
242 *((char **) options[i].value) = xstrdup(value);
249 * @brief libopkg API: Install package
250 * @param package_name The name of package in which is going to install
251 * @param progress_callback The callback function that report the status to caller.
254 opkg_install_package(const char *package_name,
255 opkg_progress_callback_t progress_callback,
259 char *stripped_filename;
260 opkg_progress_data_t pdata;
262 pkg_vec_t *deps, *all;
264 char **unresolved = NULL;
266 opkg_assert(package_name != NULL);
269 pkg_info_preinstall_check();
272 /* check to ensure package is not already installed */
273 old = pkg_hash_fetch_installed_by_name(package_name);
275 opkg_msg(ERROR, "Package %s is already installed\n",
280 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
282 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
286 new->state_flag |= SF_USER;
293 /* find dependancies and download them */
294 deps = pkg_vec_alloc();
295 /* this function does not return the original package, so we insert it later */
296 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
299 char **tmp = unresolved;
300 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
301 " for %s:\n", package_name);
303 opkg_msg(ERROR, "\t%s", *tmp);
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);
530 progress(pdata, 100);
535 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
541 pkg_src_list_elt_t *iter;
543 int sources_list_count, sources_done;
544 opkg_progress_data_t pdata;
546 pdata.action = OPKG_DOWNLOAD;
550 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
551 ? conf->default_dest->lists_dir : conf->lists_dir);
553 if (!file_is_dir(lists_dir)) {
554 if (file_exists(lists_dir)) {
555 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
560 err = file_mkdir_hier(lists_dir, 0755);
562 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
569 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
570 if (mkdtemp(tmp) == NULL) {
571 opkg_perror(ERROR, "Coundn't create temporary directory %s",
578 /* count the number of sources so we can give some progress updates */
579 sources_list_count = 0;
581 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
582 sources_list_count++;
585 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
586 char *url, *list_file_name = NULL;
588 src = (pkg_src_t *) iter->data;
590 if (src->extra_data) /* debian style? */
591 sprintf_alloc(&url, "%s/%s/%s", src->value,
593 src->gzip ? "Packages.gz" : "Packages");
595 sprintf_alloc(&url, "%s/%s", src->value,
596 src->gzip ? "Packages.gz" : "Packages");
598 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
601 struct _curl_cb_data cb_data;
602 char *tmp_file_name = NULL;
604 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
607 opkg_msg(INFO, "Downloading %s to %s...\n", url,
610 cb_data.cb = progress_callback;
611 cb_data.progress_data = &pdata;
612 cb_data.user_data = user_data;
613 cb_data.start_range =
614 100 * sources_done / sources_list_count;
615 cb_data.finish_range =
616 100 * (sources_done + 1) / sources_list_count;
618 err = opkg_download(url, tmp_file_name,
619 (curl_progress_func) curl_progress_cb,
623 opkg_msg(INFO, "Inflating %s...\n",
625 in = fopen(tmp_file_name, "r");
626 out = fopen(list_file_name, "w");
635 unlink(tmp_file_name);
639 err = opkg_download(url, list_file_name, NULL, NULL);
642 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
647 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
648 if (conf->check_signature) {
650 /* download detached signitures to verify the package lists */
651 /* get the url for the sig file */
652 if (src->extra_data) /* debian style? */
653 sprintf_alloc(&url, "%s/%s/%s", src->value,
654 src->extra_data, "Packages.sig");
656 sprintf_alloc(&url, "%s/%s", src->value,
659 /* create filename for signature */
660 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
663 /* make sure there is no existing signature file */
664 unlink(sig_file_name);
666 err = opkg_download(url, sig_file_name, NULL, NULL);
668 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
671 err = opkg_verify_file(list_file_name,
674 opkg_msg(INFO, "Signature check "
678 opkg_msg(ERROR, "Signature check "
684 free(list_file_name);
688 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
689 " has not been enabled in this build\n",
694 progress(pdata, 100 * sources_done / sources_list_count);
701 /* Now re-read the package lists to update package hash tables. */
702 opkg_re_read_config_files();
709 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
714 opkg_assert(callback);
716 all = pkg_vec_alloc();
717 pkg_hash_fetch_available(all);
718 for (i = 0; i < all->len; i++) {
723 callback(pkg, user_data);
732 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
734 struct active_list *head;
735 struct active_list *node;
736 pkg_t *old = NULL, *new = NULL;
738 opkg_assert(callback);
740 /* ensure all data is valid */
741 pkg_info_preinstall_check();
743 head = prepare_upgrade_list();
744 for (node = active_list_next(head, head); node;
745 active_list_next(head, node)) {
746 old = list_entry(node, pkg_t, list);
747 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
750 callback(new, user_data);
752 active_list_head_delete(head);
757 opkg_find_package(const char *name, const char *ver, const char *arch,
763 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
765 all = pkg_vec_alloc();
766 pkg_hash_fetch_available(all);
767 for (i = 0; i < all->len; i++) {
773 if (sstrcmp(pkg->name, name))
777 pkgv = pkg_version_str_alloc(pkg);
778 if (sstrcmp(pkgv, ver)) {
784 /* check architecture */
786 if (sstrcmp(pkg->architecture, arch))
790 /* check repository */
792 if (sstrcmp(pkg->src->name, repo))
806 #include <curl/curl.h>
809 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
810 * @return return how many repositories cannot access. 0 means all okay.
813 opkg_repository_accessibility_check(void)
815 pkg_src_list_elt_t *iter;
816 str_list_elt_t *iter1;
818 int repositories = 0;
825 src = str_list_alloc();
827 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
828 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
829 end = index(host, '/');
830 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
831 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
832 end - ((pkg_src_t *) iter->data)->value);
834 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
836 for (iter1 = str_list_first(src); iter1;
837 iter1 = str_list_next(src, iter1)) {
838 if (strstr(iter1->data, stmp))
844 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
847 str_list_append(src, repo_ptr);
851 while (repositories > 0) {
852 iter1 = str_list_pop(src);
855 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
857 if (!(err == CURLE_OK ||
858 err == CURLE_HTTP_RETURNED_ERROR ||
859 err == CURLE_FILE_COULDNT_READ_FILE ||
860 err == CURLE_REMOTE_FILE_NOT_FOUND ||
861 err == CURLE_TFTP_NOTFOUND)) {
867 str_list_elt_deinit(iter1);