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, NULL, NULL))
93 if (pkg_hash_load_status_files(NULL, NULL))
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++) {
263 char *url, *urlencoded_path;
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);
279 urlencoded_path = urlencode_path(filename);
280 sprintf_alloc(&url, "%s/%s", pkg->src->value, urlencoded_path);
281 free(urlencoded_path);
283 /* Get the filename part, without any directory */
284 stripped_filename = strrchr(filename, '/');
285 if (!stripped_filename)
286 stripped_filename = (char *)filename;
288 sprintf_alloc(&local_filename, "%s/%s", conf->tmp_dir,
291 pkg_set_string(pkg, PKG_LOCAL_FILENAME, local_filename);
293 err = opkg_download(url, local_filename, 0);
304 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
305 all = pkg_vec_alloc();
306 pkg_hash_fetch_available(all);
307 for (i = 0; i < all->len; i++) {
308 all->pkgs[i]->parent->dependencies_checked = 0;
312 /* 75% of "install" progress is for downloading */
314 pdata.action = OPKG_INSTALL;
317 /* unpack the package */
318 err = opkg_install_pkg(new, 0);
326 /* run configure scripts, etc. */
327 err = opkg_configure_packages(NULL);
332 /* write out status files and file lists */
333 opkg_conf_write_status_files();
334 pkg_write_changed_filelists();
336 progress(pdata, 100);
341 opkg_remove_package(const char *package_name,
342 opkg_progress_callback_t progress_callback, void *user_data)
346 pkg_t *pkg_to_remove;
347 opkg_progress_data_t pdata;
349 opkg_assert(package_name != NULL);
351 pkg_info_preinstall_check();
353 pkg = pkg_hash_fetch_installed_by_name(package_name);
355 if (pkg == NULL || pkg->state_status == SS_NOT_INSTALLED) {
356 opkg_msg(ERROR, "Package %s not installed\n", package_name);
360 pdata.action = OPKG_REMOVE;
364 if (conf->restrict_to_default_dest) {
365 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(pkg->name,
369 pkg_to_remove = pkg_hash_fetch_installed_by_name(pkg->name);
374 err = opkg_remove_pkg(pkg_to_remove, 0);
376 /* write out status files and file lists */
377 opkg_conf_write_status_files();
378 pkg_write_changed_filelists();
380 progress(pdata, 100);
381 return (err) ? -1 : 0;
385 opkg_upgrade_package(const char *package_name,
386 opkg_progress_callback_t progress_callback,
391 opkg_progress_data_t pdata;
393 opkg_assert(package_name != NULL);
395 pkg_info_preinstall_check();
397 if (conf->restrict_to_default_dest) {
398 pkg = pkg_hash_fetch_installed_by_name_dest(package_name,
401 pkg = pkg_hash_fetch_installed_by_name(package_name);
405 opkg_msg(ERROR, "Package %s not installed\n", package_name);
409 pdata.action = OPKG_INSTALL;
413 err = opkg_upgrade_pkg(pkg);
419 err = opkg_configure_packages(NULL);
424 /* write out status files and file lists */
425 opkg_conf_write_status_files();
426 pkg_write_changed_filelists();
428 progress(pdata, 100);
433 opkg_upgrade_all(opkg_progress_callback_t progress_callback, void *user_data)
435 pkg_vec_t *installed;
439 opkg_progress_data_t pdata;
441 pdata.action = OPKG_INSTALL;
446 installed = pkg_vec_alloc();
447 pkg_info_preinstall_check();
449 pkg_hash_fetch_all_installed(installed);
450 for (i = 0; i < installed->len; i++) {
451 pkg = installed->pkgs[i];
454 progress(pdata, 99 * i / installed->len);
456 err += opkg_upgrade_pkg(pkg);
458 pkg_vec_free(installed);
463 err = opkg_configure_packages(NULL);
467 /* write out status files and file lists */
468 opkg_conf_write_status_files();
469 pkg_write_changed_filelists();
472 progress(pdata, 100);
477 opkg_update_package_lists(opkg_progress_callback_t progress_callback,
483 pkg_src_list_elt_t *iter;
485 int sources_list_count, sources_done;
486 opkg_progress_data_t pdata;
488 pdata.action = OPKG_DOWNLOAD;
492 sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
493 ? conf->default_dest->lists_dir : conf->lists_dir);
495 if (!file_is_dir(lists_dir)) {
496 if (file_exists(lists_dir)) {
497 opkg_msg(ERROR, "%s is not a directory\n", lists_dir);
502 err = file_mkdir_hier(lists_dir, 0755);
504 opkg_msg(ERROR, "Couldn't create lists_dir %s\n",
511 sprintf_alloc(&tmp, "%s/update-XXXXXX", conf->tmp_dir);
512 if (mkdtemp(tmp) == NULL) {
513 opkg_perror(ERROR, "Coundn't create temporary directory %s",
520 /* count the number of sources so we can give some progress updates */
521 sources_list_count = 0;
523 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
524 sources_list_count++;
527 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
528 char *url, *list_file_name = NULL;
530 src = (pkg_src_t *) iter->data;
532 if (src->extra_data) /* debian style? */
533 sprintf_alloc(&url, "%s/%s/%s", src->value,
535 src->gzip ? "Packages.gz" : "Packages");
537 sprintf_alloc(&url, "%s/%s", src->value,
538 src->gzip ? "Packages.gz" : "Packages");
540 sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
542 if (opkg_download(url, list_file_name, 0)) {
543 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
548 #if defined(HAVE_USIGN)
549 if (conf->check_signature) {
551 /* download detached signitures to verify the package lists */
552 /* get the url for the sig file */
553 if (src->extra_data) /* debian style? */
554 sprintf_alloc(&url, "%s/%s/%s", src->value,
555 src->extra_data, "Packages.sig");
557 sprintf_alloc(&url, "%s/%s", src->value,
560 /* create filename for signature */
561 sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
564 /* make sure there is no existing signature file */
565 unlink(sig_file_name);
567 err = opkg_download(url, sig_file_name, 0);
569 opkg_msg(ERROR, "Couldn't retrieve %s\n", url);
572 err = opkg_verify_file(list_file_name,
575 opkg_msg(INFO, "Signature check "
579 opkg_msg(ERROR, "Signature check "
588 opkg_msg(INFO, "Signature check skipped for %s as GPG support"
589 " has not been enabled in this build\n",
592 free(list_file_name);
595 progress(pdata, 100 * sources_done / sources_list_count);
602 /* Now re-read the package lists to update package hash tables. */
603 opkg_re_read_config_files();
608 static int pkg_compare_names_and_version(const void *a0, const void *b0)
610 const pkg_t *a = *(const pkg_t **)a0;
611 const pkg_t *b = *(const pkg_t **)b0;
614 ret = strcmp(a->name, b->name);
617 ret = pkg_compare_versions(a, b);
622 int opkg_list_packages(opkg_package_callback_t callback, void *user_data)
627 opkg_assert(callback);
629 all = pkg_vec_alloc();
630 pkg_hash_fetch_available(all);
632 pkg_vec_sort(all, pkg_compare_names_and_version);
634 for (i = 0; i < all->len; i++) {
639 callback(pkg, user_data);
648 opkg_list_upgradable_packages(opkg_package_callback_t callback, void *user_data)
650 struct active_list *head;
651 struct active_list *node;
652 pkg_t *old = NULL, *new = NULL;
654 opkg_assert(callback);
656 /* ensure all data is valid */
657 pkg_info_preinstall_check();
659 head = prepare_upgrade_list();
660 for (node = active_list_next(head, head); node;
661 node = active_list_next(head, node)) {
664 pkg_hash_fetch_best_installation_candidate_by_name(old->
668 callback(new, user_data);
670 active_list_head_delete(head);
674 pkg_t *opkg_find_package(const char *name, const char *ver, const char *arch,
681 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
683 all = pkg_vec_alloc();
684 pkg_hash_fetch_available(all);
685 for (i = 0; i < all->len; i++) {
691 if (sstrcmp(pkg->name, name))
695 pkgv = pkg_version_str_alloc(pkg);
696 if (sstrcmp(pkgv, ver)) {
702 /* check architecture */
704 if (sstrcmp(pkg_get_architecture(pkg), arch))
708 /* check repository */
710 if (sstrcmp(pkg->src->name, repo))
721 return pkg_found ? pkg : NULL;
725 * @brief Check the accessibility of repositories.
726 * @return return how many repositories cannot access. 0 means all okay.
728 int opkg_repository_accessibility_check(void)
730 pkg_src_list_elt_t *iter;
731 str_list_elt_t *iter1;
733 int repositories = 0;
739 src = str_list_alloc();
741 list_for_each_entry(iter, &conf->pkg_src_list.head, node) {
742 host = strstr(((pkg_src_t *) iter->data)->value, "://") + 3;
743 end = index(host, '/');
744 if (strstr(((pkg_src_t *) iter->data)->value, "://") && end)
745 stmp = xstrndup(((pkg_src_t *) iter->data)->value,
747 ((pkg_src_t *) iter->data)->value);
749 stmp = xstrdup(((pkg_src_t *) iter->data)->value);
751 for (iter1 = str_list_first(src); iter1;
752 iter1 = str_list_next(src, iter1)) {
753 if (strstr(iter1->data, stmp))
759 sprintf_alloc(&repo_ptr, "%s/index.html", stmp);
762 str_list_append(src, repo_ptr);
767 while (repositories > 0) {
768 iter1 = str_list_pop(src);
771 if (opkg_download(iter1->data, "/dev/null", 0))
773 str_list_elt_deinit(iter1);