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>
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);
122 args = xcalloc(1, sizeof(args_t));
125 err = opkg_conf_init(args);
146 opkg_re_read_config_files(void)
148 /* Unfortunately, the easiest way to re-read the config files right now is to
149 * throw away conf and start again */
151 memset(conf, '\0', sizeof(opkg_conf_t));
157 opkg_get_option(char *option, void **value)
160 extern opkg_option_t options[];
162 /* look up the option
163 * TODO: this would be much better as a hash table
165 while (options[i].name) {
166 if (strcmp(options[i].name, option) != 0) {
173 switch (options[i].type) {
174 case OPKG_OPT_TYPE_BOOL:
175 *((int *) value) = *((int *) options[i].value);
178 case OPKG_OPT_TYPE_INT:
179 *((int *) value) = *((int *) options[i].value);
182 case OPKG_OPT_TYPE_STRING:
183 *((char **) value) = xstrdup(options[i].value);
190 opkg_set_option(char *option, void *value)
192 int i = 0, found = 0;
193 extern opkg_option_t options[];
195 opkg_assert(option != NULL);
196 opkg_assert(value != NULL);
198 /* look up the option
199 * TODO: this would be much better as a hash table
201 while (options[i].name) {
202 if (strcmp(options[i].name, option) == 0) {
210 opkg_msg(ERROR, "Invalid option: %s\n", option);
215 switch (options[i].type) {
216 case OPKG_OPT_TYPE_BOOL:
217 if (*((int *) value) == 0)
218 *((int *) options[i].value) = 0;
220 *((int *) options[i].value) = 1;
223 case OPKG_OPT_TYPE_INT:
224 *((int *) options[i].value) = *((int *) value);
227 case OPKG_OPT_TYPE_STRING:
228 *((char **) options[i].value) = xstrdup(value);
235 * @brief libopkg API: Install package
236 * @param package_name The name of package in which is going to install
237 * @param progress_callback The callback function that report the status to caller.
240 opkg_install_package(const char *package_name,
241 opkg_progress_callback_t progress_callback,
245 char *stripped_filename;
246 opkg_progress_data_t pdata;
248 pkg_vec_t *deps, *all;
250 char **unresolved = NULL;
252 opkg_assert(package_name != NULL);
255 pkg_info_preinstall_check();
258 /* check to ensure package is not already installed */
259 old = pkg_hash_fetch_installed_by_name(package_name);
261 opkg_msg(ERROR, "Package %s is already installed\n",
266 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
268 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
272 new->state_flag |= SF_USER;
279 /* find dependancies and download them */
280 deps = pkg_vec_alloc();
281 /* this function does not return the original package, so we insert it later */
282 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
285 char **tmp = unresolved;
286 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
287 " for %s:\n", package_name);
289 opkg_msg(ERROR, "\t%s", *tmp);
298 /* insert the package we are installing so that we download it */
299 pkg_vec_insert(deps, new);
301 /* download package and dependencies */
302 for (i = 0; i < deps->len; i++) {
304 struct _curl_cb_data cb_data;
308 if (pkg->local_filename)
312 pdata.action = OPKG_DOWNLOAD;
314 if (pkg->src == NULL) {
315 opkg_msg(ERROR, "Package %s not available from any "
316 "configured src\n", package_name);
320 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
322 /* Get the filename part, without any directory */
323 stripped_filename = strrchr(pkg->filename, '/');
324 if (!stripped_filename)
325 stripped_filename = pkg->filename;
327 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
330 cb_data.cb = progress_callback;
331 cb_data.progress_data = &pdata;
332 cb_data.user_data = user_data;
333 /* 75% of "install" progress is for downloading */
334 cb_data.start_range = 75 * i / deps->len;
335 cb_data.finish_range = 75 * (i + 1) / deps->len;
337 err = opkg_download(url, pkg->local_filename,
338 (curl_progress_func) curl_progress_cb,
350 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
351 all = pkg_vec_alloc();
352 pkg_hash_fetch_available(all);
353 for (i = 0; i < all->len; i++) {
354 all->pkgs[i]->parent->dependencies_checked = 0;
359 /* 75% of "install" progress is for downloading */
361 pdata.action = OPKG_INSTALL;
364 /* unpack the package */
365 err = opkg_install_pkg(new, 0);
373 /* run configure scripts, etc. */
374 err = opkg_configure_packages(NULL);
379 /* write out status files and file lists */
380 opkg_conf_write_status_files();
381 pkg_write_changed_filelists();
383 progress(pdata, 100);
388 opkg_remove_package(const char *package_name,
389 opkg_progress_callback_t progress_callback, void *user_data)
393 pkg_t *pkg_to_remove;
394 opkg_progress_data_t pdata;
396 opkg_assert(package_name != NULL);
398 pkg_info_preinstall_check();
400 pkg = pkg_hash_fetch_installed_by_name(package_name);
402 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
403 opkg_msg(ERROR, "Package %s not installed\n", package_name);
407 pdata.action = OPKG_REMOVE;
411 if (conf->restrict_to_default_dest) {
412 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
415 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
421 err = opkg_remove_pkg(pkg_to_remove, 0);
423 /* write out status files and file lists */
424 opkg_conf_write_status_files();
425 pkg_write_changed_filelists();
428 progress(pdata, 100);
429 return (err) ? -1 : 0;
433 opkg_upgrade_package(const char *package_name,
434 opkg_progress_callback_t progress_callback,
439 opkg_progress_data_t pdata;
441 opkg_assert(package_name != NULL);
443 pkg_info_preinstall_check();
445 if (conf->restrict_to_default_dest) {
446 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
449 pkg = pkg_hash_fetch_installed_by_name(package_name);
453 opkg_msg(ERROR, "Package %s not installed\n", package_name);
457 pdata.action = OPKG_INSTALL;
461 err = opkg_upgrade_pkg(pkg);
467 err = opkg_configure_packages(NULL);
472 /* write out status files and file lists */
473 opkg_conf_write_status_files();
474 pkg_write_changed_filelists();
476 progress(pdata, 100);
481 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
483 pkg_vec_t *installed;
487 opkg_progress_data_t pdata;
489 pdata.action = OPKG_INSTALL;
494 installed = pkg_vec_alloc();
495 pkg_info_preinstall_check();
497 pkg_hash_fetch_all_installed(installed);
498 for (i = 0; i < installed->len; i++) {
499 pkg = installed->pkgs[i];
502 progress(pdata, 99 * i / installed->len);
504 err += opkg_upgrade_pkg(pkg);
506 pkg_vec_free(installed);
511 err = opkg_configure_packages(NULL);
516 progress(pdata, 100);
521 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
527 pkg_src_list_elt_t *iter;
529 int sources_list_count, sources_done;
530 opkg_progress_data_t pdata;
532 pdata.action = OPKG_DOWNLOAD;
536 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
537 ? conf->default_dest->lists_dir : conf->lists_dir);
539 if (!file_is_dir(lists_dir)) {
540 if (file_exists(lists_dir)) {
541 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
546 err = file_mkdir_hier(lists_dir, 0755);
548 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
555 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
556 if (mkdtemp(tmp) == NULL) {
557 opkg_perror(ERROR, "Coundn't create temporary directory %s",
564 /* count the number of sources so we can give some progress updates */
565 sources_list_count = 0;
567 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
568 sources_list_count++;
571 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
572 char *url, *list_file_name = NULL;
574 src = (pkg_src_t *) iter->data;
576 if (src->extra_data) /* debian style? */
577 sprintf_alloc(&url, "%s/%s/%s", src->value,
579 src->gzip ? "Packages.gz" : "Packages");
581 sprintf_alloc(&url, "%s/%s", src->value,
582 src->gzip ? "Packages.gz" : "Packages");
584 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
587 struct _curl_cb_data cb_data;
588 char *tmp_file_name = NULL;
590 sprintf_alloc(&tmp_file_name, "%s/%s.gz", tmp,
593 opkg_msg(INFO, "Downloading %s to %s...\n", url,
596 cb_data.cb = progress_callback;
597 cb_data.progress_data = &pdata;
598 cb_data.user_data = user_data;
599 cb_data.start_range =
600 100 * sources_done / sources_list_count;
601 cb_data.finish_range =
602 100 * (sources_done + 1) / sources_list_count;
604 err = opkg_download(url, tmp_file_name,
605 (curl_progress_func) curl_progress_cb,
609 opkg_msg(INFO, "Inflating %s...\n",
611 in = fopen(tmp_file_name, "r");
612 out = fopen(list_file_name, "w");
621 unlink(tmp_file_name);
625 err = opkg_download(url, list_file_name, NULL, NULL);
628 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
633 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
634 if (conf->check_signature) {
636 /* download detached signitures to verify the package lists */
637 /* get the url for the sig file */
638 if (src->extra_data) /* debian style? */
639 sprintf_alloc(&url, "%s/%s/%s", src->value,
640 src->extra_data, "Packages.sig");
642 sprintf_alloc(&url, "%s/%s", src->value,
645 /* create filename for signature */
646 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
649 /* make sure there is no existing signature file */
650 unlink(sig_file_name);
652 err = opkg_download(url, sig_file_name, NULL, NULL);
654 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
657 err = opkg_verify_file(list_file_name,
660 opkg_msg(INFO, "Signature check "
664 opkg_msg(ERROR, "Signature check "
670 free(list_file_name);
674 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
675 " has not been enabled in this build\n",
680 progress(pdata, 100 * sources_done / sources_list_count);
687 /* Now re-read the package lists to update package hash tables. */
688 opkg_re_read_config_files();
695 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
700 opkg_assert(callback);
702 all = pkg_vec_alloc();
703 pkg_hash_fetch_available(all);
704 for (i = 0; i < all->len; i++) {
709 callback(pkg, user_data);
718 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
720 struct active_list *head;
721 struct active_list *node;
722 pkg_t *old = NULL, *new = NULL;
724 opkg_assert(callback);
726 /* ensure all data is valid */
727 pkg_info_preinstall_check();
729 head = prepare_upgrade_list();
730 for (node = active_list_next(head, head); node;
731 active_list_next(head, node)) {
732 old = list_entry(node, pkg_t, list);
733 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
736 callback(new, user_data);
738 active_list_head_delete(head);
743 opkg_find_package(const char *name, const char *ver, const char *arch,
749 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
751 all = pkg_vec_alloc();
752 pkg_hash_fetch_available(all);
753 for (i = 0; i < all->len; i++) {
759 if (sstrcmp(pkg->name, name))
763 pkgv = pkg_version_str_alloc(pkg);
764 if (sstrcmp(pkgv, ver)) {
770 /* check architecture */
772 if (sstrcmp(pkg->architecture, arch))
776 /* check repository */
778 if (sstrcmp(pkg->src->name, repo))
792 #include <curl/curl.h>
795 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
796 * @return return how many repositories cannot access. 0 means all okay.
799 opkg_repository_accessibility_check(void)
801 pkg_src_list_elt_t *iter;
802 str_list_elt_t *iter1;
804 int repositories = 0;
811 src = str_list_alloc();
813 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
814 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
815 end = index(host, '/');
816 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
817 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
818 end - ((pkg_src_t *) iter->data)->value);
820 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
822 for (iter1 = str_list_first(src); iter1;
823 iter1 = str_list_next(src, iter1)) {
824 if (strstr(iter1->data, stmp))
830 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
833 str_list_append(src, repo_ptr);
837 while (repositories > 0) {
838 iter1 = str_list_pop(src);
841 err = opkg_download(iter1->data, "/dev/null", NULL, NULL);
843 if (!(err == CURLE_OK ||
844 err == CURLE_HTTP_RETURNED_ERROR ||
845 err == CURLE_FILE_COULDNT_READ_FILE ||
846 err == CURLE_REMOTE_FILE_NOT_FOUND ||
847 err == CURLE_TFTP_NOTFOUND)) {
853 str_list_elt_deinit(iter1);