]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/job.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / filed / job.c
index 814b7e003b3aff1177c2ab60d396a205c5fb9ad4..fb0b90ab112edbc7d6220b1da13c172954f6be9c 100644 (file)
@@ -28,7 +28,6 @@
 
 #include "bacula.h"
 #include "filed.h"
-#include "host.h"
 
 extern char my_name[];
 extern CLIENT *me;                   /* our client resource */
@@ -55,6 +54,9 @@ static int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd);
 static void filed_free_jcr(JCR *jcr);
 static int open_sd_read_session(JCR *jcr);
 static int send_bootstrap_file(JCR *jcr);
+static int runbefore_cmd(JCR *jcr);
+static int runafter_cmd(JCR *jcr);
+static int run_cmd(JCR *jcr, char *cmd, char *name);
 
 
 /* Exported functions */
@@ -68,21 +70,23 @@ struct s_cmds {
  * The following are the recognized commands from the Director. 
  */
 static struct s_cmds cmds[] = {
-   {"backup",   backup_cmd},
-   {"cancel",   cancel_cmd},
-   {"setdebug=", setdebug_cmd},
-   {"estimate", estimate_cmd},
-   {"exclude",  exclude_cmd},
-   {"Hello",    hello_cmd},
-   {"include",  include_cmd},
-   {"JobId=",   job_cmd},
-   {"level = ", level_cmd},
-   {"restore",  restore_cmd},
-   {"session",  session_cmd},
-   {"status",   status_cmd},
-   {"storage ", storage_cmd},
-   {"verify",   verify_cmd},
-   {"bootstrap",bootstrap_cmd},
+   {"backup",       backup_cmd},
+   {"cancel",       cancel_cmd},
+   {"setdebug=",    setdebug_cmd},
+   {"estimate",     estimate_cmd},
+   {"exclude",      exclude_cmd},
+   {"Hello",        hello_cmd},
+   {"include",      include_cmd},
+   {"JobId=",       job_cmd},
+   {"level = ",     level_cmd},
+   {"restore",      restore_cmd},
+   {"session",      session_cmd},
+   {"status",       status_cmd},
+   {"storage ",     storage_cmd},
+   {"verify",       verify_cmd},
+   {"bootstrap",    bootstrap_cmd},
+   {"RunBeforeJob", runbefore_cmd},
+   {"RunAfterJob",  runafter_cmd},
    {NULL,      NULL}                  /* list terminator */
 };
 
@@ -90,15 +94,18 @@ static struct s_cmds cmds[] = {
 static char jobcmd[]      = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
 static char storaddr[]    = "storage address=%s port=%d ssl=%d\n";
 static char sessioncmd[]  = "session %127s %ld %ld %ld %ld %ld %ld\n";
-static char restorecmd[]  = "restore replace=%c where=%s\n";
-static char restorecmd1[] = "restore replace=%c where=\n";
+static char restorecmd[]  = "restore replace=%c prelinks=%d where=%s\n";
+static char restorecmd1[] = "restore replace=%c prelinks=%d where=\n";
 static char verifycmd[]   = "verify level=%30s\n";
+static char estimatecmd[] = "estimate listing=%d\n";
+static char runbefore[]   = "RunBeforeJob %s\n";
+static char runafter[]    = "RunAfterJob %s\n";
 
 /* Responses sent to Director */
 static char errmsg[]      = "2999 Invalid command\n";
 static char no_auth[]     = "2998 No Authorization\n";
 static char OKinc[]       = "2000 OK include\n";
-static char OKest[]       = "2000 OK estimate files=%ld bytes=%ld\n";
+static char OKest[]       = "2000 OK estimate files=%u bytes=%s\n";
 static char OKexc[]       = "2000 OK exclude\n";
 static char OKlevel[]     = "2000 OK level\n";
 static char OKbackup[]    = "2000 OK backup\n";
@@ -107,11 +114,12 @@ static char OKverify[]    = "2000 OK verify\n";
 static char OKrestore[]   = "2000 OK restore\n";
 static char OKsession[]   = "2000 OK session\n";
 static char OKstore[]     = "2000 OK storage\n";
-static char OKjob[]       = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
+static char OKjob[]       = "2000 OK Job " HOST_OS "," DISTNAME "," DISTVER;
 static char OKsetdebug[]  = "2000 OK setdebug=%d\n";
 static char BADjob[]      = "2901 Bad Job\n";
-static char EndRestore[]  = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
-static char EndBackup[]   = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%" lld " JobBytes=%" lld "\n";
+static char EndJob[]      = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
+static char OKRunBefore[] = "2000 OK RunBefore\n";
+static char OKRunAfter[]  = "2000 OK RunAfter\n";
 
 /* Responses received from Storage Daemon */
 static char OK_end[]       = "3000 OK end\n";
@@ -162,8 +170,8 @@ void *handle_client_request(void *dirp)
    jcr->last_fname[0] = 0;
    jcr->client_name = get_memory(strlen(my_name) + 1);
    pm_strcpy(&jcr->client_name, my_name);
-   dir->jcr = (void *)jcr;
-   get_backup_privileges(NULL, 1 /* ignore_errors */);
+   dir->jcr = jcr;
+   enable_backup_privileges(NULL, 1 /* ignore_errors */);
 
    /**********FIXME******* add command handler error code */
 
@@ -185,7 +193,7 @@ void *handle_client_request(void *dirp)
            found = TRUE;                /* indicate command found */
            if (!cmds[i].func(jcr)) {    /* do command */
               quit = TRUE;              /* error or fully terminated,  get out */
-               Pmsg0(20, "Command error or Job done.\n");
+               Dmsg0(20, "Command error or Job done.\n");
            }
            break;
         }
@@ -202,6 +210,10 @@ void *handle_client_request(void *dirp)
       bnet_sig(jcr->store_bsock, BNET_TERMINATE);
    }
 
+   if (jcr->RunAfterJob && !job_canceled(jcr)) {
+      run_cmd(jcr, jcr->RunAfterJob, "ClientRunAfterJob");
+   }
+
    /* Inform Director that we are done */
    bnet_sig(dir, BNET_TERMINATE);
 
@@ -241,6 +253,15 @@ static int cancel_cmd(JCR *jcr)
       if (!(cjcr=get_jcr_by_full_name(Job))) {
          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
       } else {
+        if (cjcr->store_bsock) {
+           P(cjcr->mutex);
+           cjcr->store_bsock->timed_out = 1;
+           cjcr->store_bsock->terminated = 1;
+#ifndef HAVE_CYGWIN
+           pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
+#endif
+           V(cjcr->mutex);
+        }
         set_jcr_job_status(cjcr, JS_Canceled);
         free_jcr(cjcr);
          bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
@@ -264,7 +285,8 @@ static int setdebug_cmd(JCR *jcr)
 
    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
-      bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      bnet_fsend(dir, "2991 Bad setdebug command: %s\n", jcr->errmsg);
       return 0;   
    }
    debug_level = level;
@@ -275,8 +297,19 @@ static int setdebug_cmd(JCR *jcr)
 static int estimate_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
+   char ed2[50];
+
+   if (sscanf(dir->msg, estimatecmd, &jcr->listing) != 1) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg(jcr, M_FATAL, 0, _("Bad estimate command: %s"), jcr->errmsg);
+      bnet_fsend(dir, "2992 Bad estimate command.\n");
+      return 0;
+   }
    make_estimate(jcr);
-   return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
+   bnet_fsend(dir, OKest, jcr->num_files_examined, 
+      edit_uint64_with_commas(jcr->JobBytes, ed2));
+   bnet_sig(dir, BNET_EOD);
+   return 1;
 }
 
 /*
@@ -291,8 +324,9 @@ static int job_cmd(JCR *jcr)
    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
              &jcr->VolSessionId, &jcr->VolSessionTime,
              sd_auth_key) != 5) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s"), jcr->errmsg);
       bnet_fsend(dir, BADjob);
-      Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
       free_pool_memory(sd_auth_key);
       return 0;
    }
@@ -302,22 +336,172 @@ static int job_cmd(JCR *jcr)
    return bnet_fsend(dir, OKjob);
 }
 
+static int runbefore_cmd(JCR *jcr)
+{
+   int stat;
+   BSOCK *dir = jcr->dir_bsock;
+   POOLMEM *cmd = get_memory(dir->msglen+1);
+
+   Dmsg1(100, "runbefore_cmd: %s", dir->msg);
+   if (sscanf(dir->msg, runbefore, cmd) != 1) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg1(jcr, M_FATAL, 0, _("Bad RunBeforeJob command: %s\n"), jcr->errmsg);
+      bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
+      free_memory(cmd);
+      return 0;
+   }
+   unbash_spaces(cmd);
+
+   /* Run the command now */
+   stat = run_cmd(jcr, cmd, "ClientRunBeforeJob");
+   free_memory(cmd);
+   if (stat) {
+      bnet_fsend(dir, OKRunBefore);
+      return 1;
+   } else {
+      bnet_fsend(dir, "2905 Bad RunBeforeJob command.\n");
+      return 0;
+   }
+}
+
+static int runafter_cmd(JCR *jcr)
+{
+   BSOCK *dir = jcr->dir_bsock;
+   POOLMEM *msg = get_memory(dir->msglen+1);
+
+   Dmsg1(100, "runafter_cmd: %s", dir->msg);
+   if (sscanf(dir->msg, runafter, msg) != 1) {
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg1(jcr, M_FATAL, 0, _("Bad RunAfter command: %s\n"), jcr->errmsg);
+      bnet_fsend(dir, "2905 Bad RunAfterJob command.\n");
+      free_memory(msg);
+      return 0;
+   }
+   unbash_spaces(msg);
+   if (jcr->RunAfterJob) {
+      free_pool_memory(jcr->RunAfterJob);
+   }
+   jcr->RunAfterJob = get_pool_memory(PM_FNAME);
+   pm_strcpy(&jcr->RunAfterJob, msg);
+   free_pool_memory(msg);
+   return bnet_fsend(dir, OKRunAfter);
+}
+
+static int run_cmd(JCR *jcr, char *cmd, char *name)
+{
+   POOLMEM *ecmd = get_pool_memory(PM_FNAME);
+   int status;
+   BPIPE *bpipe;
+   char line[MAXSTRING];
+   
+   ecmd = edit_job_codes(jcr, ecmd, cmd, "");
+   bpipe = open_bpipe(ecmd, 0, "r");
+   free_pool_memory(ecmd);
+   while (fgets(line, sizeof(line), bpipe->rfd)) {
+      Jmsg(jcr, M_INFO, 0, _("%s: %s"), name, line);
+   }
+   status = close_bpipe(bpipe);
+   if (status != 0) {
+      Jmsg(jcr, M_FATAL, 0, _("%s returned non-zero status=%d\n"), name,
+        status);
+      set_jcr_job_status(jcr, JS_FatalError);
+      return 0;
+   }
+   return 1;
+}
+
+
 #define INC_LIST 0
 #define EXC_LIST 1
 
 static void add_fname_to_list(JCR *jcr, char *fname, int list)
 {
-   char *p;  
-   if (list == INC_LIST) {
-      add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
-   } else {
-      /* Skip leading options -- currently ignored */
-      for (p=fname; *p && *p != ' '; p++)
-        { }
-      /* Skip spaces */
-      for ( ; *p && *p == ' '; p++)
-        { }
-      add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
+   char *p, *q;
+   BPIPE *bpipe;
+   POOLMEM *fn;
+   FILE *ffd;
+   char buf[1000];
+   int optlen;
+   int stat;
+
+   /* Skip leading options -- currently ignored */
+   for (p=fname; *p && *p != ' '; p++)
+      { }
+   /* Skip spaces, and q points to first space */
+   for (q=NULL; *p && *p == ' '; p++) {
+      if (!q) {
+        q = p;
+      }
+   }
+
+   switch (*p) {
+   case '|':
+      p++;                           /* skip over | */
+      fn = get_pool_memory(PM_FNAME);
+      fn = edit_job_codes(jcr, fn, p, "");
+      bpipe = open_bpipe(fn, 0, "r");
+      free_pool_memory(fn);
+      if (!bpipe) {
+         Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
+           p, strerror(errno));
+        return;
+      }
+      /* Copy File options */
+      if (list == INC_LIST) {
+        *q = 0;                      /* terminate options */
+        strcpy(buf, fname);
+         strcat(buf, " ");
+        optlen = strlen(buf);
+      } else {
+        optlen = 0;
+      }
+      while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
+        strip_trailing_junk(buf);
+        if (list == INC_LIST) {
+           add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
+        } else {
+           add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
+        }
+      }
+      if ((stat=close_bpipe(bpipe)) != 0) {
+         Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. RtnStat=%d ERR=%s\n"),
+           p, stat, strerror(errno));
+        return;
+      }
+      break;
+   case '<':
+      p++;                     /* skip over < */
+      if ((ffd = fopen(p, "r")) == NULL) {
+         Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
+            list==INC_LIST?"included":"excluded", p, strerror(errno));
+        return;
+      }
+      /* Copy File options */
+      if (list == INC_LIST) {
+        *q = 0;                      /* terminate options */
+        strcpy(buf, fname);
+         strcat(buf, " ");
+        optlen = strlen(buf);
+      } else {
+        optlen = 0;
+      }
+      while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
+        strip_trailing_junk(buf);
+        if (list == INC_LIST) {
+           add_fname_to_include_list((FF_PKT *)jcr->ff, 1, buf);
+        } else {
+           add_fname_to_exclude_list((FF_PKT *)jcr->ff, buf);
+        }
+      }
+      fclose(ffd);
+      break;
+   default:
+      if (list == INC_LIST) {
+        add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
+      } else {
+        add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
+      }
+      break;
    }
 }
 
@@ -375,6 +559,13 @@ static int bootstrap_cmd(JCR *jcr)
    jcr->RestoreBootstrap = fname;
    bs = fopen(fname, "a+");           /* create file */
    if (!bs) {
+      /* 
+       * Suck up what he is sending to us so that he will then
+       *   read our error message.
+       */
+      while (bnet_recv(dir) >= 0)
+       {  }
+
       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
         jcr->RestoreBootstrap, strerror(errno));
       free_pool_memory(jcr->RestoreBootstrap);
@@ -400,51 +591,112 @@ static int bootstrap_cmd(JCR *jcr)
 static int level_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
-   POOLMEM *level;
+   POOLMEM *level, *buf = NULL;
    struct tm tm;
    time_t mtime;
+   int mtime_only;
 
    level = get_memory(dir->msglen+1);
    Dmsg1(110, "level_cmd: %s", dir->msg);
    if (sscanf(dir->msg, "level = %s ", level) != 1) {
-      Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
-      free_memory(level);
-      return 0;
+      goto bail_out;
    }
    /* Base backup requested? */
    if (strcmp(level, "base") == 0) {
-      jcr->save_level = L_BASE;
+      jcr->JobLevel = L_BASE;
    /* Full backup requested? */ 
    } else if (strcmp(level, "full") == 0) {
-      jcr->save_level = L_FULL;
+      jcr->JobLevel = L_FULL;
    /* 
     * Backup requested since <date> <time>
     *  This form is also used for incremental and differential
+    *  This code is deprecated.  See since_utime for new code.
     */
    } else if (strcmp(level, "since") == 0) {
-      jcr->save_level = L_SINCE;
-      if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
+      jcr->JobLevel = L_SINCE;
+      if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d", 
                 &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
-                &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
-         Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
-        free_memory(level);
-        return 0;
+                &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
+        goto bail_out;
       }
       tm.tm_year -= 1900;
       tm.tm_mon  -= 1;
       tm.tm_wday = tm.tm_yday = 0;             
       tm.tm_isdst = -1;
       mtime = mktime(&tm);
-      Dmsg1(100, "Got since time: %s", ctime(&mtime));
+      Dmsg2(100, "Got since time: %s mtime_only=%d\n", ctime(&mtime), mtime_only);
       jcr->incremental = 1;          /* set incremental or decremental backup */
       jcr->mtime = mtime;            /* set since time */
+      jcr->mtime_only = mtime_only;   /* and what to compare */
+   /*
+    * We get his UTC since time, then sync the clocks and correct it
+    *  to agree with our clock.
+    */
+   } else if (strcmp(level, "since_utime") == 0) {
+      buf = get_memory(dir->msglen+1);
+      utime_t since_time, adj;
+      btime_t his_time, bt_start, rt=0, bt_adj=0;
+      jcr->JobLevel = L_SINCE;
+      if (sscanf(dir->msg, "level = since_utime %s mtime_only=%d", 
+                buf, &mtime_only) != 2) { 
+        goto bail_out;
+      }
+      since_time = str_to_uint64(buf); /* this is the since time */
+      char ed1[50], ed2[50];
+      /* 
+       * Sync clocks by polling him for the time. We take       
+       *   10 samples of his time throwing out the first two.
+       */
+      for (int i=0; i<10; i++) {
+        bt_start = get_current_btime();
+        bnet_sig(dir, BNET_BTIME);   /* poll for time */
+        if (bnet_recv(dir) <= 0) {   /* get response */
+           goto bail_out;
+        }
+         if (sscanf(dir->msg, "btime %s", buf) != 1) {
+           goto bail_out;
+        }
+        if (i < 2) {                 /* toss first two results */
+           continue;
+        }
+        his_time = str_to_uint64(buf);
+        rt = get_current_btime() - bt_start; /* compute round trip time */
+        bt_adj -= his_time - bt_start - rt/2;
+         Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
+      }
+
+      bt_adj = bt_adj / 8;           /* compute average time */
+      Dmsg2(100, "rt=%s adj=%s\n", edit_uint64(rt, ed1), edit_uint64(bt_adj, ed2));
+      adj = btime_to_utime(bt_adj);
+      since_time += adj;             /* adjust for clock difference */
+      if (adj != 0) {
+         Jmsg(jcr, M_INFO, 0, _("Since time adjusted by %d seconds.\n"), adj);
+      }
+      bnet_sig(dir, BNET_EOD);
+
+      Dmsg2(100, "adj = %d since_time=%d\n", (int)adj, (int)since_time);
+      jcr->incremental = 1;          /* set incremental or decremental backup */
+      jcr->mtime = since_time;       /* set since time */
+      jcr->mtime_only = mtime_only;   /* and what to compare */
    } else {
       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
       free_memory(level);
       return 0;
    }
    free_memory(level);
+   if (buf) {
+      free_memory(buf);
+   }
    return bnet_fsend(dir, OKlevel);
+
+bail_out:
+   pm_strcpy(&jcr->errmsg, dir->msg);
+   Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), jcr->errmsg);
+   free_memory(level);
+   if (buf) {
+      free_memory(buf);
+   }
+   return 0;
 }
 
 /*
@@ -459,7 +711,8 @@ static int session_cmd(JCR *jcr)
              &jcr->VolSessionId, &jcr->VolSessionTime,
              &jcr->StartFile, &jcr->EndFile, 
              &jcr->StartBlock, &jcr->EndBlock) != 7) {
-      Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", jcr->errmsg);
       return 0;
    }
 
@@ -479,13 +732,14 @@ static int storage_cmd(JCR *jcr)
 
    Dmsg1(100, "StorageCmd: %s", dir->msg);
    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
-      Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
+      pm_strcpy(&jcr->errmsg, dir->msg);
+      Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), jcr->errmsg);
       return 0;
    }
    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
    /* Open command communications with Storage daemon */
    /* Try to connect for 1 hour at 10 second intervals */
-   sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
+   sd = bnet_connect(jcr, 10, me->SDConnectTimeout, _("Storage daemon"), 
                     jcr->stored_addr, NULL, stored_port, 1);
    if (sd == NULL) {
       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
@@ -516,6 +770,7 @@ static int backup_cmd(JCR *jcr)
    BSOCK *sd = jcr->store_bsock;
    int ok = 0;
    int SDJobStatus;
+   char ed1[50], ed2[50];
 
    set_jcr_job_status(jcr, JS_Blocked);
    jcr->JobType = JT_BACKUP;
@@ -523,7 +778,6 @@ static int backup_cmd(JCR *jcr)
 
    if (sd == NULL) {
       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
-      set_jcr_job_status(jcr, JS_ErrorTerminated);
       goto cleanup;
    }
 
@@ -542,13 +796,11 @@ static int backup_cmd(JCR *jcr)
       Dmsg1(110, "<stored: %s", sd->msg);
       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
-        set_jcr_job_status(jcr, JS_ErrorTerminated);
         goto cleanup;
       }
       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
    } else {
       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
-      set_jcr_job_status(jcr, JS_ErrorTerminated);
       goto cleanup;
    }
 
@@ -563,7 +815,6 @@ static int backup_cmd(JCR *jcr)
     */
    Dmsg1(110, "<stored: %s", sd->msg);
    if (!response(jcr, sd, OK_data, "Append Data")) {
-      set_jcr_job_status(jcr, JS_ErrorTerminated);
       goto cleanup;
    }
       
@@ -610,19 +861,19 @@ static int backup_cmd(JCR *jcr)
       }
       if (!ok) {
          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
-        set_jcr_job_status(jcr, JS_ErrorTerminated);
         goto cleanup;
       }
       if (SDJobStatus != JS_Terminated) {
          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
            SDJobStatus);
-        set_jcr_job_status(jcr, JS_ErrorTerminated);
       }
    }
 
 cleanup:
 
-   bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, jcr->ReadBytes, jcr->JobBytes);
+   bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
+      edit_uint64(jcr->ReadBytes, ed1), 
+      edit_uint64(jcr->JobBytes, ed2), jcr->Errors);   
 
    return 0;                         /* return and stop command loop */
 }
@@ -635,7 +886,7 @@ static int verify_cmd(JCR *jcr)
 { 
    BSOCK *dir = jcr->dir_bsock;
    BSOCK *sd  = jcr->store_bsock;
-   char level[100];
+   char level[100], ed1[50], ed2[50];
 
    jcr->JobType = JT_VERIFY;
    if (sscanf(dir->msg, verifycmd, level) != 1) {
@@ -650,6 +901,8 @@ static int verify_cmd(JCR *jcr)
       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
    } else if (strcasecmp(level, "data") == 0){
       jcr->JobLevel = L_VERIFY_DATA;
+   } else if (strcasecmp(level, "disk_to_catalog") == 0) {
+      jcr->JobLevel = L_VERIFY_DISK_TO_CATALOG;
    } else {   
       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
       return 0;   
@@ -682,12 +935,22 @@ static int verify_cmd(JCR *jcr)
       /* Inform Storage daemon that we are done */
       bnet_sig(sd, BNET_TERMINATE);
 
+      break;
+   case L_VERIFY_DISK_TO_CATALOG:
+      do_verify(jcr);
       break;
    default:
       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
       return 0; 
    }
 
+   bnet_sig(dir, BNET_EOD);
+
+   /* Send termination status back to Dir */
+   bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
+      edit_uint64(jcr->ReadBytes, ed1), 
+      edit_uint64(jcr->JobBytes, ed2), jcr->Errors);   
+
    /* Inform Director that we are done */
    bnet_sig(dir, BNET_TERMINATE);
    return 0;                         /* return and terminate command loop */
@@ -702,7 +965,9 @@ static int restore_cmd(JCR *jcr)
    BSOCK *dir = jcr->dir_bsock;
    BSOCK *sd = jcr->store_bsock;
    POOLMEM *where;
+   int prefix_links;
    char replace;
+   char ed1[50], ed2[50];
 
    /*
     * Scan WHERE (base directory for restore) from command
@@ -712,9 +977,10 @@ static int restore_cmd(JCR *jcr)
    where = get_memory(dir->msglen+1);
    *where = 0;
 
-   if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
-      if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
-         Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
+   if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, where) != 3) {
+      if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
+        pm_strcpy(&jcr->errmsg, dir->msg);
+         Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
         return 0;
       }
       *where = 0;
@@ -726,8 +992,10 @@ static int restore_cmd(JCR *jcr)
       
    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
    unbash_spaces(where);
-   jcr->where = where;
+   jcr->where = bstrdup(where);
+   free_pool_memory(where);
    jcr->replace = replace;
+   jcr->prefix_links = prefix_links;
 
    bnet_fsend(dir, OKrestore);
    Dmsg1(110, "bfiled>dird: %s", dir->msg);
@@ -766,9 +1034,11 @@ static int restore_cmd(JCR *jcr)
    bnet_sig(sd, BNET_TERMINATE);
 
 bail_out:
+
    /* Send termination status back to Dir */
-   bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined, 
-             jcr->JobBytes);
+   bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
+      edit_uint64(jcr->ReadBytes, ed1), 
+      edit_uint64(jcr->JobBytes, ed2), jcr->Errors);   
 
    /* Inform Director that we are done */
    bnet_sig(dir, BNET_TERMINATE);
@@ -839,16 +1109,17 @@ static void filed_free_jcr(JCR *jcr)
    if (jcr->store_bsock) {
       bnet_close(jcr->store_bsock);
    }
-   if (jcr->where) {
-      free_pool_memory(jcr->where);
-   }
    if (jcr->RestoreBootstrap) {
       unlink(jcr->RestoreBootstrap);
       free_pool_memory(jcr->RestoreBootstrap);
+      jcr->RestoreBootstrap = NULL;
    }
    if (jcr->last_fname) {
       free_pool_memory(jcr->last_fname);
    }
+   if (jcr->RunAfterJob) {
+      free_pool_memory(jcr->RunAfterJob);
+   }
    return;
 }
 
@@ -861,17 +1132,18 @@ static void filed_free_jcr(JCR *jcr)
  */
 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
 {
-   int n;
-
    if (sd->errors) {
       return 0;
    }
-   if ((n = bget_msg(sd)) > 0) {
+   if (bget_msg(sd) > 0) {
       Dmsg0(110, sd->msg);
       if (strcmp(sd->msg, resp) == 0) {
         return 1;
       }
    } 
+   if (job_canceled(jcr)) {
+      return 0;                      /* if canceled avoid useless error messages */
+   }
    if (is_bnet_error(sd)) {
       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
         cmd, bnet_strerror(sd));
@@ -888,6 +1160,7 @@ static int send_bootstrap_file(JCR *jcr)
    char buf[2000];
    BSOCK *sd = jcr->store_bsock;
    char *bootstrap = "bootstrap\n";
+   int stat = 0;
 
    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
    if (!jcr->RestoreBootstrap) {
@@ -898,9 +1171,9 @@ static int send_bootstrap_file(JCR *jcr)
       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
         jcr->RestoreBootstrap, strerror(errno));
       set_jcr_job_status(jcr, JS_ErrorTerminated);
-      return 0;
+      goto bail_out;
    }
-   strcpy(sd->msg, bootstrap); 
+   pm_strcpy(&sd->msg, bootstrap);  
    sd->msglen = strlen(sd->msg);
    bnet_send(sd);
    while (fgets(buf, sizeof(buf), bs)) {
@@ -911,7 +1184,16 @@ static int send_bootstrap_file(JCR *jcr)
    fclose(bs);
    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
       set_jcr_job_status(jcr, JS_ErrorTerminated);
-      return 0;
+      goto bail_out;
    }
-   return 1;
+   stat = 1;
+
+bail_out:
+   if (jcr->RestoreBootstrap) {
+      unlink(jcr->RestoreBootstrap);
+      free_pool_memory(jcr->RestoreBootstrap);
+      jcr->RestoreBootstrap = NULL;
+   }
+
+   return stat;
 }