Skip to content

Commit

Permalink
Add bEventNewPluginOptions event.
Browse files Browse the repository at this point in the history
When pushing explicit plugin arguments use a new event
bEventNewPluginOptions instead of reusing the bEventPluginCommand
event which is also used when parsing the fileset when doing a backup.

Most plugins should now also allow multiple plugin lines within one
include block of the fileset which was broken when it used the new
config parser. In combination with forcing certain explicit restore
options by the director using the plugin options passing this may lead
to some surprises however so when using specific plugin options its wise
to only restore one file at a time to be sure the right restore options
are used.
  • Loading branch information
Marco van Wieringen committed Jan 8, 2014
1 parent 3112000 commit 1bdf0ff
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/filed/dir_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ static bool pluginoptions_cmd(JCR *jcr)
}

unbash_spaces(msg);
generate_plugin_event(jcr, bEventPluginCommand, (void *)msg);
generate_plugin_event(jcr, bEventNewPluginOptions, (void *)msg);
free_memory(msg);

return dir->fsend(OKPluginOptions);
Expand Down
3 changes: 2 additions & 1 deletion src/filed/fd_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ typedef enum {
bEventVssPrepareSnapshot = 22,
bEventOptionPlugin = 23,
bEventHandleBackupFile = 24, /* Used with Options Plugin */
bEventComponentInfo = 25 /* Plugin component */
bEventComponentInfo = 25, /* Plugin component */
bEventNewPluginOptions = 26
} bEventType;

#define FD_NR_EVENTS bEventHandleBackupFile /* keep this updated ! */
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/filed/bareos_fd_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
bEventVssPrepareSnapshot = 22,
bEventOptionPlugin = 23,
bEventHandleBackupFile = 24,
bEventComponentInfo = 25
bEventComponentInfo = 25,
bEventNewPluginOptions = 26
)

bIOPS = dict(
Expand Down
86 changes: 74 additions & 12 deletions src/plugins/filed/bpipe-fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ static pFuncs pluginFuncs = {
struct plugin_ctx {
boffset_t offset;
BPIPE *pfd; /* bpipe() descriptor */
char *plugin_options; /* Override of plugin options passed in */
char *fname; /* Filename to "backup/restore" */
char *reader; /* Reader program for backup */
char *writer; /* Writer program for backup */
Expand Down Expand Up @@ -189,7 +190,8 @@ static bRC newPlugin(bpContext *ctx)
ctx->pContext = (void *)p_ctx; /* set our context pointer */

bfuncs->registerBareosEvents(ctx,
5,
6,
bEventNewPluginOptions,
bEventPluginCommand,
bEventJobStart,
bEventRestoreCommand,
Expand All @@ -213,13 +215,19 @@ static bRC freePlugin(bpContext *ctx)
if (p_ctx->fname) {
free(p_ctx->fname);
}

if (p_ctx->reader) {
free(p_ctx->reader);
}

if (p_ctx->writer) {
free(p_ctx->writer);
}

if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
}

free(p_ctx); /* free our private context */
p_ctx = NULL;
return bRC_OK;
Expand All @@ -246,7 +254,9 @@ static bRC setPluginValue(bpContext *ctx, pVariable var, void *value)
*/
static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
{
bRC retval = bRC_OK;
struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;

if (!p_ctx) {
return bRC_Error;
}
Expand All @@ -268,14 +278,32 @@ static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
* Fall-through wanted
*/
case bEventPluginCommand:
return parse_plugin_definition(ctx, value);
retval = parse_plugin_definition(ctx, value);
break;
case bEventNewPluginOptions:
/*
* Free any previous value.
*/
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
p_ctx->plugin_options = NULL;
}

retval = parse_plugin_definition(ctx, value);

/*
* Save that we got a plugin override.
*/
p_ctx->plugin_options = bstrdup((char *)value);
break;
default:
Jmsg(ctx, M_FATAL, "bpipe-fd: unknown event=%d\n", event->eventType);
Dmsg(ctx, dbglvl, "bpipe-fd: unknown event=%d\n", event->eventType);
retval = bRC_Error;
break;
}

return bRC_OK;
return retval;
}

/*
Expand Down Expand Up @@ -431,6 +459,11 @@ static bRC startRestoreFile(bpContext *ctx, const char *cmd)
*/
static bRC endRestoreFile(bpContext *ctx)
{
struct plugin_ctx *p_ctx = (struct plugin_ctx *)ctx->pContext;
if (!p_ctx) {
return bRC_Error;
}

return bRC_OK;
}

Expand Down Expand Up @@ -575,13 +608,25 @@ static inline bool parse_boolean(const char *argument_value)
/*
* Only set destination to value when it has no previous setting.
*/
static inline void set_if_null(char **destination, char *value)
static inline void set_string_if_null(char **destination, char *value)
{
if (!*destination) {
*destination = bstrdup(value);
}
}

/*
* Always set destination to value and clean any previous one.
*/
static inline void set_string(char **destination, char *value)
{
if (*destination) {
free(*destination);
}

*destination = bstrdup(value);
}

/*
* Parse the plugin definition passed in.
*
Expand All @@ -594,12 +639,15 @@ static bRC parse_plugin_definition(bpContext *ctx, void *value)
int i, cnt;
char *plugin_definition, *bp, *argument, *argument_value;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;
bool keep_existing;
bool allow_old_plugin_definition = true;

if (!p_ctx || !value) {
return bRC_Error;
}

keep_existing = (p_ctx->plugin_options) ? true : false;

/*
* Parse the plugin definition.
* Make a private copy of the whole string.
Expand Down Expand Up @@ -672,11 +720,18 @@ static bRC parse_plugin_definition(bpContext *ctx, void *value)
break;
}

/*
* Keep the first value, ignore any next setting.
*/
if (str_destination) {
set_if_null(str_destination, argument);
if (keep_existing) {
/*
* Keep the first value, ignore any next setting.
*/
set_string_if_null(str_destination, argument);
} else {
/*
* Overwrite any existing value.
*/
set_string(str_destination, argument);
}
}

/*
Expand Down Expand Up @@ -720,11 +775,18 @@ static bRC parse_plugin_definition(bpContext *ctx, void *value)
break;
}

/*
* Keep the first value, ignore any next setting.
*/
if (str_destination) {
set_if_null(str_destination, argument_value);
if (keep_existing) {
/*
* Keep the first value, ignore any next setting.
*/
set_string_if_null(str_destination, argument);
} else {
/*
* Overwrite any existing value.
*/
set_string(str_destination, argument);
}
}

/*
Expand Down
59 changes: 53 additions & 6 deletions src/plugins/filed/python-fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ struct plugin_ctx {
int32_t backup_level;
#ifdef HAVE_PYTHON
bool python_loaded;
char *plugin_options;
char *module_path;
char *module_name;
char *fname;
Expand Down Expand Up @@ -235,12 +236,13 @@ static bRC newPlugin(bpContext *ctx)
* any other events it is interested in.
*/
bfuncs->registerBareosEvents(ctx,
5,
bEventBackupCommand,
6,
bEventNewPluginOptions,
bEventPluginCommand,
bEventJobStart,
bEventRestoreCommand,
bEventEstimateCommand,
bEventLevel,
bEventPluginCommand);
bEventBackupCommand);

return bRC_OK;
}
Expand All @@ -258,6 +260,10 @@ static bRC freePlugin(bpContext *ctx)
}

#ifdef HAVE_PYTHON
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
}

if (p_ctx->module_path) {
free(p_ctx->module_path);
}
Expand Down Expand Up @@ -391,6 +397,24 @@ static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
case bEventPluginCommand:
event_dispatched = true;
retval = parse_plugin_definition(ctx, value);
break;
case bEventNewPluginOptions:
/*
* Free any previous value.
*/
if (p_ctx->plugin_options) {
free(p_ctx->plugin_options);
p_ctx->plugin_options = NULL;
}

event_dispatched = true;
retval = parse_plugin_definition(ctx, value);

/*
* Save that we got a plugin override.
*/
p_ctx->plugin_options = bstrdup((char *)value);
break;
default:
break;
}
Expand Down Expand Up @@ -421,6 +445,10 @@ static bRC handlePluginEvent(bpContext *ctx, bEvent *event, void *value)
* Fall-through wanted
*/
case bEventPluginCommand:
/*
* Fall-through wanted
*/
case bEventNewPluginOptions:
/*
* See if we already loaded the Python modules.
*/
Expand Down Expand Up @@ -666,13 +694,25 @@ static inline bool parse_boolean(const char *argument_value)
/*
* Only set destination to value when it has no previous setting.
*/
static inline void set_if_null(char **destination, char *value)
static inline void set_string_if_null(char **destination, char *value)
{
if (!*destination) {
*destination = bstrdup(value);
}
}

/*
* Always set destination to value and clean any previous one.
*/
static inline void set_string(char **destination, char *value)
{
if (*destination) {
free(*destination);
}

*destination = bstrdup(value);
}

/*
* Parse the plugin definition passed in.
*
Expand All @@ -683,13 +723,16 @@ static inline void set_if_null(char **destination, char *value)
static bRC parse_plugin_definition(bpContext *ctx, void *value)
{
int i;
bool keep_existing;
char *plugin_definition, *bp, *argument, *argument_value;
plugin_ctx *p_ctx = (plugin_ctx *)ctx->pContext;

if (!value) {
return bRC_Error;
}

keep_existing = (p_ctx->plugin_options) ? true : false;

/*
* Parse the plugin definition.
* Make a private copy of the whole string.
Expand Down Expand Up @@ -756,7 +799,11 @@ static bRC parse_plugin_definition(bpContext *ctx, void *value)
* Keep the first value, ignore any next setting.
*/
if (str_destination) {
set_if_null(str_destination, argument_value);
if (keep_existing) {
set_string_if_null(str_destination, argument_value);
} else {
set_string(str_destination, argument_value);
}
}

/*
Expand Down

0 comments on commit 1bdf0ff

Please sign in to comment.