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(percent) if (progress_callback) progress_callback (opkg, percent, user_data);
49 /** Private Functions ***/
53 opkg_configure_packages(opkg_conf_t
*conf
, char *pkg_name
)
60 all
= pkg_vec_alloc ();
61 pkg_hash_fetch_available (&conf
->pkg_hash
, all
);
63 for (i
= 0; i
< all
->len
; i
++)
67 if (pkg_name
&& fnmatch (pkg_name
, pkg
->name
, 0))
70 if (pkg
->state_status
== SS_UNPACKED
)
72 r
= opkg_configure (conf
, pkg
);
75 pkg
->state_status
= SS_INSTALLED
;
76 pkg
->parent
->state_status
= SS_INSTALLED
;
77 pkg
->state_flag
&= ~SF_PREFER
;
93 opkg_progress_callback_t cb
;
101 curl_progress_cb (struct _curl_cb_data
*cb_data
,
102 double t
, /* dltotal */
103 double d
, /* dlnow */
107 int p
= (t
) ? d
* 100 / t
: 0;
108 static int prev
= -1;
110 /* prevent the same value being sent twice (can occur due to rounding) */
118 (cb_data
->cb
)(cb_data
->opkg
,
119 cb_data
->start_range
+ (d
/ t
* ((cb_data
->finish_range
- cb_data
->start_range
))),
134 p
= malloc (sizeof (opkg_package_t
));
135 memset (p
, 0, sizeof (opkg_package_t
));
141 opkg_package_new_with_values (const char *name
, const char *version
,
142 const char *arch
, const char *desc
, const char *tags
, int installed
)
144 opkg_package_t
*package
;
145 package
= opkg_package_new ();
147 #define sstrdup(x) (x) ? strdup (x) : NULL;
149 package
->name
= sstrdup (name
);
150 package
->version
= sstrdup (version
);
151 package
->architecture
= sstrdup (arch
);
152 package
->description
= sstrdup (desc
);
153 package
->tags
= sstrdup (tags
);
154 package
->installed
= (installed
!= 0);
160 opkg_package_free (opkg_package_t
*p
)
164 free (p
->architecture
);
165 free (p
->description
);
175 opkg
= malloc (sizeof (opkg_t
));
177 opkg
->args
= malloc (sizeof (args_t
));
178 args_init (opkg
->args
);
180 opkg
->conf
= malloc (sizeof (opkg_conf_t
));
181 opkg_conf_init (opkg
->conf
, opkg
->args
);
183 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
188 opkg_free (opkg_t
*opkg
)
190 opkg_assert (opkg
!= NULL
);
192 opkg_conf_deinit (opkg
->conf
);
193 args_deinit (opkg
->args
);
197 opkg_read_config_files (opkg_t
*opkg
)
202 opkg_assert (opkg
!= NULL
);
207 /* Unfortunatly, the easiest way to re-read the config files right now is to
208 * throw away opkg->conf and start again */
210 /* copy the settings we need to keep */
211 a
->autoremove
= c
->autoremove
;
212 a
->force_depends
= c
->force_depends
;
213 a
->force_defaults
= c
->force_defaults
;
214 a
->force_overwrite
= c
->force_overwrite
;
215 a
->force_downgrade
= c
->force_downgrade
;
216 a
->force_reinstall
= c
->force_reinstall
;
217 a
->force_removal_of_dependent_packages
= c
->force_removal_of_dependent_packages
;
218 a
->force_removal_of_essential_packages
= c
->force_removal_of_essential_packages
;
219 a
->nodeps
= c
->nodeps
;
220 a
->noaction
= c
->noaction
;
221 a
->query_all
= c
->query_all
;
222 a
->multiple_providers
= c
->multiple_providers
;
223 a
->verbosity
= c
->verbosity
;
227 if (a
->offline_root
) free (a
->offline_root
);
228 a
->offline_root
= strdup (c
->offline_root
);
231 if (c
->offline_root_pre_script_cmd
)
233 if (a
->offline_root_pre_script_cmd
) free (a
->offline_root_pre_script_cmd
);
234 a
->offline_root_pre_script_cmd
= strdup (c
->offline_root_pre_script_cmd
);
237 if (c
->offline_root_post_script_cmd
)
239 if (a
->offline_root_post_script_cmd
) free (a
->offline_root_post_script_cmd
);
240 a
->offline_root_post_script_cmd
= strdup (c
->offline_root_post_script_cmd
);
243 /* throw away old opkg_conf and start again */
244 opkg_conf_deinit (opkg
->conf
);
245 opkg_conf_init (opkg
->conf
, opkg
->args
);
247 free (opkg
->options
);
248 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
254 opkg_get_option (opkg_t
*opkg
, char *option
, void **value
)
257 opkg_option_t
*options
;
259 opkg_assert (opkg
!= NULL
);
260 opkg_assert (option
!= NULL
);
261 opkg_assert (value
!= NULL
);
263 options
= opkg
->options
;
265 /* look up the option
266 * TODO: this would be much better as a hash table
268 while (options
[i
].name
)
270 if (strcmp (options
[i
].name
, option
) != 0)
278 switch (options
[i
].type
)
280 case OPKG_OPT_TYPE_BOOL
:
281 *((int *) value
) = *((int *) options
[i
].value
);
284 case OPKG_OPT_TYPE_INT
:
285 *((int *) value
) = *((int *) options
[i
].value
);
288 case OPKG_OPT_TYPE_STRING
:
289 *((char **)value
) = strdup (options
[i
].value
);
296 opkg_set_option (opkg_t
*opkg
, char *option
, void *value
)
299 opkg_option_t
*options
;
301 opkg_assert (opkg
!= NULL
);
302 opkg_assert (option
!= NULL
);
303 opkg_assert (value
!= NULL
);
305 options
= opkg
->options
;
307 /* look up the option
308 * TODO: this would be much better as a hash table
310 while (options
[i
].name
)
312 if (strcmp (options
[i
].name
, option
) == 0)
320 switch (options
[i
].type
)
322 case OPKG_OPT_TYPE_BOOL
:
323 if (*((int *) value
) == 0)
324 *((int *)options
[i
].value
) = 0;
326 *((int *)options
[i
].value
) = 1;
329 case OPKG_OPT_TYPE_INT
:
330 *((int *) options
[i
].value
) = *((int *) value
);
333 case OPKG_OPT_TYPE_STRING
:
334 *((char **)options
[i
].value
) = strdup (value
);
341 opkg_install_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
344 char *package_id
= NULL
;
346 opkg_assert (opkg
!= NULL
);
347 opkg_assert (package_name
!= NULL
);
351 /* download the package */
352 opkg_prepare_url_for_install (opkg
->conf
, package_name
, &package_id
);
357 pkg_info_preinstall_check (opkg
->conf
);
360 package_id
= strdup (package_name
);
362 /* unpack the package */
363 if (opkg
->conf
->multiple_providers
)
365 err
= opkg_install_multi_by_name (opkg
->conf
, package_id
);
369 err
= opkg_install_by_name (opkg
->conf
, package_id
);
377 /* run configure scripts, etc. */
378 err
= opkg_configure_packages (opkg
->conf
, NULL
);
382 /* write out status files and file lists */
383 opkg_conf_write_status_files (opkg
->conf
);
384 pkg_write_changed_filelists (opkg
->conf
);
391 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
394 pkg_t
*pkg_to_remove
;
396 opkg_assert (opkg
!= NULL
);
397 opkg_assert (package_name
!= NULL
);
401 pkg_info_preinstall_check (opkg
->conf
);
403 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc ();
405 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed_pkgs
);
409 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
413 /* XXX: Error: Package not installed. */
417 if (pkg
->state_status
== SS_NOT_INSTALLED
)
419 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
425 if (opkg
->conf
->restrict_to_default_dest
)
427 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
429 opkg
->conf
->default_dest
);
433 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
439 opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
441 /* write out status files and file lists */
442 opkg_conf_write_status_files (opkg
->conf
);
443 pkg_write_changed_filelists (opkg
->conf
);
451 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
455 opkg_assert (opkg
!= NULL
);
456 opkg_assert (package_name
!= NULL
);
460 pkg_info_preinstall_check (opkg
->conf
);
462 if (opkg
->conf
->restrict_to_default_dest
)
464 pkg
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
466 opkg
->conf
->default_dest
);
469 /* XXX: Error: Package not installed in default_dest */
475 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
,
481 /* XXX: Error: Package not installed */
487 opkg_upgrade_pkg (opkg
->conf
, pkg
);
490 opkg_configure_packages (opkg
->conf
, NULL
);
496 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
498 pkg_vec_t
*installed
;
503 opkg_assert (opkg
!= NULL
);
506 installed
= pkg_vec_alloc ();
507 pkg_info_preinstall_check (opkg
->conf
);
509 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed
);
510 for (i
= 0; i
< installed
->len
; i
++)
512 pkg
= installed
->pkgs
[i
];
513 err
+= opkg_upgrade_pkg (opkg
->conf
, pkg
);
514 progress (100 * i
/ installed
->len
);
516 pkg_vec_free (installed
);
521 err
= opkg_configure_packages (opkg
->conf
, NULL
);
530 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
535 pkg_src_list_elt_t
*iter
;
537 int sources_list_count
, sources_done
;
539 opkg_assert (opkg
!= NULL
);
543 sprintf_alloc (&lists_dir
, "%s",
544 (opkg
->conf
->restrict_to_default_dest
)
545 ? opkg
->conf
->default_dest
->lists_dir
546 : opkg
->conf
->lists_dir
);
548 if (!file_is_dir (lists_dir
))
550 if (file_exists (lists_dir
))
552 /* XXX: Error: file exists but is not a directory */
557 err
= file_mkdir_hier (lists_dir
, 0755);
560 /* XXX: Error: failed to create directory */
566 tmp
= strdup ("/tmp/opkg.XXXXXX");
568 if (mkdtemp (tmp
) == NULL
)
570 /* XXX: Error: could not create temporary file name */
576 /* cout the number of sources so we can give some progress updates */
577 sources_list_count
= 0;
579 iter
= opkg
->conf
->pkg_src_list
.head
;
582 sources_list_count
++;
586 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
588 char *url
, *list_file_name
;
592 if (src
->extra_data
) /* debian style? */
593 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
594 src
->gzip
? "Packages.gz" : "Packages");
596 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
598 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
603 struct _curl_cb_data cb_data
;
605 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
607 /* XXX: Note: downloading url */
609 cb_data
.cb
= progress_callback
;
611 cb_data
.user_data
= user_data
;
612 cb_data
.start_range
= 100 * sources_done
/ sources_list_count
;
613 cb_data
.finish_range
= 100 * (sources_done
+ 1) / sources_list_count
;
615 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, (curl_progress_func
) curl_progress_cb
, &cb_data
);
619 /* XXX: Note: Inflating downloaded file */
620 in
= fopen (tmp_file_name
, "r");
621 out
= fopen (list_file_name
, "w");
630 unlink (tmp_file_name
);
634 err
= opkg_download (opkg
->conf
, url
, list_file_name
, NULL
, NULL
);
638 /* XXX: Error: download error */
643 /* download detached signitures to verify the package lists */
644 /* get the url for the sig file */
645 if (src
->extra_data
) /* debian style? */
646 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
649 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
651 /* create temporary file for it */
654 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
656 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, NULL
, NULL
);
659 /* XXX: Warning: Download failed */
664 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
667 /* XXX: Notice: Signature check passed */
671 /* XXX: Warning: Signature check failed */
674 unlink (tmp_file_name
);
675 free (tmp_file_name
);
678 /* XXX: Note: Signiture check for %s skipped because GPG support was not
679 * enabled in this build
682 free (list_file_name
);
685 progress (100 * sources_done
/ sources_list_count
);
697 opkg_list_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
703 opkg_assert (callback
);
705 all
= pkg_vec_alloc ();
706 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
707 for (i
= 0; i
< all
->len
; i
++)
710 opkg_package_t
*package
;
714 package
= opkg_package_new_with_values (
720 (pkg
->state_status
== SS_INSTALLED
));
722 callback (opkg
, package
, user_data
);