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"
30 #include "sprintf_alloc.h"
31 #include "file_util.h"
33 #include <libbb/libbb.h>
39 opkg_option_t *options;
42 #define opkg_assert(expr) if (!(expr)) { \
43 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
44 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
46 #define progress(percent) if (progress_callback) progress_callback (opkg, percent, user_data);
48 /** Private Functions ***/
52 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
59 all = pkg_vec_alloc ();
60 pkg_hash_fetch_available (&conf->pkg_hash, all);
62 for (i = 0; i < all->len; i++)
66 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
69 if (pkg->state_status == SS_UNPACKED)
71 r = opkg_configure (conf, pkg);
74 pkg->state_status = SS_INSTALLED;
75 pkg->parent->state_status = SS_INSTALLED;
76 pkg->state_flag &= ~SF_PREFER;
92 opkg_progress_callback_t cb;
100 curl_progress_cb (struct _curl_cb_data *cb_data,
101 double t, /* dltotal */
102 double d, /* dlnow */
106 int p = (t) ? d * 100 / t : 0;
107 static int prev = -1;
109 /* prevent the same value being sent twice (can occur due to rounding) */
117 (cb_data->cb)(cb_data->opkg,
118 cb_data->start_range + (d / t * ((cb_data->finish_range - cb_data->start_range))),
131 opkg = malloc (sizeof (opkg_t));
133 opkg->args = malloc (sizeof (args_t));
134 args_init (opkg->args);
136 opkg->conf = malloc (sizeof (opkg_conf_t));
137 opkg_conf_init (opkg->conf, opkg->args);
139 opkg_init_options_array (opkg->conf, &opkg->options);
144 opkg_free (opkg_t *opkg)
146 opkg_assert (opkg != NULL);
148 opkg_conf_deinit (opkg->conf);
149 args_deinit (opkg->args);
153 opkg_read_config_files (opkg_t *opkg)
158 opkg_assert (opkg != NULL);
163 /* Unfortunatly, the easiest way to re-read the config files right now is to
164 * throw away opkg->conf and start again */
166 /* copy the settings we need to keep */
167 a->autoremove = c->autoremove;
168 a->force_depends = c->force_depends;
169 a->force_defaults = c->force_defaults;
170 a->force_overwrite = c->force_overwrite;
171 a->force_downgrade = c->force_downgrade;
172 a->force_reinstall = c->force_reinstall;
173 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
174 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
175 a->nodeps = c->nodeps;
176 a->noaction = c->noaction;
177 a->query_all = c->query_all;
178 a->multiple_providers = c->multiple_providers;
179 a->verbosity = c->verbosity;
183 if (a->offline_root) free (a->offline_root);
184 a->offline_root = strdup (c->offline_root);
187 if (c->offline_root_pre_script_cmd)
189 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
190 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
193 if (c->offline_root_post_script_cmd)
195 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
196 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
199 /* throw away old opkg_conf and start again */
200 opkg_conf_deinit (opkg->conf);
201 opkg_conf_init (opkg->conf, opkg->args);
203 free (opkg->options);
204 opkg_init_options_array (opkg->conf, &opkg->options);
210 opkg_get_option (opkg_t *opkg, char *option, void **value)
213 opkg_option_t *options;
215 opkg_assert (opkg != NULL);
216 opkg_assert (option != NULL);
217 opkg_assert (value != NULL);
219 options = opkg->options;
221 /* look up the option
222 * TODO: this would be much better as a hash table
224 while (options[i].name)
226 if (strcmp (options[i].name, option) != 0)
234 switch (options[i].type)
236 case OPKG_OPT_TYPE_BOOL:
237 *((int *) value) = *((int *) options[i].value);
240 case OPKG_OPT_TYPE_INT:
241 *((int *) value) = *((int *) options[i].value);
244 case OPKG_OPT_TYPE_STRING:
245 *((char **)value) = strdup (options[i].value);
252 opkg_set_option (opkg_t *opkg, char *option, void *value)
255 opkg_option_t *options;
257 opkg_assert (opkg != NULL);
258 opkg_assert (option != NULL);
259 opkg_assert (value != NULL);
261 options = opkg->options;
263 /* look up the option
264 * TODO: this would be much better as a hash table
266 while (options[i].name)
268 if (strcmp (options[i].name, option) == 0)
276 switch (options[i].type)
278 case OPKG_OPT_TYPE_BOOL:
279 if (*((int *) value) == 0)
280 *((int *)options[i].value) = 0;
282 *((int *)options[i].value) = 1;
285 case OPKG_OPT_TYPE_INT:
286 *((int *) options[i].value) = *((int *) value);
289 case OPKG_OPT_TYPE_STRING:
290 *((char **)options[i].value) = strdup (value);
297 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
300 char *package_id = NULL;
302 opkg_assert (opkg != NULL);
303 opkg_assert (package_name != NULL);
307 /* download the package */
308 opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
313 pkg_info_preinstall_check (opkg->conf);
316 package_id = strdup (package_name);
318 /* unpack the package */
319 if (opkg->conf->multiple_providers)
321 err = opkg_install_multi_by_name (opkg->conf, package_id);
325 err = opkg_install_by_name (opkg->conf, package_id);
333 /* run configure scripts, etc. */
334 err = opkg_configure_packages (opkg->conf, NULL);
338 /* write out status files and file lists */
339 opkg_conf_write_status_files (opkg->conf);
340 pkg_write_changed_filelists (opkg->conf);
347 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
350 pkg_t *pkg_to_remove;
352 opkg_assert (opkg != NULL);
353 opkg_assert (package_name != NULL);
357 pkg_info_preinstall_check (opkg->conf);
359 pkg_vec_t *installed_pkgs = pkg_vec_alloc ();
361 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed_pkgs);
365 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
369 /* XXX: Error: Package not installed. */
373 if (pkg->state_status == SS_NOT_INSTALLED)
375 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
381 if (opkg->conf->restrict_to_default_dest)
383 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
385 opkg->conf->default_dest);
389 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
395 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
397 /* write out status files and file lists */
398 opkg_conf_write_status_files (opkg->conf);
399 pkg_write_changed_filelists (opkg->conf);
407 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
411 opkg_assert (opkg != NULL);
412 opkg_assert (package_name != NULL);
416 pkg_info_preinstall_check (opkg->conf);
418 if (opkg->conf->restrict_to_default_dest)
420 pkg = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
422 opkg->conf->default_dest);
425 /* XXX: Error: Package not installed in default_dest */
431 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash,
437 /* XXX: Error: Package not installed */
443 opkg_upgrade_pkg (opkg->conf, pkg);
446 opkg_configure_packages (opkg->conf, NULL);
452 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
454 pkg_vec_t *installed;
459 opkg_assert (opkg != NULL);
462 installed = pkg_vec_alloc ();
463 pkg_info_preinstall_check (opkg->conf);
465 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed);
466 for (i = 0; i < installed->len; i++)
468 pkg = installed->pkgs[i];
469 err += opkg_upgrade_pkg (opkg->conf, pkg);
470 progress (100 * i / installed->len);
472 pkg_vec_free (installed);
477 err = opkg_configure_packages (opkg->conf, NULL);
486 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
491 pkg_src_list_elt_t *iter;
493 int sources_list_count, sources_done;
495 opkg_assert (opkg != NULL);
499 sprintf_alloc (&lists_dir, "%s",
500 (opkg->conf->restrict_to_default_dest)
501 ? opkg->conf->default_dest->lists_dir
502 : opkg->conf->lists_dir);
504 if (!file_is_dir (lists_dir))
506 if (file_exists (lists_dir))
508 /* XXX: Error: file exists but is not a directory */
513 err = file_mkdir_hier (lists_dir, 0755);
516 /* XXX: Error: failed to create directory */
522 tmp = strdup ("/tmp/opkg.XXXXXX");
524 if (mkdtemp (tmp) == NULL)
526 /* XXX: Error: could not create temporary file name */
532 /* cout the number of sources so we can give some progress updates */
533 sources_list_count = 0;
535 iter = opkg->conf->pkg_src_list.head;
538 sources_list_count++;
542 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
544 char *url, *list_file_name;
548 if (src->extra_data) /* debian style? */
549 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
550 src->gzip ? "Packages.gz" : "Packages");
552 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
554 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
559 struct _curl_cb_data cb_data;
561 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
563 /* XXX: Note: downloading url */
565 cb_data.cb = progress_callback;
567 cb_data.user_data = user_data;
568 cb_data.start_range = 100 * sources_done / sources_list_count;
569 cb_data.finish_range = 100 * (sources_done + 1) / sources_list_count;
571 err = opkg_download (opkg->conf, url, tmp_file_name, (curl_progress_func) curl_progress_cb, &cb_data);
575 /* XXX: Note: Inflating downloaded file */
576 in = fopen (tmp_file_name, "r");
577 out = fopen (list_file_name, "w");
586 unlink (tmp_file_name);
590 err = opkg_download (opkg->conf, url, list_file_name, NULL, NULL);
594 /* XXX: Error: download error */
599 /* download detached signitures to verify the package lists */
600 /* get the url for the sig file */
601 if (src->extra_data) /* debian style? */
602 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
605 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
607 /* create temporary file for it */
610 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
612 err = opkg_download (opkg->conf, url, tmp_file_name, NULL, NULL);
615 /* XXX: Warning: Download failed */
620 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
623 /* XXX: Notice: Signature check passed */
627 /* XXX: Warning: Signature check failed */
630 unlink (tmp_file_name);
631 free (tmp_file_name);
634 /* XXX: Note: Signiture check for %s skipped because GPG support was not
635 * enabled in this build
638 free (list_file_name);
641 progress (100 * sources_done / sources_list_count);