From: ticktock35 Date: Mon, 15 Dec 2008 05:11:12 +0000 (+0000) Subject: opkg: add progress callbacks to libopkg api X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=bd09c115217e960cc1dff73d322e3e9581cf2d33;p=oweals%2Fopkg-lede.git opkg: add progress callbacks to libopkg api libopkg: fix opkg_install_package() git-svn-id: http://opkg.googlecode.com/svn/trunk@80 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- diff --git a/libopkg/opkg.c b/libopkg/opkg.c index b4aff13..7340a06 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -243,59 +243,72 @@ opkg_set_option (opkg_t *opkg, char *option, void *value) } int -opkg_install_package (opkg_t *opkg, char *package_name) +opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data) { int err; + char *package_id; + progress_callback (opkg, 0, user_data); + + /* download the package */ + opkg_prepare_url_for_install (opkg->conf, package_name, &package_id); + progress_callback (opkg, 50, user_data); + + /* ... */ pkg_info_preinstall_check (opkg->conf); + /* unpack the package */ if (opkg->conf->multiple_providers) { - err = opkg_install_multi_by_name (opkg->conf, package_name); + err = opkg_install_multi_by_name (opkg->conf, package_id); } else { - err = opkg_install_by_name (opkg->conf, package_name); + err = opkg_install_by_name (opkg->conf, package_id); } - err = opkg_configure_packages (opkg->conf, NULL); + progress_callback (opkg, 75, user_data); - if (opkg->conf->noaction) - return err; + /* run configure scripts, etc. */ + err = opkg_configure_packages (opkg->conf, NULL); + /* write out status files and file lists */ opkg_conf_write_status_files (opkg->conf); pkg_write_changed_filelists (opkg->conf); + progress_callback (opkg, 100, user_data); return err; } int -opkg_remove_package (opkg_t *opkg, char *package_name) +opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data) { return 1; } int -opkg_upgrade_package (opkg_t *opkg, char *package_name) +opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t progress_callback, void *user_data) { return 1; } int -opkg_upgrade_all (opkg_t *opkg) +opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data) { return 1; } int -opkg_update_package_lists (opkg_t *opkg) +opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t progress_callback, void *user_data) { char *tmp; int err; char *lists_dir; pkg_src_list_elt_t *iter; pkg_src_t *src; + int sources_list_count, sources_done; + progress_callback (opkg, 0, user_data); sprintf_alloc (&lists_dir, "%s", (opkg->conf->restrict_to_default_dest) @@ -330,6 +343,15 @@ opkg_update_package_lists (opkg_t *opkg) return 1; } + /* cout the number of sources so we can give some progress updates */ + sources_list_count = 0; + sources_done = 0; + iter = opkg->conf->pkg_src_list.head; + while (iter) + { + sources_list_count++; + iter = iter->next; + } for (iter = opkg->conf->pkg_src_list.head; iter; iter = iter->next) { @@ -420,6 +442,9 @@ opkg_update_package_lists (opkg_t *opkg) */ #endif free (list_file_name); + + sources_done++; + progress_callback (opkg, 100 * sources_done / sources_list_count, user_data); } rmdir (tmp); free (tmp); diff --git a/libopkg/opkg.h b/libopkg/opkg.h index 0ee1887..b35635a 100644 --- a/libopkg/opkg.h +++ b/libopkg/opkg.h @@ -16,6 +16,7 @@ */ typedef struct _opkg_t opkg_t; +typedef void (*opkg_progress_callback_t) (opkg_t *opkg, int percentage, void *user_data); opkg_t* opkg_new (); void opkg_free (opkg_t *opkg); @@ -23,8 +24,8 @@ void opkg_get_option (opkg_t *opkg, char *option, void **value); void opkg_set_option (opkg_t *opkg, char *option, void *value); int opkg_read_config_files (opkg_t *opkg); -int opkg_install_package (opkg_t *opkg, char *package_name); -int opkg_remove_package (opkg_t *opkg, char *package_name); -int opkg_upgrade_package (opkg_t *opkg, char *package_name); -int opkg_upgrade_all (opkg_t *opkg); -int opkg_update_package_lists (opkg_t *opkg); +int opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t callback, void *user_data); +int opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t callback, void *user_data); +int opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_callback_t callback, void *user_data); +int opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t callback, void *user_data); +int opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t callback, void *user_data); diff --git a/tests/libopkg_test.c b/tests/libopkg_test.c index fd39778..49aef22 100644 --- a/tests/libopkg_test.c +++ b/tests/libopkg_test.c @@ -1,5 +1,13 @@ #include #include +#include + +void +progress_callback (opkg_t *opkg, int percent, void *data) +{ + printf ("%s %d\n", (char*) data, percent); +} + int main (int argc, char **argv) @@ -13,9 +21,13 @@ main (int argc, char **argv) opkg_read_config_files (opkg); - err = opkg_update_package_lists (opkg); + err = opkg_update_package_lists (opkg, progress_callback, "Updating..."); printf ("opkg_update_package_lists returned %d\n", err); + err = opkg_install_package (opkg, "aspell", progress_callback, "Installing..."); + + printf ("opkg_install_package returned %d\n", err); + opkg_free (opkg); }