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
);
186 opkg
= malloc (sizeof (opkg_t
));
188 opkg
->args
= malloc (sizeof (args_t
));
189 args_init (opkg
->args
);
191 opkg
->conf
= malloc (sizeof (opkg_conf_t
));
192 opkg_conf_init (opkg
->conf
, opkg
->args
);
194 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
199 opkg_free (opkg_t
*opkg
)
201 opkg_assert (opkg
!= NULL
);
203 opkg_conf_deinit (opkg
->conf
);
204 args_deinit (opkg
->args
);
208 opkg_re_read_config_files (opkg_t
*opkg
)
213 opkg_assert (opkg
!= NULL
);
218 /* Unfortunatly, the easiest way to re-read the config files right now is to
219 * throw away opkg->conf and start again */
221 /* copy the settings we need to keep */
222 a
->autoremove
= c
->autoremove
;
223 a
->force_depends
= c
->force_depends
;
224 a
->force_defaults
= c
->force_defaults
;
225 a
->force_overwrite
= c
->force_overwrite
;
226 a
->force_downgrade
= c
->force_downgrade
;
227 a
->force_reinstall
= c
->force_reinstall
;
228 a
->force_removal_of_dependent_packages
= c
->force_removal_of_dependent_packages
;
229 a
->force_removal_of_essential_packages
= c
->force_removal_of_essential_packages
;
230 a
->nodeps
= c
->nodeps
;
231 a
->noaction
= c
->noaction
;
232 a
->query_all
= c
->query_all
;
233 a
->multiple_providers
= c
->multiple_providers
;
234 a
->verbosity
= c
->verbosity
;
238 if (a
->offline_root
) free (a
->offline_root
);
239 a
->offline_root
= strdup (c
->offline_root
);
242 if (c
->offline_root_pre_script_cmd
)
244 if (a
->offline_root_pre_script_cmd
) free (a
->offline_root_pre_script_cmd
);
245 a
->offline_root_pre_script_cmd
= strdup (c
->offline_root_pre_script_cmd
);
248 if (c
->offline_root_post_script_cmd
)
250 if (a
->offline_root_post_script_cmd
) free (a
->offline_root_post_script_cmd
);
251 a
->offline_root_post_script_cmd
= strdup (c
->offline_root_post_script_cmd
);
254 /* throw away old opkg_conf and start again */
255 opkg_conf_deinit (opkg
->conf
);
256 opkg_conf_init (opkg
->conf
, opkg
->args
);
258 free (opkg
->options
);
259 opkg_init_options_array (opkg
->conf
, &opkg
->options
);
265 opkg_get_option (opkg_t
*opkg
, char *option
, void **value
)
268 opkg_option_t
*options
;
270 opkg_assert (opkg
!= NULL
);
271 opkg_assert (option
!= NULL
);
272 opkg_assert (value
!= NULL
);
274 options
= opkg
->options
;
276 /* look up the option
277 * TODO: this would be much better as a hash table
279 while (options
[i
].name
)
281 if (strcmp (options
[i
].name
, option
) != 0)
289 switch (options
[i
].type
)
291 case OPKG_OPT_TYPE_BOOL
:
292 *((int *) value
) = *((int *) options
[i
].value
);
295 case OPKG_OPT_TYPE_INT
:
296 *((int *) value
) = *((int *) options
[i
].value
);
299 case OPKG_OPT_TYPE_STRING
:
300 *((char **)value
) = strdup (options
[i
].value
);
307 opkg_set_option (opkg_t
*opkg
, char *option
, void *value
)
309 int i
= 0, found
= 0;
310 opkg_option_t
*options
;
312 opkg_assert (opkg
!= NULL
);
313 opkg_assert (option
!= NULL
);
314 opkg_assert (value
!= NULL
);
316 options
= opkg
->options
;
318 /* look up the option
319 * TODO: this would be much better as a hash table
321 while (options
[i
].name
)
323 if (strcmp (options
[i
].name
, option
) == 0)
333 /* XXX: Warning: Option not found */
338 switch (options
[i
].type
)
340 case OPKG_OPT_TYPE_BOOL
:
341 if (*((int *) value
) == 0)
342 *((int *)options
[i
].value
) = 0;
344 *((int *)options
[i
].value
) = 1;
347 case OPKG_OPT_TYPE_INT
:
348 *((int *) options
[i
].value
) = *((int *) value
);
351 case OPKG_OPT_TYPE_STRING
:
352 *((char **)options
[i
].value
) = strdup (value
);
359 opkg_install_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
362 char *package_id
= NULL
, *stripped_filename
;
363 opkg_progress_data_t pdata
;
365 pkg_vec_t
*deps
, *all
;
367 char **unresolved
= NULL
;
369 opkg_assert (opkg
!= NULL
);
370 opkg_assert (package_name
!= NULL
);
373 pkg_info_preinstall_check (opkg
->conf
);
376 /* check to ensure package is not already installed */
377 old
= pkg_hash_fetch_installed_by_name(&opkg
->conf
->pkg_hash
, package_name
);
380 /* XXX: Error: Package is already installed. */
381 return OPKG_PACKAGE_ALREADY_INSTALLED
;
384 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg
->conf
, package_name
);
387 /* XXX: Error: Could not find package to install */
388 return OPKG_PACKAGE_NOT_FOUND
;
391 new->state_flag
|= SF_USER
;
393 pdata
.action
= OPKG_INSTALL
;
394 pdata
.package
= old_pkg_to_new (new);
398 /* find dependancies and download them */
399 deps
= pkg_vec_alloc ();
400 /* this function does not return the original package, so we insert it later */
401 ndepends
= pkg_hash_fetch_unsatisfied_dependencies (opkg
->conf
, new, deps
, &unresolved
);
404 /* XXX: Error: Could not satisfy dependencies */
406 return OPKG_DEPENDANCIES_FAILED
;
409 /* insert the package we are installing so that we download it */
410 pkg_vec_insert (deps
, new);
412 /* download package and dependancies */
413 for (i
= 0; i
< deps
->len
; i
++)
416 struct _curl_cb_data cb_data
;
420 if (pkg
->local_filename
)
423 opkg_package_free (pdata
.package
);
424 pdata
.package
= old_pkg_to_new (pkg
);
425 pdata
.action
= OPKG_DOWNLOAD
;
427 if (pkg
->src
== NULL
)
429 /* XXX: Error: Package not available from any configured src */
430 return OPKG_PACKAGE_NOT_AVAILABLE
;
433 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, pkg
->filename
);
435 /* Get the filename part, without any directory */
436 stripped_filename
= strrchr(pkg
->filename
, '/');
437 if ( ! stripped_filename
)
438 stripped_filename
= pkg
->filename
;
440 sprintf_alloc(&pkg
->local_filename
, "%s/%s", opkg
->conf
->tmp_dir
, stripped_filename
);
442 cb_data
.cb
= progress_callback
;
443 cb_data
.progress_data
= &pdata
;
445 cb_data
.user_data
= user_data
;
446 /* 75% of "install" progress is for downloading */
447 cb_data
.start_range
= 75 * i
/ deps
->len
;
448 cb_data
.finish_range
= 75 * (i
+ 1) / deps
->len
;
450 err
= opkg_download(opkg
->conf
, url
, pkg
->local_filename
,
451 (curl_progress_func
) curl_progress_cb
, &cb_data
);
457 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
458 all
= pkg_vec_alloc ();
459 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
460 for (i
= 0; i
< all
->len
; i
++)
462 all
->pkgs
[i
]->parent
->dependencies_checked
= 0;
467 /* 75% of "install" progress is for downloading */
468 opkg_package_free (pdata
.package
);
469 pdata
.package
= old_pkg_to_new (new);
470 pdata
.action
= OPKG_INSTALL
;
471 progress (pdata
, 75);
474 package_id
= strdup (package_name
);
476 /* unpack the package */
477 err
= opkg_install_pkg(opkg
->conf
, new, 0);
482 progress (pdata
, 75);
484 /* run configure scripts, etc. */
485 err
= opkg_configure_packages (opkg
->conf
, NULL
);
489 /* write out status files and file lists */
490 opkg_conf_write_status_files (opkg
->conf
);
491 pkg_write_changed_filelists (opkg
->conf
);
493 progress (pdata
, 100);
494 opkg_package_free (pdata
.package
);
499 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
502 pkg_t
*pkg_to_remove
;
503 opkg_progress_data_t pdata
;
505 opkg_assert (opkg
!= NULL
);
506 opkg_assert (package_name
!= NULL
);
510 pkg_info_preinstall_check (opkg
->conf
);
513 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
517 /* XXX: Error: Package not installed. */
518 return OPKG_PACKAGE_NOT_INSTALLED
;
521 pdata
.action
= OPKG_REMOVE
;
522 pdata
.package
= old_pkg_to_new (pkg
);
526 if (pkg
->state_status
== SS_NOT_INSTALLED
)
528 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
529 return OPKG_PACKAGE_NOT_INSTALLED
;
531 progress (pdata
, 25);
533 if (opkg
->conf
->restrict_to_default_dest
)
535 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
537 opkg
->conf
->default_dest
);
541 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
545 progress (pdata
, 75);
547 opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
549 /* write out status files and file lists */
550 opkg_conf_write_status_files (opkg
->conf
);
551 pkg_write_changed_filelists (opkg
->conf
);
554 progress (pdata
, 100);
555 opkg_package_free (pdata
.package
);
560 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
563 opkg_progress_data_t pdata
;
567 opkg_assert (opkg
!= NULL
);
568 opkg_assert (package_name
!= NULL
);
570 pkg_info_preinstall_check (opkg
->conf
);
572 if (opkg
->conf
->restrict_to_default_dest
)
574 pkg
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
576 opkg
->conf
->default_dest
);
579 /* XXX: Error: Package not installed in default_dest */
580 return OPKG_PACKAGE_NOT_INSTALLED
;
585 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
,
591 /* XXX: Error: Package not installed */
592 return OPKG_PACKAGE_NOT_INSTALLED
;
595 pdata
.action
= OPKG_INSTALL
;
596 pdata
.package
= old_pkg_to_new (pkg
);
599 opkg_upgrade_pkg (opkg
->conf
, pkg
);
600 progress (pdata
, 75);
602 opkg_configure_packages (opkg
->conf
, NULL
);
603 progress (pdata
, 100);
604 opkg_package_free (pdata
.package
);
609 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
611 pkg_vec_t
*installed
;
615 opkg_progress_data_t pdata
;
617 pdata
.action
= OPKG_INSTALL
;
618 pdata
.package
= NULL
;
620 opkg_assert (opkg
!= NULL
);
623 installed
= pkg_vec_alloc ();
624 pkg_info_preinstall_check (opkg
->conf
);
626 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed
);
627 for (i
= 0; i
< installed
->len
; i
++)
629 pkg
= installed
->pkgs
[i
];
631 pdata
.package
= old_pkg_to_new (pkg
);
632 progress (pdata
, 99 * i
/ installed
->len
);
633 opkg_package_free (pdata
.package
);
635 err
+= opkg_upgrade_pkg (opkg
->conf
, pkg
);
637 pkg_vec_free (installed
);
642 err
= opkg_configure_packages (opkg
->conf
, NULL
);
646 pdata
.package
= NULL
;
647 progress (pdata
, 100);
652 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
657 pkg_src_list_elt_t
*iter
;
659 int sources_list_count
, sources_done
;
660 opkg_progress_data_t pdata
;
662 opkg_assert (opkg
!= NULL
);
664 pdata
.action
= OPKG_DOWNLOAD
;
665 pdata
.package
= NULL
;
668 sprintf_alloc (&lists_dir
, "%s",
669 (opkg
->conf
->restrict_to_default_dest
)
670 ? opkg
->conf
->default_dest
->lists_dir
671 : opkg
->conf
->lists_dir
);
673 if (!file_is_dir (lists_dir
))
675 if (file_exists (lists_dir
))
677 /* XXX: Error: file exists but is not a directory */
682 err
= file_mkdir_hier (lists_dir
, 0755);
685 /* XXX: Error: failed to create directory */
691 tmp
= strdup ("/tmp/opkg.XXXXXX");
693 if (mkdtemp (tmp
) == NULL
)
695 /* XXX: Error: could not create temporary file name */
701 /* cout the number of sources so we can give some progress updates */
702 sources_list_count
= 0;
704 iter
= opkg
->conf
->pkg_src_list
.head
;
707 sources_list_count
++;
711 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
713 char *url
, *list_file_name
;
717 if (src
->extra_data
) /* debian style? */
718 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
719 src
->gzip
? "Packages.gz" : "Packages");
721 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
723 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
728 struct _curl_cb_data cb_data
;
730 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
732 /* XXX: Note: downloading url */
734 cb_data
.cb
= progress_callback
;
735 cb_data
.progress_data
= &pdata
;
737 cb_data
.user_data
= user_data
;
738 cb_data
.start_range
= 100 * sources_done
/ sources_list_count
;
739 cb_data
.finish_range
= 100 * (sources_done
+ 1) / sources_list_count
;
741 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, (curl_progress_func
) curl_progress_cb
, &cb_data
);
745 /* XXX: Note: Inflating downloaded file */
746 in
= fopen (tmp_file_name
, "r");
747 out
= fopen (list_file_name
, "w");
756 unlink (tmp_file_name
);
760 err
= opkg_download (opkg
->conf
, url
, list_file_name
, NULL
, NULL
);
764 /* XXX: Error: download error */
765 result
= OPKG_DOWNLOAD_FAILED
;
770 /* download detached signitures to verify the package lists */
771 /* get the url for the sig file */
772 if (src
->extra_data
) /* debian style? */
773 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
776 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
778 /* create temporary file for it */
781 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
783 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, NULL
, NULL
);
786 /* XXX: Warning: Download failed */
791 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
794 /* XXX: Notice: Signature check passed */
798 /* XXX: Warning: Signature check failed */
801 unlink (tmp_file_name
);
802 free (tmp_file_name
);
805 /* XXX: Note: Signiture check for %s skipped because GPG support was not
806 * enabled in this build
809 free (list_file_name
);
812 progress (pdata
, 100 * sources_done
/ sources_list_count
);
819 /* Now re-read the package lists to update package hash tables. */
820 opkg_re_read_config_files (opkg
);
827 opkg_list_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
833 opkg_assert (callback
);
835 all
= pkg_vec_alloc ();
836 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
837 for (i
= 0; i
< all
->len
; i
++)
840 opkg_package_t
*package
;
844 package
= old_pkg_to_new (pkg
);
845 callback (opkg
, package
, user_data
);
854 opkg_list_upgradable_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
860 opkg_assert (callback
);
862 all
= pkg_vec_alloc ();
863 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
864 for (i
= 0; i
< all
->len
; i
++)
868 opkg_package_t
*package
;
872 if (old
->state_status
!= SS_INSTALLED
)
875 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg
->conf
, old
->name
);
877 /* XXX: Notice: Assuming locally install package is up to date */
881 cmp
= pkg_compare_versions(old
, new);
885 package
= old_pkg_to_new (new);
886 callback (opkg
, package
, user_data
);
896 opkg_find_package (opkg_t
*opkg
, const char *name
, const char *ver
, const char *arch
, const char *repo
)
899 opkg_package_t
*package
= NULL
;
901 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
905 all
= pkg_vec_alloc ();
906 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
907 for (i
= 0; i
< all
->len
; i
++)
915 if (sstrcmp (pkg
->name
, name
))
919 pkgv
= pkg_version_str_alloc (pkg
);
920 if (sstrcmp (pkgv
, ver
))
927 /* check architecture */
930 if (sstrcmp (pkg
->architecture
, arch
))
934 /* check repository */
937 if (sstrcmp (pkg
->src
->name
, repo
))
942 package
= old_pkg_to_new (pkg
);