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.
22 #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"
30 #include "opkg_error.h"
32 #include "sprintf_alloc.h"
33 #include "file_util.h"
35 #include <libbb/libbb.h>
41 opkg_option_t *options;
44 #define opkg_assert(expr) if (!(expr)) { \
45 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
46 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
48 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
49 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
51 /** Private Functions ***/
56 static opkg_package_t*
57 pkg_t_to_opkg_package_t (pkg_t *old)
61 new = opkg_package_new ();
63 new->name = xstrdup(old->name);
64 new->version = pkg_version_str_alloc (old);
65 new->architecture = xstrdup(old->architecture);
67 new->repository = xstrdup(old->src->name);
68 new->description = xstrdup(old->description);
69 new->tags = xstrdup(old->tags);
70 new->url = xstrdup(old->url);
72 new->size = (old->size) ? atoi (old->size) : 0;
73 new->installed = (old->state_status == SS_INSTALLED);
79 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
86 all = pkg_vec_alloc ();
87 pkg_hash_fetch_available (&conf->pkg_hash, all);
89 for (i = 0; i < all->len; i++)
93 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
96 if (pkg->state_status == SS_UNPACKED)
98 r = opkg_configure (conf, pkg);
101 pkg->state_status = SS_INSTALLED;
102 pkg->parent->state_status = SS_INSTALLED;
103 pkg->state_flag &= ~SF_PREFER;
119 opkg_progress_callback_t cb;
120 opkg_progress_data_t *progress_data;
128 curl_progress_cb (struct _curl_cb_data *cb_data,
129 double t, /* dltotal */
130 double d, /* dlnow */
134 int p = (t) ? d * 100 / t : 0;
135 static int prev = -1;
138 /* prevent the same value being sent twice (can occur due to rounding) */
146 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
147 cb_data->progress_data->percentage = progress;
149 (cb_data->cb)(cb_data->opkg,
150 cb_data->progress_data,
165 p = xcalloc(1, sizeof (opkg_package_t));
171 opkg_package_free (opkg_package_t *p)
175 free (p->architecture);
176 free (p->description);
179 free (p->repository);
190 opkg = xcalloc(1, sizeof (opkg_t));
192 opkg->args = xcalloc(1, sizeof (args_t));
193 args_init (opkg->args);
195 opkg->conf = xcalloc(1, sizeof (opkg_conf_t));
196 err = opkg_conf_init (opkg->conf, opkg->args);
205 opkg_init_options_array (opkg->conf, &opkg->options);
210 opkg_free (opkg_t *opkg)
212 opkg_assert (opkg != NULL);
217 opkg_conf_deinit (opkg->conf);
218 args_deinit (opkg->args);
219 free (opkg->options);
226 opkg_re_read_config_files (opkg_t *opkg)
231 opkg_assert (opkg != NULL);
236 /* Unfortunatly, the easiest way to re-read the config files right now is to
237 * throw away opkg->conf and start again */
239 /* copy the settings we need to keep */
240 a->autoremove = c->autoremove;
241 a->force_depends = c->force_depends;
242 a->force_defaults = c->force_defaults;
243 a->force_maintainer = c->force_maintainer;
244 a->force_overwrite = c->force_overwrite;
245 a->force_downgrade = c->force_downgrade;
246 a->force_reinstall = c->force_reinstall;
247 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
248 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
249 a->nodeps = c->nodeps;
250 a->noaction = c->noaction;
251 a->query_all = c->query_all;
252 a->verbosity = c->verbosity;
256 if (a->offline_root) free (a->offline_root);
257 a->offline_root = xstrdup(c->offline_root);
260 if (c->offline_root_pre_script_cmd)
262 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
263 a->offline_root_pre_script_cmd = xstrdup(c->offline_root_pre_script_cmd);
266 if (c->offline_root_post_script_cmd)
268 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
269 a->offline_root_post_script_cmd = xstrdup(c->offline_root_post_script_cmd);
275 a->cache = xstrdup(c->cache);
278 /* throw away old opkg_conf and start again */
279 opkg_conf_deinit (opkg->conf);
280 opkg_conf_init (opkg->conf, opkg->args);
282 free (opkg->options);
283 opkg_init_options_array (opkg->conf, &opkg->options);
289 opkg_get_option (opkg_t *opkg, char *option, void **value)
292 opkg_option_t *options;
294 opkg_assert (opkg != NULL);
295 opkg_assert (option != NULL);
296 opkg_assert (value != NULL);
298 options = opkg->options;
300 /* look up the option
301 * TODO: this would be much better as a hash table
303 while (options[i].name)
305 if (strcmp (options[i].name, option) != 0)
313 switch (options[i].type)
315 case OPKG_OPT_TYPE_BOOL:
316 *((int *) value) = *((int *) options[i].value);
319 case OPKG_OPT_TYPE_INT:
320 *((int *) value) = *((int *) options[i].value);
323 case OPKG_OPT_TYPE_STRING:
324 *((char **)value) = xstrdup(options[i].value);
331 opkg_set_option (opkg_t *opkg, char *option, void *value)
333 int i = 0, found = 0;
334 opkg_option_t *options;
336 opkg_assert (opkg != NULL);
337 opkg_assert (option != NULL);
338 opkg_assert (value != NULL);
340 options = opkg->options;
342 /* look up the option
343 * TODO: this would be much better as a hash table
345 while (options[i].name)
347 if (strcmp (options[i].name, option) == 0)
357 /* XXX: Warning: Option not found */
362 switch (options[i].type)
364 case OPKG_OPT_TYPE_BOOL:
365 if (*((int *) value) == 0)
366 *((int *)options[i].value) = 0;
368 *((int *)options[i].value) = 1;
371 case OPKG_OPT_TYPE_INT:
372 *((int *) options[i].value) = *((int *) value);
375 case OPKG_OPT_TYPE_STRING:
376 *((char **)options[i].value) = xstrdup(value);
383 * @brief libopkg API: Install package
384 * @param opkg Then opkg handler
385 * @param package_name The name of package in which is going to install
386 * @param progress_callback The callback function that report the status to caller.
389 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
392 char *stripped_filename;
393 opkg_progress_data_t pdata;
395 pkg_vec_t *deps, *all;
397 char **unresolved = NULL;
399 opkg_assert (opkg != NULL);
400 opkg_assert (package_name != NULL);
403 pkg_info_preinstall_check (opkg->conf);
406 /* check to ensure package is not already installed */
407 old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
410 /* XXX: Error: Package is already installed. */
411 return OPKG_PACKAGE_ALREADY_INSTALLED;
414 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name);
417 /* XXX: Error: Could not find package to install */
418 return OPKG_PACKAGE_NOT_FOUND;
421 new->state_flag |= SF_USER;
423 pdata.action = OPKG_INSTALL;
424 pdata.package = pkg_t_to_opkg_package_t (new);
428 /* find dependancies and download them */
429 deps = pkg_vec_alloc ();
430 /* this function does not return the original package, so we insert it later */
431 ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
434 /* XXX: Error: Could not satisfy dependencies */
436 return OPKG_DEPENDENCIES_FAILED;
439 /* insert the package we are installing so that we download it */
440 pkg_vec_insert (deps, new);
442 /* download package and dependancies */
443 for (i = 0; i < deps->len; i++)
446 struct _curl_cb_data cb_data;
450 if (pkg->local_filename)
453 opkg_package_free (pdata.package);
454 pdata.package = pkg_t_to_opkg_package_t (pkg);
455 pdata.action = OPKG_DOWNLOAD;
457 if (pkg->src == NULL)
459 /* XXX: Error: Package not available from any configured src */
460 return OPKG_PACKAGE_NOT_AVAILABLE;
463 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
465 /* Get the filename part, without any directory */
466 stripped_filename = strrchr(pkg->filename, '/');
467 if ( ! stripped_filename )
468 stripped_filename = pkg->filename;
470 sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
472 cb_data.cb = progress_callback;
473 cb_data.progress_data = &pdata;
475 cb_data.user_data = user_data;
476 /* 75% of "install" progress is for downloading */
477 cb_data.start_range = 75 * i / deps->len;
478 cb_data.finish_range = 75 * (i + 1) / deps->len;
480 err = opkg_download(opkg->conf, url, pkg->local_filename,
481 (curl_progress_func) curl_progress_cb, &cb_data);
487 opkg_package_free (pdata.package);
488 return OPKG_DOWNLOAD_FAILED;
494 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
495 all = pkg_vec_alloc ();
496 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
497 for (i = 0; i < all->len; i++)
499 all->pkgs[i]->parent->dependencies_checked = 0;
504 /* 75% of "install" progress is for downloading */
505 opkg_package_free (pdata.package);
506 pdata.package = pkg_t_to_opkg_package_t (new);
507 pdata.action = OPKG_INSTALL;
508 progress (pdata, 75);
510 /* unpack the package */
511 err = opkg_install_pkg(opkg->conf, new, 0);
515 opkg_package_free (pdata.package);
516 return OPKG_UNKNOWN_ERROR;
519 progress (pdata, 75);
521 /* run configure scripts, etc. */
522 err = opkg_configure_packages (opkg->conf, NULL);
525 opkg_package_free (pdata.package);
526 return OPKG_UNKNOWN_ERROR;
529 /* write out status files and file lists */
530 opkg_conf_write_status_files (opkg->conf);
531 pkg_write_changed_filelists (opkg->conf);
533 progress (pdata, 100);
534 opkg_package_free (pdata.package);
539 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
543 pkg_t *pkg_to_remove;
544 opkg_progress_data_t pdata;
546 opkg_assert (opkg != NULL);
547 opkg_assert (package_name != NULL);
549 pkg_info_preinstall_check (opkg->conf);
551 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
555 /* XXX: Error: Package not installed. */
556 return OPKG_PACKAGE_NOT_INSTALLED;
559 pdata.action = OPKG_REMOVE;
560 pdata.package = pkg_t_to_opkg_package_t (pkg);
564 if (pkg->state_status == SS_NOT_INSTALLED)
566 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
567 opkg_package_free (pdata.package);
568 return OPKG_PACKAGE_NOT_INSTALLED;
570 progress (pdata, 25);
572 if (opkg->conf->restrict_to_default_dest)
574 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
576 opkg->conf->default_dest);
580 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
584 progress (pdata, 75);
586 err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
588 /* write out status files and file lists */
589 opkg_conf_write_status_files (opkg->conf);
590 pkg_write_changed_filelists (opkg->conf);
593 progress (pdata, 100);
594 opkg_package_free (pdata.package);
595 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
599 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
603 opkg_progress_data_t pdata;
607 opkg_assert (opkg != NULL);
608 opkg_assert (package_name != NULL);
610 pkg_info_preinstall_check (opkg->conf);
612 if (opkg->conf->restrict_to_default_dest)
614 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
616 opkg->conf->default_dest);
619 /* XXX: Error: Package not installed in default_dest */
620 return OPKG_PACKAGE_NOT_INSTALLED;
625 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
631 /* XXX: Error: Package not installed */
632 return OPKG_PACKAGE_NOT_INSTALLED;
635 pdata.action = OPKG_INSTALL;
636 pdata.package = pkg_t_to_opkg_package_t (pkg);
639 err = opkg_upgrade_pkg (opkg->conf, pkg);
640 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
643 opkg_package_free (pdata.package);
644 return OPKG_UNKNOWN_ERROR;
646 progress (pdata, 75);
648 err = opkg_configure_packages (opkg->conf, NULL);
650 opkg_package_free (pdata.package);
651 return OPKG_UNKNOWN_ERROR;
654 /* write out status files and file lists */
655 opkg_conf_write_status_files (opkg->conf);
656 pkg_write_changed_filelists (opkg->conf);
658 progress (pdata, 100);
659 opkg_package_free (pdata.package);
664 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
666 pkg_vec_t *installed;
670 opkg_progress_data_t pdata;
672 pdata.action = OPKG_INSTALL;
673 pdata.package = NULL;
675 opkg_assert (opkg != NULL);
678 installed = pkg_vec_alloc ();
679 pkg_info_preinstall_check (opkg->conf);
681 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
682 for (i = 0; i < installed->len; i++)
684 pkg = installed->pkgs[i];
686 pdata.package = pkg_t_to_opkg_package_t (pkg);
687 progress (pdata, 99 * i / installed->len);
688 opkg_package_free (pdata.package);
690 err += opkg_upgrade_pkg (opkg->conf, pkg);
692 pkg_vec_free (installed);
697 err = opkg_configure_packages (opkg->conf, NULL);
701 pdata.package = NULL;
702 progress (pdata, 100);
707 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
712 pkg_src_list_elt_t *iter;
714 int sources_list_count, sources_done;
715 opkg_progress_data_t pdata;
717 opkg_assert (opkg != NULL);
719 pdata.action = OPKG_DOWNLOAD;
720 pdata.package = NULL;
723 sprintf_alloc (&lists_dir, "%s",
724 (opkg->conf->restrict_to_default_dest)
725 ? opkg->conf->default_dest->lists_dir
726 : opkg->conf->lists_dir);
728 if (!file_is_dir (lists_dir))
730 if (file_exists (lists_dir))
732 /* XXX: Error: file exists but is not a directory */
737 err = file_mkdir_hier (lists_dir, 0755);
740 /* XXX: Error: failed to create directory */
746 sprintf_alloc(&tmp, "%s/update-XXXXXX", opkg->conf->tmp_dir);
747 if (mkdtemp (tmp) == NULL) {
748 /* XXX: Error: could not create temporary file name */
754 /* count the number of sources so we can give some progress updates */
755 sources_list_count = 0;
757 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
759 sources_list_count++;
762 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
764 char *url, *list_file_name = NULL;
766 src = (pkg_src_t *)iter->data;
768 if (src->extra_data) /* debian style? */
769 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
770 src->gzip ? "Packages.gz" : "Packages");
772 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
774 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
778 struct _curl_cb_data cb_data;
779 char *tmp_file_name = NULL;
781 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
783 /* XXX: Note: downloading url */
785 cb_data.cb = progress_callback;
786 cb_data.progress_data = &pdata;
788 cb_data.user_data = user_data;
789 cb_data.start_range = 100 * sources_done / sources_list_count;
790 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
792 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
796 /* XXX: Note: Inflating downloaded file */
797 in = fopen (tmp_file_name, "r");
798 out = fopen (list_file_name, "w");
807 unlink (tmp_file_name);
809 free (tmp_file_name);
812 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
816 /* XXX: Error: download error */
817 result = OPKG_DOWNLOAD_FAILED;
821 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL)
822 if ( opkg->conf->check_signature ) {
824 /* download detached signitures to verify the package lists */
825 /* get the url for the sig file */
826 if (src->extra_data) /* debian style? */
827 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
830 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
832 /* create filename for signature */
833 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
835 /* make sure there is no existing signature file */
836 unlink (sig_file_name);
838 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
841 /* XXX: Warning: Download failed */
846 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
849 /* XXX: Notice: Signature check passed */
853 /* XXX: Warning: Signature check failed */
856 free (sig_file_name);
857 free (list_file_name);
861 /* XXX: Note: Signature check for %s skipped because GPG support was not
862 * enabled in this build
867 progress (pdata, 100 * sources_done / sources_list_count);
874 /* Now re-read the package lists to update package hash tables. */
875 opkg_re_read_config_files (opkg);
882 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
888 opkg_assert (callback);
890 all = pkg_vec_alloc ();
891 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
892 for (i = 0; i < all->len; i++)
895 opkg_package_t *package;
899 package = pkg_t_to_opkg_package_t (pkg);
900 callback (opkg, package, user_data);
901 opkg_package_free (package);
910 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
912 struct active_list *head;
913 struct active_list *node;
914 pkg_t *old=NULL, *new = NULL;
915 static opkg_package_t* package=NULL;
919 opkg_assert (callback);
921 /* ensure all data is valid */
922 pkg_info_preinstall_check (opkg->conf);
924 head = prepare_upgrade_list(opkg->conf);
925 for (node=active_list_next(head, head); node; active_list_next(head,node)) {
926 old = list_entry(node, pkg_t, list);
927 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
930 package = pkg_t_to_opkg_package_t (new);
931 callback (opkg, package, user_data);
932 opkg_package_free (package);
934 active_list_head_delete(head);
939 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
942 opkg_package_t *package = NULL;
944 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
948 all = pkg_vec_alloc ();
949 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
950 for (i = 0; i < all->len; i++)
958 if (sstrcmp (pkg->name, name))
962 pkgv = pkg_version_str_alloc (pkg);
963 if (sstrcmp (pkgv, ver))
970 /* check architecture */
973 if (sstrcmp (pkg->architecture, arch))
977 /* check repository */
980 if (sstrcmp (pkg->src->name, repo))
985 package = pkg_t_to_opkg_package_t (pkg);
995 #include <curl/curl.h>
998 * @brief Check the accessibility of repositories. It will try to access the repository to check if the respository is accessible throught current network status.
999 * @param opkg The opkg_t
1000 * @return return how many repositories cannot access. 0 means all okay.
1002 int opkg_repository_accessibility_check(opkg_t *opkg)
1004 pkg_src_list_elt_t *iter;
1005 str_list_elt_t *iter1;
1012 opkg_assert(opkg != NULL);
1014 src = str_list_alloc();
1016 list_for_each_entry(iter, &opkg->conf->pkg_src_list.head, node)
1018 if (strstr(((pkg_src_t *)iter->data)->value, "://") &&
1019 index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/'))
1020 stmp = xstrndup(((pkg_src_t *)iter->data)->value,
1021 (index(strstr(((pkg_src_t *)iter->data)->value, "://") + 3, '/') - ((pkg_src_t *)iter->data)->value)*sizeof(char));
1024 stmp = xstrdup(((pkg_src_t *)iter->data)->value);
1026 for (iter1 = str_list_first(src); iter1; iter1 = str_list_next(src, iter1))
1028 if (strstr(iter1->data, stmp))
1034 sprintf_alloc(&repo_ptr, "%s/index.html",stmp);
1037 str_list_append(src, repo_ptr);
1041 while (repositories > 0)
1043 iter1 = str_list_pop(src);
1046 err = opkg_download(opkg->conf, iter1->data, "/dev/null", NULL, NULL);
1048 if (!(err == CURLE_OK ||
1049 err == CURLE_HTTP_RETURNED_ERROR ||
1050 err == CURLE_FILE_COULDNT_READ_FILE ||
1051 err == CURLE_REMOTE_FILE_NOT_FOUND ||
1052 err == CURLE_TFTP_NOTFOUND
1059 str_list_elt_deinit(iter1);