Hi,

On Thursday 13 December 2007, Kern Sibbald wrote:
> On Thursday 13 December 2007 15:36, Bastian Friedrich wrote:
>
> > I'd like to access the name of the current file set in a "RunScript"
> > statement in my Job configuration. Substitutions such as "%n" (for Job
> > name) are available in such statements; a substitution for the file set
> > name is not available.
> >
[...]
> Simply add a new argument to the subroutine, which is a callback subroutine
> that can be called if the main edit_job_codes() does not recognize a code. 

Thanks a lot for your guidance. I'm attaching a patch that implements your 
idea.

Seems to work well for "RunScript" and "Run Before Job" statements; I'm not 
sure whether I miss some locations where similar statements could be used...

The attached patch is vs. the current SVN; if you prefer, I can submit a 2.1.6 
patch as well.

Thx again,
   Bastian

-- 
Collax GmbH . Burkheimer Straße 3 . 79111 Freiburg . Germany
p: +49 (0) 761-45684-24
f: +49 (0) 761-45684-10        www.collax.com

Geschäftsführer: William K. Hite / Boris Nalbach
AG München HRB 158898 . Ust.-IdNr: DE 814464942
\ Please Tell Me if you Don't Get This Message
Index: src/dird/dird_conf.c
===================================================================
--- src/dird/dird_conf.c	(revision 6062)
+++ src/dird/dird_conf.c	(working copy)
@@ -1742,6 +1742,7 @@
 
    if (pass == 2) {
       RUNSCRIPT *script = new_runscript();
+      script->set_job_code_callback(job_code_callback_filesetname);
 
       script->set_command(lc->str);
 
@@ -1882,6 +1883,7 @@
 
       RUNSCRIPT *script = new_runscript();
       memcpy(script, &res_runscript, sizeof(RUNSCRIPT));
+      script->set_job_code_callback(job_code_callback_filesetname);
       
       if (*runscripts == NULL) {
         *runscripts = New(alist(10, not_owned_by_alist));
@@ -1894,3 +1896,13 @@
    scan_to_eol(lc);
    set_bit(index, res_all.hdr.item_present);
 }
+
+/* callback function for edit_job_codes */
+char *job_code_callback_filesetname(JCR *jcr, const char* param) {
+	if (param[0] == 'f') {
+		return jcr->fileset->name();
+	} else {
+		return NULL;
+	}
+}
+
Index: src/dird/protos.h
===================================================================
--- src/dird/protos.h	(revision 6062)
+++ src/dird/protos.h	(working copy)
@@ -75,6 +75,7 @@
 
 /* dird_conf.c */
 extern const char *level_to_str(int level);
+extern char *job_code_callback_filesetname(JCR *jcr, const char*);
 
 /* expand.c */
 int variable_expansion(JCR *jcr, char *inp, POOLMEM **exp);
Index: src/lib/runscript.h
===================================================================
--- src/lib/runscript.h	(revision 6062)
+++ src/lib/runscript.h	(working copy)
@@ -66,6 +66,7 @@
 public:
    POOLMEM *command;            /* command string */
    POOLMEM *target;             /* host target */
+   job_code_callback_t job_code_callback;
    int  when;                   /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
    char level;                  /* Base|Full|Incr...|All (NYI) */
    bool on_success;             /* execute command on job success (After) */
@@ -81,6 +82,8 @@
    void reset_default(bool free_string = false);
    bool is_local();             /* true if running on local host */
    void debug();
+
+   void set_job_code_callback(job_code_callback_t job_code_callback);
 };
 
 /* create new RUNSCRIPT (set all value to 0) */
Index: src/lib/util.c
===================================================================
--- src/lib/util.c	(revision 6062)
+++ src/lib/util.c	(working copy)
@@ -609,7 +609,7 @@
  *  to = recepients list
  *
  */
-POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to)
+POOLMEM *edit_job_codes(JCR *jcr, char *omsg, char *imsg, const char *to, job_code_callback_t callback)
 {
    char *p, *q;
    const char *str;
@@ -707,10 +707,17 @@
             }
             break;
          default:
-            add[0] = '%';
-            add[1] = *p;
-            add[2] = 0;
-            str = add;
+            str = NULL;
+            if (callback != NULL) {
+                str = callback(jcr, p);
+            }
+
+            if (!str) {
+                add[0] = '%';
+                add[1] = *p;
+                add[2] = 0;
+                str = add;
+            }
             break;
          }
       } else {
Index: src/lib/protos.h
===================================================================
--- src/lib/protos.h	(revision 6062)
+++ src/lib/protos.h	(working copy)
@@ -300,6 +300,9 @@
 
 
 /* util.c */
+
+typedef char *(*job_code_callback_t)(JCR *, const char *);
+
 bool             is_buf_zero             (char *buf, int len);
 void             lcase                   (char *str);
 void             bash_spaces             (char *str);
@@ -318,7 +321,7 @@
 void             make_session_key        (char *key, char *seed, int mode);
 void             encode_session_key      (char *encode, char *session, char *key, int maxlen);
 void             decode_session_key      (char *decode, char *session, char *key, int maxlen);
-POOLMEM *        edit_job_codes          (JCR *jcr, char *omsg, char *imsg, const char *to);
+POOLMEM *        edit_job_codes          (JCR *jcr, char *omsg, char *imsg, const char *to, job_code_callback_t job_code_callback = NULL);
 void             set_working_directory   (char *wd);
 const char *     last_path_separator     (const char *str);
 
Index: src/lib/runscript.c
===================================================================
--- src/lib/runscript.c	(revision 6062)
+++ src/lib/runscript.c	(working copy)
@@ -66,6 +66,7 @@
    fail_on_error = true;
    when = SCRIPT_Never;
    old_proto = false;        /* TODO: drop this with bacula 1.42 */
+   this->job_code_callback = NULL;
 }
 
 RUNSCRIPT *copy_runscript(RUNSCRIPT *src)
@@ -207,7 +208,7 @@
    BPIPE *bpipe;
    char line[MAXSTRING];
 
-   ecmd = edit_job_codes(jcr, ecmd, this->command, "");
+   ecmd = edit_job_codes(jcr, ecmd, this->command, "", this->job_code_callback);
    Dmsg1(100, "runscript: running '%s'...\n", ecmd);
    Jmsg(jcr, M_INFO, 0, _("%s: run command \"%s\"\n"), name, ecmd);
 
@@ -266,3 +267,7 @@
    Dmsg1(200,  _("  --> FailJobOnError=%u\n"),  fail_on_error);
    Dmsg1(200,  _("  --> RunWhen=%u\n"),  when);
 }
+
+void RUNSCRIPT::set_job_code_callback(job_code_callback_t job_code_callback) {
+	this->job_code_callback = job_code_callback;
+}
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Bacula-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bacula-devel

Reply via email to