From 365bd6a3a04a802626e01c097ea9248037f2e7fc Mon Sep 17 00:00:00 2001 From: Eric Bollengier Date: Thu, 6 May 2010 13:18:14 +0200 Subject: [PATCH] Implement bEventPluginCommand event for systemstate plugin --- bacula/src/filed/fd_plugins.c | 53 +++++++++++++++++++++++++++-------- bacula/src/filed/fd_plugins.h | 3 +- bacula/src/filed/job.c | 45 +++++++++++++++-------------- 3 files changed, 66 insertions(+), 35 deletions(-) diff --git a/bacula/src/filed/fd_plugins.c b/bacula/src/filed/fd_plugins.c index a55427796c..26cc35ebf4 100644 --- a/bacula/src/filed/fd_plugins.c +++ b/bacula/src/filed/fd_plugins.c @@ -72,6 +72,7 @@ static bRC baculaAddWild(bpContext *ctx, const char *item, int type); static bRC baculaNewOptions(bpContext *ctx); static bRC baculaNewInclude(bpContext *ctx); static bool is_plugin_compatible(Plugin *plugin); +static bool get_plugin_name(JCR *jcr, char *cmd, int *ret); /* * These will be plugged into the global pointer structure for @@ -140,19 +141,37 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value) bEvent event; Plugin *plugin; int i = 0; + char *name=NULL; + int len; + bRC rc; if (!plugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) { return; /* Return if no plugins loaded */ } + + /* Some events are sent to only a particular plugin */ + switch(eventType) { + case bEventPluginCommand: + name = (char *)value; + if (!get_plugin_name(jcr, name, &len)) { + return; + } + break; + default: + break; + } bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list; event.eventType = eventType; Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId); - /* Pass event to every plugin */ + /* Pass event to every plugin (except if name is set) */ foreach_alist(plugin, plugin_list) { - bRC rc; + if (name && strncmp(plugin->file, name, len) != 0) { + i++; + continue; + } jcr->plugin_ctx = &plugin_ctx_list[i++]; jcr->plugin = plugin; if (is_plugin_disabled(jcr)) { @@ -207,6 +226,26 @@ bool plugin_check_file(JCR *jcr, char *fname) return rc == bRC_Seen; } +static bool get_plugin_name(JCR *jcr, char *cmd, int *ret) +{ + char *p; + int len; + if (!cmd) { + return false; + } + /* Handle plugin command here backup */ + Dmsg1(dbglvl, "plugin cmd=%s\n", cmd); + if (!(p = strchr(cmd, ':'))) { + Jmsg1(jcr, M_ERROR, 0, "Malformed plugin command: %s\n", cmd); + return false; + } + len = p - cmd; + if (len <= 0) { + return false; + } + *ret = len; + return true; +} /** * Sequence of calls for a backup: @@ -228,7 +267,6 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level) Plugin *plugin; int i = 0; int len; - char *p; char *cmd = ff_pkt->top_fname; struct save_pkt sp; bEvent event; @@ -244,14 +282,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level) bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list; event.eventType = bEventBackupCommand; - /* Handle plugin command here backup */ - Dmsg1(dbglvl, "plugin cmd=%s\n", cmd); - if (!(p = strchr(cmd, ':'))) { - Jmsg1(jcr, M_ERROR, 0, "Malformed plugin command: %s\n", cmd); - goto bail_out; - } - len = p - cmd; - if (len <= 0) { + if (!get_plugin_name(jcr, cmd, &len)) { goto bail_out; } diff --git a/bacula/src/filed/fd_plugins.h b/bacula/src/filed/fd_plugins.h index 9745db1988..e66d770de1 100644 --- a/bacula/src/filed/fd_plugins.h +++ b/bacula/src/filed/fd_plugins.h @@ -197,7 +197,8 @@ typedef enum { bEventVssRestoreLoadComponentMetadata = 15, bEventVssRestoreSetComponentsSelected = 16, bEventRestoreObject = 17, - bEventEndFileSet = 18 + bEventEndFileSet = 18, + bEventPluginCommand = 19 } bEventType; typedef struct s_bEvent { diff --git a/bacula/src/filed/job.c b/bacula/src/filed/job.c index 91ead1f317..4fa151f379 100644 --- a/bacula/src/filed/job.c +++ b/bacula/src/filed/job.c @@ -734,6 +734,22 @@ static bool init_fileset(JCR *jcr) return true; } +static void append_file(JCR *jcr, findINCEXE *incexe, + const char *buf, bool is_file) +{ + if (is_file) { + incexe->name_list.append(new_dlistString(buf)); + + } else if (me->plugin_directory) { + generate_plugin_event(jcr, bEventPluginCommand, (void *)buf); + incexe->plugin_list.append(new_dlistString(buf)); + + } else { + Jmsg(jcr, M_FATAL, 0, + _("Plugin Directory not defined. Cannot use plugin: \"%s\"\n"), + buf); + } +} /** * Add fname to include/exclude fileset list. First check for @@ -768,11 +784,7 @@ void add_file_to_fileset(JCR *jcr, const char *fname, bool is_file) free_pool_memory(fn); while (fgets(buf, sizeof(buf), bpipe->rfd)) { strip_trailing_junk(buf); - if (is_file) { - fileset->incexe->name_list.append(new_dlistString(buf)); - } else { - fileset->incexe->plugin_list.append(new_dlistString(buf)); - } + append_file(jcr, fileset->incexe, buf, is_file); } if ((stat=close_bpipe(bpipe)) != 0) { berrno be; @@ -786,32 +798,19 @@ void add_file_to_fileset(JCR *jcr, const char *fname, bool is_file) p++; /* skip over < */ if ((ffd = fopen(p, "rb")) == NULL) { berrno be; - Jmsg(jcr, M_FATAL, 0, _("Cannot open FileSet input file: %s. ERR=%s\n"), + Jmsg(jcr, M_FATAL, 0, + _("Cannot open FileSet input file: %s. ERR=%s\n"), p, be.bstrerror()); return; } while (fgets(buf, sizeof(buf), ffd)) { strip_trailing_junk(buf); - Dmsg1(100, "%s\n", buf); - if (is_file) { - fileset->incexe->name_list.append(new_dlistString(buf)); - } else { - fileset->incexe->plugin_list.append(new_dlistString(buf)); - } - } + append_file(jcr, fileset->incexe, buf, is_file); + } fclose(ffd); break; default: - if (is_file) { - fileset->incexe->name_list.append(new_dlistString(fname)); - } else { - if (me->plugin_directory) { - fileset->incexe->plugin_list.append(new_dlistString(fname)); - } else { - Jmsg(jcr, M_FATAL, 0, _("Plugin Directory not defined. Cannot use plugin: \"%\"\n"), - fname); - } - } + append_file(jcr, fileset->incexe, fname, is_file); break; } } -- 2.39.5