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
;
98 opkg
= malloc (sizeof (opkg_t
));
100 opkg
->args
= malloc (sizeof (args_t
));
101 args_init (opkg
->args
);
103 opkg
->conf
= malloc (sizeof (opkg_conf_t
));
104 opkg_conf_init (opkg
->conf
, opkg
->args
);
106 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
111 opkg_free (opkg_t
*opkg
)
113 opkg_assert (opkg
!= NULL
);
115 opkg_conf_deinit (opkg
->conf
);
116 args_deinit (opkg
->args
);
120 opkg_read_config_files (opkg_t
*opkg
)
125 opkg_assert (opkg
!= NULL
);
130 /* Unfortunatly, the easiest way to re-read the config files right now is to
131 * throw away opkg->conf and start again */
133 /* copy the settings we need to keep */
134 a
->autoremove
= c
->autoremove
;
135 a
->force_depends
= c
->force_depends
;
136 a
->force_defaults
= c
->force_defaults
;
137 a
->force_overwrite
= c
->force_overwrite
;
138 a
->force_downgrade
= c
->force_downgrade
;
139 a
->force_reinstall
= c
->force_reinstall
;
140 a
->force_removal_of_dependent_packages
= c
->force_removal_of_dependent_packages
;
141 a
->force_removal_of_essential_packages
= c
->force_removal_of_essential_packages
;
142 a
->nodeps
= c
->nodeps
;
143 a
->noaction
= c
->noaction
;
144 a
->query_all
= c
->query_all
;
145 a
->multiple_providers
= c
->multiple_providers
;
146 a
->verbosity
= c
->verbosity
;
150 if (a
->offline_root
) free (a
->offline_root
);
151 a
->offline_root
= strdup (c
->offline_root
);
154 if (c
->offline_root_pre_script_cmd
)
156 if (a
->offline_root_pre_script_cmd
) free (a
->offline_root_pre_script_cmd
);
157 a
->offline_root_pre_script_cmd
= strdup (c
->offline_root_pre_script_cmd
);
160 if (c
->offline_root_post_script_cmd
)
162 if (a
->offline_root_post_script_cmd
) free (a
->offline_root_post_script_cmd
);
163 a
->offline_root_post_script_cmd
= strdup (c
->offline_root_post_script_cmd
);
166 /* throw away old opkg_conf and start again */
167 opkg_conf_deinit (opkg
->conf
);
168 opkg_conf_init (opkg
->conf
, opkg
->args
);
170 free (opkg
->options
);
171 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
177 opkg_get_option (opkg_t
*opkg
, char *option
, void **value
)
180 opkg_option_t
*options
;
182 opkg_assert (opkg
!= NULL
);
183 opkg_assert (option
!= NULL
);
184 opkg_assert (value
!= NULL
);
186 options
= opkg
->options
;
188 /* look up the option
189 * TODO: this would be much better as a hash table
191 while (options
[i
].name
)
193 if (strcmp (options
[i
].name
, option
) != 0)
201 switch (options
[i
].type
)
203 case OPKG_OPT_TYPE_BOOL
:
204 *((int *) value
) = *((int *) options
[i
].value
);
207 case OPKG_OPT_TYPE_INT
:
208 *((int *) value
) = *((int *) options
[i
].value
);
211 case OPKG_OPT_TYPE_STRING
:
212 *((char **)value
) = strdup (options
[i
].value
);
219 opkg_set_option (opkg_t
*opkg
, char *option
, void *value
)
222 opkg_option_t
*options
;
224 opkg_assert (opkg
!= NULL
);
225 opkg_assert (option
!= NULL
);
226 opkg_assert (value
!= NULL
);
228 options
= opkg
->options
;
230 /* look up the option
231 * TODO: this would be much better as a hash table
233 while (options
[i
].name
)
235 if (strcmp (options
[i
].name
, option
) == 0)
243 switch (options
[i
].type
)
245 case OPKG_OPT_TYPE_BOOL
:
246 if (*((int *) value
) == 0)
247 *((int *)options
[i
].value
) = 0;
249 *((int *)options
[i
].value
) = 1;
252 case OPKG_OPT_TYPE_INT
:
253 *((int *) options
[i
].value
) = *((int *) value
);
256 case OPKG_OPT_TYPE_STRING
:
257 *((char **)options
[i
].value
) = strdup (value
);
264 opkg_install_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
267 char *package_id
= NULL
;
269 opkg_assert (opkg
!= NULL
);
270 opkg_assert (package_name
!= NULL
);
274 /* download the package */
275 opkg_prepare_url_for_install (opkg
->conf
, package_name
, &package_id
);
280 pkg_info_preinstall_check (opkg
->conf
);
283 package_id
= strdup (package_name
);
285 /* unpack the package */
286 if (opkg
->conf
->multiple_providers
)
288 err
= opkg_install_multi_by_name (opkg
->conf
, package_id
);
292 err
= opkg_install_by_name (opkg
->conf
, package_id
);
300 /* run configure scripts, etc. */
301 err
= opkg_configure_packages (opkg
->conf
, NULL
);
305 /* write out status files and file lists */
306 opkg_conf_write_status_files (opkg
->conf
);
307 pkg_write_changed_filelists (opkg
->conf
);
314 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
317 pkg_t
*pkg_to_remove
;
319 opkg_assert (opkg
!= NULL
);
320 opkg_assert (package_name
!= NULL
);
324 pkg_info_preinstall_check (opkg
->conf
);
326 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc ();
328 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed_pkgs
);
332 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
336 /* XXX: Error: Package not installed. */
340 if (pkg
->state_status
== SS_NOT_INSTALLED
)
342 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
348 if (opkg
->conf
->restrict_to_default_dest
)
350 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
352 opkg
->conf
->default_dest
);
356 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
362 opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
364 /* write out status files and file lists */
365 opkg_conf_write_status_files (opkg
->conf
);
366 pkg_write_changed_filelists (opkg
->conf
);
374 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
380 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
386 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
391 pkg_src_list_elt_t
*iter
;
393 int sources_list_count
, sources_done
;
395 opkg_assert (opkg
!= NULL
);
399 sprintf_alloc (&lists_dir
, "%s",
400 (opkg
->conf
->restrict_to_default_dest
)
401 ? opkg
->conf
->default_dest
->lists_dir
402 : opkg
->conf
->lists_dir
);
404 if (!file_is_dir (lists_dir
))
406 if (file_exists (lists_dir
))
408 /* XXX: Error: file exists but is not a directory */
413 err
= file_mkdir_hier (lists_dir
, 0755);
416 /* XXX: Error: failed to create directory */
422 tmp
= strdup ("/tmp/opkg.XXXXXX");
424 if (mkdtemp (tmp
) == NULL
)
426 /* XXX: Error: could not create temporary file name */
432 /* cout the number of sources so we can give some progress updates */
433 sources_list_count
= 0;
435 iter
= opkg
->conf
->pkg_src_list
.head
;
438 sources_list_count
++;
442 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
444 char *url
, *list_file_name
;
448 if (src
->extra_data
) /* debian style? */
449 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
450 src
->gzip
? "Packages.gz" : "Packages");
452 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
454 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
460 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
462 /* XXX: Note: downloading url */
463 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
);
467 /* XXX: Note: Inflating downloaded file */
468 in
= fopen (tmp_file_name
, "r");
469 out
= fopen (list_file_name
, "w");
478 unlink (tmp_file_name
);
482 err
= opkg_download (opkg
->conf
, url
, list_file_name
);
486 /* XXX: Error: download error */
491 /* download detached signitures to verify the package lists */
492 /* get the url for the sig file */
493 if (src
->extra_data
) /* debian style? */
494 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
497 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
499 /* create temporary file for it */
502 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
504 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
);
507 /* XXX: Warning: Download failed */
512 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
515 /* XXX: Notice: Signature check passed */
519 /* XXX: Warning: Signature check failed */
522 unlink (tmp_file_name
);
523 free (tmp_file_name
);
526 /* XXX: Note: Signiture check for %s skipped because GPG support was not
527 * enabled in this build
530 free (list_file_name
);
533 progress (100 * sources_done
/ sources_list_count
);