]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/fd_plugins.c
Ensure that StorageId is updated after write
[bacula/bacula] / bacula / src / filed / fd_plugins.c
index cc31de09b61134f54462b737e6a833befc9dc560..89895b6473f72a6a0ee3ee7081c85c826df342fb 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2012 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
 #include "filed.h"
 
 extern CLIENT *me;
+extern DLL_IMP_EXP char *exepath;
+extern DLL_IMP_EXP char *version;
+extern DLL_IMP_EXP char *dist_name;
+extern DLL_IMP_EXP int beef;
 
 const int dbglvl = 150;
 #ifdef HAVE_WIN32
@@ -44,6 +48,7 @@ const char *plugin_type = "-fd.so";
 #endif
 
 extern int save_file(JCR *jcr, FF_PKT *ff_pkt, bool top_level);
+extern bool check_changes(JCR *jcr, FF_PKT *ff_pkt);
 
 /* Function pointers to be set here */
 extern DLL_IMP_EXP int     (*plugin_bopen)(BFILE *bfd, const char *fname, int flags, mode_t mode);
@@ -71,8 +76,10 @@ 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 bRC baculaNewPreInclude(bpContext *ctx);
 static bool is_plugin_compatible(Plugin *plugin);
 static bool get_plugin_name(JCR *jcr, char *cmd, int *ret);
+static bRC baculaCheckChanges(bpContext *ctx, struct save_pkt *sp);
 
 /*
  * These will be plugged into the global pointer structure for
@@ -84,6 +91,7 @@ static ssize_t my_plugin_bread(BFILE *bfd, void *buf, size_t count);
 static ssize_t my_plugin_bwrite(BFILE *bfd, void *buf, size_t count);
 static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence);
 
+#define for_this_plug(plugin, str, len) (((len) == (plugin)->file_len) && strncmp((plugin)->file, str, len) == 0)
 
 /* Bacula info */
 static bInfo binfo = {
@@ -108,7 +116,9 @@ static bFuncs bfuncs = {
    baculaAddRegex,
    baculaAddWild,
    baculaNewOptions,
-   baculaNewInclude
+   baculaNewInclude,
+   baculaNewPreInclude,
+   baculaCheckChanges
 };
 
 /* 
@@ -129,6 +139,9 @@ static bool is_plugin_disabled(bpContext *plugin_ctx)
       return true;
    }
    b_ctx = (bacula_ctx *)plugin_ctx->bContext;
+   if (!b_ctx) {
+      return true;
+   }
    return b_ctx->disabled;
 }
 
@@ -138,54 +151,94 @@ static bool is_plugin_disabled(JCR *jcr)
 }
 
 /**
- * Create a plugin event 
- * When receiving bEventCancelCommand, this function is called by an other thread. 
+ * 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;
+   int i;
+   char *name = NULL;
+   int len = 0;
+   bool call_if_canceled = false;
+   restore_object_pkt *rop;
 
-   if (!plugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
+   if (!bplugin_list || !jcr || !jcr->plugin_ctx_list) {
       return;                         /* Return if no plugins loaded */
    }
    
-   /* Some events are sent to only a particular plugin */
+   /*
+    * Some events are sent to only a particular plugin or must be
+    *  called even if the job is canceled
+    */
    switch(eventType) {
    case bEventPluginCommand:
+   case bEventOptionPlugin:
       name = (char *)value;
       if (!get_plugin_name(jcr, name, &len)) {
          return;
       }
       break;
+   case bEventRestoreObject:
+      /* After all RestoreObject, we have it one more time with value=NULL */
+      if (value) {
+         /* Some RestoreObjects may not have a plugin name */
+         rop = (restore_object_pkt *)value;
+         if (*rop->plugin_name) {
+            name = rop->plugin_name;
+            get_plugin_name(jcr, name, &len);
+         }
+      }
+
+      break;
+   case bEventEndBackupJob:
+   case bEventEndVerifyJob:
+      call_if_canceled = true;
+      break;
+   case bEventStartRestoreJob:
+      foreach_alist_index(i, plugin, bplugin_list) {
+         plugin->restoreFileStarted = false;
+         plugin->createFileCalled = false;
+      }
+      break;
+   case bEventEndRestoreJob:
+      call_if_canceled = true;
+      if (jcr->plugin && jcr->plugin->restoreFileStarted) {
+         plug_func(jcr->plugin)->endRestoreFile(jcr->plugin_ctx);
+      }
+      if (jcr->plugin) {
+         jcr->plugin->restoreFileStarted = false;
+         jcr->plugin->createFileCalled = false;
+      }
+      break;
    default:
       break;
    }
 
+   if (!call_if_canceled && jcr->is_job_canceled()) {
+      return;
+   }
+
    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 (except if name is set) */
-   foreach_alist(plugin, plugin_list) {
-      if (name && strncmp(plugin->file, name, len) != 0) {
-         i++;
+   /*
+    * Pass event to every plugin (except if name is set). If name
+    *   is set, we pass it only to the plugin with that name.
+    */
+   foreach_alist_index(i, plugin, bplugin_list) {
+      if (name && !for_this_plug(plugin, name, len)) {
          continue;
       }
-      plugin_ctx = &plugin_ctx_list[i++];
+      plugin_ctx = &plugin_ctx_list[i];
       if (is_plugin_disabled(plugin_ctx)) {
          continue;
       }
-      rc = plug_func(plugin)->handlePluginEvent(plugin_ctx, &event, value);
-      if (rc != bRC_OK) {
-         break;
-      }
+      plug_func(plugin)->handlePluginEvent(plugin_ctx, &event, value);
    }
    return;
 }
@@ -197,9 +250,9 @@ bool plugin_check_file(JCR *jcr, char *fname)
 {
    Plugin *plugin;
    int rc = bRC_OK;
-   int i = 0;
+   int i;
 
-   if (!plugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
+   if (!bplugin_list || !jcr || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
       return false;                      /* Return if no plugins loaded */
    }
 
@@ -208,8 +261,8 @@ bool plugin_check_file(JCR *jcr, char *fname)
    Dmsg2(dbglvl, "plugin_ctx=%p JobId=%d\n", jcr->plugin_ctx_list, jcr->JobId);
 
    /* Pass event to every plugin */
-   foreach_alist(plugin, plugin_list) {
-      jcr->plugin_ctx = &plugin_ctx_list[i++];
+   foreach_alist_index(i, plugin, bplugin_list) {
+      jcr->plugin_ctx = &plugin_ctx_list[i];
       jcr->plugin = plugin;
       if (is_plugin_disabled(jcr)) {
          continue;
@@ -231,7 +284,7 @@ bool plugin_check_file(JCR *jcr, char *fname)
 /* Get the first part of the the plugin command
  *  systemstate:/@SYSTEMSTATE/ 
  * => ret = 11
- * => can use strncmp(plugin_name, cmd, ret);
+ * => can use for_this_plug(plug, cmd, ret);
  *
  * The plugin command can contain only the plugin name
  *  Plugin = alldrives
@@ -263,6 +316,90 @@ static bool get_plugin_name(JCR *jcr, char *cmd, int *ret)
    return true;
 }
 
+
+static void update_ff_pkt(FF_PKT *ff_pkt, struct save_pkt *sp)
+{
+   ff_pkt->no_read = sp->no_read;
+   ff_pkt->delta_seq = sp->delta_seq;
+   if (sp->flags & FO_DELTA) {
+      ff_pkt->flags |= FO_DELTA;
+      ff_pkt->delta_seq++;          /* make new delta sequence number */
+   } else {
+      ff_pkt->flags &= ~FO_DELTA;   /* clean delta sequence number */
+      ff_pkt->delta_seq = 0;
+   }
+   
+   if (sp->flags & FO_OFFSETS) {
+      ff_pkt->flags |= FO_OFFSETS;
+   }
+   if (sp->flags & FO_PORTABLE_DATA) {
+      ff_pkt->flags |= FO_PORTABLE_DATA;
+   }
+   ff_pkt->flags |= FO_PLUGIN;       /* data from plugin */
+}
+
+/* Ask to a Option Plugin what to do with the current file */
+bRC plugin_option_handle_file(JCR *jcr, FF_PKT *ff_pkt, struct save_pkt *sp)
+{
+   Plugin *plugin;
+   bRC ret = bRC_Error;
+   char *cmd = ff_pkt->plugin;
+   int len;
+   int i=0;
+   bEvent event;
+   event.eventType = bEventHandleBackupFile;
+
+   bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
+   memset(sp, 0, sizeof(struct save_pkt));
+   sp->pkt_size = sp->pkt_end = sizeof(struct save_pkt);
+   sp->portable = true;
+   sp->cmd = cmd;
+   sp->link = ff_pkt->link;
+   sp->cmd = ff_pkt->plugin;
+   sp->statp = ff_pkt->statp;
+   sp->fname = ff_pkt->fname;
+   sp->delta_seq = ff_pkt->delta_seq;
+   sp->accurate_found = ff_pkt->accurate_found;
+
+   if (!bplugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
+      Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
+      goto bail_out;         /* Return if no plugins loaded */
+   }
+
+   if (!get_plugin_name(jcr, cmd, &len)) {
+      goto bail_out;
+   }
+
+   /* Note, we stop the loop on the first plugin that matches the name */
+   foreach_alist_index(i, plugin, bplugin_list) {
+      Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
+      if (!for_this_plug(plugin, cmd, len)) {
+         continue;
+      }
+
+      if (is_plugin_disabled(&plugin_ctx_list[i])) {
+         goto bail_out;
+      }
+      
+      ret = plug_func(plugin)->handlePluginEvent(&plugin_ctx_list[i], 
+                                                 &event, sp);
+      
+      /* TODO: would be better to set this in save_file() */
+      if (ret == bRC_OK) {
+         jcr->opt_plugin = true;
+         jcr->plugin = plugin;
+         jcr->plugin_sp = sp;      /* Unset sp in save_file */
+         jcr->plugin_ctx = &plugin_ctx_list[i];
+
+         update_ff_pkt(ff_pkt, sp);
+      }
+
+      goto bail_out;
+   } /* end foreach loop */
+bail_out:
+   return ret;
+}
+
 /**  
  * Sequence of calls for a backup:
  * 1. plugin_save() here is called with ff_pkt
@@ -281,7 +418,7 @@ static bool get_plugin_name(JCR *jcr, char *cmd, int *ret)
 int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
 {
    Plugin *plugin;
-   int i = 0;
+   int i;
    int len;
    char *cmd = ff_pkt->top_fname;
    struct save_pkt sp;
@@ -289,7 +426,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
    POOL_MEM fname(PM_FNAME);
    POOL_MEM link(PM_FNAME);
 
-   if (!plugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
+   if (!bplugin_list || !jcr->plugin_ctx_list || jcr->is_job_canceled()) {
       Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
       return 1;                            /* Return if no plugins loaded */
    }
@@ -303,10 +440,9 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
    }
 
    /* Note, we stop the loop on the first plugin that matches the name */
-   foreach_alist(plugin, plugin_list) {
-      Dmsg3(dbglvl, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
-      if (strncmp(plugin->file, cmd, len) != 0) {
-         i++;
+   foreach_alist_index(i, plugin, bplugin_list) {
+      Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
+      if (!for_this_plug(plugin, cmd, len)) {
          continue;
       }
       /* 
@@ -331,6 +467,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
          sp.pkt_size = sizeof(sp);
          sp.pkt_end = sizeof(sp);
          sp.portable = true;
+         sp.flags = 0;
          sp.cmd = cmd;
          Dmsg3(dbglvl, "startBackup st_size=%p st_blocks=%p sp=%p\n", &sp.statp.st_size, &sp.statp.st_blocks,
                 &sp);
@@ -350,7 +487,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
           *  avoids zaping the plugin's strings.
           */
          ff_pkt->type = sp.type;
-         if (sp.type == FT_RESTORE_FIRST) {
+         if (IS_FT_OBJECT(sp.type)) {
             if (!sp.object_name) {
                Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no object_name in startBackupFile packet.\n"),
                   cmd);
@@ -370,8 +507,11 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
             }
             pm_strcpy(fname, sp.fname);
             pm_strcpy(link, sp.link);
+
+
             ff_pkt->fname = fname.c_str();
             ff_pkt->link = link.c_str();
+            update_ff_pkt(ff_pkt, &sp);
          }
 
          memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
@@ -389,9 +529,136 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
             continue;
          }
          goto bail_out;
-      }
+      } /* end while loop */
       goto bail_out;
+   } /* end loop over all plugins */
+   Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not found.\n", cmd);
+
+bail_out:
+   jcr->cmd_plugin = false;
+   jcr->plugin = NULL;
+   jcr->plugin_ctx = NULL;
+   return 1;
+}
+
+
+/**  
+ * Sequence of calls for a estimate:
+ * 1. plugin_estimate() here is called with ff_pkt
+ * 2. we find the plugin requested on the command string
+ * 3. we generate a bEventEstimateCommand event to the specified plugin
+ *    and pass it the command string.
+ * 4. we make a startPluginBackup call to the plugin, which gives
+ *    us the data we need in save_pkt
+ *
+ */
+int plugin_estimate(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
+{
+   Plugin *plugin;
+   int len;
+   int i;
+   char *cmd = ff_pkt->top_fname;
+   struct save_pkt sp;
+   bEvent event;
+   POOL_MEM fname(PM_FNAME);
+   POOL_MEM link(PM_FNAME);
+   ATTR attr;
+
+   if (!bplugin_list || !jcr->plugin_ctx_list) {
+      Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" requested, but is not loaded.\n", cmd);
+      return 1;                            /* Return if no plugins loaded */
    }
+
+   jcr->cmd_plugin = true;
+   bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
+   event.eventType = bEventEstimateCommand;
+
+   if (!get_plugin_name(jcr, cmd, &len)) {
+      goto bail_out;
+   }
+
+   /* Note, we stop the loop on the first plugin that matches the name */
+   foreach_alist_index(i, plugin, bplugin_list) {
+      Dmsg4(dbglvl, "plugin=%s plen=%d cmd=%s len=%d\n", plugin->file, plugin->file_len, cmd, len);
+      if (!for_this_plug(plugin, cmd, len)) {
+         continue;
+      }
+      /* 
+       * We put the current plugin pointer, and the plugin context
+       *  into the jcr, because during save_file(), the plugin
+       *  will be called many times and these values are needed.
+       */
+      jcr->plugin_ctx = &plugin_ctx_list[i];
+      jcr->plugin = plugin;
+      if (is_plugin_disabled(jcr)) {
+         goto bail_out;
+      }
+
+      Dmsg1(dbglvl, "Command plugin = %s\n", cmd);
+      /* Send the backup command to the right plugin*/
+      if (plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx, &event, cmd) != bRC_OK) {
+         goto bail_out;
+      }
+      /* Loop getting filenames to backup then saving them */
+      while (!jcr->is_job_canceled()) { 
+         memset(&sp, 0, sizeof(sp));
+         sp.pkt_size = sizeof(sp);
+         sp.pkt_end = sizeof(sp);
+         sp.portable = true;
+         sp.flags = 0;
+         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. I.e. the stat pkt ... */
+         if (plug_func(plugin)->startBackupFile(jcr->plugin_ctx, &sp) != bRC_OK) {
+            goto bail_out;
+         }
+         if (sp.type == 0) {
+            Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no type in startBackupFile packet.\n"),
+               cmd);
+            goto bail_out;
+         }
+
+         if (!IS_FT_OBJECT(sp.type)) {
+            if (!sp.fname) {
+               Jmsg1(jcr, M_FATAL, 0, _("Command plugin \"%s\": no fname in startBackupFile packet.\n"),
+                  cmd);
+               goto bail_out;
+            }
+
+            jcr->num_files_examined++;
+            jcr->JobFiles++;                  /* increment number of files seen */
+
+            if (sp.type != FT_LNKSAVED && S_ISREG(sp.statp.st_mode)) {
+               if (sp.statp.st_size > 0) {
+                  jcr->JobBytes += sp.statp.st_size;
+               }
+            }
+
+            if (jcr->listing) {
+               memcpy(&attr.statp, &sp.statp, sizeof(struct stat));
+               attr.type = sp.type;
+               attr.ofname = (POOLMEM *)sp.fname;
+               attr.olname = (POOLMEM *)sp.link;
+               print_ls_output(jcr, &attr);
+            }
+         }
+
+         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);
+         }
+         bRC rc = plug_func(plugin)->endBackupFile(jcr->plugin_ctx);
+         if (rc == bRC_More || rc == bRC_OK) {
+            accurate_mark_file_as_seen(jcr, sp.fname);
+         }
+         if (rc == bRC_More) {
+            continue;
+         }
+         goto bail_out;
+      } /* end while loop */
+      goto bail_out;
+   } /* end loop over all plugins */
    Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not found.\n", cmd);
 
 bail_out:
@@ -435,7 +702,7 @@ bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
       stat = sd->fsend("%ld 1 %d %s%c", index, sp->portable, sp->cmd, 0);
    } else {
       /* Send end of data */
-      stat = sd->fsend("0 0");
+      stat = sd->fsend("%ld 0", jcr->JobFiles);
    }
    if (!stat) {
       Jmsg1(jcr, M_FATAL, 0, _("Network send error to SD. ERR=%s\n"),
@@ -458,10 +725,10 @@ bool plugin_name_stream(JCR *jcr, char *name)
 {
    char *p = name;
    char *cmd;
-   bool start, portable;
+   bool start;
    Plugin *plugin;
    int len;
-   int i = 0;
+   int i;
    bpContext *plugin_ctx_list = jcr->plugin_ctx_list;
 
    Dmsg1(dbglvl, "Read plugin stream string=%s\n", name);
@@ -472,7 +739,7 @@ bool plugin_name_stream(JCR *jcr, char *name)
       /* Start of plugin data */
       skip_nonspaces(&p);          /* skip start/end flag */
       skip_spaces(&p);
-      portable = *p == '1';
+//    portable = *p == '1';
       skip_nonspaces(&p);          /* skip portable flag */
       skip_spaces(&p);
       cmd = p;
@@ -481,9 +748,13 @@ bool plugin_name_stream(JCR *jcr, char *name)
        * End of plugin data, notify plugin, then clear flags   
        */
       Dmsg2(dbglvl, "End plugin data plugin=%p ctx=%p\n", jcr->plugin, jcr->plugin_ctx);
-      if (jcr->plugin) {
+      if (jcr->plugin && jcr->plugin->restoreFileStarted) {
          plug_func(jcr->plugin)->endRestoreFile(jcr->plugin_ctx);
       }
+      if (jcr->plugin) {
+         jcr->plugin->restoreFileStarted = false;
+         jcr->plugin->createFileCalled = false;
+      }
       jcr->plugin_ctx = NULL;
       jcr->plugin = NULL;
       goto bail_out;
@@ -502,11 +773,10 @@ bool plugin_name_stream(JCR *jcr, char *name)
    /*
     * Search for correct plugin as specified on the command 
     */
-   foreach_alist(plugin, plugin_list) {
+   foreach_alist_index(i, plugin, bplugin_list) {
       bEvent event;
       Dmsg3(dbglvl, "plugin=%s cmd=%s len=%d\n", plugin->file, cmd, len);
-      if (strncmp(plugin->file, cmd, len) != 0) {
-         i++;
+      if (!for_this_plug(plugin, cmd, len)) {
          continue;
       }
       jcr->plugin_ctx = &plugin_ctx_list[i];
@@ -514,14 +784,20 @@ bool plugin_name_stream(JCR *jcr, char *name)
       if (is_plugin_disabled(jcr)) {
          goto bail_out;
       }
-      Dmsg1(dbglvl, "Restore Command plugin = %s\n", cmd);
+      Dmsg1(000, "Restore Command plugin = %s\n", cmd);
       event.eventType = bEventRestoreCommand;     
       if (plug_func(plugin)->handlePluginEvent(jcr->plugin_ctx, 
             &event, cmd) != bRC_OK) {
          goto bail_out;
       }
-      /* ***FIXME**** check error code */
-      plug_func(plugin)->startRestoreFile((bpContext *)jcr->plugin_ctx, cmd);
+      if (plugin->restoreFileStarted) {
+         Jmsg2(jcr, M_FATAL, 0, "Unbalanced call to startRestoreFile. plugin=%s cmd=%s\n", plugin->file, cmd);
+         plugin->restoreFileStarted = false;
+         goto bail_out;
+      }
+      if (plug_func(plugin)->startRestoreFile(jcr->plugin_ctx, cmd) == bRC_OK) {
+         plugin->restoreFileStarted = true;
+      }
       goto bail_out;
    }
    Jmsg1(jcr, M_WARNING, 0, _("Plugin=%s not found.\n"), cmd);
@@ -554,6 +830,7 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
 
    rp.pkt_size = sizeof(rp);
    rp.pkt_end = sizeof(rp);
+   rp.delta_seq = attr->delta_seq;
    rp.stream = attr->stream;
    rp.data_stream = attr->data_stream;
    rp.type = attr->type;
@@ -573,12 +850,20 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
    if (rp.attrEx) {
       Dmsg1(dbglvl, "attrEx=\"%s\"\n", rp.attrEx);
    }
+   if (!plugin->restoreFileStarted || plugin->createFileCalled) {
+      Jmsg0(jcr, M_FATAL, 0, "Unbalanced call to createFile\n");
+      plugin->createFileCalled = false;
+      return CF_ERROR;
+   }
    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;
    }
+   if (rp.create_status == CF_CORE) { 
+      return CF_CORE;           /* Let Bacula core handle the file creation */
+   }
    if (rp.create_status == CF_SKIP) {
       return CF_SKIP;
    }
@@ -652,19 +937,20 @@ void dump_fd_plugin(Plugin *plugin, FILE *fp)
 void load_fd_plugins(const char *plugin_dir)
 {
    Plugin *plugin;
+   int i;
 
    if (!plugin_dir) {
       Dmsg0(dbglvl, "plugin dir is NULL\n");
       return;
    }
 
-   plugin_list = New(alist(10, not_owned_by_alist));
+   bplugin_list = New(alist(10, not_owned_by_alist));
    if (!load_plugins((void *)&binfo, (void *)&bfuncs, plugin_dir, plugin_type,
                      is_plugin_compatible)) {
       /* Either none found, or some error */
-      if (plugin_list->size() == 0) {
-         delete plugin_list;
-         plugin_list = NULL;
+      if (bplugin_list->size() == 0) {
+         delete bplugin_list;
+         bplugin_list = NULL;
          Dmsg0(dbglvl, "No plugins loaded\n");
          return;
       }
@@ -681,7 +967,7 @@ void load_fd_plugins(const char *plugin_dir)
     * Verify that the plugin is acceptable, and print information
     *  about it.
     */
-   foreach_alist(plugin, plugin_list) {
+   foreach_alist_index(i, plugin, bplugin_list) {
       Jmsg(NULL, M_INFO, 0, _("Loaded plugin: %s\n"), plugin->file);
       Dmsg1(dbglvl, "Loaded plugin: %s\n", plugin->file);
    }
@@ -723,6 +1009,12 @@ static bool is_plugin_compatible(Plugin *plugin)
            plugin->file, info->plugin_license);
       return false;
    }
+   if (info->size != sizeof(pInfo)) {
+      Jmsg(NULL, M_ERROR, 0,
+           _("Plugin size incorrect. Plugin=%s wanted=%d got=%d\n"),
+           plugin->file, sizeof(pInfo), info->size);
+      return false;
+   }
       
    return true;
 }
@@ -730,23 +1022,23 @@ static bool is_plugin_compatible(Plugin *plugin)
 
 /**
  * Create a new instance of each plugin for this Job
- *   Note, plugin_list can exist but jcr->plugin_ctx_list can
+ *   Note, bplugin_list can exist but jcr->plugin_ctx_list can
  *   be NULL if no plugins were loaded.
  */
 void new_plugins(JCR *jcr)
 {
    Plugin *plugin;
-   int i = 0;
+   int i;
 
-   if (!plugin_list) {
+   if (!bplugin_list) {
       Dmsg0(dbglvl, "plugin list is NULL\n");
       return;
    }
-   if (jcr->is_job_canceled() || jcr->JobId == 0) {
+   if (jcr->is_job_canceled()) {
       return;
    }
 
-   int num = plugin_list->size();
+   int num = bplugin_list->size();
 
    if (num == 0) {
       Dmsg0(dbglvl, "No plugins loaded\n");
@@ -757,14 +1049,14 @@ void new_plugins(JCR *jcr)
 
    bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
    Dmsg2(dbglvl, "Instantiate plugin_ctx=%p JobId=%d\n", plugin_ctx_list, jcr->JobId);
-   foreach_alist(plugin, plugin_list) {
+   foreach_alist_index(i, plugin, bplugin_list) {
       /* Start a new instance of each plugin */
       bacula_ctx *b_ctx = (bacula_ctx *)malloc(sizeof(bacula_ctx));
       memset(b_ctx, 0, sizeof(bacula_ctx));
       b_ctx->jcr = jcr;
       plugin_ctx_list[i].bContext = (void *)b_ctx;   /* Bacula private context */
       plugin_ctx_list[i].pContext = NULL;
-      if (plug_func(plugin)->newPlugin(&plugin_ctx_list[i++]) != bRC_OK) {
+      if (plug_func(plugin)->newPlugin(&plugin_ctx_list[i]) != bRC_OK) {
          b_ctx->disabled = true;
       }
    }
@@ -776,18 +1068,18 @@ void new_plugins(JCR *jcr)
 void free_plugins(JCR *jcr)
 {
    Plugin *plugin;
-   int i = 0;
+   int i;
 
-   if (!plugin_list || !jcr->plugin_ctx_list) {
+   if (!bplugin_list || !jcr->plugin_ctx_list) {
       return;                         /* no plugins, nothing to do */
    }
 
    bpContext *plugin_ctx_list = (bpContext *)jcr->plugin_ctx_list;
    Dmsg2(dbglvl, "Free instance plugin_ctx=%p JobId=%d\n", plugin_ctx_list, jcr->JobId);
-   foreach_alist(plugin, plugin_list) {   
+   foreach_alist_index(i, plugin, bplugin_list) {   
       /* Free the plugin instance */
       plug_func(plugin)->freePlugin(&plugin_ctx_list[i]);
-      free(plugin_ctx_list[i++].bContext);     /* free Bacula private context */
+      free(plugin_ctx_list[i].bContext);     /* free Bacula private context */
    }
    free(plugin_ctx_list);
    jcr->plugin_ctx_list = NULL;
@@ -870,8 +1162,10 @@ static ssize_t my_plugin_bread(BFILE *bfd, void *buf, size_t count)
    io.count = count;
    io.buf = (char *)buf;
    io.win32 = false;
+   io.offset = 0;
    io.lerror = 0;
    plug_func(plugin)->pluginIO(jcr->plugin_ctx, &io);
+   bfd->offset = io.offset;
    bfd->berrno = io.io_errno;
    if (io.win32) {
       errno = b_errno_win32;
@@ -947,24 +1241,47 @@ static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence)
 static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
 {
    JCR *jcr;
-   if (!value || !ctx) {
+   if (!value) {
       return bRC_Error;
    }
-// Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
+
+   switch (var) {               /* General variables, no need of ctx */
+   case bVarFDName:
+      *((char **)value) = my_name;
+      break;
+   case bVarWorkingDir:
+      *(void **)value = me->working_directory;
+      break;
+   case bVarExePath:
+      *(char **)value = exepath;
+      break;
+   case bVarVersion:
+      *(char **)value = version;
+      break;
+   case bVarDistName:
+      *(char **)value = dist_name;
+      break;
+   case bVarBEEF:
+      *((int *)value) = beef;
+      break;
+   default:
+      break;
+   }
+
+   if (!ctx) {                  /* Other variables need context */
+      return bRC_Error;
+   }
+
    jcr = ((bacula_ctx *)ctx->bContext)->jcr;
    if (!jcr) {
       return bRC_Error;
    }
-// Dmsg1(dbglvl, "Bacula: jcr=%p\n", jcr); 
+
    switch (var) {
    case bVarJobId:
       *((int *)value) = jcr->JobId;
       Dmsg1(dbglvl, "Bacula: return bVarJobId=%d\n", jcr->JobId);
       break;
-   case bVarFDName:
-      *((char **)value) = my_name;
-      Dmsg1(dbglvl, "Bacula: return my_name=%s\n", my_name);
-      break;
    case bVarLevel:
       *((int *)value) = jcr->getJobLevel();
       Dmsg1(dbglvl, "Bacula: return bVarJobLevel=%d\n", jcr->getJobLevel());
@@ -981,6 +1298,10 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
       *((char **)value) = jcr->Job;
       Dmsg1(dbglvl, "Bacula: return Job name=%s\n", jcr->Job);
       break;
+   case bVarPrevJobName:
+      *((char **)value) = jcr->PrevJob;
+      Dmsg1(dbglvl, "Bacula: return Previous Job name=%s\n", jcr->PrevJob);
+      break;
    case bVarJobStatus:
       *((int *)value) = jcr->JobStatus;
       Dmsg1(dbglvl, "Bacula: return bVarJobStatus=%d\n", jcr->JobStatus);
@@ -1011,8 +1332,19 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
        }
 #endif
        return bRC_Error;
+   case bVarWhere:
+      *(char **)value = jcr->where;
+      break;
+   case bVarRegexWhere:
+      *(char **)value = jcr->RegexWhere;
+      break;
+   case bVarFDName:             /* get warning with g++ if we missed one */
    case bVarWorkingDir:
-      *(void **)value = me->working_directory;
+   case bVarExePath:
+   case bVarVersion:
+   case bVarDistName:
+   case bVarBEEF:
+   case bVarPrefixLinks:
       break;
    }
    return bRC_OK;
@@ -1165,9 +1497,12 @@ static bRC baculaAddInclude(bpContext *ctx, const char *file)
    if (!file) {
       return bRC_Error;
    }
-   if (!bctx->include) {  
-      bctx->include = new_preinclude(jcr);
-      new_options(jcr, bctx->include);
+   /* Not right time to add include */
+   if (!(jcr->ff && jcr->ff->fileset && jcr->ff->fileset->incexe)) {
+      return bRC_Error;
+   }
+   if (!bctx->include) {
+      bctx->include = jcr->ff->fileset->incexe;
    }
    set_incexe(jcr, bctx->include);
    add_file_to_fileset(jcr, file, true);
@@ -1242,6 +1577,70 @@ static bRC baculaNewInclude(bpContext *ctx)
    return bRC_OK;
 }
 
+static bRC baculaNewPreInclude(bpContext *ctx)
+{
+   JCR *jcr;
+   bacula_ctx *bctx;
+   if (!is_ctx_good(ctx, jcr, bctx)) {
+      return bRC_Error;
+   }
+
+   bctx->include = new_preinclude(jcr);
+   new_options(jcr, bctx->include);
+   set_incexe(jcr, bctx->include);
+
+   return bRC_OK;
+}
+
+/* 
+ * Check if a file have to be backuped using Accurate code
+ */
+static bRC baculaCheckChanges(bpContext *ctx, struct save_pkt *sp)
+{
+   JCR *jcr;
+   bacula_ctx *bctx;
+   FF_PKT *ff_pkt;
+   bRC ret = bRC_Error;
+
+   if (!is_ctx_good(ctx, jcr, bctx)) {
+      goto bail_out;
+   }
+   if (!sp) {
+      goto bail_out;
+   }
+   
+   ff_pkt = jcr->ff;
+   /*
+    * Copy fname and link because save_file() zaps them.  This 
+    *  avoids zaping the plugin's strings.
+    */
+   ff_pkt->type = sp->type;
+   if (!sp->fname) {
+      Jmsg0(jcr, M_FATAL, 0, _("Command plugin: no fname in baculaCheckChanges packet.\n"));
+      goto bail_out;
+   }
+
+   ff_pkt->fname = sp->fname;
+   ff_pkt->link = sp->link;
+   memcpy(&ff_pkt->statp, &sp->statp, sizeof(ff_pkt->statp));
+
+   if (check_changes(jcr, ff_pkt))  {
+      ret = bRC_OK;
+   } else {
+      ret = bRC_Seen;
+   }
+
+   /* check_changes() can update delta sequence number, return it to the
+    * plugin 
+    */
+   sp->delta_seq = ff_pkt->delta_seq;
+   sp->accurate_found = ff_pkt->accurate_found;
+
+bail_out:
+   Dmsg1(100, "checkChanges=%i\n", ret);
+   return ret;
+}
+
 
 #ifdef TEST_PROGRAM