]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/fd_plugins.c
System State plugin first cut
[bacula/bacula] / bacula / src / filed / fd_plugins.c
index e484da73f2451ebc9175caa11ad80574aa0db7e5..5407a146cc6f231eda590c996451f43bede3a030 100644 (file)
@@ -1,7 +1,7 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2009 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2010 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.
@@ -25,7 +25,7 @@
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
 */
-/*
+/**
  * Main program to test loading and running Bacula plugins.
  *   Destined to become Bacula pluginloader, ...
  *
@@ -62,6 +62,7 @@ static bRC baculaDebugMsg(bpContext *ctx, const char *file, int line,
 static void *baculaMalloc(bpContext *ctx, const char *file, int line,
               size_t size);
 static void baculaFree(bpContext *ctx, const char *file, int line, void *mem);
+static bRC  baculaAddExclude(bpContext *ctx, const char *file);
 static bool is_plugin_compatible(Plugin *plugin);
 
 /*
@@ -91,7 +92,8 @@ static bFuncs bfuncs = {
    baculaJobMsg,
    baculaDebugMsg,
    baculaMalloc,
-   baculaFree
+   baculaFree,
+   baculaAddExclude
 };
 
 /* 
@@ -101,6 +103,7 @@ struct bacula_ctx {
    JCR *jcr;                             /* jcr for plugin */
    bRC  rc;                              /* last return code */
    bool disabled;                        /* set if plugin disabled */
+   findFILESET *fileset;                 /* pointer to exclude files */
 };
 
 static bool is_plugin_disabled(JCR *jcr)
@@ -114,7 +117,7 @@ static bool is_plugin_disabled(JCR *jcr)
 }
 
 
-/*
+/**
  * Create a plugin event 
  */
 void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)     
@@ -123,7 +126,7 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
    Plugin *plugin;
    int i = 0;
 
-   if (!plugin_list || !jcr || !jcr->plugin_ctx_list) {
+   if (!plugin_list || !jcr || !jcr->plugin_ctx_list || job_canceled(jcr)) {
       return;                         /* Return if no plugins loaded */
    }
 
@@ -151,7 +154,7 @@ void generate_plugin_event(JCR *jcr, bEventType eventType, void *value)
    return;
 }
 
-/*
+/**
  * Check if file was seen for accurate
  */
 bool plugin_check_file(JCR *jcr, char *fname)
@@ -160,7 +163,7 @@ bool plugin_check_file(JCR *jcr, char *fname)
    int rc = bRC_OK;
    int i = 0;
 
-   if (!plugin_list || !jcr || !jcr->plugin_ctx_list) {
+   if (!plugin_list || !jcr || !jcr->plugin_ctx_list || job_canceled(jcr)) {
       return false;                      /* Return if no plugins loaded */
    }
 
@@ -190,7 +193,7 @@ bool plugin_check_file(JCR *jcr, char *fname)
 }
 
 
-/*   
+/**  
  * Sequence of calls for a backup:
  * 1. plugin_save() here is called with ff_pkt
  * 2. we find the plugin requested on the command string
@@ -214,8 +217,11 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
    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) {
+   if (!plugin_list || !jcr->plugin_ctx_list || job_canceled(jcr)) {
+      Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not loaded.\n", cmd);
       return 1;                            /* Return if no plugins loaded */
    }
 
@@ -277,15 +283,21 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
          }
          jcr->plugin_sp = &sp;
          ff_pkt = jcr->ff;
-         ff_pkt->fname = sp.fname;
-         ff_pkt->link = sp.link;
+         /*
+          * 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;
          memcpy(&ff_pkt->statp, &sp.statp, sizeof(ff_pkt->statp));
-         Dmsg1(dbglvl, "Save_file: file=%s\n", ff_pkt->fname);
+         Dmsg1(dbglvl, "Save_file: file=%s\n", fname.c_str());
          save_file(jcr, ff_pkt, true);
          bRC rc = plug_func(plugin)->endBackupFile(jcr->plugin_ctx);
          if (rc == bRC_More || rc == bRC_OK) {
-            accurate_mark_file_as_seen(jcr, ff_pkt->fname);
+            accurate_mark_file_as_seen(jcr, fname.c_str());
          }
          if (rc == bRC_More) {
             continue;
@@ -294,7 +306,7 @@ int plugin_save(JCR *jcr, FF_PKT *ff_pkt, bool top_level)
       }
       goto bail_out;
    }
-   Jmsg1(jcr, M_ERROR, 0, "Command plugin \"%s\" not found.\n", cmd);
+   Jmsg1(jcr, M_FATAL, 0, "Command plugin \"%s\" not found.\n", cmd);
 
 bail_out:
    jcr->cmd_plugin = false;
@@ -303,7 +315,7 @@ bail_out:
    return 1;
 }
 
-/* 
+/**
  * Send plugin name start/end record to SD
  */
 bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
@@ -316,6 +328,9 @@ bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
       Jmsg0(jcr, M_FATAL, 0, _("Plugin save packet not found.\n"));
       return false;
    }
+   if (job_canceled(jcr)) {
+      return false;
+   }
   
    if (start) {
       index++;                  /* JobFiles not incremented yet */
@@ -346,7 +361,7 @@ bool send_plugin_name(JCR *jcr, BSOCK *sd, bool start)
    return true;
 }
 
-/*
+/**
  * Plugin name stream found during restore.  The record passed in
  *  argument name was generated in send_plugin_name() above.
  *
@@ -437,7 +452,7 @@ bail_out:
    return start;
 }
 
-/*
+/**
  * Tell the plugin to create the file.  Return values are
  *   This is called only during Restore
  *
@@ -455,7 +470,7 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
    int flags;
    int rc;
 
-   if (!plugin || !plugin_ctx || !set_cmd_plugin(bfd, jcr)) {
+   if (!plugin || !plugin_ctx || !set_cmd_plugin(bfd, jcr) || job_canceled(jcr)) {
       return CF_ERROR;
    }
    rp.pkt_size = sizeof(rp);
@@ -510,7 +525,7 @@ int plugin_create_file(JCR *jcr, ATTR *attr, BFILE *bfd, int replace)
    return CF_EXTRACT;
 }
 
-/*
+/**
  * Reset the file attributes after all file I/O is done -- this allows
  *  the previous access time/dates to be set properly, and it also allows
  *  us to properly set directory permissions.
@@ -544,7 +559,7 @@ void dump_fd_plugin(Plugin *plugin, FILE *fp)
    fprintf(fp, "\tdescription=%s\n", NPRTB(info->plugin_description));
 }
 
-/*
+/**
  * This entry point is called internally by Bacula to ensure
  *  that the plugin IO calls come into this code.
  */
@@ -588,7 +603,7 @@ void load_fd_plugins(const char *plugin_dir)
    dbg_plugin_add_hook(dump_fd_plugin);
 }
 
-/*
+/**
  * Check if a plugin is compatible.  Called by the load_plugin function
  *  to allow us to verify the plugin.
  */
@@ -627,7 +642,7 @@ 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
  *   be NULL if no plugins were loaded.
@@ -641,6 +656,9 @@ void new_plugins(JCR *jcr)
       Dmsg0(dbglvl, "plugin list is NULL\n");
       return;
    }
+   if (job_canceled(jcr)) {
+      return;
+   }
 
    int num = plugin_list->size();
 
@@ -666,7 +684,7 @@ void new_plugins(JCR *jcr)
    }
 }
 
-/*
+/**
  * Free the plugin instances for this Job
  */
 void free_plugins(JCR *jcr)
@@ -696,6 +714,9 @@ static int my_plugin_bopen(BFILE *bfd, const char *fname, int flags, mode_t mode
    struct io_pkt io;
 
    Dmsg1(dbglvl, "plugin_bopen flags=%x\n", flags);
+   if (!plugin || !jcr->plugin_ctx) {
+      return 0;
+   }
    io.pkt_size = sizeof(io);
    io.pkt_end = sizeof(io);
    io.func = IO_OPEN;
@@ -723,7 +744,11 @@ static int my_plugin_bclose(BFILE *bfd)
    JCR *jcr = bfd->jcr;
    Plugin *plugin = (Plugin *)jcr->plugin;
    struct io_pkt io;
+
    Dmsg0(dbglvl, "===== plugin_bclose\n");
+   if (!plugin || !jcr->plugin_ctx) {
+      return 0;
+   }
    io.pkt_size = sizeof(io);
    io.pkt_end = sizeof(io);
    io.func = IO_CLOSE;
@@ -748,7 +773,11 @@ static ssize_t my_plugin_bread(BFILE *bfd, void *buf, size_t count)
    JCR *jcr = bfd->jcr;
    Plugin *plugin = (Plugin *)jcr->plugin;
    struct io_pkt io;
+
    Dmsg0(dbglvl, "plugin_bread\n");
+   if (!plugin || !jcr->plugin_ctx) {
+      return 0;
+   }
    io.pkt_size = sizeof(io);
    io.pkt_end = sizeof(io);
    io.func = IO_READ;
@@ -772,7 +801,11 @@ static ssize_t my_plugin_bwrite(BFILE *bfd, void *buf, size_t count)
    JCR *jcr = bfd->jcr;
    Plugin *plugin = (Plugin *)jcr->plugin;
    struct io_pkt io;
+
    Dmsg0(dbglvl, "plugin_bwrite\n");
+   if (!plugin || !jcr->plugin_ctx) {
+      return 0;
+   }
    io.pkt_size = sizeof(io);
    io.pkt_end = sizeof(io);
    io.func = IO_WRITE;
@@ -796,7 +829,11 @@ static boffset_t my_plugin_blseek(BFILE *bfd, boffset_t offset, int whence)
    JCR *jcr = bfd->jcr;
    Plugin *plugin = (Plugin *)jcr->plugin;
    struct io_pkt io;
+
    Dmsg0(dbglvl, "plugin_bseek\n");
+   if (!plugin || !jcr->plugin_ctx) {
+      return 0;
+   }
    io.pkt_size = sizeof(io);
    io.pkt_end = sizeof(io);
    io.func = IO_SEEK;
@@ -827,7 +864,6 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
    if (!value || !ctx) {
       return bRC_Error;
    }
-   jcr = ((bacula_ctx *)ctx->bContext)->jcr;
 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
    jcr = ((bacula_ctx *)ctx->bContext)->jcr;
    if (!jcr) {
@@ -844,12 +880,12 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
       Dmsg1(dbglvl, "Bacula: return my_name=%s\n", my_name);
       break;
    case bVarLevel:
-      *((int *)value) = jcr->get_JobLevel();
-      Dmsg1(dbglvl, "Bacula: return bVarJobLevel=%d\n", jcr->get_JobLevel());
+      *((int *)value) = jcr->getJobLevel();
+      Dmsg1(dbglvl, "Bacula: return bVarJobLevel=%d\n", jcr->getJobLevel());
       break;
    case bVarType:
-      *((int *)value) = jcr->get_JobType();
-      Dmsg1(dbglvl, "Bacula: return bVarJobType=%d\n", jcr->get_JobType());
+      *((int *)value) = jcr->getJobType();
+      Dmsg1(dbglvl, "Bacula: return bVarJobType=%d\n", jcr->getJobType());
       break;
    case bVarClient:
       *((char **)value) = jcr->client_name;
@@ -873,6 +909,14 @@ static bRC baculaGetValue(bpContext *ctx, bVariable var, void *value)
       break;
    case bVarFileSeen:
       break;                 /* a write only variable, ignore read request */
+   case bVarVssObject:
+#ifdef HAVE_WIN32
+      if (g_pVSSClient) {
+         *(void **)value = g_pVSSClient->GetVssObject();
+         break;
+       }
+#endif
+       return bRC_Error;
    }
    return bRC_OK;
 }
@@ -883,7 +927,6 @@ static bRC baculaSetValue(bpContext *ctx, bVariable var, void *value)
    if (!value || !ctx) {
       return bRC_Error;
    }
-   jcr = ((bacula_ctx *)ctx->bContext)->jcr;
 // Dmsg1(dbglvl, "bacula: baculaGetValue var=%d\n", var);
    jcr = ((bacula_ctx *)ctx->bContext)->jcr;
    if (!jcr) {
@@ -971,6 +1014,34 @@ static void baculaFree(bpContext *ctx, const char *file, int line, void *mem)
 #endif
 }
 
+/**
+ * Let the plugin define files/directories to be excluded
+ *  from the main backup.
+ */
+static bRC baculaAddExclude(bpContext *ctx, const char *file)
+{
+   JCR *jcr;
+   bacula_ctx *bctx;
+   if (ctx) {
+      return bRC_Error;
+   }
+   if (!file) {
+      return bRC_Error;
+   }
+   bctx = (bacula_ctx *)ctx->bContext;
+   if (!bctx) {
+      return bRC_Error;
+   }
+   jcr = bctx->jcr;
+   if (!jcr) {
+      return bRC_Error;
+   }
+   if (!bctx->fileset) {  
+      bctx->fileset = new_exclude(jcr);
+   }
+   add_file_to_fileset(jcr, file, bctx->fileset, true);
+   return bRC_OK;
+}
 
 
 #ifdef TEST_PROGRAM