#include "bacula.h"
#include "filed.h"
+extern CLIENT *me;
+
const int dbglvl = 150;
#ifdef HAVE_WIN32
const char *plugin_type = "-fd.dll";
size_t size);
static void baculaFree(bpContext *ctx, const char *file, int line, void *mem);
static bRC baculaAddExclude(bpContext *ctx, const char *file);
+static bRC baculaAddInclude(bpContext *ctx, const char *file);
+static bRC baculaAddOptions(bpContext *ctx, const char *opts);
+static bRC baculaAddRegex(bpContext *ctx, const char *item, int type);
+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
baculaDebugMsg,
baculaMalloc,
baculaFree,
- baculaAddExclude
+ baculaAddExclude,
+ baculaAddInclude,
+ baculaAddOptions,
+ baculaAddRegex,
+ baculaAddWild,
+ baculaNewOptions,
+ baculaNewInclude
};
/*
JCR *jcr; /* jcr for plugin */
bRC rc; /* last return code */
bool disabled; /* set if plugin disabled */
- findFILESET *fileset; /* pointer to exclude files */
+ findINCEXE *exclude; /* pointer to exclude files */
+ findINCEXE *include; /* pointer to include/exclude files */
};
-static bool is_plugin_disabled(JCR *jcr)
+static bool is_plugin_disabled(bpContext *plugin_ctx)
{
bacula_ctx *b_ctx;
- if (!jcr->plugin_ctx) {
+ if (!plugin_ctx) {
return true;
}
- b_ctx = (bacula_ctx *)jcr->plugin_ctx->bContext;
+ b_ctx = (bacula_ctx *)plugin_ctx->bContext;
return b_ctx->disabled;
}
+static bool is_plugin_disabled(JCR *jcr)
+{
+ return is_plugin_disabled(jcr->plugin_ctx);
+}
/**
* Create a plugin event
+ * When receiving bEventCancelCommand, this function is called by an other thread.
*/
void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
{
+ bpContext *plugin_ctx;
bEvent event;
Plugin *plugin;
int i = 0;
+ char *name=NULL;
+ int len;
+ bRC rc;
- if (!plugin_list || !jcr || !jcr->plugin_ctx_list || job_canceled(jcr)) {
+ 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;
- jcr->plugin_ctx = &plugin_ctx_list[i++];
- jcr->plugin = plugin;
- if (is_plugin_disabled(jcr)) {
+ if (name && strncmp(plugin->file, name, len) != 0) {
+ i++;
+ continue;
+ }
+ plugin_ctx = &plugin_ctx_list[i++];
+ if (is_plugin_disabled(plugin_ctx)) {
continue;
}
- rc = plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx, &event, value);
+ rc = plug_func(plugin)->handlePluginEvent(plugin_ctx, &event, value);
if (rc != bRC_OK) {
break;
}
}
-
- jcr->plugin = NULL;
- jcr->plugin_ctx = NULL;
return;
}
int rc = bRC_OK;
int i = 0;
- if (!plugin_list || !jcr || !jcr->plugin_ctx_list || job_canceled(jcr)) {
+ if (!plugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
return false; /* Return if no plugins loaded */
}
return rc == bRC_Seen;
}
+/* Get the first part of the the plugin command
+ * systemstate:/@SYSTEMSTATE/
+ * => ret = 11
+ * => can use strncmp(plugin_name, cmd, ret);
+ */
+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:
Plugin *plugin;
int i = 0;
int len;
- char *p;
char *cmd = ff_pkt->top_fname;
struct save_pkt sp;
bEvent event;
POOL_MEM fname(PM_FNAME);
POOL_MEM link(PM_FNAME);
- if (!plugin_list || !jcr->plugin_ctx_list || job_canceled(jcr)) {
+ if (!plugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not loaded.\n", cmd);
return 1; /* Return if no plugins loaded */
}
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;
}
goto bail_out;
}
/* Loop getting filenames to backup then saving them */
- while (!job_canceled(jcr)) {
+ while (!jcr->is_job_canceled()) {
memset(&sp, 0, sizeof(sp));
sp.pkt_size = sizeof(sp);
sp.pkt_end = sizeof(sp);
sp.cmd = cmd;
Dmsg3(dbglvl, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
&sp);
- /* Get the file save parameters */
+ /* Get the file save parameters. I.e. the stat pkt ... */
if (plug_func(plugin)->startBackupFile(jcr->plugin_ctx, &sp) != bRC_OK) {
goto bail_out;
}
- if (sp.type == 0 || sp.fname == NULL) {
- Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\" returned bad startBackupFile packet.\n"),
+ if (sp.type == 0) {
+ Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no type in startBackupFile packet.\n"),
cmd);
goto bail_out;
}
* Copy fname and link because save_file() zaps them. This
* avoids zaping the plugin's strings.
*/
- pm_strcpy(fname, sp.fname);
- pm_strcpy(link, sp.link);
- ff_pkt->fname = fname.c_str();
- ff_pkt->link = link.c_str();
ff_pkt->type = sp.type;
+ if (sp.type == FT_RESTORE_FIRST) {
+ if (!sp.object_name) {
+ Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no object_name in startBackupFile packet.\n"),
+ cmd);
+ goto bail_out;
+ }
+ ff_pkt->fname = cmd; /* full plugin string */
+ ff_pkt->object_name = sp.object_name;
+ ff_pkt->object_index = sp.index; /* restore object index */
+ ff_pkt->object_compression = 0; /* no compression for now */
+ ff_pkt->object = sp.object;
+ ff_pkt->object_len = sp.object_len;
+ } else {
+ if (!sp.fname) {
+ Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no fname in startBackupFile packet.\n"),
+ cmd);
+ goto bail_out;
+ }
+ pm_strcpy(fname, sp.fname);
+ pm_strcpy(link, sp.link);
+ ff_pkt->fname = fname.c_str();
+ ff_pkt->link = link.c_str();
+ }
+
memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
- Dmsg1(dbglvl, "Save_file: file=%s\n", fname.c_str());
+ Dmsg2(dbglvl, "startBackup returned type=%d, fname=%s\n", sp.type, sp.fname);
+ if (sp.object) {
+ Dmsg2(dbglvl, "index=%d object=%s\n", sp.index, sp.object);
+ }
+ /* Call Bacula core code to backup the plugin's file */
save_file(jcr, ff_pkt, true);
bRC rc = plug_func(plugin)->endBackupFile(jcr->plugin_ctx);
if (rc == bRC_More || rc == bRC_OK) {
Jmsg0(jcr, M_FATAL, 0, _("Plugin save packet not found.\n"));
return false;
}
- if (job_canceled(jcr)) {
+ if (jcr->is_job_canceled()) {
return false;
}
sd->bstrerror());
return false;
}
- Dmsg1(50, "send: %s\n", sd->msg);
+ Dmsg1(50, "send plugin name hdr: %s\n", sd->msg);
if (start) {
/* Send data -- not much */
sd->bstrerror());
return false;
}
- Dmsg1(dbglvl, "send: %s\n", sd->msg);
+ Dmsg1(dbglvl, "send plugin start/end: %s\n", sd->msg);
sd->signal(BNET_EOD); /* indicate end of plugin name data */
return true;
}
/*
* After this point, we are dealing with a restore start
*/
-
-// Dmsg1(dbglvl, "plugin restore cmd=%s\n", cmd);
- if (!(p = strchr(cmd, ':'))) {
- Jmsg1(jcr, M_ERROR, 0,
- _("Malformed plugin command. Name not terminated by colon: %s\n"), cmd);
- goto bail_out;
- }
- len = p - cmd;
- if (len <= 0) {
+ if (!get_plugin_name(jcr, cmd, &len)) {
goto bail_out;
}
int flags;
int rc;
- if (!plugin || !plugin_ctx || !set_cmd_plugin(bfd, jcr) || job_canceled(jcr)) {
+ if (!plugin || !plugin_ctx || !set_cmd_plugin(bfd, jcr) || jcr->is_job_canceled()) {
return CF_ERROR;
}
+
rp.pkt_size = sizeof(rp);
rp.pkt_end = sizeof(rp);
rp.stream = attr->stream;
rp.RegexWhere = jcr->RegexWhere;
rp.replace = jcr->replace;
rp.create_status = CF_ERROR;
- Dmsg1(dbglvl, "call plugin createFile=%s\n", rp.ofname);
+ Dmsg4(dbglvl, "call plugin createFile stream=%d type=%d LinkFI=%d File=%s\n",
+ rp.stream, rp.type, rp.LinkFI, rp.ofname);
+ if (rp.attrEx) {
+ Dmsg1(dbglvl, "attrEx=\"%s\"\n", rp.attrEx);
+ }
rc = plug_func(plugin)->createFile(plugin_ctx, &rp);
if (rc != bRC_OK) {
Qmsg2(jcr, M_ERROR, 0, _("Plugin createFile call failed. Stat=%d file=%s\n"),
Dmsg0(dbglvl, "plugin list is NULL\n");
return;
}
- if (job_canceled(jcr)) {
+ if (jcr->is_job_canceled() || jcr->JobId == 0) {
return;
}
case bVarVssObject:
#ifdef HAVE_WIN32
if (g_pVSSClient) {
- value = g_pVSSClient->GetVssObject();
+ *(void **)value = g_pVSSClient->GetVssObject();
+ break;
+ }
+#endif
+ return bRC_Error;
+ case bVarVssDllHandle:
+#ifdef HAVE_WIN32
+ if (g_pVSSClient) {
+ *(void **)value = g_pVSSClient->GetVssDllHandle();
break;
}
#endif
return bRC_Error;
+ case bVarWorkingDir:
+ *(void **)value = me->working_directory;
+ break;
}
return bRC_OK;
}
#endif
}
+static bool is_ctx_good(bpContext *ctx, JCR *&jcr, bacula_ctx *&bctx)
+{
+ if (!ctx) {
+ return false;
+ }
+ bctx = (bacula_ctx *)ctx->bContext;
+ if (!bctx) {
+ return false;
+ }
+ jcr = bctx->jcr;
+ if (!jcr) {
+ return false;
+ }
+ return true;
+}
+
/**
* Let the plugin define files/directories to be excluded
* from the main backup.
{
JCR *jcr;
bacula_ctx *bctx;
- if (ctx) {
+ if (!is_ctx_good(ctx, jcr, bctx)) {
return bRC_Error;
}
if (!file) {
return bRC_Error;
}
- bctx = (bacula_ctx *)ctx->bContext;
- if (!bctx) {
+ if (!bctx->exclude) {
+ bctx->exclude = new_exclude(jcr);
+ new_options(jcr, bctx->exclude);
+ }
+ set_incexe(jcr, bctx->exclude);
+ add_file_to_fileset(jcr, file, true);
+ Dmsg1(100, "Add exclude file=%s\n", file);
+ return bRC_OK;
+}
+
+/**
+ * Let the plugin define files/directories to be excluded
+ * from the main backup.
+ */
+static bRC baculaAddInclude(bpContext *ctx, const char *file)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
return bRC_Error;
}
- jcr = bctx->jcr;
- if (!jcr) {
+ if (!file) {
+ return bRC_Error;
+ }
+ if (!bctx->include) {
+ bctx->include = new_preinclude(jcr);
+ new_options(jcr, bctx->include);
+ }
+ set_incexe(jcr, bctx->include);
+ add_file_to_fileset(jcr, file, true);
+ Dmsg1(100, "Add include file=%s\n", file);
+ return bRC_OK;
+}
+
+static bRC baculaAddOptions(bpContext *ctx, const char *opts)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
+ return bRC_Error;
+ }
+ if (!opts) {
+ return bRC_Error;
+ }
+ add_options_to_fileset(jcr, opts);
+ Dmsg1(1000, "Add options=%s\n", opts);
+ return bRC_OK;
+}
+
+static bRC baculaAddRegex(bpContext *ctx, const char *item, int type)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
+ return bRC_Error;
+ }
+ if (!item) {
+ return bRC_Error;
+ }
+ add_regex_to_fileset(jcr, item, type);
+ Dmsg1(100, "Add regex=%s\n", item);
+ return bRC_OK;
+}
+
+static bRC baculaAddWild(bpContext *ctx, const char *item, int type)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
+ return bRC_Error;
+ }
+ if (!item) {
+ return bRC_Error;
+ }
+ add_wild_to_fileset(jcr, item, type);
+ Dmsg1(100, "Add wild=%s\n", item);
+ return bRC_OK;
+}
+
+static bRC baculaNewOptions(bpContext *ctx)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
return bRC_Error;
}
- if (!bctx->fileset) {
- bctx->fileset = new_exclude(jcr);
+ (void)new_options(jcr, NULL);
+ return bRC_OK;
+}
+
+static bRC baculaNewInclude(bpContext *ctx)
+{
+ JCR *jcr;
+ bacula_ctx *bctx;
+ if (!is_ctx_good(ctx, jcr, bctx)) {
+ return bRC_Error;
}
- add_file_to_fileset(jcr, file, bctx->fileset, true);
+ (void)new_include(jcr);
return bRC_OK;
}
Dmsg0(dbglvl, "bacula: OK ...\n");
close_memory_pool();
- sm_dump(false);
+ sm_dump(false); /* unit test */
return 0;
}