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 OLD_PKG_TO_NEW(pkg) opkg_package_new_with_values (pkg->name, pkg->version, pkg->architecture, pkg->description, pkg->tags, pkg->url, (pkg->size ? atoi (pkg->size) : 0), (pkg->state_status == SS_INSTALLED));
50 /** Private Functions ***/
54 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
61 all = pkg_vec_alloc ();
62 pkg_hash_fetch_available (&conf->pkg_hash, all);
64 for (i = 0; i < all->len; i++)
68 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
71 if (pkg->state_status == SS_UNPACKED)
73 r = opkg_configure (conf, pkg);
76 pkg->state_status = SS_INSTALLED;
77 pkg->parent->state_status = SS_INSTALLED;
78 pkg->state_flag &= ~SF_PREFER;
94 opkg_progress_callback_t cb;
95 opkg_progress_data_t *progress_data;
103 curl_progress_cb (struct _curl_cb_data *cb_data,
104 double t, /* dltotal */
105 double d, /* dlnow */
109 int p = (t) ? d * 100 / t : 0;
110 static int prev = -1;
113 /* prevent the same value being sent twice (can occur due to rounding) */
121 progress = cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range)));
122 cb_data->progress_data->percentage = progress;
124 (cb_data->cb)(cb_data->opkg,
125 cb_data->progress_data,
140 p = malloc (sizeof (opkg_package_t));
141 memset (p, 0, sizeof (opkg_package_t));
147 opkg_package_new_with_values (const char *name, const char *version,
148 const char *arch, const char *desc, const char *tags, const char *url, int size, int installed)
150 opkg_package_t *package;
151 package = opkg_package_new ();
153 #define sstrdup(x) (x) ? strdup (x) : NULL;
155 package->name = sstrdup (name);
156 package->version = sstrdup (version);
157 package->architecture = sstrdup (arch);
158 package->description = sstrdup (desc);
159 package->tags = sstrdup (tags);
160 package->url = sstrdup (url);
162 package->size = size;
163 package->installed = (installed != 0);
169 opkg_package_free (opkg_package_t *p)
173 free (p->architecture);
174 free (p->description);
185 opkg = malloc (sizeof (opkg_t));
187 opkg->args = malloc (sizeof (args_t));
188 args_init (opkg->args);
190 opkg->conf = malloc (sizeof (opkg_conf_t));
191 opkg_conf_init (opkg->conf, opkg->args);
193 opkg_init_options_array (opkg->conf, &opkg->options);
198 opkg_free (opkg_t *opkg)
200 opkg_assert (opkg != NULL);
202 opkg_conf_deinit (opkg->conf);
203 args_deinit (opkg->args);
207 opkg_re_read_config_files (opkg_t *opkg)
212 opkg_assert (opkg != NULL);
217 /* Unfortunatly, the easiest way to re-read the config files right now is to
218 * throw away opkg->conf and start again */
220 /* copy the settings we need to keep */
221 a->autoremove = c->autoremove;
222 a->force_depends = c->force_depends;
223 a->force_defaults = c->force_defaults;
224 a->force_overwrite = c->force_overwrite;
225 a->force_downgrade = c->force_downgrade;
226 a->force_reinstall = c->force_reinstall;
227 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
228 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
229 a->nodeps = c->nodeps;
230 a->noaction = c->noaction;
231 a->query_all = c->query_all;
232 a->multiple_providers = c->multiple_providers;
233 a->verbosity = c->verbosity;
237 if (a->offline_root) free (a->offline_root);
238 a->offline_root = strdup (c->offline_root);
241 if (c->offline_root_pre_script_cmd)
243 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
244 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
247 if (c->offline_root_post_script_cmd)
249 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
250 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
253 /* throw away old opkg_conf and start again */
254 opkg_conf_deinit (opkg->conf);
255 opkg_conf_init (opkg->conf, opkg->args);
257 free (opkg->options);
258 opkg_init_options_array (opkg->conf, &opkg->options);
264 opkg_get_option (opkg_t *opkg, char *option, void **value)
267 opkg_option_t *options;
269 opkg_assert (opkg != NULL);
270 opkg_assert (option != NULL);
271 opkg_assert (value != NULL);
273 options = opkg->options;
275 /* look up the option
276 * TODO: this would be much better as a hash table
278 while (options[i].name)
280 if (strcmp (options[i].name, option) != 0)
288 switch (options[i].type)
290 case OPKG_OPT_TYPE_BOOL:
291 *((int *) value) = *((int *) options[i].value);
294 case OPKG_OPT_TYPE_INT:
295 *((int *) value) = *((int *) options[i].value);
298 case OPKG_OPT_TYPE_STRING:
299 *((char **)value) = strdup (options[i].value);
306 opkg_set_option (opkg_t *opkg, char *option, void *value)
308 int i = 0, found = 0;
309 opkg_option_t *options;
311 opkg_assert (opkg != NULL);
312 opkg_assert (option != NULL);
313 opkg_assert (value != NULL);
315 options = opkg->options;
317 /* look up the option
318 * TODO: this would be much better as a hash table
320 while (options[i].name)
322 if (strcmp (options[i].name, option) == 0)
332 /* XXX: Warning: Option not found */
337 switch (options[i].type)
339 case OPKG_OPT_TYPE_BOOL:
340 if (*((int *) value) == 0)
341 *((int *)options[i].value) = 0;
343 *((int *)options[i].value) = 1;
346 case OPKG_OPT_TYPE_INT:
347 *((int *) options[i].value) = *((int *) value);
350 case OPKG_OPT_TYPE_STRING:
351 *((char **)options[i].value) = strdup (value);
358 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
361 char *package_id = NULL;
362 opkg_progress_data_t pdata;
365 opkg_assert (opkg != NULL);
366 opkg_assert (package_name != NULL);
369 pkg_info_preinstall_check (opkg->conf);
371 new = pkg_hash_fetch_best_installation_candidate_by_name (opkg->conf, package_name);
375 /* XXX: Error: Could not find package to install */
378 pdata.action = OPKG_INSTALL;
379 pdata.package = OLD_PKG_TO_NEW (new);
383 /* download the package */
384 opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
386 progress (pdata, 50);
389 package_id = strdup (package_name);
391 /* unpack the package */
392 if (opkg->conf->multiple_providers)
394 err = opkg_install_multi_by_name (opkg->conf, package_id);
398 err = opkg_install_by_name (opkg->conf, package_id);
404 progress (pdata, 75);
406 /* run configure scripts, etc. */
407 err = opkg_configure_packages (opkg->conf, NULL);
411 /* write out status files and file lists */
412 opkg_conf_write_status_files (opkg->conf);
413 pkg_write_changed_filelists (opkg->conf);
415 progress (pdata, 100);
416 opkg_package_free (pdata.package);
421 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
424 pkg_t *pkg_to_remove;
425 opkg_progress_data_t pdata;
427 opkg_assert (opkg != NULL);
428 opkg_assert (package_name != NULL);
432 pkg_info_preinstall_check (opkg->conf);
435 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
439 /* XXX: Error: Package not installed. */
443 pdata.action = OPKG_REMOVE;
444 pdata.package = OLD_PKG_TO_NEW (pkg);
448 if (pkg->state_status == SS_NOT_INSTALLED)
450 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
453 progress (pdata, 25);
455 if (opkg->conf->restrict_to_default_dest)
457 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
459 opkg->conf->default_dest);
463 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
467 progress (pdata, 75);
469 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
471 /* write out status files and file lists */
472 opkg_conf_write_status_files (opkg->conf);
473 pkg_write_changed_filelists (opkg->conf);
476 progress (pdata, 100);
477 opkg_package_free (pdata.package);
482 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
485 opkg_progress_data_t pdata;
489 opkg_assert (opkg != NULL);
490 opkg_assert (package_name != NULL);
492 pkg_info_preinstall_check (opkg->conf);
494 if (opkg->conf->restrict_to_default_dest)
496 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
498 opkg->conf->default_dest);
501 /* XXX: Error: Package not installed in default_dest */
507 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
513 /* XXX: Error: Package not installed */
517 pdata.action = OPKG_INSTALL;
518 pdata.package = OLD_PKG_TO_NEW (pkg);
521 opkg_upgrade_pkg (opkg->conf, pkg);
522 progress (pdata, 75);
524 opkg_configure_packages (opkg->conf, NULL);
525 progress (pdata, 100);
526 opkg_package_free (pdata.package);
531 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
533 pkg_vec_t *installed;
537 opkg_progress_data_t pdata;
539 pdata.action = OPKG_INSTALL;
540 pdata.package = NULL;
542 opkg_assert (opkg != NULL);
545 installed = pkg_vec_alloc ();
546 pkg_info_preinstall_check (opkg->conf);
548 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
549 for (i = 0; i < installed->len; i++)
551 pkg = installed->pkgs[i];
553 pdata.package = OLD_PKG_TO_NEW (pkg);
554 progress (pdata, 99 * i / installed->len);
555 opkg_package_free (pdata.package);
557 err += opkg_upgrade_pkg (opkg->conf, pkg);
559 pkg_vec_free (installed);
564 err = opkg_configure_packages (opkg->conf, NULL);
568 pdata.package = NULL;
569 progress (pdata, 100);
574 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
579 pkg_src_list_elt_t *iter;
581 int sources_list_count, sources_done;
582 opkg_progress_data_t pdata;
584 opkg_assert (opkg != NULL);
586 pdata.action = OPKG_DOWNLOAD;
587 pdata.package = NULL;
590 sprintf_alloc (&lists_dir, "%s",
591 (opkg->conf->restrict_to_default_dest)
592 ? opkg->conf->default_dest->lists_dir
593 : opkg->conf->lists_dir);
595 if (!file_is_dir (lists_dir))
597 if (file_exists (lists_dir))
599 /* XXX: Error: file exists but is not a directory */
604 err = file_mkdir_hier (lists_dir, 0755);
607 /* XXX: Error: failed to create directory */
613 tmp = strdup ("/tmp/opkg.XXXXXX");
615 if (mkdtemp (tmp) == NULL)
617 /* XXX: Error: could not create temporary file name */
623 /* cout the number of sources so we can give some progress updates */
624 sources_list_count = 0;
626 iter = opkg->conf->pkg_src_list.head;
629 sources_list_count++;
633 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
635 char *url, *list_file_name;
639 if (src->extra_data) /* debian style? */
640 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
641 src->gzip ? "Packages.gz" : "Packages");
643 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
645 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
650 struct _curl_cb_data cb_data;
652 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
654 /* XXX: Note: downloading url */
656 cb_data.cb = progress_callback;
657 cb_data.progress_data = &pdata;
659 cb_data.user_data = user_data;
660 cb_data.start_range = 100 * sources_done / sources_list_count;
661 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
663 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
667 /* XXX: Note: Inflating downloaded file */
668 in = fopen (tmp_file_name, "r");
669 out = fopen (list_file_name, "w");
678 unlink (tmp_file_name);
682 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
686 /* XXX: Error: download error */
691 /* download detached signitures to verify the package lists */
692 /* get the url for the sig file */
693 if (src->extra_data) /* debian style? */
694 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
697 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
699 /* create temporary file for it */
702 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
704 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
707 /* XXX: Warning: Download failed */
712 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
715 /* XXX: Notice: Signature check passed */
719 /* XXX: Warning: Signature check failed */
722 unlink (tmp_file_name);
723 free (tmp_file_name);
726 /* XXX: Note: Signiture check for %s skipped because GPG support was not
727 * enabled in this build
730 free (list_file_name);
733 progress (pdata, 100 * sources_done / sources_list_count);
745 opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
751 opkg_assert (callback);
753 all = pkg_vec_alloc ();
754 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
755 for (i = 0; i < all->len; i++)
758 opkg_package_t *package;
762 package = OLD_PKG_TO_NEW (pkg);
763 callback (opkg, package, user_data);
772 opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data)
778 opkg_assert (callback);
780 all = pkg_vec_alloc ();
781 pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
782 for (i = 0; i < all->len; i++)
786 opkg_package_t *package;
790 if (old->state_status != SS_INSTALLED)
793 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg->conf, old->name);
795 /* XXX: Notice: Assuming locally install package is up to date */
799 cmp = pkg_compare_versions(old, new);
803 package = OLD_PKG_TO_NEW (new);
804 callback (opkg, package, user_data);