]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/fd_plugins.c
Correct binfo size per James
[bacula/bacula] / bacula / src / filed / fd_plugins.c
index 8bcf51d17c5aa486b8a98e197f85f239b570a208..db73eae976d9776effa4fc351bad7131695f8522 100644 (file)
@@ -69,7 +69,7 @@ static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence);
 
 /* Bacula info */
 static bInfo binfo = {
-   sizeof(bFuncs),
+   sizeof(bInfo),
    FD_PLUGIN_INTERFACE_VERSION 
 };
 
@@ -144,6 +144,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
       return 1;                            /* Return if no plugins loaded */
    }
 
+   jcr->cmd_plugin = true;
    bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
    event.eventType = bEventBackupCommand;
 
@@ -165,14 +166,15 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
          continue;
       }
       Dmsg1(dbglvl, "Command plugin = %s\n", cmd);
-      /* Send the backup command */
+      /* Send the backup command to the right plugin*/
       if (plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i], &event, cmd) != bRC_OK) {
          goto bail_out;
       }
       /* Loop getting filenames to backup then saving them */
       while (!job_canceled(jcr)) { 
          memset(&sp, 0, sizeof(sp));
-         sp.type = FT_REG;
+         sp.pkt_size = sizeof(sp);
+         sp.pkt_end = sizeof(sp);
          sp.portable = true;
          sp.cmd = cmd;
          Dmsg3(dbglvl, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
@@ -181,23 +183,32 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
          if (plug_func(plugin)->startBackupFile(&plugin_ctx_list[i], &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"),
+               cmd);
+            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->link = sp.link;
          ff_pkt->type = sp.type;
          memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
          Dmsg1(dbglvl, "Save_file: file=%s\n", ff_pkt->fname);
          save_file(jcr, ff_pkt, true);
-         if (plug_func(plugin)->endBackupFile(&plugin_ctx_list[i]) != bRC_More) {
-            goto bail_out;
+         bRC rc = plug_func(plugin)->endBackupFile(&plugin_ctx_list[i]);
+         if (rc == bRC_More) {
+            continue;
          }
+         goto bail_out;
       }
    }
    Jmsg1(jcr, M_ERROR, 0, "Command plugin \"%s\" not found.\n", cmd);
 
 bail_out:
+   jcr->cmd_plugin = false;
    return 1;
 }
 
@@ -216,7 +227,7 @@ bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
            sd->bstrerror());
      return false;
    }
-   Dmsg1(000, "send: %s\n", sd->msg);
+   Dmsg1(50, "send: %s\n", sd->msg);
 
    if (start) {
       /* Send data -- not much */
@@ -238,8 +249,11 @@ bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
 /*
  * Plugin name stream found during restore.  The record passed in
  *  argument name was generated in send_plugin_name() above.
+ *
+ * Returns: true  if start of stream
+ *          false if end of steam
  */
-void plugin_name_stream(JCR *jcr, char *name)    
+bool plugin_name_stream(JCR *jcr, char *name)    
 {
    char *p = name;
    char *cmd;
@@ -248,9 +262,6 @@ void plugin_name_stream(JCR *jcr, char *name)
    int len;
    int i = 0;
    bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
-   if (!plugin_ctx_list) {
-      goto bail_out;
-   }
 
    Dmsg1(dbglvl, "Read plugin stream string=%s\n", name);
    skip_nonspaces(&p);             /* skip over jcr->JobFiles */
@@ -268,20 +279,27 @@ void plugin_name_stream(JCR *jcr, char *name)
       /*
        * End of plugin data, notify plugin, then clear flags   
        */
-      plugin = (Plugin *)jcr->plugin;
-      plug_func(plugin)->endRestoreFile(&plugin_ctx_list[i]);
+      Dmsg2(dbglvl, "End plugin data plugin=%p ctx=%p\n", jcr->plugin, jcr->plugin_ctx);
+      if (jcr->plugin) {
+         plugin = (Plugin *)jcr->plugin;
+         plug_func(plugin)->endRestoreFile((bpContext *)jcr->plugin_ctx);
+      }
       jcr->plugin_ctx = NULL;
       jcr->plugin = NULL;
       goto bail_out;
    }
+   if (!plugin_ctx_list) {
+      goto bail_out;
+   }
       
    /*
     * After this point, we are dealing with a restore start
     */
 
-   Dmsg1(dbglvl, "plugin restore cmd=%s\n", cmd);
+// Dmsg1(dbglvl, "plugin restore cmd=%s\n", cmd);
    if (!(p = strchr(cmd, ':'))) {
-      Jmsg1(jcr, M_ERROR, 0, "Malformed plugin command: %s\n", cmd);
+      Jmsg1(jcr, M_ERROR, 0,
+           _("Malformed plugin command. Name not terminated by colon: %s\n"), cmd);
       goto bail_out;
    }
    len = p - cmd;
@@ -307,10 +325,11 @@ void plugin_name_stream(JCR *jcr, char *name)
       }
       jcr->plugin_ctx = &plugin_ctx_list[i];
       jcr->plugin = plugin;
+      plug_func(plugin)->startRestoreFile((bpContext *)jcr->plugin_ctx, cmd);
       goto bail_out;
    }
 bail_out:
-   return;
+   return start;
 }
 
 /*
@@ -327,11 +346,14 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    Plugin *plugin = (Plugin *)jcr->plugin;
    struct restore_pkt rp;
-   struct io_pkt io;
+   int flags;
+   int rc;
 
    if (!set_cmd_plugin(bfd, jcr)) {
       return CF_ERROR;
    }
+   rp.pkt_size = sizeof(rp);
+   rp.pkt_end = sizeof(rp);
    rp.stream = attr->stream;
    rp.data_stream = attr->data_stream;
    rp.type = attr->type;
@@ -345,17 +367,40 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
    rp.where = jcr->where;
    rp.RegexWhere = jcr->RegexWhere;
    rp.replace = jcr->replace;
-   if (plug_func(plugin)->createFile(plugin_ctx, &rp) != bRC_OK) {
+   rp.create_status = CF_ERROR;
+   Dmsg1(dbglvl, "call plugin createFile=%s\n", rp.ofname);
+   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"),
+            rc, attr->ofname);
       return CF_ERROR;
    }
-   io.func = IO_OPEN;
-   io.count = 0;
-   io.buf = NULL;
-   io.mode = 0777 & attr->statp.st_mode;
-   io.flags = O_WRONLY;
-   if (plug_func(plugin)->pluginIO(plugin_ctx, &io) != bRC_OK) {
+   if (rp.create_status == CF_ERROR) {
+      Qmsg1(jcr, M_ERROR, 0, _("Plugin createFile call failed. Returned CF_ERROR file=%s\n"),
+            attr->ofname);
       return CF_ERROR;
    }
+   /* Created link or directory? */
+   if (rp.create_status == CF_CREATED) {
+      return rp.create_status;        /* yes, no need to bopen */
+   }
+
+   flags =  O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
+   Dmsg0(dbglvl, "call bopen\n");
+   int stat = bopen(bfd, attr->ofname, flags, S_IRUSR | S_IWUSR);
+   Dmsg1(50, "bopen status=%d\n", stat);
+   if (stat < 0) {
+      berrno be;
+      be.set_errno(bfd->berrno);
+      Qmsg2(jcr, M_ERROR, 0, _("Could not create %s: ERR=%s\n"),
+            attr->ofname, be.bstrerror());
+      Dmsg2(dbglvl,"Could not bopen file %s: ERR=%s\n", attr->ofname, be.bstrerror());
+      return CF_ERROR;
+   }
+
+   if (!is_bopen(bfd)) {
+      Dmsg0(000, "===== BFD is not open!!!!\n");
+   }
    return CF_EXTRACT;
 }
 
@@ -366,6 +411,11 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
  */
 bool plugin_set_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
 {
+   Dmsg0(dbglvl, "plugin_set_attributes\n");
+   if (is_bopen(ofd)) {
+      bclose(ofd);
+   }
+   pm_strcpy(attr->ofname, "*none*");
    return true;
 }
 
@@ -404,7 +454,6 @@ void load_fd_plugins(const char *plugin_dir)
       Dmsg1(dbglvl, "Loaded plugin: %s\n", plugin->file);
 
    }
-
 }
 
 /*
@@ -469,7 +518,9 @@ static int my_plugin_bopen(BFILE *bfd, const char *fname, int flags, mode_t mode
    Plugin *plugin = (Plugin *)jcr->plugin;
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    struct io_pkt io;
-   Dmsg0(dbglvl, "plugin_bopen\n");
+   Dmsg1(dbglvl, "plugin_bopen flags=%x\n", flags);
+   io.pkt_size = sizeof(io);
+   io.pkt_end = sizeof(io);
    io.func = IO_OPEN;
    io.count = 0;
    io.buf = NULL;
@@ -486,6 +537,7 @@ static int my_plugin_bopen(BFILE *bfd, const char *fname, int flags, mode_t mode
       errno = io.io_errno;
       bfd->lerror = io.lerror;
    }
+   Dmsg1(50, "Return from plugin open status=%d\n", io.status);
    return io.status;
 }
 
@@ -495,7 +547,9 @@ static int my_plugin_bclose(BFILE *bfd)
    Plugin *plugin = (Plugin *)jcr->plugin;
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    struct io_pkt io;
-   Dmsg0(dbglvl, "plugin_bclose\n");
+   Dmsg0(dbglvl, "===== plugin_bclose\n");
+   io.pkt_size = sizeof(io);
+   io.pkt_end = sizeof(io);
    io.func = IO_CLOSE;
    io.count = 0;
    io.buf = NULL;
@@ -509,6 +563,7 @@ static int my_plugin_bclose(BFILE *bfd)
       errno = io.io_errno;
       bfd->lerror = io.lerror;
    }
+   Dmsg1(dbglvl, "plugin_bclose stat=%d\n", io.status);
    return io.status;
 }
 
@@ -519,6 +574,8 @@ static ssize_t my_plugin_bread(BFILE *bfd, void *buf, size_t count)
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    struct io_pkt io;
    Dmsg0(dbglvl, "plugin_bread\n");
+   io.pkt_size = sizeof(io);
+   io.pkt_end = sizeof(io);
    io.func = IO_READ;
    io.count = count;
    io.buf = (char *)buf;
@@ -542,6 +599,8 @@ static ssize_t my_plugin_bwrite(BFILE *bfd, void *buf, size_t count)
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    struct io_pkt io;
    Dmsg0(dbglvl, "plugin_bwrite\n");
+   io.pkt_size = sizeof(io);
+   io.pkt_end = sizeof(io);
    io.func = IO_WRITE;
    io.count = count;
    io.buf = (char *)buf;
@@ -565,6 +624,8 @@ static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence)
    bpContext *plugin_ctx = (bpContext *)jcr->plugin_ctx;
    struct io_pkt io;
    Dmsg0(dbglvl, "plugin_bseek\n");
+   io.pkt_size = sizeof(io);
+   io.pkt_end = sizeof(io);
    io.func = IO_SEEK;
    io.offset = offset;
    io.whence = whence;