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"
29 #include "sprintf_alloc.h"
30 #include "file_util.h"
32 #include <libbb/libbb.h>
38 opkg_option_t
*options
;
41 /** Private Functions ***/
45 opkg_configure_packages(opkg_conf_t
*conf
, char *pkg_name
)
52 all
= pkg_vec_alloc ();
53 pkg_hash_fetch_available (&conf
->pkg_hash
, all
);
55 for (i
= 0; i
< all
->len
; i
++)
59 if (pkg_name
&& fnmatch (pkg_name
, pkg
->name
, 0))
62 if (pkg
->state_status
== SS_UNPACKED
)
64 r
= opkg_configure (conf
, pkg
);
67 pkg
->state_status
= SS_INSTALLED
;
68 pkg
->parent
->state_status
= SS_INSTALLED
;
69 pkg
->state_flag
&= ~SF_PREFER
;
91 opkg
= malloc (sizeof (opkg_t
));
93 opkg
->args
= malloc (sizeof (args_t
));
94 args_init (opkg
->args
);
96 opkg
->conf
= malloc (sizeof (opkg_conf_t
));
97 opkg_conf_init (opkg
->conf
, opkg
->args
);
99 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
104 opkg_free (opkg_t
*opkg
)
106 opkg_conf_deinit (opkg
->conf
);
107 args_deinit (opkg
->args
);
111 opkg_read_config_files (opkg_t
*opkg
)
113 args_t
*a
= opkg
->args
;
114 opkg_conf_t
*c
= opkg
->conf
;
116 /* Unfortunatly, the easiest way to re-read the config files right now is to
117 * throw away opkg->conf and start again */
119 /* copy the settings we need to keep */
120 a
->autoremove
= c
->autoremove
;
121 a
->force_depends
= c
->force_depends
;
122 a
->force_defaults
= c
->force_defaults
;
123 a
->force_overwrite
= c
->force_overwrite
;
124 a
->force_downgrade
= c
->force_downgrade
;
125 a
->force_reinstall
= c
->force_reinstall
;
126 a
->force_removal_of_dependent_packages
= c
->force_removal_of_dependent_packages
;
127 a
->force_removal_of_essential_packages
= c
->force_removal_of_essential_packages
;
128 a
->nodeps
= c
->nodeps
;
129 a
->noaction
= c
->noaction
;
130 a
->query_all
= c
->query_all
;
131 a
->multiple_providers
= c
->multiple_providers
;
132 a
->verbosity
= c
->verbosity
;
136 if (a
->offline_root
) free (a
->offline_root
);
137 a
->offline_root
= strdup (c
->offline_root
);
140 if (c
->offline_root_pre_script_cmd
)
142 if (a
->offline_root_pre_script_cmd
) free (a
->offline_root_pre_script_cmd
);
143 a
->offline_root_pre_script_cmd
= strdup (c
->offline_root_pre_script_cmd
);
146 if (c
->offline_root_post_script_cmd
)
148 if (a
->offline_root_post_script_cmd
) free (a
->offline_root_post_script_cmd
);
149 a
->offline_root_post_script_cmd
= strdup (c
->offline_root_post_script_cmd
);
152 /* throw away old opkg_conf and start again */
153 opkg_conf_deinit (opkg
->conf
);
154 opkg_conf_init (opkg
->conf
, opkg
->args
);
156 free (opkg
->options
);
157 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
163 opkg_get_option (opkg_t
*opkg
, char *option
, void **value
)
166 opkg_option_t
*options
= opkg
->options
;
168 /* can't store a value in a NULL pointer! */
172 /* look up the option
173 * TODO: this would be much better as a hash table
175 while (options
[i
].name
)
177 if (strcmp (options
[i
].name
, option
) != 0)
185 switch (options
[i
].type
)
187 case OPKG_OPT_TYPE_BOOL
:
188 *((int *) value
) = *((int *) options
[i
].value
);
191 case OPKG_OPT_TYPE_INT
:
192 *((int *) value
) = *((int *) options
[i
].value
);
195 case OPKG_OPT_TYPE_STRING
:
196 *((char **)value
) = strdup (options
[i
].value
);
203 opkg_set_option (opkg_t
*opkg
, char *option
, void *value
)
206 opkg_option_t
*options
= opkg
->options
;
208 /* NULL values are not defined */
212 /* look up the option
213 * TODO: this would be much better as a hash table
215 while (options
[i
].name
)
217 if (strcmp (options
[i
].name
, option
) == 0)
225 switch (options
[i
].type
)
227 case OPKG_OPT_TYPE_BOOL
:
228 if (*((int *) value
) == 0)
229 *((int *)options
[i
].value
) = 0;
231 *((int *)options
[i
].value
) = 1;
234 case OPKG_OPT_TYPE_INT
:
235 *((int *) options
[i
].value
) = *((int *) value
);
238 case OPKG_OPT_TYPE_STRING
:
239 *((char **)options
[i
].value
) = strdup (value
);
246 opkg_install_package (opkg_t
*opkg
, char *package_name
)
250 pkg_info_preinstall_check (opkg
->conf
);
252 if (opkg
->conf
->multiple_providers
)
254 err
= opkg_install_multi_by_name (opkg
->conf
, package_name
);
258 err
= opkg_install_by_name (opkg
->conf
, package_name
);
261 err
= opkg_configure_packages (opkg
->conf
, NULL
);
263 if (opkg
->conf
->noaction
)
266 opkg_conf_write_status_files (opkg
->conf
);
267 pkg_write_changed_filelists (opkg
->conf
);
273 opkg_remove_package (opkg_t
*opkg
, char *package_name
)
279 opkg_upgrade_package (opkg_t
*opkg
, char *package_name
)
285 opkg_upgrade_all (opkg_t
*opkg
)
291 opkg_update_package_lists (opkg_t
*opkg
)
296 pkg_src_list_elt_t
*iter
;
300 sprintf_alloc (&lists_dir
, "%s",
301 (opkg
->conf
->restrict_to_default_dest
)
302 ? opkg
->conf
->default_dest
->lists_dir
303 : opkg
->conf
->lists_dir
);
305 if (!file_is_dir (lists_dir
))
307 if (file_exists (lists_dir
))
309 /* XXX: Error: file exists but is not a directory */
314 err
= file_mkdir_hier (lists_dir
, 0755);
317 /* XXX: Error: failed to create directory */
323 tmp
= strdup ("/tmp/opkg.XXXXXX");
325 if (mkdtemp (tmp
) == NULL
)
327 /* XXX: Error: could not create temporary file name */
334 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
336 char *url
, *list_file_name
;
340 if (src
->extra_data
) /* debian style? */
341 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
342 src
->gzip
? "Packages.gz" : "Packages");
344 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
346 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
352 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
354 /* XXX: Note: downloading url */
355 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
);
359 /* XXX: Note: Inflating downloaded file */
360 in
= fopen (tmp_file_name
, "r");
361 out
= fopen (list_file_name
, "w");
370 unlink (tmp_file_name
);
374 err
= opkg_download (opkg
->conf
, url
, list_file_name
);
378 /* XXX: Error: download error */
383 /* download detached signitures to verify the package lists */
384 /* get the url for the sig file */
385 if (src
->extra_data
) /* debian style? */
386 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
389 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
391 /* create temporary file for it */
394 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
396 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
);
399 /* XXX: Warning: Download failed */
404 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
407 /* XXX: Notice: Signature check passed */
411 /* XXX: Warning: Signature check failed */
414 unlink (tmp_file_name
);
415 free (tmp_file_name
);
418 /* XXX: Note: Signiture check for %s skipped because GPG support was not
419 * enabled in this build
422 free (list_file_name
);