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 ***/
53 static opkg_package_t*
54 old_pkg_to_new (pkg_t *old)
58 new = opkg_package_new ();
60 #define sstrdup(x) (x) ? strdup (x) : NULL;
62 new->name = sstrdup (old->name);
63 new->version = pkg_version_str_alloc (old);
64 new->architecture = sstrdup (old->architecture);
66 new->repository = sstrdup (old->src->name);
67 new->description = sstrdup (old->description);
68 new->tags = sstrdup (old->tags);
69 new->url = sstrdup (old->url);
71 new->size = (old->size) ? atoi (old->size) : 0;
72 new->installed = (old->state_status == SS_INSTALLED);
78 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
85 all = pkg_vec_alloc ();
86 pkg_hash_fetch_available (&conf->pkg_hash, all);
88 for (i = 0; i < all->len; i++)
92 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
95 if (pkg->state_status == SS_UNPACKED)
97 r = opkg_configure (conf, pkg);
100 pkg->state_status = SS_INSTALLED;
101 pkg->parent->state_status = SS_INSTALLED;
102 pkg->state_flag &= ~SF_PREFER;
118 opkg_progress_callback_t cb;
119 opkg_progress_data_t *progress_data;
127 curl_progress_cb (struct _curl_cb_data *cb_data,
128 double t, /* dltotal */
129 double d, /* dlnow */
133 int p = (t) ? d * 100 / t : 0;
134 static int prev = -1;
137 /* prevent the same value being sent twice (can occur due to rounding) */
145 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
146 cb_data->progress_data->percentage = progress;
148 (cb_data->cb)(cb_data->opkg,
149 cb_data->progress_data,
164 p = malloc (sizeof (opkg_package_t));
165 memset (p, 0, sizeof (opkg_package_t));
171 opkg_package_free (opkg_package_t *p)
175 free (p->architecture);
176 free (p->description);
179 free (p->repository);
188 opkg = malloc (sizeof (opkg_t));
190 opkg->args = malloc (sizeof (args_t));
191 args_init (opkg->args);
193 opkg->conf = malloc (sizeof (opkg_conf_t));
194 opkg_conf_init (opkg->conf, opkg->args);
196 opkg_init_options_array (opkg->conf, &opkg->options);
201 opkg_free (opkg_t *opkg)
203 opkg_assert (opkg != NULL);
205 opkg_conf_deinit (opkg->conf);
206 args_deinit (opkg->args);
207 free (opkg->options);
214 opkg_re_read_config_files (opkg_t *opkg)
219 opkg_assert (opkg != NULL);
224 /* Unfortunatly, the easiest way to re-read the config files right now is to
225 * throw away opkg->conf and start again */
227 /* copy the settings we need to keep */
228 a->autoremove = c->autoremove;
229 a->force_depends = c->force_depends;
230 a->force_defaults = c->force_defaults;
231 a->force_overwrite = c->force_overwrite;
232 a->force_downgrade = c->force_downgrade;
233 a->force_reinstall = c->force_reinstall;
234 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
235 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
236 a->nodeps = c->nodeps;
237 a->noaction = c->noaction;
238 a->query_all = c->query_all;
239 a->verbosity = c->verbosity;
243 if (a->offline_root) free (a->offline_root);
244 a->offline_root = strdup (c->offline_root);
247 if (c->offline_root_pre_script_cmd)
249 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
250 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
253 if (c->offline_root_post_script_cmd)
255 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
256 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
259 /* throw away old opkg_conf and start again */
260 opkg_conf_deinit (opkg->conf);
261 opkg_conf_init (opkg->conf, opkg->args);
263 free (opkg->options);
264 opkg_init_options_array (opkg->conf, &opkg->options);
270 opkg_get_option (opkg_t *opkg, char *option, void **value)
273 opkg_option_t *options;
275 opkg_assert (opkg != NULL);
276 opkg_assert (option != NULL);
277 opkg_assert (value != NULL);
279 options = opkg->options;
281 /* look up the option
282 * TODO: this would be much better as a hash table
284 while (options[i].name)
286 if (strcmp (options[i].name, option) != 0)
294 switch (options[i].type)
296 case OPKG_OPT_TYPE_BOOL:
297 *((int *) value) = *((int *) options[i].value);
300 case OPKG_OPT_TYPE_INT:
301 *((int *) value) = *((int *) options[i].value);
304 case OPKG_OPT_TYPE_STRING:
305 *((char **)value) = strdup (options[i].value);
312 opkg_set_option (opkg_t *opkg, char *option, void *value)
314 int i = 0, found = 0;
315 opkg_option_t *options;
317 opkg_assert (opkg != NULL);
318 opkg_assert (option != NULL);
319 opkg_assert (value != NULL);
321 options = opkg->options;
323 /* look up the option
324 * TODO: this would be much better as a hash table
326 while (options[i].name)
328 if (strcmp (options[i].name, option) == 0)
338 /* XXX: Warning: Option not found */
343 switch (options[i].type)
345 case OPKG_OPT_TYPE_BOOL:
346 if (*((int *) value) == 0)
347 *((int *)options[i].value) = 0;
349 *((int *)options[i].value) = 1;
352 case OPKG_OPT_TYPE_INT:
353 *((int *) options[i].value) = *((int *) value);
356 case OPKG_OPT_TYPE_STRING:
357 *((char **)options[i].value) = strdup (value);
364 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
367 char *stripped_filename;
368 opkg_progress_data_t pdata;
370 pkg_vec_t *deps, *all;
372 char **unresolved = NULL;
374 opkg_assert (opkg != NULL);
375 opkg_assert (package_name != NULL);
378 pkg_info_preinstall_check (opkg->conf);
381 /* check to ensure package is not already installed */
382 old = pkg_hash_fetch_installed_by_name(&opkg->conf->pkg_hash, package_name);
385 /* XXX: Error: Package is already installed. */
386 return OPKG_PACKAGE_ALREADY_INSTALLED;
389 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, package_name, NULL);
392 /* XXX: Error: Could not find package to install */
393 return OPKG_PACKAGE_NOT_FOUND;
396 new->state_flag |= SF_USER;
398 pdata.action = OPKG_INSTALL;
399 pdata.package = old_pkg_to_new (new);
403 /* find dependancies and download them */
404 deps = pkg_vec_alloc ();
405 /* this function does not return the original package, so we insert it later */
406 ndepends = pkg_hash_fetch_unsatisfied_dependencies (opkg->conf, new, deps, &unresolved);
409 /* XXX: Error: Could not satisfy dependencies */
411 return OPKG_DEPENDENCIES_FAILED;
414 /* insert the package we are installing so that we download it */
415 pkg_vec_insert (deps, new);
417 /* download package and dependancies */
418 for (i = 0; i < deps->len; i++)
421 struct _curl_cb_data cb_data;
425 if (pkg->local_filename)
428 opkg_package_free (pdata.package);
429 pdata.package = old_pkg_to_new (pkg);
430 pdata.action = OPKG_DOWNLOAD;
432 if (pkg->src == NULL)
434 /* XXX: Error: Package not available from any configured src */
435 return OPKG_PACKAGE_NOT_AVAILABLE;
438 sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
440 /* Get the filename part, without any directory */
441 stripped_filename = strrchr(pkg->filename, '/');
442 if ( ! stripped_filename )
443 stripped_filename = pkg->filename;
445 sprintf_alloc(&pkg->local_filename, "%s/%s", opkg->conf->tmp_dir, stripped_filename);
447 cb_data.cb = progress_callback;
448 cb_data.progress_data = &pdata;
450 cb_data.user_data = user_data;
451 /* 75% of "install" progress is for downloading */
452 cb_data.start_range = 75 * i / deps->len;
453 cb_data.finish_range = 75 * (i + 1) / deps->len;
455 err = opkg_download(opkg->conf, url, pkg->local_filename,
456 (curl_progress_func) curl_progress_cb, &cb_data);
462 opkg_package_free (pdata.package);
463 return OPKG_DOWNLOAD_FAILED;
469 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
470 all = pkg_vec_alloc ();
471 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
472 for (i = 0; i < all->len; i++)
474 all->pkgs[i]->parent->dependencies_checked = 0;
479 /* 75% of "install" progress is for downloading */
480 opkg_package_free (pdata.package);
481 pdata.package = old_pkg_to_new (new);
482 pdata.action = OPKG_INSTALL;
483 progress (pdata, 75);
485 /* unpack the package */
486 err = opkg_install_pkg(opkg->conf, new, 0);
490 opkg_package_free (pdata.package);
493 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
494 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
495 case OPKG_INSTALL_ERR_DEPENDENCIES:
496 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
497 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
498 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
499 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
500 default: return OPKG_UNKNOWN_ERROR;
504 progress (pdata, 75);
506 /* run configure scripts, etc. */
507 err = opkg_configure_packages (opkg->conf, NULL);
510 opkg_package_free (pdata.package);
511 return OPKG_UNKNOWN_ERROR;
514 /* write out status files and file lists */
515 opkg_conf_write_status_files (opkg->conf);
516 pkg_write_changed_filelists (opkg->conf);
518 progress (pdata, 100);
519 opkg_package_free (pdata.package);
524 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
528 pkg_t *pkg_to_remove;
529 opkg_progress_data_t pdata;
531 opkg_assert (opkg != NULL);
532 opkg_assert (package_name != NULL);
534 pkg_info_preinstall_check (opkg->conf);
536 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
540 /* XXX: Error: Package not installed. */
541 return OPKG_PACKAGE_NOT_INSTALLED;
544 pdata.action = OPKG_REMOVE;
545 pdata.package = old_pkg_to_new (pkg);
549 if (pkg->state_status == SS_NOT_INSTALLED)
551 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
552 return OPKG_PACKAGE_NOT_INSTALLED;
554 progress (pdata, 25);
556 if (opkg->conf->restrict_to_default_dest)
558 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
560 opkg->conf->default_dest);
564 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
568 progress (pdata, 75);
570 err = opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
572 /* write out status files and file lists */
573 opkg_conf_write_status_files (opkg->conf);
574 pkg_write_changed_filelists (opkg->conf);
577 progress (pdata, 100);
578 opkg_package_free (pdata.package);
579 return (err) ? OPKG_UNKNOWN_ERROR : OPKG_NO_ERROR;
583 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
587 opkg_progress_data_t pdata;
591 opkg_assert (opkg != NULL);
592 opkg_assert (package_name != NULL);
594 pkg_info_preinstall_check (opkg->conf);
596 if (opkg->conf->restrict_to_default_dest)
598 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
600 opkg->conf->default_dest);
603 /* XXX: Error: Package not installed in default_dest */
604 return OPKG_PACKAGE_NOT_INSTALLED;
609 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
615 /* XXX: Error: Package not installed */
616 return OPKG_PACKAGE_NOT_INSTALLED;
619 pdata.action = OPKG_INSTALL;
620 pdata.package = old_pkg_to_new (pkg);
623 err = opkg_upgrade_pkg (opkg->conf, pkg);
624 /* opkg_upgrade_pkg returns the error codes of opkg_install_pkg */
629 case OPKG_INSTALL_ERR_NOT_TRUSTED: return OPKG_GPG_ERROR;
630 case OPKG_INSTALL_ERR_DOWNLOAD: return OPKG_DOWNLOAD_FAILED;
631 case OPKG_INSTALL_ERR_DEPENDENCIES:
632 case OPKG_INSTALL_ERR_CONFLICTS: return OPKG_DEPENDENCIES_FAILED;
633 case OPKG_INSTALL_ERR_ALREADY_INSTALLED: return OPKG_PACKAGE_ALREADY_INSTALLED;
634 case OPKG_INSTALL_ERR_SIGNATURE: return OPKG_GPG_ERROR;
635 case OPKG_INSTALL_ERR_MD5: return OPKG_MD5_ERROR;
636 default: return OPKG_UNKNOWN_ERROR;
639 progress (pdata, 75);
641 err = opkg_configure_packages (opkg->conf, NULL);
643 return OPKG_UNKNOWN_ERROR;
644 progress (pdata, 100);
645 opkg_package_free (pdata.package);
650 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
652 pkg_vec_t *installed;
656 opkg_progress_data_t pdata;
658 pdata.action = OPKG_INSTALL;
659 pdata.package = NULL;
661 opkg_assert (opkg != NULL);
664 installed = pkg_vec_alloc ();
665 pkg_info_preinstall_check (opkg->conf);
667 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
668 for (i = 0; i < installed->len; i++)
670 pkg = installed->pkgs[i];
672 pdata.package = old_pkg_to_new (pkg);
673 progress (pdata, 99 * i / installed->len);
674 opkg_package_free (pdata.package);
676 err += opkg_upgrade_pkg (opkg->conf, pkg);
678 pkg_vec_free (installed);
683 err = opkg_configure_packages (opkg->conf, NULL);
687 pdata.package = NULL;
688 progress (pdata, 100);
693 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
698 pkg_src_list_elt_t *iter;
700 int sources_list_count, sources_done;
701 opkg_progress_data_t pdata;
703 opkg_assert (opkg != NULL);
705 pdata.action = OPKG_DOWNLOAD;
706 pdata.package = NULL;
709 sprintf_alloc (&lists_dir, "%s",
710 (opkg->conf->restrict_to_default_dest)
711 ? opkg->conf->default_dest->lists_dir
712 : opkg->conf->lists_dir);
714 if (!file_is_dir (lists_dir))
716 if (file_exists (lists_dir))
718 /* XXX: Error: file exists but is not a directory */
723 err = file_mkdir_hier (lists_dir, 0755);
726 /* XXX: Error: failed to create directory */
732 tmp = strdup ("/tmp/opkg.XXXXXX");
734 if (mkdtemp (tmp) == NULL)
736 /* XXX: Error: could not create temporary file name */
742 /* count the number of sources so we can give some progress updates */
743 sources_list_count = 0;
745 iter = opkg->conf->pkg_src_list.head;
748 sources_list_count++;
752 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
754 char *url, *list_file_name = NULL;
758 if (src->extra_data) /* debian style? */
759 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
760 src->gzip ? "Packages.gz" : "Packages");
762 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
764 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
768 struct _curl_cb_data cb_data;
769 char *tmp_file_name = NULL;
771 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
773 /* XXX: Note: downloading url */
775 cb_data.cb = progress_callback;
776 cb_data.progress_data = &pdata;
778 cb_data.user_data = user_data;
779 cb_data.start_range = 100 * sources_done / sources_list_count;
780 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
782 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
786 /* XXX: Note: Inflating downloaded file */
787 in = fopen (tmp_file_name, "r");
788 out = fopen (list_file_name, "w");
797 unlink (tmp_file_name);
799 free (tmp_file_name);
802 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
806 /* XXX: Error: download error */
807 result = OPKG_DOWNLOAD_FAILED;
813 /* download detached signitures to verify the package lists */
814 /* get the url for the sig file */
815 if (src->extra_data) /* debian style? */
816 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
819 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
821 /* create filename for signature */
822 sprintf_alloc (&sig_file_name, "%s/%s.sig", lists_dir, src->name);
824 /* make sure there is no existing signature file */
825 unlink (sig_file_name);
827 err = opkg_download (opkg->conf, url, sig_file_name, NULL, NULL);
830 /* XXX: Warning: Download failed */
835 err = opkg_verify_file (opkg->conf, list_file_name, sig_file_name);
838 /* XXX: Notice: Signature check passed */
842 /* XXX: Warning: Signature check failed */
845 free (sig_file_name);
846 free (list_file_name);
849 /* XXX: Note: Signiture check for %s skipped because GPG support was not
850 * enabled in this build
855 progress (pdata, 100 * sources_done / sources_list_count);
862 /* Now re-read the package lists to update package hash tables. */
863 opkg_re_read_config_files (opkg);
870 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
876 opkg_assert (callback);
878 all = pkg_vec_alloc ();
879 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
880 for (i = 0; i < all->len; i++)
883 opkg_package_t *package;
887 package = old_pkg_to_new (pkg);
888 callback (opkg, package, user_data);
897 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
903 opkg_assert (callback);
905 /* ensure all data is valid */
906 pkg_info_preinstall_check (opkg->conf);
908 all = pkg_vec_alloc ();
909 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
910 for (i = 0; i < all->len; i++)
914 opkg_package_t *package;
918 if (old->state_status != SS_INSTALLED)
921 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name, NULL);
923 /* XXX: Notice: Assuming locally install package is up to date */
927 cmp = pkg_compare_versions(old, new);
931 package = old_pkg_to_new (new);
932 callback (opkg, package, user_data);
942 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
945 opkg_package_t *package = NULL;
947 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
951 all = pkg_vec_alloc ();
952 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
953 for (i = 0; i < all->len; i++)
961 if (sstrcmp (pkg->name, name))
965 pkgv = pkg_version_str_alloc (pkg);
966 if (sstrcmp (pkgv, ver))
973 /* check architecture */
976 if (sstrcmp (pkg->architecture, arch))
980 /* check repository */
983 if (sstrcmp (pkg->src->name, repo))
988 package = old_pkg_to_new (pkg);