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.
25 #include "opkg_conf.h"
27 #include "opkg_install.h"
28 #include "opkg_configure.h"
29 #include "opkg_download.h"
30 #include "opkg_remove.h"
31 #include "opkg_upgrade.h"
33 #include "sprintf_alloc.h"
34 #include "file_util.h"
36 #include <libbb/libbb.h>
38 #define opkg_assert(expr) if (!(expr)) { \
39 printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
40 __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
42 #define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (&d, user_data);
44 /** Private Functions ***/
46 static int opkg_configure_packages(char *pkg_name
)
53 all
= pkg_vec_alloc();
54 pkg_hash_fetch_available(all
);
56 for (i
= 0; i
< all
->len
; i
++) {
59 if (pkg_name
&& fnmatch(pkg_name
, pkg
->name
, 0))
62 if (pkg
->state_status
== SS_UNPACKED
) {
63 r
= opkg_configure(pkg
);
65 pkg
->state_status
= SS_INSTALLED
;
66 pkg
->parent
->state_status
= SS_INSTALLED
;
67 pkg
->state_flag
&= ~SF_PREFER
;
79 struct _curl_cb_data
{
80 opkg_progress_callback_t cb
;
81 opkg_progress_data_t
*progress_data
;
87 int curl_progress_cb(struct _curl_cb_data
*cb_data
, double t
, /* dltotal */
89 double ultotal
, double ulnow
)
91 int p
= (t
) ? d
* 100 / t
: 0;
95 /* prevent the same value being sent twice (can occur due to rounding) */
103 progress
= cb_data
->start_range
+
104 (d
/ t
* ((cb_data
->finish_range
- cb_data
->start_range
)));
105 cb_data
->progress_data
->percentage
= progress
;
107 (cb_data
->cb
) (cb_data
->progress_data
, cb_data
->user_data
);
112 static struct opkg_conf saved_conf
;
119 if (opkg_conf_init())
122 if (opkg_conf_load())
125 if (pkg_hash_load_feeds())
128 if (pkg_hash_load_status_files())
148 int opkg_re_read_config_files(void)
155 void opkg_get_option(char *option
, void **value
)
158 extern opkg_option_t options
[];
160 /* look up the option
161 * TODO: this would be much better as a hash table
163 while (options
[i
].name
) {
164 if (strcmp(options
[i
].name
, option
) != 0) {
171 switch (options
[i
].type
) {
172 case OPKG_OPT_TYPE_BOOL
:
173 *((int *)value
) = *((int *)options
[i
].value
);
176 case OPKG_OPT_TYPE_INT
:
177 *((int *)value
) = *((int *)options
[i
].value
);
180 case OPKG_OPT_TYPE_STRING
:
181 *((char **)value
) = xstrdup(options
[i
].value
);
187 void opkg_set_option(char *option
, void *value
)
189 int i
= 0, found
= 0;
190 extern opkg_option_t options
[];
192 opkg_assert(option
!= NULL
);
193 opkg_assert(value
!= NULL
);
195 /* look up the option
196 * TODO: this would be much better as a hash table
198 while (options
[i
].name
) {
199 if (strcmp(options
[i
].name
, option
) == 0) {
207 opkg_msg(ERROR
, "Invalid option: %s\n", option
);
212 switch (options
[i
].type
) {
213 case OPKG_OPT_TYPE_BOOL
:
214 if (*((int *)value
) == 0)
215 *((int *)options
[i
].value
) = 0;
217 *((int *)options
[i
].value
) = 1;
220 case OPKG_OPT_TYPE_INT
:
221 *((int *)options
[i
].value
) = *((int *)value
);
224 case OPKG_OPT_TYPE_STRING
:
225 *((char **)options
[i
].value
) = xstrdup(value
);
232 * @brief libopkg API: Install package
233 * @param package_name The name of package in which is going to install
234 * @param progress_callback The callback function that report the status to caller.
237 opkg_install_package(const char *package_name
,
238 opkg_progress_callback_t progress_callback
,
242 char *stripped_filename
, *local_filename
;
243 opkg_progress_data_t pdata
;
245 pkg_vec_t
*deps
, *all
;
247 char **unresolved
= NULL
;
248 const char *filename
;
250 opkg_assert(package_name
!= NULL
);
253 pkg_info_preinstall_check();
255 /* check to ensure package is not already installed */
256 old
= pkg_hash_fetch_installed_by_name(package_name
);
258 opkg_msg(ERROR
, "Package %s is already installed\n",
263 new = pkg_hash_fetch_best_installation_candidate_by_name(package_name
);
265 opkg_msg(ERROR
, "Couldn't find package %s\n", package_name
);
269 new->state_flag
|= SF_USER
;
276 /* find dependancies and download them */
277 deps
= pkg_vec_alloc();
278 /* this function does not return the original package, so we insert it later */
279 ndepends
= pkg_hash_fetch_unsatisfied_dependencies(new, deps
,
282 char **tmp
= unresolved
;
283 opkg_msg(ERROR
, "Couldn't satisfy the following dependencies"
284 " for %s:\n", package_name
);
286 opkg_msg(ERROR
, "\t%s", *tmp
);
292 opkg_message(ERROR
, "\n");
296 /* insert the package we are installing so that we download it */
297 pkg_vec_insert(deps
, new);
299 /* download package and dependencies */
300 for (i
= 0; i
< deps
->len
; i
++) {
302 struct _curl_cb_data cb_data
;
306 if (pkg_get_string(pkg
, PKG_LOCAL_FILENAME
))
310 pdata
.action
= OPKG_DOWNLOAD
;
312 if (pkg
->src
== NULL
) {
313 opkg_msg(ERROR
, "Package %s not available from any "
314 "configured src\n", package_name
);
318 filename
= pkg_get_string(pkg
, PKG_FILENAME
);
320 sprintf_alloc(&url
, "%s/%s", pkg
->src
->value
, filename
);
322 /* Get the filename part, without any directory */
323 stripped_filename
= strrchr(filename
, '/');
324 if (!stripped_filename
)
325 stripped_filename
= (char *)filename
;
327 sprintf_alloc(&local_filename
, "%s/%s", conf
->tmp_dir
,
330 pkg_set_string(pkg
, PKG_LOCAL_FILENAME
, local_filename
);
332 cb_data
.cb
= progress_callback
;
333 cb_data
.progress_data
= &pdata
;
334 cb_data
.user_data
= user_data
;
335 /* 75% of "install" progress is for downloading */
336 cb_data
.start_range
= 75 * i
/ deps
->len
;
337 cb_data
.finish_range
= 75 * (i
+ 1) / deps
->len
;
339 err
= opkg_download(url
, local_filename
,
340 (curl_progress_func
) curl_progress_cb
,
352 /* clear depenacy checked marks, left by pkg_hash_fetch_unsatisfied_dependencies */
353 all
= pkg_vec_alloc();
354 pkg_hash_fetch_available(all
);
355 for (i
= 0; i
< all
->len
; i
++) {
356 all
->pkgs
[i
]->parent
->dependencies_checked
= 0;
360 /* 75% of "install" progress is for downloading */
362 pdata
.action
= OPKG_INSTALL
;
365 /* unpack the package */
366 err
= opkg_install_pkg(new, 0);
374 /* run configure scripts, etc. */
375 err
= opkg_configure_packages(NULL
);
380 /* write out status files and file lists */
381 opkg_conf_write_status_files();
382 pkg_write_changed_filelists();
384 progress(pdata
, 100);
389 opkg_remove_package(const char *package_name
,
390 opkg_progress_callback_t progress_callback
, void *user_data
)
394 pkg_t
*pkg_to_remove
;
395 opkg_progress_data_t pdata
;
397 opkg_assert(package_name
!= NULL
);
399 pkg_info_preinstall_check();
401 pkg
= pkg_hash_fetch_installed_by_name(package_name
);
403 if (pkg
== NULL
|| pkg
->state_status
== SS_NOT_INSTALLED
) {
404 opkg_msg(ERROR
, "Package %s not installed\n", package_name
);
408 pdata
.action
= OPKG_REMOVE
;
412 if (conf
->restrict_to_default_dest
) {
413 pkg_to_remove
= pkg_hash_fetch_installed_by_name_dest(pkg
->name
,
417 pkg_to_remove
= pkg_hash_fetch_installed_by_name(pkg
->name
);
422 err
= opkg_remove_pkg(pkg_to_remove
, 0);
424 /* write out status files and file lists */
425 opkg_conf_write_status_files();
426 pkg_write_changed_filelists();
428 progress(pdata
, 100);
429 return (err
) ? -1 : 0;
433 opkg_upgrade_package(const char *package_name
,
434 opkg_progress_callback_t progress_callback
,
439 opkg_progress_data_t pdata
;
441 opkg_assert(package_name
!= NULL
);
443 pkg_info_preinstall_check();
445 if (conf
->restrict_to_default_dest
) {
446 pkg
= pkg_hash_fetch_installed_by_name_dest(package_name
,
449 pkg
= pkg_hash_fetch_installed_by_name(package_name
);
453 opkg_msg(ERROR
, "Package %s not installed\n", package_name
);
457 pdata
.action
= OPKG_INSTALL
;
461 err
= opkg_upgrade_pkg(pkg
);
467 err
= opkg_configure_packages(NULL
);
472 /* write out status files and file lists */
473 opkg_conf_write_status_files();
474 pkg_write_changed_filelists();
476 progress(pdata
, 100);
481 opkg_upgrade_all(opkg_progress_callback_t progress_callback
, void *user_data
)
483 pkg_vec_t
*installed
;
487 opkg_progress_data_t pdata
;
489 pdata
.action
= OPKG_INSTALL
;
494 installed
= pkg_vec_alloc();
495 pkg_info_preinstall_check();
497 pkg_hash_fetch_all_installed(installed
);
498 for (i
= 0; i
< installed
->len
; i
++) {
499 pkg
= installed
->pkgs
[i
];
502 progress(pdata
, 99 * i
/ installed
->len
);
504 err
+= opkg_upgrade_pkg(pkg
);
506 pkg_vec_free(installed
);
511 err
= opkg_configure_packages(NULL
);
515 /* write out status files and file lists */
516 opkg_conf_write_status_files();
517 pkg_write_changed_filelists();
520 progress(pdata
, 100);
525 opkg_update_package_lists(opkg_progress_callback_t progress_callback
,
531 pkg_src_list_elt_t
*iter
;
533 int sources_list_count
, sources_done
;
534 opkg_progress_data_t pdata
;
536 pdata
.action
= OPKG_DOWNLOAD
;
540 sprintf_alloc(&lists_dir
, "%s", (conf
->restrict_to_default_dest
)
541 ? conf
->default_dest
->lists_dir
: conf
->lists_dir
);
543 if (!file_is_dir(lists_dir
)) {
544 if (file_exists(lists_dir
)) {
545 opkg_msg(ERROR
, "%s is not a directory\n", lists_dir
);
550 err
= file_mkdir_hier(lists_dir
, 0755);
552 opkg_msg(ERROR
, "Couldn't create lists_dir %s\n",
559 sprintf_alloc(&tmp
, "%s/update-XXXXXX", conf
->tmp_dir
);
560 if (mkdtemp(tmp
) == NULL
) {
561 opkg_perror(ERROR
, "Coundn't create temporary directory %s",
568 /* count the number of sources so we can give some progress updates */
569 sources_list_count
= 0;
571 list_for_each_entry(iter
, &conf
->pkg_src_list
.head
, node
) {
572 sources_list_count
++;
575 list_for_each_entry(iter
, &conf
->pkg_src_list
.head
, node
) {
576 char *url
, *list_file_name
= NULL
;
578 src
= (pkg_src_t
*) iter
->data
;
580 if (src
->extra_data
) /* debian style? */
581 sprintf_alloc(&url
, "%s/%s/%s", src
->value
,
583 src
->gzip
? "Packages.gz" : "Packages");
585 sprintf_alloc(&url
, "%s/%s", src
->value
,
586 src
->gzip
? "Packages.gz" : "Packages");
588 sprintf_alloc(&list_file_name
, "%s/%s", lists_dir
, src
->name
);
590 if (opkg_download(url
, list_file_name
, NULL
, NULL
, 0)) {
591 opkg_msg(ERROR
, "Couldn't retrieve %s\n", url
);
596 #if defined(HAVE_GPGME) || defined(HAVE_OPENSSL) || defined(HAVE_USIGN)
597 if (conf
->check_signature
) {
599 /* download detached signitures to verify the package lists */
600 /* get the url for the sig file */
601 if (src
->extra_data
) /* debian style? */
602 sprintf_alloc(&url
, "%s/%s/%s", src
->value
,
603 src
->extra_data
, "Packages.sig");
605 sprintf_alloc(&url
, "%s/%s", src
->value
,
608 /* create filename for signature */
609 sprintf_alloc(&sig_file_name
, "%s/%s.sig", lists_dir
,
612 /* make sure there is no existing signature file */
613 unlink(sig_file_name
);
615 err
= opkg_download(url
, sig_file_name
, NULL
, NULL
, 0);
617 opkg_msg(ERROR
, "Couldn't retrieve %s\n", url
);
620 err
= opkg_verify_file(list_file_name
,
623 opkg_msg(INFO
, "Signature check "
627 opkg_msg(ERROR
, "Signature check "
636 opkg_msg(INFO
, "Signature check skipped for %s as GPG support"
637 " has not been enabled in this build\n",
640 free(list_file_name
);
643 progress(pdata
, 100 * sources_done
/ sources_list_count
);
650 /* Now re-read the package lists to update package hash tables. */
651 opkg_re_read_config_files();
656 static int pkg_compare_names_and_version(const void *a0
, const void *b0
)
658 const pkg_t
*a
= *(const pkg_t
**)a0
;
659 const pkg_t
*b
= *(const pkg_t
**)b0
;
662 ret
= strcmp(a
->name
, b
->name
);
665 ret
= pkg_compare_versions(a
, b
);
670 int opkg_list_packages(opkg_package_callback_t callback
, void *user_data
)
675 opkg_assert(callback
);
677 all
= pkg_vec_alloc();
678 pkg_hash_fetch_available(all
);
680 pkg_vec_sort(all
, pkg_compare_names_and_version
);
682 for (i
= 0; i
< all
->len
; i
++) {
687 callback(pkg
, user_data
);
696 opkg_list_upgradable_packages(opkg_package_callback_t callback
, void *user_data
)
698 struct active_list
*head
;
699 struct active_list
*node
;
700 pkg_t
*old
= NULL
, *new = NULL
;
702 opkg_assert(callback
);
704 /* ensure all data is valid */
705 pkg_info_preinstall_check();
707 head
= prepare_upgrade_list();
708 for (node
= active_list_next(head
, head
); node
;
709 node
= active_list_next(head
, node
)) {
712 pkg_hash_fetch_best_installation_candidate_by_name(old
->
716 callback(new, user_data
);
718 active_list_head_delete(head
);
722 pkg_t
*opkg_find_package(const char *name
, const char *ver
, const char *arch
,
729 #define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
731 all
= pkg_vec_alloc();
732 pkg_hash_fetch_available(all
);
733 for (i
= 0; i
< all
->len
; i
++) {
739 if (sstrcmp(pkg
->name
, name
))
743 pkgv
= pkg_version_str_alloc(pkg
);
744 if (sstrcmp(pkgv
, ver
)) {
750 /* check architecture */
752 if (sstrcmp(pkg_get_architecture(pkg
), arch
))
756 /* check repository */
758 if (sstrcmp(pkg
->src
->name
, repo
))
769 return pkg_found
? pkg
: NULL
;
773 * @brief Check the accessibility of repositories.
774 * @return return how many repositories cannot access. 0 means all okay.
776 int opkg_repository_accessibility_check(void)
778 pkg_src_list_elt_t
*iter
;
779 str_list_elt_t
*iter1
;
781 int repositories
= 0;
787 src
= str_list_alloc();
789 list_for_each_entry(iter
, &conf
->pkg_src_list
.head
, node
) {
790 host
= strstr(((pkg_src_t
*) iter
->data
)->value
, "://") + 3;
791 end
= index(host
, '/');
792 if (strstr(((pkg_src_t
*) iter
->data
)->value
, "://") && end
)
793 stmp
= xstrndup(((pkg_src_t
*) iter
->data
)->value
,
795 ((pkg_src_t
*) iter
->data
)->value
);
797 stmp
= xstrdup(((pkg_src_t
*) iter
->data
)->value
);
799 for (iter1
= str_list_first(src
); iter1
;
800 iter1
= str_list_next(src
, iter1
)) {
801 if (strstr(iter1
->data
, stmp
))
807 sprintf_alloc(&repo_ptr
, "%s/index.html", stmp
);
810 str_list_append(src
, repo_ptr
);
815 while (repositories
> 0) {
816 iter1
= str_list_pop(src
);
819 if (opkg_download(iter1
->data
, "/dev/null", NULL
, NULL
, 0))
821 str_list_elt_deinit(iter1
);