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.
25 #include "opkg_conf.h"
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31 #include "opkg_upgrade.h"
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
36 #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);
115 static struct opkg_conf saved_conf;
123 if (opkg_conf_init())
126 if (opkg_conf_load())
129 if (pkg_hash_load_feeds())
132 if (pkg_hash_load_status_files())
154 opkg_re_read_config_files(void)
162 opkg_get_option(char *option, void **value)
165 extern opkg_option_t options[];
167 /* look up the option
168 * TODO: this would be much better as a hash table
170 while (options[i].name) {
171 if (strcmp(options[i].name, option) != 0) {
178 switch (options[i].type) {
179 case OPKG_OPT_TYPE_BOOL:
180 *((int *) value) = *((int *) options[i].value);
183 case OPKG_OPT_TYPE_INT:
184 *((int *) value) = *((int *) options[i].value);
187 case OPKG_OPT_TYPE_STRING:
188 *((char **) value) = xstrdup(options[i].value);
195 opkg_set_option(char *option, void *value)
197 int i = 0, found = 0;
198 extern opkg_option_t options[];
200 opkg_assert(option != NULL);
201 opkg_assert(value != NULL);
203 /* look up the option
204 * TODO: this would be much better as a hash table
206 while (options[i].name) {
207 if (strcmp(options[i].name, option) == 0) {
215 opkg_msg(ERROR, "Invalid option: %s\n", option);
220 switch (options[i].type) {
221 case OPKG_OPT_TYPE_BOOL:
222 if (*((int *) value) == 0)
223 *((int *) options[i].value) = 0;
225 *((int *) options[i].value) = 1;
228 case OPKG_OPT_TYPE_INT:
229 *((int *) options[i].value) = *((int *) value);
232 case OPKG_OPT_TYPE_STRING:
233 *((char **) options[i].value) = xstrdup(value);
240 * @brief libopkg API: Install package
241 * @param package_name The name of package in which is going to install
242 * @param progress_callback The callback function that report the status to caller.
245 opkg_install_package(const char *package_name,
246 opkg_progress_callback_t progress_callback,
250 char *stripped_filename;
251 opkg_progress_data_t pdata;
253 pkg_vec_t *deps, *all;
255 char **unresolved = NULL;
257 opkg_assert(package_name != NULL);
260 pkg_info_preinstall_check();
263 /* check to ensure package is not already installed */
264 old = pkg_hash_fetch_installed_by_name(package_name);
266 opkg_msg(ERROR, "Package %s is already installed\n",
271 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
273 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
277 new->state_flag |= SF_USER;
284 /* find dependancies and download them */
285 deps = pkg_vec_alloc();
286 /* this function does not return the original package, so we insert it later */
287 ndepends = pkg_hash_fetch_unsatisfied_dependencies(new, deps,
290 char **tmp = unresolved;
291 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
292 " for %s:\n", package_name);
294 opkg_msg(ERROR, "\t%s", *tmp);
300 opkg_message(ERROR, "\n");
304 /* insert the package we are installing so that we download it */
305 pkg_vec_insert(deps, new);
307 /* download package and dependencies */
308 for (i = 0; i < deps->len; i++) {
310 struct _curl_cb_data cb_data;
314 if (pkg->local_filename)
318 pdata.action = OPKG_DOWNLOAD;
320 if (pkg->src == NULL) {
321 opkg_msg(ERROR, "Package %s not available from any "
322 "configured src\n", package_name);
326 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
328 /* Get the filename part, without any directory */
329 stripped_filename = strrchr(pkg->filename, '/');
330 if (!stripped_filename)
331 stripped_filename = pkg->filename;
333 sprintf_alloc(&pkg->local_filename, "%s/%s", conf->tmp_dir,
336 cb_data.cb = progress_callback;
337 cb_data.progress_data = &pdata;
338 cb_data.user_data = user_data;
339 /* 75% of "install" progress is for downloading */
340 cb_data.start_range = 75 * i / deps->len;
341 cb_data.finish_range = 75 * (i + 1) / deps->len;
343 err = opkg_download(url, pkg->local_filename,
344 (curl_progress_func) curl_progress_cb,
356 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
357 all = pkg_vec_alloc();
358 pkg_hash_fetch_available(all);
359 for (i = 0; i < all->len; i++) {
360 all->pkgs[i]->parent->dependencies_checked = 0;
365 /* 75% of "install" progress is for downloading */
367 pdata.action = OPKG_INSTALL;
370 /* unpack the package */
371 err = opkg_install_pkg(new, 0);
379 /* run configure scripts, etc. */
380 err = opkg_configure_packages(NULL);
385 /* write out status files and file lists */
386 opkg_conf_write_status_files();
387 pkg_write_changed_filelists();
389 progress(pdata, 100);
394 opkg_remove_package(const char *package_name,
395 opkg_progress_callback_t progress_callback, void *user_data)
399 pkg_t *pkg_to_remove;
400 opkg_progress_data_t pdata;
402 opkg_assert(package_name != NULL);
404 pkg_info_preinstall_check();
406 pkg = pkg_hash_fetch_installed_by_name(package_name);
408 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
409 opkg_msg(ERROR, "Package %s not installed\n", package_name);
413 pdata.action = OPKG_REMOVE;
417 if (conf->restrict_to_default_dest) {
418 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
421 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
427 err = opkg_remove_pkg(pkg_to_remove, 0);
429 /* write out status files and file lists */
430 opkg_conf_write_status_files();
431 pkg_write_changed_filelists();
434 progress(pdata, 100);
435 return (err) ? -1 : 0;
439 opkg_upgrade_package(const char *package_name,
440 opkg_progress_callback_t progress_callback,
445 opkg_progress_data_t pdata;
447 opkg_assert(package_name != NULL);
449 pkg_info_preinstall_check();
451 if (conf->restrict_to_default_dest) {
452 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
455 pkg = pkg_hash_fetch_installed_by_name(package_name);
459 opkg_msg(ERROR, "Package %s not installed\n", package_name);
463 pdata.action = OPKG_INSTALL;
467 err = opkg_upgrade_pkg(pkg);
473 err = opkg_configure_packages(NULL);
478 /* write out status files and file lists */
479 opkg_conf_write_status_files();
480 pkg_write_changed_filelists();
482 progress(pdata, 100);
487 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
489 pkg_vec_t *installed;
493 opkg_progress_data_t pdata;
495 pdata.action = OPKG_INSTALL;
500 installed = pkg_vec_alloc();
501 pkg_info_preinstall_check();
503 pkg_hash_fetch_all_installed(installed);
504 for (i = 0; i < installed->len; i++) {
505 pkg = installed->pkgs[i];
508 progress(pdata, 99 * i / installed->len);
510 err += opkg_upgrade_pkg(pkg);
512 pkg_vec_free(installed);
517 err = opkg_configure_packages(NULL);
521 /* write out status files and file lists */
522 opkg_conf_write_status_files();
523 pkg_write_changed_filelists();
526 progress(pdata, 100);
531 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
537 pkg_src_list_elt_t *iter;
539 int sources_list_count, sources_done;
540 opkg_progress_data_t pdata;
542 pdata.action = OPKG_DOWNLOAD;
546 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
547 ? conf->default_dest->lists_dir : conf->lists_dir);
549 if (!file_is_dir(lists_dir)) {
550 if (file_exists(lists_dir)) {
551 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
556 err = file_mkdir_hier(lists_dir, 0755);
558 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
565 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
566 if (mkdtemp(tmp) == NULL) {
567 opkg_perror(ERROR, "Coundn't create temporary directory %s",
574 /* count the number of sources so we can give some progress updates */
575 sources_list_count = 0;
577 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
578 sources_list_count++;
581 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
582 char *url, *list_file_name = NULL;
584 src = (pkg_src_t *) iter->data;
586 if (src->extra_data) /* debian style? */
587 sprintf_alloc(&url, "%s/%s/%s", src->value,
589 src->gzip ? "Packages.gz" : "Packages");
591 sprintf_alloc(&url, "%s/%s", src->value,
592 src->gzip ? "Packages.gz" : "Packages");
594 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
596 if (opkg_download(url, list_file_name, NULL, NULL, 0)) {
597 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
602 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
603 if (conf->check_signature) {
605 /* download detached signitures to verify the package lists */
606 /* get the url for the sig file */
607 if (src->extra_data) /* debian style? */
608 sprintf_alloc(&url, "%s/%s/%s", src->value,
609 src->extra_data, "Packages.sig");
611 sprintf_alloc(&url, "%s/%s", src->value,
614 /* create filename for signature */
615 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
618 /* make sure there is no existing signature file */
619 unlink(sig_file_name);
621 err = opkg_download(url, sig_file_name, NULL, NULL, 0);
623 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
626 err = opkg_verify_file(list_file_name,
629 opkg_msg(INFO, "Signature check "
633 opkg_msg(ERROR, "Signature check "
642 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
643 " has not been enabled in this build\n",
646 free(list_file_name);
649 progress(pdata, 100 * sources_done / sources_list_count);
656 /* Now re-read the package lists to update package hash tables. */
657 opkg_re_read_config_files();
663 pkg_compare_names_and_version(const void *a0, const void *b0)
665 const pkg_t *a = *(const pkg_t **)a0;
666 const pkg_t *b = *(const pkg_t **)b0;
669 ret = strcmp(a->name, b->name);
672 ret = pkg_compare_versions(a, b);
678 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
683 opkg_assert(callback);
685 all = pkg_vec_alloc();
686 pkg_hash_fetch_available(all);
688 pkg_vec_sort(all, pkg_compare_names_and_version);
690 for (i = 0; i < all->len; i++) {
695 callback(pkg, user_data);
704 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
706 struct active_list *head;
707 struct active_list *node;
708 pkg_t *old = NULL, *new = NULL;
710 opkg_assert(callback);
712 /* ensure all data is valid */
713 pkg_info_preinstall_check();
715 head = prepare_upgrade_list();
716 for (node = active_list_next(head, head); node;
717 node = active_list_next(head, node)) {
718 old = list_entry(node, pkg_t, list);
719 new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
722 callback(new, user_data);
724 active_list_head_delete(head);
729 opkg_find_package(const char *name, const char *ver, const char *arch,
736 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
738 all = pkg_vec_alloc();
739 pkg_hash_fetch_available(all);
740 for (i = 0; i < all->len; i++) {
746 if (sstrcmp(pkg->name, name))
750 pkgv = pkg_version_str_alloc(pkg);
751 if (sstrcmp(pkgv, ver)) {
757 /* check architecture */
759 if (sstrcmp(pkg->architecture, arch))
763 /* check repository */
765 if (sstrcmp(pkg->src->name, repo))
776 return pkg_found ? pkg : NULL;
780 * @brief Check the accessibility of repositories.
781 * @return return how many repositories cannot access. 0 means all okay.
784 opkg_repository_accessibility_check(void)
786 pkg_src_list_elt_t *iter;
787 str_list_elt_t *iter1;
789 int repositories = 0;
795 src = str_list_alloc();
797 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
798 host = strstr(((pkg_src_t *)iter->data)->value, "://") + 3;
799 end = index(host, '/');
800 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
801 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
802 end - ((pkg_src_t *) iter->data)->value);
804 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
806 for (iter1 = str_list_first(src); iter1;
807 iter1 = str_list_next(src, iter1)) {
808 if (strstr(iter1->data, stmp))
814 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
817 str_list_append(src, repo_ptr);
822 while (repositories > 0) {
823 iter1 = str_list_pop(src);
826 if (opkg_download(iter1->data, "/dev/null", NULL, NULL, 0))
828 str_list_elt_deinit(iter1);