]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/fd-plugins.c
Plugin update
[bacula/bacula] / bacula / src / filed / fd-plugins.c
index d407240a1d8f42d65ea7d300b139ee820ecd2a6b..4f866b766a38f6d9855af2a44ce6c69641b266f7 100644 (file)
 const int dbglvl = 50;
 const char *plugin_type = "-fd.so";
 
-extern int save_file(FF_PKT *ff_pkt, void *vjcr, bool top_level);
+extern int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level);
 
 
 /* Function pointers to be set here */
-extern int     (*plugin_bopen)(JCR *jcr, const char *fname, int flags, mode_t mode);
-extern int     (*plugin_bclose)(JCR *jcr);
-extern ssize_t (*plugin_bread)(JCR *jcr, void *buf, size_t count);
-extern ssize_t (*plugin_bwrite)(JCR *jcr, void *buf, size_t count);
+extern DLL_IMP_EXP int     (*plugin_bopen)(JCR *jcr, const char *fname, int flags, mode_t mode);
+extern DLL_IMP_EXP int     (*plugin_bclose)(JCR *jcr);
+extern DLL_IMP_EXP ssize_t (*plugin_bread)(JCR *jcr, void *buf, size_t count);
+extern DLL_IMP_EXP ssize_t (*plugin_bwrite)(JCR *jcr, void *buf, size_t count);
+extern DLL_IMP_EXP boffset_t (*plugin_blseek)(JCR *jcr, boffset_t offset, int whence);
 
 
 /* Forward referenced functions */
@@ -60,6 +61,7 @@ static int     my_plugin_bopen(JCR *jcr, const char *fname, int flags, mode_t mo
 static int     my_plugin_bclose(JCR *jcr);
 static ssize_t my_plugin_bread(JCR *jcr, void *buf, size_t count);
 static ssize_t my_plugin_bwrite(JCR *jcr, void *buf, size_t count);
+static boffset_t my_plugin_blseek(JCR *jcr, boffset_t offset, int whence);
 
 
 /* Bacula info */
@@ -79,6 +81,21 @@ static bFuncs bfuncs = {
    baculaDebugMsg
 };
 
+/*   
+ * Sequence of calls for a backup:
+ * 1. generate_plugin_event called with bEventPluginCommand
+ *    the command string is passed as an argument.
+ * 2. we find the plugin requested on the command string
+ * 3. we generate a bEventPluginCommand event to the specified plugin
+ * 4. we make a startPluginBackup call to the plugin, which gives
+ *    us the data we need in save_pkt
+ * 5. we call Bacula's save_file() subroutine to save the specified
+ *    file.  The plugin will be called at pluginIO() to supply the
+ *    file data.
+ *
+ * Sequence of calls for restore:
+ *   See subroutine plugin_name_stream() below.
+ */
 
 /*
  * Create a plugin event 
@@ -91,6 +108,8 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
    int len;
    char *p;
    char *cmd = (char *)value;
+   struct save_pkt sp;
+   FF_PKT *ff_pkt;
 
    if (!plugin_list) {
       return;
@@ -100,15 +119,139 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
    event.eventType = eventType;
 
    Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
-   if (eventType != bEventPluginCommand) {
+   switch (eventType) {
+   case bEventPluginCommand:  
+      /* Handle plugin command here backup */
+      Dmsg1(100, "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) {
+         goto bail_out;
+      }
+
+      foreach_alist(plugin, plugin_list) {
+         Dmsg3(100, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
+         if (strncmp(plugin->file, cmd, len) != 0) {
+            i++;
+            continue;
+         }
+         while (!job_canceled(jcr)) { 
+            Dmsg1(100, "Command plugin = %s\n", cmd);
+            if (plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i], &event, value) != bRC_OK) {
+               goto bail_out;
+            }
+            memset(&sp, 0, sizeof(sp));
+            sp.type = FT_REG;
+            sp.portable = true;
+            sp.cmd = cmd;
+            Dmsg3(000, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
+                   &sp);
+            if (plug_func(plugin)->startPluginBackup(&plugin_ctx_list[i], &sp) != bRC_OK) {
+               goto bail_out;
+            }
+            jcr->plugin_ctx = &plugin_ctx_list[i];
+            jcr->plugin = plugin;
+            jcr->plugin_sp = &sp;
+            ff_pkt = jcr->ff;
+            ff_pkt->fname = sp.fname;
+            ff_pkt->type = sp.type;
+            memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
+            Dmsg1(000, "Save_file: file=%s\n", ff_pkt->fname);
+            save_file(jcr, ff_pkt, true);
+            /* ***FIXME***/
+            /* add call to endPluginBackup() and loop on bRC_MORE */
+            goto bail_out;
+         }
+      }
+      Jmsg1(jcr, M_ERROR, 0, "Command plugin \"%s\" not found.\n", cmd);
+      break;
+
+   default:
       /* Pass event to every plugin */
       foreach_alist(plugin, plugin_list) {
-         plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i++], &event, value);
+         bRC rc;
+         rc = plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i++], &event, value);
+         if (rc != bRC_OK) {
+            break;
+         }
       }
+      break;
+   }
+
+      
+bail_out:
+   return;
+}
+
+/* 
+ * Send plugin name start/end record to SD
+ */
+bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
+{
+   int stat;
+   struct save_pkt *sp = (struct save_pkt *)jcr->plugin_sp;
+  
+   if (!sd->fsend("%ld %d %d", jcr->JobFiles, STREAM_PLUGIN_NAME, start)) {
+     Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
+           sd->bstrerror());
+     return false;
+   }
+   stat = sd->fsend("%ld %d %d %s%c", jcr->JobFiles, start, sp->portable, sp->cmd, 0);
+   if (!stat) {
+      Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
+            sd->bstrerror());
+         return false;
+   }
+   sd->signal(BNET_EOD);            /* indicate end of plugin name data */
+   return true;
+}
+
+/*
+ * Plugin name stream found during restore.  This is the record
+ *  that was generated in send_plugin_name() above.
+ */
+void plugin_name_stream(JCR *jcr, char *name)    
+{
+   char *p = name;
+   char *cmd;
+   bool start, portable;
+   bEvent event;
+   Plugin *plugin;
+   int len;
+   int i = 0;
+   bpContext *plugin_ctx_list;
+
+   Dmsg1(000, "plugin stream string=%s\n", name);
+   skip_nonspaces(&p);             /* skip over jcr->JobFiles */
+   skip_spaces(&p);
+   start = *p == '1';
+   skip_nonspaces(&p);             /* skip start/end flag */
+   skip_spaces(&p);
+   portable = *p == '1';
+   skip_nonspaces(&p);             /* skip portable flag */
+   skip_spaces(&p);
+   cmd = p;
+   event.eventType = start ? bEventRestoreStart : bEventRestoreEnd;
+    
+   /* Check for restore end */
+   if (!start) {
+      /*
+       * If end of restore, notify plugin, then clear flags   
+       */
+      plugin = (Plugin *)jcr->plugin;
+      plug_func(plugin)->handlePluginEvent((bpContext *)jcr->plugin_ctx, &event, cmd);
+      jcr->plugin_ctx = NULL;
+      jcr->plugin = NULL;
       goto bail_out;
    }
+      
+   /*
+    * After this point, we are dealing with a restore start
+    */
 
-   /* Handle plugin command here (backup/restore of file) */
    Dmsg1(000, "plugin cmd=%s\n", cmd);
    if (!(p = strchr(cmd, ':'))) {
       Jmsg1(jcr, M_ERROR, 0, "Malformed plugin command: %s\n", cmd);
@@ -119,39 +262,28 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
       goto bail_out;
    }
 
+   
+   plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
    foreach_alist(plugin, plugin_list) {
-      Dmsg3(000, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
-      if (strncmp(plugin->file, cmd, len) == 0) {
-         struct save_pkt sp;
-         FF_PKT *ff_pkt;
-         Dmsg1(000, "Command plugin = %s\n", cmd);
-         if (plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i], &event, value) != bRC_OK) {
-            goto bail_out;
-         }
-         memset(&sp, 0, sizeof(sp));
-         sp.type = FT_REG;
-         sp.portable = true;
-         Dmsg0(000, "Plugin startBackup\n");
-         if (plug_func(plugin)->startPluginBackup(&plugin_ctx_list[i], &sp) != bRC_OK) {
-            goto bail_out;
-         }
-         jcr->plugin_ctx = &plugin_ctx_list[i];
-         jcr->plugin = plugin;
-         ff_pkt = jcr->ff;
-         ff_pkt->fname = sp.fname;
-         ff_pkt->type = sp.type;
-         ff_pkt->statp = sp.statp;        /* structure copy */
-         Dmsg1(000, "Save_file: file=%s\n", ff_pkt->fname);
-         save_file(ff_pkt, (void *)jcr, true);
+      Dmsg3(100, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
+      if (strncmp(plugin->file, cmd, len) != 0) {
+         i++;
+         continue;
+      }
+      Dmsg1(100, "Command plugin = %s\n", cmd);
+      if (plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i], 
+            &event, (void *)name) != bRC_OK) {
          goto bail_out;
       }
-      i++;
+      jcr->plugin_ctx = &plugin_ctx_list[i];
+      jcr->plugin = plugin;
+      goto bail_out;
    }
-      
 bail_out:
    return;
 }
 
+
 void load_fd_plugins(const char *plugin_dir)
 {
    if (!plugin_dir) {
@@ -164,6 +296,7 @@ void load_fd_plugins(const char *plugin_dir)
    plugin_bclose = my_plugin_bclose;
    plugin_bread  = my_plugin_bread;
    plugin_bwrite = my_plugin_bwrite;
+   plugin_blseek = my_plugin_blseek;
 
 }
 
@@ -271,6 +404,19 @@ static ssize_t my_plugin_bwrite(JCR *jcr, void *buf, size_t count)
    return (ssize_t)io.status;
 }
 
+static boffset_t my_plugin_blseek(JCR *jcr, boffset_t offset, int whence)
+{
+   Plugin *plugin = (Plugin *)jcr->plugin;
+   bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
+   struct io_pkt io;
+   Dmsg0(000, "plugin_bseek\n");
+   io.func = IO_SEEK;
+   io.offset = offset;
+   io.whence = whence;
+   plug_func(plugin)->pluginIO(plugin_ctx, &io);
+   return (boffset_t)io.offset;
+}
+
 /* ==============================================================
  *
  * Callbacks from the plugin
@@ -346,6 +492,7 @@ int     (*plugin_bopen)(JCR *jcr, const char *fname, int flags, mode_t mode) = N
 int     (*plugin_bclose)(JCR *jcr) = NULL;
 ssize_t (*plugin_bread)(JCR *jcr, void *buf, size_t count) = NULL;
 ssize_t (*plugin_bwrite)(JCR *jcr, void *buf, size_t count) = NULL;
+boffset_t (*plugin_blseek)(JCR *jcr, boffset_t offset, int whence) = NULL;
 
 int save_file(FF_PKT *ff_pkt, void *vjcr, bool top_level)
 {