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
;
363 opkg_progress_data_t pdata
;
366 opkg_assert (opkg
!= NULL
);
367 opkg_assert (package_name
!= NULL
);
370 pkg_info_preinstall_check (opkg
->conf
);
372 new = pkg_hash_fetch_best_installation_candidate_by_name (opkg
->conf
, package_name
);
376 /* XXX: Error: Could not find package to install */
379 pdata
.action
= OPKG_INSTALL
;
380 pdata
.package
= old_pkg_to_new (new);
384 /* download the package */
385 opkg_prepare_url_for_install (opkg
->conf
, package_name
, &package_id
);
387 progress (pdata
, 50);
390 package_id
= strdup (package_name
);
392 /* unpack the package */
393 if (opkg
->conf
->multiple_providers
)
395 err
= opkg_install_multi_by_name (opkg
->conf
, package_id
);
399 err
= opkg_install_by_name (opkg
->conf
, package_id
);
405 progress (pdata
, 75);
407 /* run configure scripts, etc. */
408 err
= opkg_configure_packages (opkg
->conf
, NULL
);
412 /* write out status files and file lists */
413 opkg_conf_write_status_files (opkg
->conf
);
414 pkg_write_changed_filelists (opkg
->conf
);
416 progress (pdata
, 100);
417 opkg_package_free (pdata
.package
);
422 opkg_remove_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
425 pkg_t
*pkg_to_remove
;
426 opkg_progress_data_t pdata
;
428 opkg_assert (opkg
!= NULL
);
429 opkg_assert (package_name
!= NULL
);
433 pkg_info_preinstall_check (opkg
->conf
);
436 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, package_name
);
440 /* XXX: Error: Package not installed. */
444 pdata
.action
= OPKG_REMOVE
;
445 pdata
.package
= old_pkg_to_new (pkg
);
449 if (pkg
->state_status
== SS_NOT_INSTALLED
)
451 /* XXX: Error: Package seems to be not installed (STATUS = NOT_INSTALLED). */
454 progress (pdata
, 25);
456 if (opkg
->conf
->restrict_to_default_dest
)
458 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
460 opkg
->conf
->default_dest
);
464 pkg_to_remove
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
, pkg
->name
);
468 progress (pdata
, 75);
470 opkg_remove_pkg (opkg
->conf
, pkg_to_remove
, 0);
472 /* write out status files and file lists */
473 opkg_conf_write_status_files (opkg
->conf
);
474 pkg_write_changed_filelists (opkg
->conf
);
477 progress (pdata
, 100);
478 opkg_package_free (pdata
.package
);
483 opkg_upgrade_package (opkg_t
*opkg
, const char *package_name
, opkg_progress_callback_t progress_callback
, void *user_data
)
486 opkg_progress_data_t pdata
;
490 opkg_assert (opkg
!= NULL
);
491 opkg_assert (package_name
!= NULL
);
493 pkg_info_preinstall_check (opkg
->conf
);
495 if (opkg
->conf
->restrict_to_default_dest
)
497 pkg
= pkg_hash_fetch_installed_by_name_dest (&opkg
->conf
->pkg_hash
,
499 opkg
->conf
->default_dest
);
502 /* XXX: Error: Package not installed in default_dest */
508 pkg
= pkg_hash_fetch_installed_by_name (&opkg
->conf
->pkg_hash
,
514 /* XXX: Error: Package not installed */
518 pdata
.action
= OPKG_INSTALL
;
519 pdata
.package
= old_pkg_to_new (pkg
);
522 opkg_upgrade_pkg (opkg
->conf
, pkg
);
523 progress (pdata
, 75);
525 opkg_configure_packages (opkg
->conf
, NULL
);
526 progress (pdata
, 100);
527 opkg_package_free (pdata
.package
);
532 opkg_upgrade_all (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
534 pkg_vec_t
*installed
;
538 opkg_progress_data_t pdata
;
540 pdata
.action
= OPKG_INSTALL
;
541 pdata
.package
= NULL
;
543 opkg_assert (opkg
!= NULL
);
546 installed
= pkg_vec_alloc ();
547 pkg_info_preinstall_check (opkg
->conf
);
549 pkg_hash_fetch_all_installed (&opkg
->conf
->pkg_hash
, installed
);
550 for (i
= 0; i
< installed
->len
; i
++)
552 pkg
= installed
->pkgs
[i
];
554 pdata
.package
= old_pkg_to_new (pkg
);
555 progress (pdata
, 99 * i
/ installed
->len
);
556 opkg_package_free (pdata
.package
);
558 err
+= opkg_upgrade_pkg (opkg
->conf
, pkg
);
560 pkg_vec_free (installed
);
565 err
= opkg_configure_packages (opkg
->conf
, NULL
);
569 pdata
.package
= NULL
;
570 progress (pdata
, 100);
575 opkg_update_package_lists (opkg_t
*opkg
, opkg_progress_callback_t progress_callback
, void *user_data
)
580 pkg_src_list_elt_t
*iter
;
582 int sources_list_count
, sources_done
;
583 opkg_progress_data_t pdata
;
585 opkg_assert (opkg
!= NULL
);
587 pdata
.action
= OPKG_DOWNLOAD
;
588 pdata
.package
= NULL
;
591 sprintf_alloc (&lists_dir
, "%s",
592 (opkg
->conf
->restrict_to_default_dest
)
593 ? opkg
->conf
->default_dest
->lists_dir
594 : opkg
->conf
->lists_dir
);
596 if (!file_is_dir (lists_dir
))
598 if (file_exists (lists_dir
))
600 /* XXX: Error: file exists but is not a directory */
605 err
= file_mkdir_hier (lists_dir
, 0755);
608 /* XXX: Error: failed to create directory */
614 tmp
= strdup ("/tmp/opkg.XXXXXX");
616 if (mkdtemp (tmp
) == NULL
)
618 /* XXX: Error: could not create temporary file name */
624 /* cout the number of sources so we can give some progress updates */
625 sources_list_count
= 0;
627 iter
= opkg
->conf
->pkg_src_list
.head
;
630 sources_list_count
++;
634 for (iter
= opkg
->conf
->pkg_src_list
.head
; iter
; iter
= iter
->next
)
636 char *url
, *list_file_name
;
640 if (src
->extra_data
) /* debian style? */
641 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
642 src
->gzip
? "Packages.gz" : "Packages");
644 sprintf_alloc (&url
, "%s/%s", src
->value
, src
->gzip
? "Packages.gz" : "Packages");
646 sprintf_alloc (&list_file_name
, "%s/%s", lists_dir
, src
->name
);
651 struct _curl_cb_data cb_data
;
653 sprintf_alloc (&tmp_file_name
, "%s/%s.gz", tmp
, src
->name
);
655 /* XXX: Note: downloading url */
657 cb_data
.cb
= progress_callback
;
658 cb_data
.progress_data
= &pdata
;
660 cb_data
.user_data
= user_data
;
661 cb_data
.start_range
= 100 * sources_done
/ sources_list_count
;
662 cb_data
.finish_range
= 100 * (sources_done
+ 1) / sources_list_count
;
664 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, (curl_progress_func
) curl_progress_cb
, &cb_data
);
668 /* XXX: Note: Inflating downloaded file */
669 in
= fopen (tmp_file_name
, "r");
670 out
= fopen (list_file_name
, "w");
679 unlink (tmp_file_name
);
683 err
= opkg_download (opkg
->conf
, url
, list_file_name
, NULL
, NULL
);
687 /* XXX: Error: download error */
692 /* download detached signitures to verify the package lists */
693 /* get the url for the sig file */
694 if (src
->extra_data
) /* debian style? */
695 sprintf_alloc (&url
, "%s/%s/%s", src
->value
, src
->extra_data
,
698 sprintf_alloc (&url
, "%s/%s", src
->value
, "Packages.sig");
700 /* create temporary file for it */
703 sprintf_alloc (&tmp_file_name
, "%s/%s", tmp
, "Packages.sig");
705 err
= opkg_download (opkg
->conf
, url
, tmp_file_name
, NULL
, NULL
);
708 /* XXX: Warning: Download failed */
713 err
= opkg_verify_file (opkg
->conf
, list_file_name
, tmp_file_name
);
716 /* XXX: Notice: Signature check passed */
720 /* XXX: Warning: Signature check failed */
723 unlink (tmp_file_name
);
724 free (tmp_file_name
);
727 /* XXX: Note: Signiture check for %s skipped because GPG support was not
728 * enabled in this build
731 free (list_file_name
);
734 progress (pdata
, 100 * sources_done
/ sources_list_count
);
746 opkg_list_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
752 opkg_assert (callback
);
754 all
= pkg_vec_alloc ();
755 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
756 for (i
= 0; i
< all
->len
; i
++)
759 opkg_package_t
*package
;
763 package
= old_pkg_to_new (pkg
);
764 callback (opkg
, package
, user_data
);
773 opkg_list_upgradable_packages (opkg_t
*opkg
, opkg_package_callback_t callback
, void *user_data
)
779 opkg_assert (callback
);
781 all
= pkg_vec_alloc ();
782 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
783 for (i
= 0; i
< all
->len
; i
++)
787 opkg_package_t
*package
;
791 if (old
->state_status
!= SS_INSTALLED
)
794 new = pkg_hash_fetch_best_installation_candidate_by_name(opkg
->conf
, old
->name
);
796 /* XXX: Notice: Assuming locally install package is up to date */
800 cmp
= pkg_compare_versions(old
, new);
804 package
= old_pkg_to_new (new);
805 callback (opkg
, package
, user_data
);
815 opkg_find_package (opkg_t
*opkg
, const char *name
, const char *ver
, const char *arch
, const char *repo
)
818 opkg_package_t
*package
= NULL
;
820 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
824 all
= pkg_vec_alloc ();
825 pkg_hash_fetch_available (&opkg
->conf
->pkg_hash
, all
);
826 for (i
= 0; i
< all
->len
; i
++)
834 if (sstrcmp (pkg
->name
, name
))
838 pkgv
= pkg_version_str_alloc (pkg
);
839 if (sstrcmp (pkgv
, ver
))
846 /* check architecture */
849 if (sstrcmp (pkg
->architecture
, arch
))
853 /* check repository */
856 if (sstrcmp (pkg
->src
->name
, repo
))
861 package
= old_pkg_to_new (pkg
);