Introduces the ALPM_EVENT_HOOK_RUN_{START,DONE} events that are triggered
at the start and end of running an individual hook.

Signed-off-by: Allan McRae <[email protected]>
---

The output looks like:

$ sudo ./src/pacman/pacman -S abs
warning: abs-2.4.4-2 is up to date -- reinstalling
resolving dependencies...
looking for conflicting packages...

Packages (1) abs-2.4.4-2

Total Installed Size:  0.03 MiB
Net Upgrade Size:      0.00 MiB

:: Proceed with installation? [Y/n] 
(1/1) checking keys in keyring                     [######################] 100%
(1/1) checking package integrity                   [######################] 100%
(1/1) loading package files                        [######################] 100%
(1/1) checking for file conflicts                  [######################] 100%
:: Processing package changes...
(1/1) reinstalling abs                             [######################] 100%
:: Running post-transaction hooks...
(1/2) one.hook
(2/2) two.hook



 lib/libalpm/alpm.h    | 18 +++++++++++++++++-
 lib/libalpm/hook.c    | 14 +++++++++++++-
 src/pacman/callback.c | 13 +++++++++++++
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 8fcdfdf..f6e6b86 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -461,7 +461,11 @@ typedef enum _alpm_event_type_t {
        /** Processing hooks will be started. */
        ALPM_EVENT_HOOK_START,
        /** Processing hooks is finished. */
-       ALPM_EVENT_HOOK_DONE
+       ALPM_EVENT_HOOK_DONE,
+       /** A hook is starting */
+       ALPM_EVENT_HOOK_RUN_START,
+       /** A hook has finnished runnning */
+       ALPM_EVENT_HOOK_RUN_DONE
 } alpm_event_type_t;
 
 typedef struct _alpm_event_any_t {
@@ -559,6 +563,17 @@ typedef struct _alpm_event_hook_t {
        alpm_hook_when_t when;
 } alpm_event_hook_t;
 
+typedef struct _alpm_event_hook_run_t {
+       /** Type of event.*/
+       alpm_event_type_t type;
+       /** Text to be outputted */
+       const char *desc;
+       /** position of hook being run */
+       size_t position;
+       /** total hooks being run */
+       size_t total;
+} alpm_event_hook_run_t;
+
 /** Events.
  * This is an union passed to the callback, that allows the frontend to know
  * which type of event was triggered (via type). It is then possible to
@@ -576,6 +591,7 @@ typedef union _alpm_event_t {
        alpm_event_pacnew_created_t pacnew_created;
        alpm_event_pacsave_created_t pacsave_created;
        alpm_event_hook_t hook;
+       alpm_event_hook_run_t hook_run;
 } alpm_event_t;
 
 /** Event callback. */
diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c
index df41fce..acd571d 100644
--- a/lib/libalpm/hook.c
+++ b/lib/libalpm/hook.c
@@ -611,6 +611,7 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, 
struct _alpm_hook_t *hook)
 int _alpm_hook_run(alpm_handle_t *handle, alpm_hook_when_t when)
 {
        alpm_event_hook_t event = { .when = when };
+       alpm_event_hook_run_t hook_event;
        alpm_list_t *i, *hooks = NULL, *hooks_triggered = NULL;
        const char *suffix = ".hook";
        size_t suflen = strlen(suffix), triggered = 0;
@@ -725,12 +726,23 @@ int _alpm_hook_run(alpm_handle_t *handle, 
alpm_hook_when_t when)
        event.type = ALPM_EVENT_HOOK_START;
        EVENT(handle, &event);
 
-       for(i = hooks_triggered; i; i = i->next) {
+       hook_event.position = 1;
+       hook_event.total = triggered;
+
+       for(i = hooks_triggered; i; i = i->next, hook_event.position++) {
                struct _alpm_hook_t *hook = i->data;
                _alpm_log(handle, ALPM_LOG_DEBUG, "running hook %s\n", 
hook->name);
+
+               hook_event.type = ALPM_EVENT_HOOK_RUN_START;
+               hook_event.desc = hook->name;
+               EVENT(handle, &hook_event);
+
                if(_alpm_hook_run_hook(handle, hook) != 0 && 
hook->abort_on_fail) {
                        ret = -1;
                }
+
+               hook_event.type = ALPM_EVENT_HOOK_RUN_DONE;
+               EVENT(handle, &hook_event);
        }
 
        event.type = ALPM_EVENT_HOOK_DONE;
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index c33bd24..ae725d3 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -172,6 +172,18 @@ void cb_event(alpm_event_t *event)
                                        event->hook.when == 
ALPM_HOOK_PRE_TRANSACTION
                                        ? _("pre-transaction") : 
_("post-transaction"));
                        break;
+               case ALPM_EVENT_HOOK_RUN_START:
+                       {
+                               alpm_event_hook_run_t *e = &event->hook_run;
+                               int digits = 1;
+                               size_t tmp = e->total;
+                               while((tmp /= 10)) {
+                                       ++digits;
+                               }
+                               printf("(%*zu/%*zu) %s\n", digits, e->position,
+                                               digits, e->total, e->desc);
+                       }
+                       break;
                case ALPM_EVENT_CHECKDEPS_START:
                        printf(_("checking dependencies...\n"));
                        break;
@@ -339,6 +351,7 @@ void cb_event(alpm_event_t *event)
                case ALPM_EVENT_RETRIEVE_DONE:
                case ALPM_EVENT_RETRIEVE_FAILED:
                case ALPM_EVENT_HOOK_DONE:
+               case ALPM_EVENT_HOOK_RUN_DONE:
                /* we can safely ignore those as well */
                case ALPM_EVENT_PKGDOWNLOAD_START:
                case ALPM_EVENT_PKGDOWNLOAD_DONE:
-- 
2.6.4

Reply via email to