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);
302 opkg_message(ERROR, "\n");
306 /* insert the package we are installing so that we download it */
307 pkg_vec_insert(deps, new);
309 /* download package and dependencies */
310 for (i = 0; i < deps->len; i++) {
312 struct _curl_cb_data cb_data;
316 if (pkg->local_filename)
320 pdata.action = OPKG_DOWNLOAD;
322 if (pkg->src == NULL) {
323 opkg_msg(ERROR, "Package %s not available from any "
324 "configured src\n", package_name);
328 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
330 /* Get the filename part, without any directory */
331 stripped_filename = strrchr(pkg->filename, '/');
332 if (!stripped_filename)
333 stripped_filename = pkg->filename;
335 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
338 cb_data.cb = progress_callback;
339 cb_data.progress_data = &pdata;
340 cb_data.user_data = user_data;
341 /* 75% of "install" progress is for downloading */
342 cb_data.start_range = 75 * i / deps->len;
343 cb_data.finish_range = 75 * (i + 1) / deps->len;
345 err = opkg_download(url, pkg->local_filename,
346 (curl_progress_func) curl_progress_cb,
358 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
359 all = pkg_vec_alloc();
360 pkg_hash_fetch_available(all);
361 for (i = 0; i < all->len; i++) {
362 all->pkgs[i]->parent->dependencies_checked = 0;
367 /* 75% of "install" progress is for downloading */
369 pdata.action = OPKG_INSTALL;
372 /* unpack the package */
373 err = opkg_install_pkg(new, 0);
381 /* run configure scripts, etc. */
382 err = opkg_configure_packages(NULL);
387 /* write out status files and file lists */
388 opkg_conf_write_status_files();
389 pkg_write_changed_filelists();
391 progress(pdata, 100);
396 opkg_remove_package(const char *package_name,
397 opkg_progress_callback_t progress_callback, void *user_data)
401 pkg_t *pkg_to_remove;
402 opkg_progress_data_t pdata;
404 opkg_assert(package_name != NULL);
406 pkg_info_preinstall_check();
408 pkg = pkg_hash_fetch_installed_by_name(package_name);
410 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
411 opkg_msg(ERROR, "Package %s not installed\n", package_name);
415 pdata.action = OPKG_REMOVE;
419 if (conf->restrict_to_default_dest) {
420 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
423 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
429 err = opkg_remove_pkg(pkg_to_remove, 0);
431 /* write out status files and file lists */
432 opkg_conf_write_status_files();
433 pkg_write_changed_filelists();
436 progress(pdata, 100);
437 return (err) ? -1 : 0;
441 opkg_upgrade_package(const char *package_name,
442 opkg_progress_callback_t progress_callback,
447 opkg_progress_data_t pdata;
449 opkg_assert(package_name != NULL);
451 pkg_info_preinstall_check();
453 if (conf->restrict_to_default_dest) {
454 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
457 pkg = pkg_hash_fetch_installed_by_name(package_name);
461 opkg_msg(ERROR, "Package %s not installed\n", package_name);
465 pdata.action = OPKG_INSTALL;
469 err = opkg_upgrade_pkg(pkg);
475 err = opkg_configure_packages(NULL);
480 /* write out status files and file lists */
481 opkg_conf_write_status_files();
482 pkg_write_changed_filelists();
484 progress(pdata, 100);
489 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
491 pkg_vec_t *installed;
495 opkg_progress_data_t pdata;
497 pdata.action = OPKG_INSTALL;
502 installed = pkg_vec_alloc();
503 pkg_info_preinstall_check();
505 pkg_hash_fetch_all_installed(installed);
506 for (i = 0; i < installed->len; i++) {
507 pkg = installed->pkgs[i];
510 progress(pdata, 99 * i / installed->len);
512 err += opkg_upgrade_pkg(pkg);
514 pkg_vec_free(installed);
519 err = opkg_configure_packages(NULL);
524 progress(pdata, 100);
529 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
535 pkg_src_list_elt_t *iter;
537 int sources_list_count, sources_done;
538 opkg_progress_data_t pdata;
540 pdata.action = OPKG_DOWNLOAD;
544 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
545 ? conf->default_dest->lists_dir : conf->lists_dir);
547 if (!file_is_dir(lists_dir)) {
548 if (file_exists(lists_dir)) {
549 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
554 err = file_mkdir_hier(lists_dir, 0755);
556 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
563 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
564 if (mkdtemp(tmp) == NULL) {
565 opkg_perror(ERROR, "Coundn't create temporary directory %s",
572 /* count the number of sources so we can give some progress updates */
573 sources_list_count = 0;
575 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
576 sources_list_count++;
579 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
580 char *url, *list_file_name = NULL;
582 src = (pkg_src_t *) iter->data;
584 if (src->extra_data) /* debian style? */
585 sprintf_alloc(&url, "%s/%s/%s", src->value,
587 src->gzip ? "Packages.gz" : "Packages");
589 sprintf_alloc(&url, "%s/%s", src->value,
590 src->gzip ? "Packages.gz" : "Packages");
592 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
595 struct _curl_cb_data cb_data;
596 char *tmp_file_name = NULL;
598 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
601 opkg_msg(INFO, "Downloading %s to %s...\n", url,
604 cb_data.cb = progress_callback;
605 cb_data.progress_data = &pdata;
606 cb_data.user_data = user_data;
607 cb_data.start_range =
608 100 * sources_done / sources_list_count;
609 cb_data.finish_range =
610 100 * (sources_done + 1) / sources_list_count;
612 err = opkg_download(url, tmp_file_name,
613 (curl_progress_func) curl_progress_cb,
617 opkg_msg(INFO, "Inflating %s...\n",
619 in = fopen(tmp_file_name, "r");
620 out = fopen(list_file_name, "w");
629 unlink(tmp_file_name);
633 err = opkg_download(url, list_file_name, NULL, NULL);
636 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
641 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
642 if (conf->check_signature) {
644 /* download detached signitures to verify the package lists */
645 /* get the url for the sig file */
646 if (src->extra_data) /* debian style? */
647 sprintf_alloc(&url, "%s/%s/%s", src->value,
648 src->extra_data, "Packages.sig");
650 sprintf_alloc(&url, "%s/%s", src->value,
653 /* create filename for signature */
654 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
657 /* make sure there is no existing signature file */
658 unlink(sig_file_name);
660 err = opkg_download(url, sig_file_name, NULL, NULL);
662 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
665 err = opkg_verify_file(list_file_name,
668 opkg_msg(INFO, "Signature check "
672 opkg_msg(ERROR, "Signature check "
678 free(list_file_name);
682 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
683 " has not been enabled in this build\n",
688 progress(pdata, 100 * sources_done / sources_list_count);
695 /* Now re-read the package lists to update package hash tables. */
696 opkg_re_read_config_files();
702 pkg_compare_names_and_version(const void *a0, const void *b0)
704 const pkg_t *a = *(const pkg_t **)a0;
705 const pkg_t *b = *(const pkg_t **)b0;
708 ret = strcmp(a->name, b->name);
711 ret = pkg_compare_versions(a, b);
717 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
722 opkg_assert(callback);
724 all = pkg_vec_alloc();
725 pkg_hash_fetch_available(all);
727 pkg_vec_sort(all, pkg_compare_names_and_version);
729 for (i = 0; i < all->len; i++) {
734 callback(pkg, user_data);
743 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
745 struct active_list *head;
746 struct active_list *node;
747 pkg_t *old = NULL, *new = NULL;
749 opkg_assert(callback);
751 /* ensure all data is valid */
752 pkg_info_preinstall_check();
754 head = prepare_upgrade_list();
755 for (node = active_list_next(head, head); node;
756 active_list_next(head, node)) {
757 old = list_entry(node, pkg_t, list);
758 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
761 callback(new, user_data);
763 active_list_head_delete(head);
768 opkg_find_package(const char *name, const char *ver, const char *arch,
774 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
776 all = pkg_vec_alloc();
777 pkg_hash_fetch_available(all);
778 for (i = 0; i < all->len; i++) {
784 if (sstrcmp(pkg->name, name))
788 pkgv = pkg_version_str_alloc(pkg);
789 if (sstrcmp(pkgv, ver)) {
795 /* check architecture */
797 if (sstrcmp(pkg->architecture, arch))
801 /* check repository */
803 if (sstrcmp(pkg->src->name, repo))
817 #include <curl/curl.h>
820 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
821 * @return return how many repositories cannot access. 0 means all okay.
824 opkg_repository_accessibility_check(void)
826 pkg_src_list_elt_t *iter;
827 str_list_elt_t *iter1;
829 int repositories = 0;
836 src = str_list_alloc();
838 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
839 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
840 end = index(host, '/');
841 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
842 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
843 end - ((pkg_src_t *) iter->data)->value);
845 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
847 for (iter1 = str_list_first(src); iter1;
848 iter1 = str_list_next(src, iter1)) {
849 if (strstr(iter1->data, stmp))
855 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
858 str_list_append(src, repo_ptr);
862 while (repositories > 0) {
863 iter1 = str_list_pop(src);
866 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
868 if (!(err == CURLE_OK ||
869 err == CURLE_HTTP_RETURNED_ERROR ||
870 err == CURLE_FILE_COULDNT_READ_FILE ||
871 err == CURLE_REMOTE_FILE_NOT_FOUND ||
872 err == CURLE_TFTP_NOTFOUND)) {
878 str_list_elt_deinit(iter1);