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"
31 #include "sprintf_alloc.h"
32 #include "file_util.h"
34 #include <libbb/libbb.h>
40 opkg_option_t *options;
43 #define opkg_assert(expr) if (!(expr)) { \
44 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
45 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
47 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
48 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
50 /** Private Functions ***/
52 static opkg_package_t*
53 old_pkg_to_new (pkg_t *old)
57 new = opkg_package_new ();
59 #define sstrdup(x) (x) ? strdup (x) : NULL;
61 new->name = sstrdup (old->name);
62 new->version = pkg_version_str_alloc (old);
63 new->architecture = sstrdup (old->architecture);
65 new->repository = sstrdup (old->src->name);
66 new->description = sstrdup (old->description);
67 new->tags = sstrdup (old->tags);
68 new->url = sstrdup (old->url);
70 new->size = (old->size) ? atoi (old->size) : 0;
71 new->installed = (old->state_status == SS_INSTALLED);
77 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
84 all = pkg_vec_alloc ();
85 pkg_hash_fetch_available (&conf->pkg_hash, all);
87 for (i = 0; i < all->len; i++)
91 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
94 if (pkg->state_status == SS_UNPACKED)
96 r = opkg_configure (conf, pkg);
99 pkg->state_status = SS_INSTALLED;
100 pkg->parent->state_status = SS_INSTALLED;
101 pkg->state_flag &= ~SF_PREFER;
117 opkg_progress_callback_t cb;
118 opkg_progress_data_t *progress_data;
126 curl_progress_cb (struct _curl_cb_data *cb_data,
127 double t, /* dltotal */
128 double d, /* dlnow */
132 int p = (t) ? d * 100 / t : 0;
133 static int prev = -1;
136 /* prevent the same value being sent twice (can occur due to rounding) */
144 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
145 cb_data->progress_data->percentage = progress;
147 (cb_data->cb)(cb_data->opkg,
148 cb_data->progress_data,
163 p = malloc (sizeof (opkg_package_t));
164 memset (p, 0, sizeof (opkg_package_t));
170 opkg_package_free (opkg_package_t *p)
174 free (p->architecture);
175 free (p->description);
178 free (p->repository);
187 opkg = malloc (sizeof (opkg_t));
189 opkg->args = malloc (sizeof (args_t));
190 args_init (opkg->args);
192 opkg->conf = malloc (sizeof (opkg_conf_t));
193 opkg_conf_init (opkg->conf, opkg->args);
195 opkg_init_options_array (opkg->conf, &opkg->options);
200 opkg_free (opkg_t *opkg)
202 opkg_assert (opkg != NULL);
204 opkg_conf_deinit (opkg->conf);
205 args_deinit (opkg->args);
206 free (opkg->options);
213 opkg_re_read_config_files (opkg_t *opkg)
218 opkg_assert (opkg != NULL);
223 /* Unfortunatly, the easiest way to re-read the config files right now is to
224 * throw away opkg->conf and start again */
226 /* copy the settings we need to keep */
227 a->autoremove = c->autoremove;
228 a->force_depends = c->force_depends;
229 a->force_defaults = c->force_defaults;
230 a->force_overwrite = c->force_overwrite;
231 a->force_downgrade = c->force_downgrade;
232 a->force_reinstall = c->force_reinstall;
233 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
234 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
235 a->nodeps = c->nodeps;
236 a->noaction = c->noaction;
237 a->query_all = c->query_all;
238 a->multiple_providers = c->multiple_providers;
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);
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_DEPENDANCIES_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 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
463 all = pkg_vec_alloc ();
464 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
465 for (i = 0; i < all->len; i++)
467 all->pkgs[i]->parent->dependencies_checked = 0;
472 /* 75% of "install" progress is for downloading */
473 opkg_package_free (pdata.package);
474 pdata.package = old_pkg_to_new (new);
475 pdata.action = OPKG_INSTALL;
476 progress (pdata, 75);
478 /* unpack the package */
479 err = opkg_install_pkg(opkg->conf, new, 0);
484 progress (pdata, 75);
486 /* run configure scripts, etc. */
487 err = opkg_configure_packages (opkg->conf, NULL);
491 /* write out status files and file lists */
492 opkg_conf_write_status_files (opkg->conf);
493 pkg_write_changed_filelists (opkg->conf);
495 progress (pdata, 100);
496 opkg_package_free (pdata.package);
501 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
504 pkg_t *pkg_to_remove;
505 opkg_progress_data_t pdata;
507 opkg_assert (opkg != NULL);
508 opkg_assert (package_name != NULL);
512 pkg_info_preinstall_check (opkg->conf);
515 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
519 /* XXX: Error: Package not installed. */
520 return OPKG_PACKAGE_NOT_INSTALLED;
523 pdata.action = OPKG_REMOVE;
524 pdata.package = old_pkg_to_new (pkg);
528 if (pkg->state_status == SS_NOT_INSTALLED)
530 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
531 return OPKG_PACKAGE_NOT_INSTALLED;
533 progress (pdata, 25);
535 if (opkg->conf->restrict_to_default_dest)
537 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
539 opkg->conf->default_dest);
543 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
547 progress (pdata, 75);
549 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
551 /* write out status files and file lists */
552 opkg_conf_write_status_files (opkg->conf);
553 pkg_write_changed_filelists (opkg->conf);
556 progress (pdata, 100);
557 opkg_package_free (pdata.package);
562 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
565 opkg_progress_data_t pdata;
569 opkg_assert (opkg != NULL);
570 opkg_assert (package_name != NULL);
572 pkg_info_preinstall_check (opkg->conf);
574 if (opkg->conf->restrict_to_default_dest)
576 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
578 opkg->conf->default_dest);
581 /* XXX: Error: Package not installed in default_dest */
582 return OPKG_PACKAGE_NOT_INSTALLED;
587 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
593 /* XXX: Error: Package not installed */
594 return OPKG_PACKAGE_NOT_INSTALLED;
597 pdata.action = OPKG_INSTALL;
598 pdata.package = old_pkg_to_new (pkg);
601 opkg_upgrade_pkg (opkg->conf, pkg);
602 progress (pdata, 75);
604 opkg_configure_packages (opkg->conf, NULL);
605 progress (pdata, 100);
606 opkg_package_free (pdata.package);
611 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
613 pkg_vec_t *installed;
617 opkg_progress_data_t pdata;
619 pdata.action = OPKG_INSTALL;
620 pdata.package = NULL;
622 opkg_assert (opkg != NULL);
625 installed = pkg_vec_alloc ();
626 pkg_info_preinstall_check (opkg->conf);
628 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
629 for (i = 0; i < installed->len; i++)
631 pkg = installed->pkgs[i];
633 pdata.package = old_pkg_to_new (pkg);
634 progress (pdata, 99 * i / installed->len);
635 opkg_package_free (pdata.package);
637 err += opkg_upgrade_pkg (opkg->conf, pkg);
639 pkg_vec_free (installed);
644 err = opkg_configure_packages (opkg->conf, NULL);
648 pdata.package = NULL;
649 progress (pdata, 100);
654 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
659 pkg_src_list_elt_t *iter;
661 int sources_list_count, sources_done;
662 opkg_progress_data_t pdata;
663 char *tmp_file_name = NULL;
665 opkg_assert (opkg != NULL);
667 pdata.action = OPKG_DOWNLOAD;
668 pdata.package = NULL;
671 sprintf_alloc (&lists_dir, "%s",
672 (opkg->conf->restrict_to_default_dest)
673 ? opkg->conf->default_dest->lists_dir
674 : opkg->conf->lists_dir);
676 if (!file_is_dir (lists_dir))
678 if (file_exists (lists_dir))
680 /* XXX: Error: file exists but is not a directory */
685 err = file_mkdir_hier (lists_dir, 0755);
688 /* XXX: Error: failed to create directory */
694 tmp = strdup ("/tmp/opkg.XXXXXX");
696 if (mkdtemp (tmp) == NULL)
698 /* XXX: Error: could not create temporary file name */
704 /* cout the number of sources so we can give some progress updates */
705 sources_list_count = 0;
707 iter = opkg->conf->pkg_src_list.head;
710 sources_list_count++;
714 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
716 char *url, *list_file_name;
720 if (src->extra_data) /* debian style? */
721 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
722 src->gzip ? "Packages.gz" : "Packages");
724 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
726 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
730 struct _curl_cb_data cb_data;
732 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
734 /* XXX: Note: downloading url */
736 cb_data.cb = progress_callback;
737 cb_data.progress_data = &pdata;
739 cb_data.user_data = user_data;
740 cb_data.start_range = 100 * sources_done / sources_list_count;
741 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
743 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
747 /* XXX: Note: Inflating downloaded file */
748 in = fopen (tmp_file_name, "r");
749 out = fopen (list_file_name, "w");
758 unlink (tmp_file_name);
762 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
763 free (tmp_file_name);
767 /* XXX: Error: download error */
768 result = OPKG_DOWNLOAD_FAILED;
773 /* download detached signitures to verify the package lists */
774 /* get the url for the sig file */
775 if (src->extra_data) /* debian style? */
776 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
779 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
781 /* create temporary file for it */
782 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
784 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
787 /* XXX: Warning: Download failed */
792 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
795 /* XXX: Notice: Signature check passed */
799 /* XXX: Warning: Signature check failed */
802 unlink (tmp_file_name);
803 free (tmp_file_name);
806 /* XXX: Note: Signiture check for %s skipped because GPG support was not
807 * enabled in this build
810 free (list_file_name);
813 progress (pdata, 100 * sources_done / sources_list_count);
820 /* Now re-read the package lists to update package hash tables. */
821 opkg_re_read_config_files (opkg);
828 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
834 opkg_assert (callback);
836 all = pkg_vec_alloc ();
837 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
838 for (i = 0; i < all->len; i++)
841 opkg_package_t *package;
845 package = old_pkg_to_new (pkg);
846 callback (opkg, package, user_data);
855 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
861 opkg_assert (callback);
863 all = pkg_vec_alloc ();
864 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
865 for (i = 0; i < all->len; i++)
869 opkg_package_t *package;
873 if (old->state_status != SS_INSTALLED)
876 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
878 /* XXX: Notice: Assuming locally install package is up to date */
882 cmp = pkg_compare_versions(old, new);
886 package = old_pkg_to_new (new);
887 callback (opkg, package, user_data);
897 opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
900 opkg_package_t *package = NULL;
902 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
906 all = pkg_vec_alloc ();
907 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
908 for (i = 0; i < all->len; i++)
916 if (sstrcmp (pkg->name, name))
920 pkgv = pkg_version_str_alloc (pkg);
921 if (sstrcmp (pkgv, ver))
928 /* check architecture */
931 if (sstrcmp (pkg->architecture, arch))
935 /* check repository */
938 if (sstrcmp (pkg->src->name, repo))
943 package = old_pkg_to_new (pkg);