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())
143 int opkg_re_read_config_files(void)
150 void opkg_get_option(char *option, void **value)
153 extern opkg_option_t options[];
155 /* look up the option
156 * TODO: this would be much better as a hash table
158 while (options[i].name) {
159 if (strcmp(options[i].name, option) != 0) {
166 switch (options[i].type) {
167 case OPKG_OPT_TYPE_BOOL:
168 *((int *)value) = *((int *)options[i].value);
171 case OPKG_OPT_TYPE_INT:
172 *((int *)value) = *((int *)options[i].value);
175 case OPKG_OPT_TYPE_STRING:
176 *((char **)value) = xstrdup(options[i].value);
182 void opkg_set_option(char *option, void *value)
184 int i = 0, found = 0;
185 extern opkg_option_t options[];
187 opkg_assert(option != NULL);
188 opkg_assert(value != NULL);
190 /* look up the option
191 * TODO: this would be much better as a hash table
193 while (options[i].name) {
194 if (strcmp(options[i].name, option) == 0) {
202 opkg_msg(ERROR, "Invalid option: %s\n", option);
207 switch (options[i].type) {
208 case OPKG_OPT_TYPE_BOOL:
209 if (*((int *)value) == 0)
210 *((int *)options[i].value) = 0;
212 *((int *)options[i].value) = 1;
215 case OPKG_OPT_TYPE_INT:
216 *((int *)options[i].value) = *((int *)value);
219 case OPKG_OPT_TYPE_STRING:
220 *((char **)options[i].value) = xstrdup(value);
227 * @brief libopkg API: Install package
228 * @param package_name The name of package in which is going to install
229 * @param progress_callback The callback function that report the status to caller.
232 opkg_install_package(const char *package_name,
233 opkg_progress_callback_t progress_callback,
237 char *stripped_filename, *local_filename;
238 opkg_progress_data_t pdata;
240 pkg_vec_t *deps, *all;
242 char **unresolved = NULL;
243 const char *filename;
245 opkg_assert(package_name != NULL);
248 pkg_info_preinstall_check();
250 /* check to ensure package is not already installed */
251 old = pkg_hash_fetch_installed_by_name(package_name);
253 opkg_msg(ERROR, "Package %s is already installed\n",
258 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
260 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
264 new->state_flag |= SF_USER;
271 /* find dependancies and download them */
272 deps = pkg_vec_alloc();
273 /* this function does not return the original package, so we insert it later */
274 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
277 char **tmp = unresolved;
278 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
279 " for %s:\n", package_name);
281 opkg_msg(ERROR, "\t%s", *tmp);
287 opkg_message(ERROR, "\n");
291 /* insert the package we are installing so that we download it */
292 pkg_vec_insert(deps, new);
294 /* download package and dependencies */
295 for (i = 0; i < deps->len; i++) {
297 struct _curl_cb_data cb_data;
301 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
305 pdata.action = OPKG_DOWNLOAD;
307 if (pkg->src == NULL) {
308 opkg_msg(ERROR, "Package %s not available from any "
309 "configured src\n", package_name);
313 filename = pkg_get_string(pkg, PKG_FILENAME);
315 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
317 /* Get the filename part, without any directory */
318 stripped_filename = strrchr(filename, '/');
319 if (!stripped_filename)
320 stripped_filename = (char *)filename;
322 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
325 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
327 cb_data.cb = progress_callback;
328 cb_data.progress_data = &pdata;
329 cb_data.user_data = user_data;
330 /* 75% of "install" progress is for downloading */
331 cb_data.start_range = 75 * i / deps->len;
332 cb_data.finish_range = 75 * (i + 1) / deps->len;
334 err = opkg_download(url, local_filename,
335 (curl_progress_func) curl_progress_cb,
347 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
348 all = pkg_vec_alloc();
349 pkg_hash_fetch_available(all);
350 for (i = 0; i < all->len; i++) {
351 all->pkgs[i]->parent->dependencies_checked = 0;
355 /* 75% of "install" progress is for downloading */
357 pdata.action = OPKG_INSTALL;
360 /* unpack the package */
361 err = opkg_install_pkg(new, 0);
369 /* run configure scripts, etc. */
370 err = opkg_configure_packages(NULL);
375 /* write out status files and file lists */
376 opkg_conf_write_status_files();
377 pkg_write_changed_filelists();
379 progress(pdata, 100);
384 opkg_remove_package(const char *package_name,
385 opkg_progress_callback_t progress_callback, void *user_data)
389 pkg_t *pkg_to_remove;
390 opkg_progress_data_t pdata;
392 opkg_assert(package_name != NULL);
394 pkg_info_preinstall_check();
396 pkg = pkg_hash_fetch_installed_by_name(package_name);
398 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
399 opkg_msg(ERROR, "Package %s not installed\n", package_name);
403 pdata.action = OPKG_REMOVE;
407 if (conf->restrict_to_default_dest) {
408 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
412 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
417 err = opkg_remove_pkg(pkg_to_remove, 0);
419 /* write out status files and file lists */
420 opkg_conf_write_status_files();
421 pkg_write_changed_filelists();
423 progress(pdata, 100);
424 return (err) ? -1 : 0;
428 opkg_upgrade_package(const char *package_name,
429 opkg_progress_callback_t progress_callback,
434 opkg_progress_data_t pdata;
436 opkg_assert(package_name != NULL);
438 pkg_info_preinstall_check();
440 if (conf->restrict_to_default_dest) {
441 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
444 pkg = pkg_hash_fetch_installed_by_name(package_name);
448 opkg_msg(ERROR, "Package %s not installed\n", package_name);
452 pdata.action = OPKG_INSTALL;
456 err = opkg_upgrade_pkg(pkg);
462 err = opkg_configure_packages(NULL);
467 /* write out status files and file lists */
468 opkg_conf_write_status_files();
469 pkg_write_changed_filelists();
471 progress(pdata, 100);
476 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
478 pkg_vec_t *installed;
482 opkg_progress_data_t pdata;
484 pdata.action = OPKG_INSTALL;
489 installed = pkg_vec_alloc();
490 pkg_info_preinstall_check();
492 pkg_hash_fetch_all_installed(installed);
493 for (i = 0; i < installed->len; i++) {
494 pkg = installed->pkgs[i];
497 progress(pdata, 99 * i / installed->len);
499 err += opkg_upgrade_pkg(pkg);
501 pkg_vec_free(installed);
506 err = opkg_configure_packages(NULL);
510 /* write out status files and file lists */
511 opkg_conf_write_status_files();
512 pkg_write_changed_filelists();
515 progress(pdata, 100);
520 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
526 pkg_src_list_elt_t *iter;
528 int sources_list_count, sources_done;
529 opkg_progress_data_t pdata;
531 pdata.action = OPKG_DOWNLOAD;
535 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
536 ? conf->default_dest->lists_dir : conf->lists_dir);
538 if (!file_is_dir(lists_dir)) {
539 if (file_exists(lists_dir)) {
540 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
545 err = file_mkdir_hier(lists_dir, 0755);
547 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
554 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
555 if (mkdtemp(tmp) == NULL) {
556 opkg_perror(ERROR, "Coundn't create temporary directory %s",
563 /* count the number of sources so we can give some progress updates */
564 sources_list_count = 0;
566 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
567 sources_list_count++;
570 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
571 char *url, *list_file_name = NULL;
573 src = (pkg_src_t *) iter->data;
575 if (src->extra_data) /* debian style? */
576 sprintf_alloc(&url, "%s/%s/%s", src->value,
578 src->gzip ? "Packages.gz" : "Packages");
580 sprintf_alloc(&url, "%s/%s", src->value,
581 src->gzip ? "Packages.gz" : "Packages");
583 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
585 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
586 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
591 #if defined(HAVE_USIGN)
592 if (conf->check_signature) {
594 /* download detached signitures to verify the package lists */
595 /* get the url for the sig file */
596 if (src->extra_data) /* debian style? */
597 sprintf_alloc(&url, "%s/%s/%s", src->value,
598 src->extra_data, "Packages.sig");
600 sprintf_alloc(&url, "%s/%s", src->value,
603 /* create filename for signature */
604 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
607 /* make sure there is no existing signature file */
608 unlink(sig_file_name);
610 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
612 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
615 err = opkg_verify_file(list_file_name,
618 opkg_msg(INFO, "Signature check "
622 opkg_msg(ERROR, "Signature check "
631 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
632 " has not been enabled in this build\n",
635 free(list_file_name);
638 progress(pdata, 100 * sources_done / sources_list_count);
645 /* Now re-read the package lists to update package hash tables. */
646 opkg_re_read_config_files();
651 static int pkg_compare_names_and_version(const void *a0, const void *b0)
653 const pkg_t *a = *(const pkg_t **)a0;
654 const pkg_t *b = *(const pkg_t **)b0;
657 ret = strcmp(a->name, b->name);
660 ret = pkg_compare_versions(a, b);
665 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
670 opkg_assert(callback);
672 all = pkg_vec_alloc();
673 pkg_hash_fetch_available(all);
675 pkg_vec_sort(all, pkg_compare_names_and_version);
677 for (i = 0; i < all->len; i++) {
682 callback(pkg, user_data);
691 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
693 struct active_list *head;
694 struct active_list *node;
695 pkg_t *old = NULL, *new = NULL;
697 opkg_assert(callback);
699 /* ensure all data is valid */
700 pkg_info_preinstall_check();
702 head = prepare_upgrade_list();
703 for (node = active_list_next(head, head); node;
704 node = active_list_next(head, node)) {
707 pkg_hash_fetch_best_installation_candidate_by_name(old->
711 callback(new, user_data);
713 active_list_head_delete(head);
717 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
724 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
726 all = pkg_vec_alloc();
727 pkg_hash_fetch_available(all);
728 for (i = 0; i < all->len; i++) {
734 if (sstrcmp(pkg->name, name))
738 pkgv = pkg_version_str_alloc(pkg);
739 if (sstrcmp(pkgv, ver)) {
745 /* check architecture */
747 if (sstrcmp(pkg_get_architecture(pkg), arch))
751 /* check repository */
753 if (sstrcmp(pkg->src->name, repo))
764 return pkg_found ? pkg : NULL;
768 * @brief Check the accessibility of repositories.
769 * @return return how many repositories cannot access. 0 means all okay.
771 int opkg_repository_accessibility_check(void)
773 pkg_src_list_elt_t *iter;
774 str_list_elt_t *iter1;
776 int repositories = 0;
782 src = str_list_alloc();
784 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
785 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
786 end = index(host, '/');
787 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
788 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
790 ((pkg_src_t *) iter->data)->value);
792 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
794 for (iter1 = str_list_first(src); iter1;
795 iter1 = str_list_next(src, iter1)) {
796 if (strstr(iter1->data, stmp))
802 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
805 str_list_append(src, repo_ptr);
810 while (repositories > 0) {
811 iter1 = str_list_pop(src);
814 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
816 str_list_elt_deinit(iter1);