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(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
48 #define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
50 /** Private Functions ***/
52 static opkg_package_t
*
53 old_pkg_to_new (pkg_t
*old
)
57 new = opkg_package_new ();
59 #define sstrdup(x) (x) ? strdup (x) : NULL;
61 new->name
= sstrdup (old
->name
);
62 new->version
= pkg_version_str_alloc (old
);
63 new->architecture
= sstrdup (old
->architecture
);
65 new->repository
= sstrdup (old
->src
->name
);
66 new->description
= sstrdup (old
->description
);
67 new->tags
= sstrdup (old
->tags
);
68 new->url
= sstrdup (old
->url
);
70 new->size
= (old
->size
) ? atoi (old
->size
) : 0;
71 new->installed
= (old
->state_status
== SS_INSTALLED
);
77 opkg_configure_packages(opkg_conf_t
*conf
, char *pkg_name
)
84 all
= pkg_vec_alloc ();
85 pkg_hash_fetch_available (&conf
->pkg_hash
, all
);
87 for (i
= 0; i
< all
->len
; i
++)
91 if (pkg_name
&& fnmatch (pkg_name
, pkg
->name
, 0))
94 if (pkg
->state_status
== SS_UNPACKED
)
96 r
= opkg_configure (conf
, pkg
);
99 pkg
->state_status
= SS_INSTALLED
;
100 pkg
->parent
->state_status
= SS_INSTALLED
;
101 pkg
->state_flag
&= ~SF_PREFER
;
117 opkg_progress_callback_t cb
;
118 opkg_progress_data_t
*progress_data
;
126 curl_progress_cb (struct _curl_cb_data
*cb_data
,
127 double t
, /* dltotal */
128 double d
, /* dlnow */
132 int p
= (t
) ? d
* 100 / t
: 0;
133 static int prev
= -1;
136 /* prevent the same value being sent twice (can occur due to rounding) */
144 progress
= cb_data
->start_range
+ (d
/ t
* ((cb_data
->finish_range
- cb_data
->start_range
)));
145 cb_data
->progress_data
->percentage
= progress
;
147 (cb_data
->cb
)(cb_data
->opkg
,
148 cb_data
->progress_data
,
163 p
= malloc (sizeof (opkg_package_t
));
164 memset (p
, 0, sizeof (opkg_package_t
));
170 opkg_package_free (opkg_package_t
*p
)
174 free (p
->architecture
);
175 free (p
->description
);
178 free (p
->repository
);
187 opkg
= malloc (sizeof (opkg_t
));
189 opkg
->args
= malloc (sizeof (args_t
));
190 args_init (opkg
->args
);
192 opkg
->conf
= malloc (sizeof (opkg_conf_t
));
193 opkg_conf_init (opkg
->conf
, opkg
->args
);
195 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
200 opkg_free (opkg_t
*opkg
)
202 opkg_assert (opkg
!= NULL
);
204 opkg_conf_deinit (opkg
->conf
);
205 args_deinit (opkg
->args
);
206 free (opkg
->options
);
213 opkg_re_read_config_files (opkg_t
*opkg
)
218 opkg_assert (opkg
!= NULL
);
223 /* Unfortunatly, the easiest way to re-read the config files right now is to
224 * throw away opkg->conf and start again */
226 /* copy the settings we need to keep */
227 a
->autoremove
= c
->autoremove
;
228 a
->force_depends
= c
->force_depends
;
229 a
->force_defaults
= c
->force_defaults
;
230 a
->force_overwrite
= c
->force_overwrite
;
231 a
->force_downgrade
= c
->force_downgrade
;
232 a
->force_reinstall
= c
->force_reinstall
;
233 a
->force_removal_of_dependent_packages
= c
->force_removal_of_dependent_packages
;
234 a
->force_removal_of_essential_packages
= c
->force_removal_of_essential_packages
;
235 a
->nodeps
= c
->nodeps
;
236 a
->noaction
= c
->noaction
;
237 a
->query_all
= c
->query_all
;
238 a
->verbosity
= c
->verbosity
;
242 if (a
->offline_root
) free (a
->offline_root
);
243 a
->offline_root
= strdup (c
->offline_root
);
246 if (c
->offline_root_pre_script_cmd
)
248 if (a
->offline_root_pre_script_cmd
) free (a
->offline_root_pre_script_cmd
);
249 a
->offline_root_pre_script_cmd
= strdup (c
->offline_root_pre_script_cmd
);
252 if (c
->offline_root_post_script_cmd
)
254 if (a
->offline_root_post_script_cmd
) free (a
->offline_root_post_script_cmd
);
255 a
->offline_root_post_script_cmd
= strdup (c
->offline_root_post_script_cmd
);
258 /* throw away old opkg_conf and start again */
259 opkg_conf_deinit (opkg
->conf
);
260 opkg_conf_init (opkg
->conf
, opkg
->args
);
262 free (opkg
->options
);
263 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
269 opkg_get_option (opkg_t
*opkg
, char *option
, void **value
)
272 opkg_option_t
*options
;
274 opkg_assert (opkg
!= NULL
);
275 opkg_assert (option
!= NULL
);
276 opkg_assert (value
!= NULL
);
278 options
= opkg
->options
;
280 /* look up the option
281 * TODO: this would be much better as a hash table
283 while (options
[i
].name
)
285 if (strcmp (options
[i
].name
, option
) != 0)
293 switch (options
[i
].type
)
295 case OPKG_OPT_TYPE_BOOL
:
296 *((int *) value
) = *((int *) options
[i
].value
);
299 case OPKG_OPT_TYPE_INT
:
300 *((int *) value
) = *((int *) options
[i
].value
);
303 case OPKG_OPT_TYPE_STRING
:
304 *((char **)value
) = strdup (options
[i
].value
);
311 opkg_set_option (opkg_t
*opkg
, char *option
, void *value
)
313 int i
= 0, found
= 0;
314 opkg_option_t
*options
;
316 opkg_assert (opkg
!= NULL
);
317 opkg_assert (option
!= NULL
);
318 opkg_assert (value
!= NULL
);
320 options
= opkg
->options
;
322 /* look up the option
323 * TODO: this would be much better as a hash table
325 while (options
[i
].name
)
327 if (strcmp (options
[i
].name
, option
) == 0)
337 /* XXX: Warning: Option not found */
342 switch (options
[i
].type
)
344 case OPKG_OPT_TYPE_BOOL
:
345 if (*((int *) value
) == 0)
346 *((int *)options
[i
].value
) = 0;
348 *((int *)options
[i
].value
) = 1;
351 case OPKG_OPT_TYPE_INT
:
352 *((int *) options
[i
].value
) = *((int *) value
);
355 case OPKG_OPT_TYPE_STRING
:
356 *((char **)options
[i
].value
) = strdup (value
);
363 opkg_install_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
366 char *stripped_filename
;
367 opkg_progress_data_t pdata
;
369 pkg_vec_t
*deps
, *all
;
371 char **unresolved
= NULL
;
373 opkg_assert (opkg
!= NULL
);
374 opkg_assert (package_name
!= NULL
);
377 pkg_info_preinstall_check (opkg
->conf
);
380 /* check to ensure package is not already installed */
381 old
= pkg_hash_fetch_installed_by_name(&opkg
->conf
->pkg_hash
, package_name
);
384 /* XXX: Error: Package is already installed. */
385 return OPKG_PACKAGE_ALREADY_INSTALLED
;
388 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg
->conf
, package_name
);
391 /* XXX: Error: Could not find package to install */
392 return OPKG_PACKAGE_NOT_FOUND
;
395 new->state_flag
|= SF_USER
;
397 pdata
.action
= OPKG_INSTALL
;
398 pdata
.package
= old_pkg_to_new (new);
402 /* find dependancies and download them */
403 deps
= pkg_vec_alloc ();
404 /* this function does not return the original package, so we insert it later */
405 ndepends
= pkg_hash_fetch_unsatisfied_dependencies (opkg
->conf
, new, deps
, &unresolved
);
408 /* XXX: Error: Could not satisfy dependencies */
410 return OPKG_DEPENDENCIES_FAILED
;
413 /* insert the package we are installing so that we download it */
414 pkg_vec_insert (deps
, new);
416 /* download package and dependancies */
417 for (i
= 0; i
< deps
->len
; i
++)
420 struct _curl_cb_data cb_data
;
424 if (pkg
->local_filename
)
427 opkg_package_free (pdata
.package
);
428 pdata
.package
= old_pkg_to_new (pkg
);
429 pdata
.action
= OPKG_DOWNLOAD
;
431 if (pkg
->src
== NULL
)
433 /* XXX: Error: Package not available from any configured src */
434 return OPKG_PACKAGE_NOT_AVAILABLE
;
437 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, pkg
->filename
);
439 /* Get the filename part, without any directory */
440 stripped_filename
= strrchr(pkg
->filename
, '/');
441 if ( ! stripped_filename
)
442 stripped_filename
= pkg
->filename
;
444 sprintf_alloc(&pkg
->local_filename
, "%s/%s", opkg
->conf
->tmp_dir
, stripped_filename
);
446 cb_data
.cb
= progress_callback
;
447 cb_data
.progress_data
= &pdata
;
449 cb_data
.user_data
= user_data
;
450 /* 75% of "install" progress is for downloading */
451 cb_data
.start_range
= 75 * i
/ deps
->len
;
452 cb_data
.finish_range
= 75 * (i
+ 1) / deps
->len
;
454 err
= opkg_download(opkg
->conf
, url
, pkg
->local_filename
,
455 (curl_progress_func
) curl_progress_cb
, &cb_data
);
461 opkg_package_free (pdata
.package
);
462 return OPKG_DOWNLOAD_FAILED
;
468 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
469 all
= pkg_vec_alloc ();
470 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
471 for (i
= 0; i
< all
->len
; i
++)
473 all
->pkgs
[i
]->parent
->dependencies_checked
= 0;
478 /* 75% of "install" progress is for downloading */
479 opkg_package_free (pdata
.package
);
480 pdata
.package
= old_pkg_to_new (new);
481 pdata
.action
= OPKG_INSTALL
;
482 progress (pdata
, 75);
484 /* unpack the package */
485 err
= opkg_install_pkg(opkg
->conf
, new, 0);
489 opkg_package_free (pdata
.package
);
492 case PKG_INSTALL_ERR_NOT_TRUSTED
: return OPKG_GPG_ERROR
;
493 case PKG_INSTALL_ERR_DOWNLOAD
: return OPKG_DOWNLOAD_FAILED
;
494 case PKG_INSTALL_ERR_DEPENDENCIES
:
495 case PKG_INSTALL_ERR_CONFLICTS
: return OPKG_DEPENDENCIES_FAILED
;
496 case PKG_INSTALL_ERR_ALREADY_INSTALLED
: return OPKG_PACKAGE_ALREADY_INSTALLED
;
497 case PKG_INSTALL_ERR_SIGNATURE
: return OPKG_GPG_ERROR
;
498 case PKG_INSTALL_ERR_MD5
: return OPKG_MD5_ERROR
;
499 default: return OPKG_UNKNOWN_ERROR
;
503 progress (pdata
, 75);
505 /* run configure scripts, etc. */
506 err
= opkg_configure_packages (opkg
->conf
, NULL
);
509 opkg_package_free (pdata
.package
);
510 return OPKG_UNKNOWN_ERROR
;
513 /* write out status files and file lists */
514 opkg_conf_write_status_files (opkg
->conf
);
515 pkg_write_changed_filelists (opkg
->conf
);
517 progress (pdata
, 100);
518 opkg_package_free (pdata
.package
);
523 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
527 pkg_t
*pkg_to_remove
;
528 opkg_progress_data_t pdata
;
530 opkg_assert (opkg
!= NULL
);
531 opkg_assert (package_name
!= NULL
);
533 pkg_info_preinstall_check (opkg
->conf
);
535 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
539 /* XXX: Error: Package not installed. */
540 return OPKG_PACKAGE_NOT_INSTALLED
;
543 pdata
.action
= OPKG_REMOVE
;
544 pdata
.package
= old_pkg_to_new (pkg
);
548 if (pkg
->state_status
== SS_NOT_INSTALLED
)
550 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
551 return OPKG_PACKAGE_NOT_INSTALLED
;
553 progress (pdata
, 25);
555 if (opkg
->conf
->restrict_to_default_dest
)
557 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
559 opkg
->conf
->default_dest
);
563 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
567 progress (pdata
, 75);
569 err
= opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
571 /* write out status files and file lists */
572 opkg_conf_write_status_files (opkg
->conf
);
573 pkg_write_changed_filelists (opkg
->conf
);
576 progress (pdata
, 100);
577 opkg_package_free (pdata
.package
);
578 return (err
) ? OPKG_UNKNOWN_ERROR
: OPKG_NO_ERROR
;
582 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
586 opkg_progress_data_t pdata
;
590 opkg_assert (opkg
!= NULL
);
591 opkg_assert (package_name
!= NULL
);
593 pkg_info_preinstall_check (opkg
->conf
);
595 if (opkg
->conf
->restrict_to_default_dest
)
597 pkg
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
599 opkg
->conf
->default_dest
);
602 /* XXX: Error: Package not installed in default_dest */
603 return OPKG_PACKAGE_NOT_INSTALLED
;
608 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
,
614 /* XXX: Error: Package not installed */
615 return OPKG_PACKAGE_NOT_INSTALLED
;
618 pdata
.action
= OPKG_INSTALL
;
619 pdata
.package
= old_pkg_to_new (pkg
);
622 err
= opkg_upgrade_pkg (opkg
->conf
, pkg
);
624 return OPKG_UNKNOWN_ERROR
;
625 progress (pdata
, 75);
627 err
= opkg_configure_packages (opkg
->conf
, NULL
);
629 return OPKG_UNKNOWN_ERROR
;
630 progress (pdata
, 100);
631 opkg_package_free (pdata
.package
);
636 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
638 pkg_vec_t
*installed
;
642 opkg_progress_data_t pdata
;
644 pdata
.action
= OPKG_INSTALL
;
645 pdata
.package
= NULL
;
647 opkg_assert (opkg
!= NULL
);
650 installed
= pkg_vec_alloc ();
651 pkg_info_preinstall_check (opkg
->conf
);
653 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed
);
654 for (i
= 0; i
< installed
->len
; i
++)
656 pkg
= installed
->pkgs
[i
];
658 pdata
.package
= old_pkg_to_new (pkg
);
659 progress (pdata
, 99 * i
/ installed
->len
);
660 opkg_package_free (pdata
.package
);
662 err
+= opkg_upgrade_pkg (opkg
->conf
, pkg
);
664 pkg_vec_free (installed
);
669 err
= opkg_configure_packages (opkg
->conf
, NULL
);
673 pdata
.package
= NULL
;
674 progress (pdata
, 100);
679 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
684 pkg_src_list_elt_t
*iter
;
686 int sources_list_count
, sources_done
;
687 opkg_progress_data_t pdata
;
689 opkg_assert (opkg
!= NULL
);
691 pdata
.action
= OPKG_DOWNLOAD
;
692 pdata
.package
= NULL
;
695 sprintf_alloc (&lists_dir
, "%s",
696 (opkg
->conf
->restrict_to_default_dest
)
697 ? opkg
->conf
->default_dest
->lists_dir
698 : opkg
->conf
->lists_dir
);
700 if (!file_is_dir (lists_dir
))
702 if (file_exists (lists_dir
))
704 /* XXX: Error: file exists but is not a directory */
709 err
= file_mkdir_hier (lists_dir
, 0755);
712 /* XXX: Error: failed to create directory */
718 tmp
= strdup ("/tmp/opkg.XXXXXX");
720 if (mkdtemp (tmp
) == NULL
)
722 /* XXX: Error: could not create temporary file name */
728 /* count the number of sources so we can give some progress updates */
729 sources_list_count
= 0;
731 iter
= opkg
->conf
->pkg_src_list
.head
;
734 sources_list_count
++;
738 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
740 char *url
, *list_file_name
= NULL
, *sig_file_name
= NULL
;
744 if (src
->extra_data
) /* debian style? */
745 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
746 src
->gzip
? "Packages.gz" : "Packages");
748 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
750 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
754 struct _curl_cb_data cb_data
;
755 char *tmp_file_name
= NULL
;
757 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
759 /* XXX: Note: downloading url */
761 cb_data
.cb
= progress_callback
;
762 cb_data
.progress_data
= &pdata
;
764 cb_data
.user_data
= user_data
;
765 cb_data
.start_range
= 100 * sources_done
/ sources_list_count
;
766 cb_data
.finish_range
= 100 * (sources_done
+ 1) / sources_list_count
;
768 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, (curl_progress_func
) curl_progress_cb
, &cb_data
);
772 /* XXX: Note: Inflating downloaded file */
773 in
= fopen (tmp_file_name
, "r");
774 out
= fopen (list_file_name
, "w");
783 unlink (tmp_file_name
);
785 free (tmp_file_name
);
788 err
= opkg_download (opkg
->conf
, url
, list_file_name
, NULL
, NULL
);
792 /* XXX: Error: download error */
793 result
= OPKG_DOWNLOAD_FAILED
;
798 /* download detached signitures to verify the package lists */
799 /* get the url for the sig file */
800 if (src
->extra_data
) /* debian style? */
801 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
804 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
806 /* create filename for signature */
807 sprintf_alloc (&sig_file_name
, "%s/%s.sig", lists_dir
, src
->name
);
809 /* make sure there is no existing signature file */
810 unlink (sig_file_name
);
812 err
= opkg_download (opkg
->conf
, url
, sig_file_name
, NULL
, NULL
);
815 /* XXX: Warning: Download failed */
820 err
= opkg_verify_file (opkg
->conf
, list_file_name
, sig_file_name
);
823 /* XXX: Notice: Signature check passed */
827 /* XXX: Warning: Signature check failed */
830 free (sig_file_name
);
831 free (list_file_name
);
834 /* XXX: Note: Signiture check for %s skipped because GPG support was not
835 * enabled in this build
840 progress (pdata
, 100 * sources_done
/ sources_list_count
);
847 /* Now re-read the package lists to update package hash tables. */
848 opkg_re_read_config_files (opkg
);
855 opkg_list_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
861 opkg_assert (callback
);
863 all
= pkg_vec_alloc ();
864 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
865 for (i
= 0; i
< all
->len
; i
++)
868 opkg_package_t
*package
;
872 package
= old_pkg_to_new (pkg
);
873 callback (opkg
, package
, user_data
);
882 opkg_list_upgradable_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
888 opkg_assert (callback
);
890 all
= pkg_vec_alloc ();
891 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
892 for (i
= 0; i
< all
->len
; i
++)
896 opkg_package_t
*package
;
900 if (old
->state_status
!= SS_INSTALLED
)
903 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg
->conf
, old
->name
);
905 /* XXX: Notice: Assuming locally install package is up to date */
909 cmp
= pkg_compare_versions(old
, new);
913 package
= old_pkg_to_new (new);
914 callback (opkg
, package
, user_data
);
924 opkg_find_package (opkg_t
*opkg
, const char *name
, const char *ver
, const char *arch
, const char *repo
)
927 opkg_package_t
*package
= NULL
;
929 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
933 all
= pkg_vec_alloc ();
934 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
935 for (i
= 0; i
< all
->len
; i
++)
943 if (sstrcmp (pkg
->name
, name
))
947 pkgv
= pkg_version_str_alloc (pkg
);
948 if (sstrcmp (pkgv
, ver
))
955 /* check architecture */
958 if (sstrcmp (pkg
->architecture
, arch
))
962 /* check repository */
965 if (sstrcmp (pkg
->src
->name
, repo
))
970 package
= old_pkg_to_new (pkg
);