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 static struct opkg_conf saved_conf;
90 if (pkg_hash_load_feeds(0))
93 if (pkg_hash_load_status_files())
110 int opkg_re_read_config_files(void)
117 void opkg_get_option(char *option, void **value)
120 extern opkg_option_t options[];
122 /* look up the option
123 * TODO: this would be much better as a hash table
125 while (options[i].name) {
126 if (strcmp(options[i].name, option) != 0) {
133 switch (options[i].type) {
134 case OPKG_OPT_TYPE_BOOL:
135 *((int *)value) = *((int *)options[i].value);
138 case OPKG_OPT_TYPE_INT:
139 *((int *)value) = *((int *)options[i].value);
142 case OPKG_OPT_TYPE_STRING:
143 *((char **)value) = xstrdup(options[i].value);
149 void opkg_set_option(char *option, void *value)
151 int i = 0, found = 0;
152 extern opkg_option_t options[];
154 opkg_assert(option != NULL);
155 opkg_assert(value != NULL);
157 /* look up the option
158 * TODO: this would be much better as a hash table
160 while (options[i].name) {
161 if (strcmp(options[i].name, option) == 0) {
169 opkg_msg(ERROR, "Invalid option: %s\n", option);
174 switch (options[i].type) {
175 case OPKG_OPT_TYPE_BOOL:
176 if (*((int *)value) == 0)
177 *((int *)options[i].value) = 0;
179 *((int *)options[i].value) = 1;
182 case OPKG_OPT_TYPE_INT:
183 *((int *)options[i].value) = *((int *)value);
186 case OPKG_OPT_TYPE_STRING:
187 *((char **)options[i].value) = xstrdup(value);
194 * @brief libopkg API: Install package
195 * @param package_name The name of package in which is going to install
196 * @param progress_callback The callback function that report the status to caller.
199 opkg_install_package(const char *package_name,
200 opkg_progress_callback_t progress_callback,
204 char *stripped_filename, *local_filename;
205 opkg_progress_data_t pdata;
207 pkg_vec_t *deps, *all;
209 char **unresolved = NULL;
210 const char *filename;
212 opkg_assert(package_name != NULL);
215 pkg_info_preinstall_check();
217 /* check to ensure package is not already installed */
218 old = pkg_hash_fetch_installed_by_name(package_name);
220 opkg_msg(ERROR, "Package %s is already installed\n",
225 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name);
227 opkg_msg(ERROR, "Couldn't find package %s\n", package_name);
231 new->state_flag |= SF_USER;
238 /* find dependancies and download them */
239 deps = pkg_vec_alloc();
240 /* this function does not return the original package, so we insert it later */
241 pkg_hash_fetch_unsatisfied_dependencies(new, deps, &unresolved);
243 char **tmp = unresolved;
244 opkg_msg(ERROR, "Couldn't satisfy the following dependencies"
245 " for %s:\n", package_name);
247 opkg_msg(ERROR, "\t%s", *tmp);
253 opkg_message(ERROR, "\n");
257 /* insert the package we are installing so that we download it */
258 pkg_vec_insert(deps, new);
260 /* download package and dependencies */
261 for (i = 0; i < deps->len; i++) {
266 if (pkg_get_string(pkg, PKG_LOCAL_FILENAME))
270 pdata.action = OPKG_DOWNLOAD;
272 if (pkg->src == NULL) {
273 opkg_msg(ERROR, "Package %s not available from any "
274 "configured src\n", package_name);
278 filename = pkg_get_string(pkg, PKG_FILENAME);
280 sprintf_alloc(&url, "%s/%s", pkg->src->value, filename);
282 /* Get the filename part, without any directory */
283 stripped_filename = strrchr(filename, '/');
284 if (!stripped_filename)
285 stripped_filename = (char *)filename;
287 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
290 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
292 err = opkg_download(url, local_filename, 0);
303 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
304 all = pkg_vec_alloc();
305 pkg_hash_fetch_available(all);
306 for (i = 0; i < all->len; i++) {
307 all->pkgs[i]->parent->dependencies_checked = 0;
311 /* 75% of "install" progress is for downloading */
313 pdata.action = OPKG_INSTALL;
316 /* unpack the package */
317 err = opkg_install_pkg(new, 0);
325 /* run configure scripts, etc. */
326 err = opkg_configure_packages(NULL);
331 /* write out status files and file lists */
332 opkg_conf_write_status_files();
333 pkg_write_changed_filelists();
335 progress(pdata, 100);
340 opkg_remove_package(const char *package_name,
341 opkg_progress_callback_t progress_callback, void *user_data)
345 pkg_t *pkg_to_remove;
346 opkg_progress_data_t pdata;
348 opkg_assert(package_name != NULL);
350 pkg_info_preinstall_check();
352 pkg = pkg_hash_fetch_installed_by_name(package_name);
354 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
355 opkg_msg(ERROR, "Package %s not installed\n", package_name);
359 pdata.action = OPKG_REMOVE;
363 if (conf->restrict_to_default_dest) {
364 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
368 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
373 err = opkg_remove_pkg(pkg_to_remove, 0);
375 /* write out status files and file lists */
376 opkg_conf_write_status_files();
377 pkg_write_changed_filelists();
379 progress(pdata, 100);
380 return (err) ? -1 : 0;
384 opkg_upgrade_package(const char *package_name,
385 opkg_progress_callback_t progress_callback,
390 opkg_progress_data_t pdata;
392 opkg_assert(package_name != NULL);
394 pkg_info_preinstall_check();
396 if (conf->restrict_to_default_dest) {
397 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
400 pkg = pkg_hash_fetch_installed_by_name(package_name);
404 opkg_msg(ERROR, "Package %s not installed\n", package_name);
408 pdata.action = OPKG_INSTALL;
412 err = opkg_upgrade_pkg(pkg);
418 err = opkg_configure_packages(NULL);
423 /* write out status files and file lists */
424 opkg_conf_write_status_files();
425 pkg_write_changed_filelists();
427 progress(pdata, 100);
432 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
434 pkg_vec_t *installed;
438 opkg_progress_data_t pdata;
440 pdata.action = OPKG_INSTALL;
445 installed = pkg_vec_alloc();
446 pkg_info_preinstall_check();
448 pkg_hash_fetch_all_installed(installed);
449 for (i = 0; i < installed->len; i++) {
450 pkg = installed->pkgs[i];
453 progress(pdata, 99 * i / installed->len);
455 err += opkg_upgrade_pkg(pkg);
457 pkg_vec_free(installed);
462 err = opkg_configure_packages(NULL);
466 /* write out status files and file lists */
467 opkg_conf_write_status_files();
468 pkg_write_changed_filelists();
471 progress(pdata, 100);
476 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
482 pkg_src_list_elt_t *iter;
484 int sources_list_count, sources_done;
485 opkg_progress_data_t pdata;
487 pdata.action = OPKG_DOWNLOAD;
491 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
492 ? conf->default_dest->lists_dir : conf->lists_dir);
494 if (!file_is_dir(lists_dir)) {
495 if (file_exists(lists_dir)) {
496 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
501 err = file_mkdir_hier(lists_dir, 0755);
503 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
510 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
511 if (mkdtemp(tmp) == NULL) {
512 opkg_perror(ERROR, "Coundn't create temporary directory %s",
519 /* count the number of sources so we can give some progress updates */
520 sources_list_count = 0;
522 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
523 sources_list_count++;
526 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
527 char *url, *list_file_name = NULL;
529 src = (pkg_src_t *) iter->data;
531 if (src->extra_data) /* debian style? */
532 sprintf_alloc(&url, "%s/%s/%s", src->value,
534 src->gzip ? "Packages.gz" : "Packages");
536 sprintf_alloc(&url, "%s/%s", src->value,
537 src->gzip ? "Packages.gz" : "Packages");
539 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
541 if (opkg_download(url, list_file_name, 0)) {
542 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
547 #if defined(HAVE_USIGN)
548 if (conf->check_signature) {
550 /* download detached signitures to verify the package lists */
551 /* get the url for the sig file */
552 if (src->extra_data) /* debian style? */
553 sprintf_alloc(&url, "%s/%s/%s", src->value,
554 src->extra_data, "Packages.sig");
556 sprintf_alloc(&url, "%s/%s", src->value,
559 /* create filename for signature */
560 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
563 /* make sure there is no existing signature file */
564 unlink(sig_file_name);
566 err = opkg_download(url, sig_file_name, 0);
568 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
571 err = opkg_verify_file(list_file_name,
574 opkg_msg(INFO, "Signature check "
578 opkg_msg(ERROR, "Signature check "
587 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
588 " has not been enabled in this build\n",
591 free(list_file_name);
594 progress(pdata, 100 * sources_done / sources_list_count);
601 /* Now re-read the package lists to update package hash tables. */
602 opkg_re_read_config_files();
607 static int pkg_compare_names_and_version(const void *a0, const void *b0)
609 const pkg_t *a = *(const pkg_t **)a0;
610 const pkg_t *b = *(const pkg_t **)b0;
613 ret = strcmp(a->name, b->name);
616 ret = pkg_compare_versions(a, b);
621 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
626 opkg_assert(callback);
628 all = pkg_vec_alloc();
629 pkg_hash_fetch_available(all);
631 pkg_vec_sort(all, pkg_compare_names_and_version);
633 for (i = 0; i < all->len; i++) {
638 callback(pkg, user_data);
647 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
649 struct active_list *head;
650 struct active_list *node;
651 pkg_t *old = NULL, *new = NULL;
653 opkg_assert(callback);
655 /* ensure all data is valid */
656 pkg_info_preinstall_check();
658 head = prepare_upgrade_list();
659 for (node = active_list_next(head, head); node;
660 node = active_list_next(head, node)) {
663 pkg_hash_fetch_best_installation_candidate_by_name(old->
667 callback(new, user_data);
669 active_list_head_delete(head);
673 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
680 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
682 all = pkg_vec_alloc();
683 pkg_hash_fetch_available(all);
684 for (i = 0; i < all->len; i++) {
690 if (sstrcmp(pkg->name, name))
694 pkgv = pkg_version_str_alloc(pkg);
695 if (sstrcmp(pkgv, ver)) {
701 /* check architecture */
703 if (sstrcmp(pkg_get_architecture(pkg), arch))
707 /* check repository */
709 if (sstrcmp(pkg->src->name, repo))
720 return pkg_found ? pkg : NULL;
724 * @brief Check the accessibility of repositories.
725 * @return return how many repositories cannot access. 0 means all okay.
727 int opkg_repository_accessibility_check(void)
729 pkg_src_list_elt_t *iter;
730 str_list_elt_t *iter1;
732 int repositories = 0;
738 src = str_list_alloc();
740 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
741 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
742 end = index(host, '/');
743 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
744 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
746 ((pkg_src_t *) iter->data)->value);
748 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
750 for (iter1 = str_list_first(src); iter1;
751 iter1 = str_list_next(src, iter1)) {
752 if (strstr(iter1->data, stmp))
758 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
761 str_list_append(src, repo_ptr);
766 while (repositories > 0) {
767 iter1 = str_list_pop(src);
770 if (opkg_download(iter1->data, "/dev/null", 0))
772 str_list_elt_deinit(iter1);