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.
23 #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 ***/
44 static int 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;
85 int curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
87 double ultotal, double ulnow)
89 int p = (t) ? d * 100 / t : 0;
93 /* prevent the same value being sent twice (can occur due to rounding) */
101 progress = cb_data->start_range +
102 (d / t * ((cb_data->finish_range - cb_data->start_range)));
103 cb_data->progress_data->percentage = progress;
105 (cb_data->cb) (cb_data->progress_data, cb_data->user_data);
110 static struct opkg_conf saved_conf;
117 if (opkg_conf_init())
120 if (opkg_conf_load())
123 if (pkg_hash_load_feeds(0))
126 if (pkg_hash_load_status_files())
146 int opkg_re_read_config_files(void)
153 void opkg_get_option(char *option, void **value)
156 extern opkg_option_t options[];
158 /* look up the option
159 * TODO: this would be much better as a hash table
161 while (options[i].name) {
162 if (strcmp(options[i].name, option) != 0) {
169 switch (options[i].type) {
170 case OPKG_OPT_TYPE_BOOL:
171 *((int *)value) = *((int *)options[i].value);
174 case OPKG_OPT_TYPE_INT:
175 *((int *)value) = *((int *)options[i].value);
178 case OPKG_OPT_TYPE_STRING:
179 *((char **)value) = xstrdup(options[i].value);
185 void opkg_set_option(char *option, void *value)
187 int i = 0, found = 0;
188 extern opkg_option_t options[];
190 opkg_assert(option != NULL);
191 opkg_assert(value != NULL);
193 /* look up the option
194 * TODO: this would be much better as a hash table
196 while (options[i].name) {
197 if (strcmp(options[i].name, option) == 0) {
205 opkg_msg(ERROR, "Invalid option: %s\n", option);
210 switch (options[i].type) {
211 case OPKG_OPT_TYPE_BOOL:
212 if (*((int *)value) == 0)
213 *((int *)options[i].value) = 0;
215 *((int *)options[i].value) = 1;
218 case OPKG_OPT_TYPE_INT:
219 *((int *)options[i].value) = *((int *)value);
222 case OPKG_OPT_TYPE_STRING:
223 *((char **)options[i].value) = xstrdup(value);
230 * @brief libopkg API: Install package
231 * @param package_name The name of package in which is going to install
232 * @param progress_callback The callback function that report the status to caller.
235 opkg_install_package(const char *package_name,
236 opkg_progress_callback_t progress_callback,
240 char *stripped_filename, *local_filename;
241 opkg_progress_data_t pdata;
243 pkg_vec_t *deps, *all;
245 char **unresolved = NULL;
246 const char *filename;
248 opkg_assert(package_name != NULL);
251 pkg_info_preinstall_check();
253 /* check to ensure package is not already installed */
254 old = pkg_hash_fetch_installed_by_name(package_name);
256 opkg_msg(ERROR, "Package %s is already installed\n",
261 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
263 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
267 new->state_flag |= SF_USER;
274 /* find dependancies and download them */
275 deps = pkg_vec_alloc();
276 /* this function does not return the original package, so we insert it later */
277 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
280 char **tmp = unresolved;
281 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
282 " for %s:\n", package_name);
284 opkg_msg(ERROR, "\t%s", *tmp);
290 opkg_message(ERROR, "\n");
294 /* insert the package we are installing so that we download it */
295 pkg_vec_insert(deps, new);
297 /* download package and dependencies */
298 for (i = 0; i < deps->len; i++) {
300 struct _curl_cb_data cb_data;
304 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
308 pdata.action = OPKG_DOWNLOAD;
310 if (pkg->src == NULL) {
311 opkg_msg(ERROR, "Package %s not available from any "
312 "configured src\n", package_name);
316 filename = pkg_get_string(pkg, PKG_FILENAME);
318 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
320 /* Get the filename part, without any directory */
321 stripped_filename = strrchr(filename, '/');
322 if (!stripped_filename)
323 stripped_filename = (char *)filename;
325 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
328 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
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, 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;
358 /* 75% of "install" progress is for downloading */
360 pdata.action = OPKG_INSTALL;
363 /* unpack the package */
364 err = opkg_install_pkg(new, 0);
372 /* run configure scripts, etc. */
373 err = opkg_configure_packages(NULL);
378 /* write out status files and file lists */
379 opkg_conf_write_status_files();
380 pkg_write_changed_filelists();
382 progress(pdata, 100);
387 opkg_remove_package(const char *package_name,
388 opkg_progress_callback_t progress_callback, void *user_data)
392 pkg_t *pkg_to_remove;
393 opkg_progress_data_t pdata;
395 opkg_assert(package_name != NULL);
397 pkg_info_preinstall_check();
399 pkg = pkg_hash_fetch_installed_by_name(package_name);
401 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
402 opkg_msg(ERROR, "Package %s not installed\n", package_name);
406 pdata.action = OPKG_REMOVE;
410 if (conf->restrict_to_default_dest) {
411 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
415 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
420 err = opkg_remove_pkg(pkg_to_remove, 0);
422 /* write out status files and file lists */
423 opkg_conf_write_status_files();
424 pkg_write_changed_filelists();
426 progress(pdata, 100);
427 return (err) ? -1 : 0;
431 opkg_upgrade_package(const char *package_name,
432 opkg_progress_callback_t progress_callback,
437 opkg_progress_data_t pdata;
439 opkg_assert(package_name != NULL);
441 pkg_info_preinstall_check();
443 if (conf->restrict_to_default_dest) {
444 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
447 pkg = pkg_hash_fetch_installed_by_name(package_name);
451 opkg_msg(ERROR, "Package %s not installed\n", package_name);
455 pdata.action = OPKG_INSTALL;
459 err = opkg_upgrade_pkg(pkg);
465 err = opkg_configure_packages(NULL);
470 /* write out status files and file lists */
471 opkg_conf_write_status_files();
472 pkg_write_changed_filelists();
474 progress(pdata, 100);
479 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
481 pkg_vec_t *installed;
485 opkg_progress_data_t pdata;
487 pdata.action = OPKG_INSTALL;
492 installed = pkg_vec_alloc();
493 pkg_info_preinstall_check();
495 pkg_hash_fetch_all_installed(installed);
496 for (i = 0; i < installed->len; i++) {
497 pkg = installed->pkgs[i];
500 progress(pdata, 99 * i / installed->len);
502 err += opkg_upgrade_pkg(pkg);
504 pkg_vec_free(installed);
509 err = opkg_configure_packages(NULL);
513 /* write out status files and file lists */
514 opkg_conf_write_status_files();
515 pkg_write_changed_filelists();
518 progress(pdata, 100);
523 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
529 pkg_src_list_elt_t *iter;
531 int sources_list_count, sources_done;
532 opkg_progress_data_t pdata;
534 pdata.action = OPKG_DOWNLOAD;
538 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
539 ? conf->default_dest->lists_dir : conf->lists_dir);
541 if (!file_is_dir(lists_dir)) {
542 if (file_exists(lists_dir)) {
543 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
548 err = file_mkdir_hier(lists_dir, 0755);
550 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
557 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
558 if (mkdtemp(tmp) == NULL) {
559 opkg_perror(ERROR, "Coundn't create temporary directory %s",
566 /* count the number of sources so we can give some progress updates */
567 sources_list_count = 0;
569 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
570 sources_list_count++;
573 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
574 char *url, *list_file_name = NULL;
576 src = (pkg_src_t *) iter->data;
578 if (src->extra_data) /* debian style? */
579 sprintf_alloc(&url, "%s/%s/%s", src->value,
581 src->gzip ? "Packages.gz" : "Packages");
583 sprintf_alloc(&url, "%s/%s", src->value,
584 src->gzip ? "Packages.gz" : "Packages");
586 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
588 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
589 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
594 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
595 if (conf->check_signature) {
597 /* download detached signitures to verify the package lists */
598 /* get the url for the sig file */
599 if (src->extra_data) /* debian style? */
600 sprintf_alloc(&url, "%s/%s/%s", src->value,
601 src->extra_data, "Packages.sig");
603 sprintf_alloc(&url, "%s/%s", src->value,
606 /* create filename for signature */
607 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
610 /* make sure there is no existing signature file */
611 unlink(sig_file_name);
613 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
615 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
618 err = opkg_verify_file(list_file_name,
621 opkg_msg(INFO, "Signature check "
625 opkg_msg(ERROR, "Signature check "
634 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
635 " has not been enabled in this build\n",
638 free(list_file_name);
641 progress(pdata, 100 * sources_done / sources_list_count);
648 /* Now re-read the package lists to update package hash tables. */
649 opkg_re_read_config_files();
654 static int pkg_compare_names_and_version(const void *a0, const void *b0)
656 const pkg_t *a = *(const pkg_t **)a0;
657 const pkg_t *b = *(const pkg_t **)b0;
660 ret = strcmp(a->name, b->name);
663 ret = pkg_compare_versions(a, b);
668 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
673 opkg_assert(callback);
675 all = pkg_vec_alloc();
676 pkg_hash_fetch_available(all);
678 pkg_vec_sort(all, pkg_compare_names_and_version);
680 for (i = 0; i < all->len; i++) {
685 callback(pkg, user_data);
694 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
696 struct active_list *head;
697 struct active_list *node;
698 pkg_t *old = NULL, *new = NULL;
700 opkg_assert(callback);
702 /* ensure all data is valid */
703 pkg_info_preinstall_check();
705 head = prepare_upgrade_list();
706 for (node = active_list_next(head, head); node;
707 node = active_list_next(head, node)) {
710 pkg_hash_fetch_best_installation_candidate_by_name(old->
714 callback(new, user_data);
716 active_list_head_delete(head);
720 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
727 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
729 all = pkg_vec_alloc();
730 pkg_hash_fetch_available(all);
731 for (i = 0; i < all->len; i++) {
737 if (sstrcmp(pkg->name, name))
741 pkgv = pkg_version_str_alloc(pkg);
742 if (sstrcmp(pkgv, ver)) {
748 /* check architecture */
750 if (sstrcmp(pkg_get_architecture(pkg), arch))
754 /* check repository */
756 if (sstrcmp(pkg->src->name, repo))
767 return pkg_found ? pkg : NULL;
771 * @brief Check the accessibility of repositories.
772 * @return return how many repositories cannot access. 0 means all okay.
774 int opkg_repository_accessibility_check(void)
776 pkg_src_list_elt_t *iter;
777 str_list_elt_t *iter1;
779 int repositories = 0;
785 src = str_list_alloc();
787 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
788 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
789 end = index(host, '/');
790 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
791 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
793 ((pkg_src_t *) iter->data)->value);
795 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
797 for (iter1 = str_list_first(src); iter1;
798 iter1 = str_list_next(src, iter1)) {
799 if (strstr(iter1->data, stmp))
805 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
808 str_list_append(src, repo_ptr);
813 while (repositories > 0) {
814 iter1 = str_list_pop(src);
817 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
819 str_list_elt_deinit(iter1);