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 /** Private Functions ***/
46 opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
53 all = pkg_vec_alloc ();
54 pkg_hash_fetch_available (&conf->pkg_hash, all);
56 for (i = 0; i < all->len; i++)
60 if (pkg_name && fnmatch (pkg_name, pkg->name, 0))
63 if (pkg->state_status == SS_UNPACKED)
65 r = opkg_configure (conf, pkg);
68 pkg->state_status = SS_INSTALLED;
69 pkg->parent->state_status = SS_INSTALLED;
70 pkg->state_flag &= ~SF_PREFER;
92 opkg = malloc (sizeof (opkg_t));
94 opkg->args = malloc (sizeof (args_t));
95 args_init (opkg->args);
97 opkg->conf = malloc (sizeof (opkg_conf_t));
98 opkg_conf_init (opkg->conf, opkg->args);
100 opkg_init_options_array (opkg->conf, &opkg->options);
105 opkg_free (opkg_t *opkg)
107 opkg_conf_deinit (opkg->conf);
108 args_deinit (opkg->args);
112 opkg_read_config_files (opkg_t *opkg)
114 args_t *a = opkg->args;
115 opkg_conf_t *c = opkg->conf;
117 /* Unfortunatly, the easiest way to re-read the config files right now is to
118 * throw away opkg->conf and start again */
120 /* copy the settings we need to keep */
121 a->autoremove = c->autoremove;
122 a->force_depends = c->force_depends;
123 a->force_defaults = c->force_defaults;
124 a->force_overwrite = c->force_overwrite;
125 a->force_downgrade = c->force_downgrade;
126 a->force_reinstall = c->force_reinstall;
127 a->force_removal_of_dependent_packages = c->force_removal_of_dependent_packages;
128 a->force_removal_of_essential_packages = c->force_removal_of_essential_packages;
129 a->nodeps = c->nodeps;
130 a->noaction = c->noaction;
131 a->query_all = c->query_all;
132 a->multiple_providers = c->multiple_providers;
133 a->verbosity = c->verbosity;
137 if (a->offline_root) free (a->offline_root);
138 a->offline_root = strdup (c->offline_root);
141 if (c->offline_root_pre_script_cmd)
143 if (a->offline_root_pre_script_cmd) free (a->offline_root_pre_script_cmd);
144 a->offline_root_pre_script_cmd = strdup (c->offline_root_pre_script_cmd);
147 if (c->offline_root_post_script_cmd)
149 if (a->offline_root_post_script_cmd) free (a->offline_root_post_script_cmd);
150 a->offline_root_post_script_cmd = strdup (c->offline_root_post_script_cmd);
153 /* throw away old opkg_conf and start again */
154 opkg_conf_deinit (opkg->conf);
155 opkg_conf_init (opkg->conf, opkg->args);
157 free (opkg->options);
158 opkg_init_options_array (opkg->conf, &opkg->options);
164 opkg_get_option (opkg_t *opkg, char *option, void **value)
167 opkg_option_t *options = opkg->options;
169 /* can't store a value in a NULL pointer! */
173 /* look up the option
174 * TODO: this would be much better as a hash table
176 while (options[i].name)
178 if (strcmp (options[i].name, option) != 0)
186 switch (options[i].type)
188 case OPKG_OPT_TYPE_BOOL:
189 *((int *) value) = *((int *) options[i].value);
192 case OPKG_OPT_TYPE_INT:
193 *((int *) value) = *((int *) options[i].value);
196 case OPKG_OPT_TYPE_STRING:
197 *((char **)value) = strdup (options[i].value);
204 opkg_set_option (opkg_t *opkg, char *option, void *value)
207 opkg_option_t *options = opkg->options;
209 /* NULL values are not defined */
213 /* look up the option
214 * TODO: this would be much better as a hash table
216 while (options[i].name)
218 if (strcmp (options[i].name, option) == 0)
226 switch (options[i].type)
228 case OPKG_OPT_TYPE_BOOL:
229 if (*((int *) value) == 0)
230 *((int *)options[i].value) = 0;
232 *((int *)options[i].value) = 1;
235 case OPKG_OPT_TYPE_INT:
236 *((int *) options[i].value) = *((int *) value);
239 case OPKG_OPT_TYPE_STRING:
240 *((char **)options[i].value) = strdup (value);
247 opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
250 char *package_id = NULL;
252 progress_callback (opkg, 0, user_data);
254 /* download the package */
255 opkg_prepare_url_for_install (opkg->conf, package_name, &package_id);
256 progress_callback (opkg, 50, user_data);
259 pkg_info_preinstall_check (opkg->conf);
262 package_id = strdup (package_name);
264 /* unpack the package */
265 if (opkg->conf->multiple_providers)
267 err = opkg_install_multi_by_name (opkg->conf, package_id);
271 err = opkg_install_by_name (opkg->conf, package_id);
277 progress_callback (opkg, 75, user_data);
279 /* run configure scripts, etc. */
280 err = opkg_configure_packages (opkg->conf, NULL);
284 /* write out status files and file lists */
285 opkg_conf_write_status_files (opkg->conf);
286 pkg_write_changed_filelists (opkg->conf);
288 progress_callback (opkg, 100, user_data);
293 opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
296 pkg_t *pkg_to_remove;
304 progress_callback (opkg, 0, user_data);
306 pkg_info_preinstall_check (opkg->conf);
308 pkg_vec_t *installed_pkgs = pkg_vec_alloc ();
310 pkg_hash_fetch_all_installed (&opkg->conf->pkg_hash, installed_pkgs);
312 progress_callback (opkg, 25, user_data);
314 pkg = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, package_name);
318 /* XXX: Error: Package not installed. */
322 if (pkg->state_status == SS_NOT_INSTALLED)
324 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
328 progress_callback (opkg, 75, user_data);
330 if (opkg->conf->restrict_to_default_dest)
332 pkg_to_remove = pkg_hash_fetch_installed_by_name_dest (&opkg->conf->pkg_hash,
334 opkg->conf->default_dest);
338 pkg_to_remove = pkg_hash_fetch_installed_by_name (&opkg->conf->pkg_hash, pkg->name );
342 progress_callback (opkg, 75, user_data);
344 opkg_remove_pkg (opkg->conf, pkg_to_remove, 0);
346 /* write out status files and file lists */
347 opkg_conf_write_status_files (opkg->conf);
348 pkg_write_changed_filelists (opkg->conf);
351 progress_callback (opkg, 100, user_data);
356 opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data)
362 opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
368 opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data)
373 pkg_src_list_elt_t *iter;
375 int sources_list_count, sources_done;
377 progress_callback (opkg, 0, user_data);
379 sprintf_alloc (&lists_dir, "%s",
380 (opkg->conf->restrict_to_default_dest)
381 ? opkg->conf->default_dest->lists_dir
382 : opkg->conf->lists_dir);
384 if (!file_is_dir (lists_dir))
386 if (file_exists (lists_dir))
388 /* XXX: Error: file exists but is not a directory */
393 err = file_mkdir_hier (lists_dir, 0755);
396 /* XXX: Error: failed to create directory */
402 tmp = strdup ("/tmp/opkg.XXXXXX");
404 if (mkdtemp (tmp) == NULL)
406 /* XXX: Error: could not create temporary file name */
412 /* cout the number of sources so we can give some progress updates */
413 sources_list_count = 0;
415 iter = opkg->conf->pkg_src_list.head;
418 sources_list_count++;
422 for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next)
424 char *url, *list_file_name;
428 if (src->extra_data) /* debian style? */
429 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
430 src->gzip ? "Packages.gz" : "Packages");
432 sprintf_alloc (&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
434 sprintf_alloc (&list_file_name, "%s/%s", lists_dir, src->name);
440 sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
442 /* XXX: Note: downloading url */
443 err = opkg_download (opkg->conf, url, tmp_file_name);
447 /* XXX: Note: Inflating downloaded file */
448 in = fopen (tmp_file_name, "r");
449 out = fopen (list_file_name, "w");
458 unlink (tmp_file_name);
462 err = opkg_download (opkg->conf, url, list_file_name);
466 /* XXX: Error: download error */
471 /* download detached signitures to verify the package lists */
472 /* get the url for the sig file */
473 if (src->extra_data) /* debian style? */
474 sprintf_alloc (&url, "%s/%s/%s", src->value, src->extra_data,
477 sprintf_alloc (&url, "%s/%s", src->value, "Packages.sig");
479 /* create temporary file for it */
482 sprintf_alloc (&tmp_file_name, "%s/%s", tmp, "Packages.sig");
484 err = opkg_download (opkg->conf, url, tmp_file_name);
487 /* XXX: Warning: Download failed */
492 err = opkg_verify_file (opkg->conf, list_file_name, tmp_file_name);
495 /* XXX: Notice: Signature check passed */
499 /* XXX: Warning: Signature check failed */
502 unlink (tmp_file_name);
503 free (tmp_file_name);
506 /* XXX: Note: Signiture check for %s skipped because GPG support was not
507 * enabled in this build
510 free (list_file_name);
513 progress_callback (opkg, 100 * sources_done / sources_list_count, user_data);