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
)
298 int i
= 0, found
= 0;
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)
322 /* XXX: Warning: Option not found */
327 switch (options
[i
].type
)
329 case OPKG_OPT_TYPE_BOOL
:
330 if (*((int *) value
) == 0)
331 *((int *)options
[i
].value
) = 0;
333 *((int *)options
[i
].value
) = 1;
336 case OPKG_OPT_TYPE_INT
:
337 *((int *) options
[i
].value
) = *((int *) value
);
340 case OPKG_OPT_TYPE_STRING
:
341 *((char **)options
[i
].value
) = strdup (value
);
348 opkg_install_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
351 char *package_id
= NULL
;
353 opkg_assert (opkg
!= NULL
);
354 opkg_assert (package_name
!= NULL
);
358 /* download the package */
359 opkg_prepare_url_for_install (opkg
->conf
, package_name
, &package_id
);
364 pkg_info_preinstall_check (opkg
->conf
);
367 package_id
= strdup (package_name
);
369 /* unpack the package */
370 if (opkg
->conf
->multiple_providers
)
372 err
= opkg_install_multi_by_name (opkg
->conf
, package_id
);
376 err
= opkg_install_by_name (opkg
->conf
, package_id
);
384 /* run configure scripts, etc. */
385 err
= opkg_configure_packages (opkg
->conf
, NULL
);
389 /* write out status files and file lists */
390 opkg_conf_write_status_files (opkg
->conf
);
391 pkg_write_changed_filelists (opkg
->conf
);
398 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
401 pkg_t
*pkg_to_remove
;
403 opkg_assert (opkg
!= NULL
);
404 opkg_assert (package_name
!= NULL
);
408 pkg_info_preinstall_check (opkg
->conf
);
410 pkg_vec_t
*installed_pkgs
= pkg_vec_alloc ();
412 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed_pkgs
);
416 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
420 /* XXX: Error: Package not installed. */
424 if (pkg
->state_status
== SS_NOT_INSTALLED
)
426 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
432 if (opkg
->conf
->restrict_to_default_dest
)
434 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
436 opkg
->conf
->default_dest
);
440 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
446 opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
448 /* write out status files and file lists */
449 opkg_conf_write_status_files (opkg
->conf
);
450 pkg_write_changed_filelists (opkg
->conf
);
458 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
462 opkg_assert (opkg
!= NULL
);
463 opkg_assert (package_name
!= NULL
);
467 pkg_info_preinstall_check (opkg
->conf
);
469 if (opkg
->conf
->restrict_to_default_dest
)
471 pkg
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
473 opkg
->conf
->default_dest
);
476 /* XXX: Error: Package not installed in default_dest */
482 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
,
488 /* XXX: Error: Package not installed */
494 opkg_upgrade_pkg (opkg
->conf
, pkg
);
497 opkg_configure_packages (opkg
->conf
, NULL
);
503 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
505 pkg_vec_t
*installed
;
510 opkg_assert (opkg
!= NULL
);
513 installed
= pkg_vec_alloc ();
514 pkg_info_preinstall_check (opkg
->conf
);
516 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed
);
517 for (i
= 0; i
< installed
->len
; i
++)
519 pkg
= installed
->pkgs
[i
];
520 err
+= opkg_upgrade_pkg (opkg
->conf
, pkg
);
521 progress (100 * i
/ installed
->len
);
523 pkg_vec_free (installed
);
528 err
= opkg_configure_packages (opkg
->conf
, NULL
);
537 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
542 pkg_src_list_elt_t
*iter
;
544 int sources_list_count
, sources_done
;
546 opkg_assert (opkg
!= NULL
);
550 sprintf_alloc (&lists_dir
, "%s",
551 (opkg
->conf
->restrict_to_default_dest
)
552 ? opkg
->conf
->default_dest
->lists_dir
553 : opkg
->conf
->lists_dir
);
555 if (!file_is_dir (lists_dir
))
557 if (file_exists (lists_dir
))
559 /* XXX: Error: file exists but is not a directory */
564 err
= file_mkdir_hier (lists_dir
, 0755);
567 /* XXX: Error: failed to create directory */
573 tmp
= strdup ("/tmp/opkg.XXXXXX");
575 if (mkdtemp (tmp
) == NULL
)
577 /* XXX: Error: could not create temporary file name */
583 /* cout the number of sources so we can give some progress updates */
584 sources_list_count
= 0;
586 iter
= opkg
->conf
->pkg_src_list
.head
;
589 sources_list_count
++;
593 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
595 char *url
, *list_file_name
;
599 if (src
->extra_data
) /* debian style? */
600 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
601 src
->gzip
? "Packages.gz" : "Packages");
603 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
605 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
610 struct _curl_cb_data cb_data
;
612 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
614 /* XXX: Note: downloading url */
616 cb_data
.cb
= progress_callback
;
618 cb_data
.user_data
= user_data
;
619 cb_data
.start_range
= 100 * sources_done
/ sources_list_count
;
620 cb_data
.finish_range
= 100 * (sources_done
+ 1) / sources_list_count
;
622 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, (curl_progress_func
) curl_progress_cb
, &cb_data
);
626 /* XXX: Note: Inflating downloaded file */
627 in
= fopen (tmp_file_name
, "r");
628 out
= fopen (list_file_name
, "w");
637 unlink (tmp_file_name
);
641 err
= opkg_download (opkg
->conf
, url
, list_file_name
, NULL
, NULL
);
645 /* XXX: Error: download error */
650 /* download detached signitures to verify the package lists */
651 /* get the url for the sig file */
652 if (src
->extra_data
) /* debian style? */
653 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
656 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
658 /* create temporary file for it */
661 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
663 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, NULL
, NULL
);
666 /* XXX: Warning: Download failed */
671 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
674 /* XXX: Notice: Signature check passed */
678 /* XXX: Warning: Signature check failed */
681 unlink (tmp_file_name
);
682 free (tmp_file_name
);
685 /* XXX: Note: Signiture check for %s skipped because GPG support was not
686 * enabled in this build
689 free (list_file_name
);
692 progress (100 * sources_done
/ sources_list_count
);
704 opkg_list_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
710 opkg_assert (callback
);
712 all
= pkg_vec_alloc ();
713 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
714 for (i
= 0; i
< all
->len
; i
++)
717 opkg_package_t
*package
;
721 package
= opkg_package_new_with_values (
727 (pkg
->state_status
== SS_INSTALLED
));
729 callback (opkg
, package
, user_data
);