]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
New var.c file + implement multiple simultaneous jobs
[bacula/bacula] / bacula / src / filed / job.c
1 /*
2  *  Bacula File Daemon Job processing
3  *
4  *    Kern Sibbald, October MM
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2000-2003 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "filed.h"
31 #include "host.h"
32
33 extern char my_name[];
34 extern CLIENT *me;                    /* our client resource */
35                         
36 /* Imported functions */
37 extern int status_cmd(JCR *jcr);
38                                    
39 /* Forward referenced functions */
40 static int backup_cmd(JCR *jcr);
41 static int bootstrap_cmd(JCR *jcr);
42 static int cancel_cmd(JCR *jcr);
43 static int setdebug_cmd(JCR *jcr);
44 static int estimate_cmd(JCR *jcr);
45 static int exclude_cmd(JCR *jcr);
46 static int hello_cmd(JCR *jcr);
47 static int job_cmd(JCR *jcr);
48 static int include_cmd(JCR *jcr);
49 static int level_cmd(JCR *jcr);
50 static int verify_cmd(JCR *jcr);
51 static int restore_cmd(JCR *jcr);
52 static int storage_cmd(JCR *jcr);
53 static int session_cmd(JCR *jcr);
54 static int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd);
55 static void filed_free_jcr(JCR *jcr);
56 static int open_sd_read_session(JCR *jcr);
57 static int send_bootstrap_file(JCR *jcr);
58
59
60 /* Exported functions */
61
62 struct s_cmds {
63    char *cmd;
64    int (*func)(JCR *);
65 };
66
67 /*  
68  * The following are the recognized commands from the Director. 
69  */
70 static struct s_cmds cmds[] = {
71    {"backup",   backup_cmd},
72    {"cancel",   cancel_cmd},
73    {"setdebug=", setdebug_cmd},
74    {"estimate", estimate_cmd},
75    {"exclude",  exclude_cmd},
76    {"Hello",    hello_cmd},
77    {"include",  include_cmd},
78    {"JobId=",   job_cmd},
79    {"level = ", level_cmd},
80    {"restore",  restore_cmd},
81    {"session",  session_cmd},
82    {"status",   status_cmd},
83    {"storage ", storage_cmd},
84    {"verify",   verify_cmd},
85    {"bootstrap",bootstrap_cmd},
86    {NULL,       NULL}                  /* list terminator */
87 };
88
89 /* Commands received from director that need scanning */
90 static char jobcmd[]      = "JobId=%d Job=%127s SDid=%d SDtime=%d Authorization=%100s";
91 static char storaddr[]    = "storage address=%s port=%d ssl=%d\n";
92 static char sessioncmd[]  = "session %127s %ld %ld %ld %ld %ld %ld\n";
93 static char restorecmd[]  = "restore replace=%c where=%s\n";
94 static char restorecmd1[] = "restore replace=%c where=\n";
95 static char verifycmd[]   = "verify level=%30s\n";
96
97 /* Responses sent to Director */
98 static char errmsg[]      = "2999 Invalid command\n";
99 static char no_auth[]     = "2998 No Authorization\n";
100 static char OKinc[]       = "2000 OK include\n";
101 static char OKest[]       = "2000 OK estimate files=%ld bytes=%ld\n";
102 static char OKexc[]       = "2000 OK exclude\n";
103 static char OKlevel[]     = "2000 OK level\n";
104 static char OKbackup[]    = "2000 OK backup\n";
105 static char OKbootstrap[] = "2000 OK bootstrap\n";
106 static char OKverify[]    = "2000 OK verify\n";
107 static char OKrestore[]   = "2000 OK restore\n";
108 static char OKsession[]   = "2000 OK session\n";
109 static char OKstore[]     = "2000 OK storage\n";
110 static char OKjob[]       = "2000 OK Job " FDHOST "," DISTNAME "," DISTVER;
111 static char OKsetdebug[]  = "2000 OK setdebug=%d\n";
112 static char BADjob[]      = "2901 Bad Job\n";
113 static char EndJob[]      = "2800 End Job TermCode=%d JobFiles=%u ReadBytes=%s JobBytes=%s Errors=%u\n";
114
115 /* Responses received from Storage Daemon */
116 static char OK_end[]       = "3000 OK end\n";
117 static char OK_close[]     = "3000 OK close Status = %d\n";
118 static char OK_open[]      = "3000 OK open ticket = %d\n";
119 static char OK_data[]      = "3000 OK data\n";
120 static char OK_append[]    = "3000 OK append data\n";
121 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
122
123
124 /* Commands sent to Storage Daemon */
125 static char append_open[]  = "append open session\n";
126 static char append_data[]  = "append data %d\n";
127 static char append_end[]   = "append end session %d\n";
128 static char append_close[] = "append close session %d\n";
129 static char read_open[]    = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
130 static char read_data[]    = "read data %d\n";
131 static char read_close[]   = "read close session %d\n";
132
133 /* 
134  * Accept requests from a Director
135  *
136  * NOTE! We are running as a separate thread
137  *
138  * Send output one line
139  * at a time followed by a zero length transmission.
140  *
141  * Return when the connection is terminated or there
142  * is an error.
143  *
144  * Basic task here is:
145  *   Authenticate Director (during Hello command).
146  *   Accept commands one at a time from the Director
147  *     and execute them.
148  *
149  */
150 void *handle_client_request(void *dirp)
151 {
152    int i, found, quit;
153    JCR *jcr;
154    BSOCK *dir = (BSOCK *)dirp;
155
156    jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
157    jcr->dir_bsock = dir;
158    jcr->ff = init_find_files();
159    jcr->start_time = time(NULL);
160    jcr->last_fname = get_pool_memory(PM_FNAME);
161    jcr->last_fname[0] = 0;
162    jcr->client_name = get_memory(strlen(my_name) + 1);
163    pm_strcpy(&jcr->client_name, my_name);
164    dir->jcr = jcr;
165    enable_backup_privileges(NULL, 1 /* ignore_errors */);
166
167    /**********FIXME******* add command handler error code */
168
169    for (quit=0; !quit;) {
170
171       /* Read command */
172       if (bnet_recv(dir) < 0) {
173          break;                       /* connection terminated */
174       }
175       dir->msg[dir->msglen] = 0;
176       Dmsg1(100, "<dird: %s", dir->msg);
177       found = FALSE;
178       for (i=0; cmds[i].cmd; i++) {
179          if (strncmp(cmds[i].cmd, dir->msg, strlen(cmds[i].cmd)) == 0) {
180             if (!jcr->authenticated && cmds[i].func != hello_cmd) {
181                bnet_fsend(dir, no_auth);
182                break;
183             }
184             found = TRUE;                /* indicate command found */
185             if (!cmds[i].func(jcr)) {    /* do command */
186                quit = TRUE;              /* error or fully terminated,  get out */
187                Dmsg0(20, "Command error or Job done.\n");
188             }
189             break;
190          }
191       }
192       if (!found) {                   /* command not found */
193          bnet_fsend(dir, errmsg);
194          quit = TRUE;
195          break;
196       }
197    }
198
199    /* Inform Storage daemon that we are done */
200    if (jcr->store_bsock) {
201       bnet_sig(jcr->store_bsock, BNET_TERMINATE);
202    }
203
204    /* Inform Director that we are done */
205    bnet_sig(dir, BNET_TERMINATE);
206
207    Dmsg0(100, "Calling term_find_files\n");
208    term_find_files((FF_PKT *)jcr->ff);
209    Dmsg0(100, "Done with term_find_files\n");
210    free_jcr(jcr);                     /* destroy JCR record */
211    Dmsg0(100, "Done with free_jcr\n");
212    return NULL;
213 }
214
215 /*
216  * Hello from Director he must identify himself and provide his 
217  *  password.
218  */
219 static int hello_cmd(JCR *jcr)
220 {
221    Dmsg0(120, "Calling Authenticate\n");
222    if (!authenticate_director(jcr)) {
223       return 0;
224    }
225    Dmsg0(120, "OK Authenticate\n");
226    jcr->authenticated = TRUE;
227    return 1;
228 }
229
230 /*
231  * Cancel a Job
232  */
233 static int cancel_cmd(JCR *jcr)
234 {
235    BSOCK *dir = jcr->dir_bsock;
236    char Job[MAX_NAME_LENGTH];
237    JCR *cjcr;
238
239    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
240       if (!(cjcr=get_jcr_by_full_name(Job))) {
241          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
242       } else {
243          if (cjcr->store_bsock) {
244             P(cjcr->mutex);
245             cjcr->store_bsock->timed_out = 1;
246             cjcr->store_bsock->terminated = 1;
247 #ifndef HAVE_CYGWIN
248             pthread_kill(cjcr->my_thread_id, TIMEOUT_SIGNAL);
249 #endif
250             V(cjcr->mutex);
251          }
252          set_jcr_job_status(cjcr, JS_Canceled);
253          free_jcr(cjcr);
254          bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
255       }
256    } else {
257       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
258    }
259    bnet_sig(dir, BNET_EOD);
260    return 1;
261 }
262
263
264 /*
265  * Set debug level as requested by the Director
266  *
267  */
268 static int setdebug_cmd(JCR *jcr)
269 {
270    BSOCK *dir = jcr->dir_bsock;
271    int level;
272
273    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
274    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
275       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
276       return 0;   
277    }
278    debug_level = level;
279    return bnet_fsend(dir, OKsetdebug, level);
280 }
281
282
283 static int estimate_cmd(JCR *jcr)
284 {
285    BSOCK *dir = jcr->dir_bsock;
286    make_estimate(jcr);
287    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
288 }
289
290 /*
291  * Get JobId and Storage Daemon Authorization key from Director
292  */
293 static int job_cmd(JCR *jcr)
294 {
295    BSOCK *dir = jcr->dir_bsock;
296    POOLMEM *sd_auth_key;
297
298    sd_auth_key = get_memory(dir->msglen);
299    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
300               &jcr->VolSessionId, &jcr->VolSessionTime,
301               sd_auth_key) != 5) {
302       bnet_fsend(dir, BADjob);
303       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
304       free_pool_memory(sd_auth_key);
305       return 0;
306    }
307    jcr->sd_auth_key = bstrdup(sd_auth_key);
308    free_pool_memory(sd_auth_key);
309    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
310    return bnet_fsend(dir, OKjob);
311 }
312
313 #define INC_LIST 0
314 #define EXC_LIST 1
315
316 static void add_fname_to_list(JCR *jcr, char *fname, int list)
317 {
318    char *p;  
319    if (list == INC_LIST) {
320       add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
321    } else {
322       /* Skip leading options -- currently ignored */
323       for (p=fname; *p && *p != ' '; p++)
324          { }
325       /* Skip spaces */
326       for ( ; *p && *p == ' '; p++)
327          { }
328       add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
329    }
330 }
331
332 /* 
333  * 
334  * Get list of files/directories to include from Director
335  *
336  */
337 static int include_cmd(JCR *jcr)
338 {
339    BSOCK *dir = jcr->dir_bsock;
340
341    while (bnet_recv(dir) >= 0) {
342       dir->msg[dir->msglen] = 0;
343       strip_trailing_junk(dir->msg);
344       Dmsg1(010, "include file: %s\n", dir->msg);
345       add_fname_to_list(jcr, dir->msg, INC_LIST);
346    }
347
348    return bnet_fsend(dir, OKinc);
349 }
350
351 /*
352  * Get list of files to exclude from Director
353  *
354  */
355 static int exclude_cmd(JCR *jcr)
356 {
357    BSOCK *dir = jcr->dir_bsock;
358
359    while (bnet_recv(dir) >= 0) {
360       dir->msg[dir->msglen] = 0;
361       strip_trailing_junk(dir->msg);
362       add_fname_to_list(jcr, dir->msg, EXC_LIST);
363       Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
364    }
365
366    return bnet_fsend(dir, OKexc);
367 }
368
369
370 static int bootstrap_cmd(JCR *jcr)
371 {
372    BSOCK *dir = jcr->dir_bsock;
373    POOLMEM *fname = get_pool_memory(PM_FNAME);
374    FILE *bs;
375
376    if (jcr->RestoreBootstrap) {
377       unlink(jcr->RestoreBootstrap);
378       free_pool_memory(jcr->RestoreBootstrap);
379    }
380    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
381       jcr->Job);
382    Dmsg1(400, "bootstrap=%s\n", fname);
383    jcr->RestoreBootstrap = fname;
384    bs = fopen(fname, "a+");           /* create file */
385    if (!bs) {
386       /* 
387        * Suck up what he is sending to us so that he will then
388        *   read our error message.
389        */
390       while (bnet_recv(dir) >= 0)
391         {  }
392
393       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
394          jcr->RestoreBootstrap, strerror(errno));
395       free_pool_memory(jcr->RestoreBootstrap);
396       jcr->RestoreBootstrap = NULL;
397       set_jcr_job_status(jcr, JS_ErrorTerminated);
398       return 0;
399    }
400
401    while (bnet_recv(dir) >= 0) {
402        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
403        fputs(dir->msg, bs);
404    }
405    fclose(bs);
406
407    return bnet_fsend(dir, OKbootstrap);
408 }
409
410
411 /*
412  * Get backup level from Director
413  *
414  */
415 static int level_cmd(JCR *jcr)
416 {
417    BSOCK *dir = jcr->dir_bsock;
418    POOLMEM *level;
419    struct tm tm;
420    time_t mtime;
421    int mtime_only;
422
423    level = get_memory(dir->msglen+1);
424    Dmsg1(110, "level_cmd: %s", dir->msg);
425    if (sscanf(dir->msg, "level = %s ", level) != 1) {
426       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
427       free_memory(level);
428       return 0;
429    }
430    /* Base backup requested? */
431    if (strcmp(level, "base") == 0) {
432       jcr->save_level = L_BASE;
433    /* Full backup requested? */ 
434    } else if (strcmp(level, "full") == 0) {
435       jcr->save_level = L_FULL;
436    /* 
437     * Backup requested since <date> <time>
438     *  This form is also used for incremental and differential
439     */
440    } else if (strcmp(level, "since") == 0) {
441       jcr->save_level = L_SINCE;
442       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d mtime_only=%d", 
443                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
444                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &mtime_only) != 7) {
445          Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
446          free_memory(level);
447          return 0;
448       }
449       tm.tm_year -= 1900;
450       tm.tm_mon  -= 1;
451       tm.tm_wday = tm.tm_yday = 0;              
452       tm.tm_isdst = -1;
453       mtime = mktime(&tm);
454       Dmsg2(100, "Got since time: %s mtime_only=%d\n", ctime(&mtime), mtime_only);
455       jcr->incremental = 1;           /* set incremental or decremental backup */
456       jcr->mtime = mtime;             /* set since time */
457       jcr->mtime_only = mtime_only;   /* and what to compare */
458    } else {
459       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
460       free_memory(level);
461       return 0;
462    }
463    free_memory(level);
464    return bnet_fsend(dir, OKlevel);
465 }
466
467 /*
468  * Get session parameters from Director -- this is for a Restore command
469  */
470 static int session_cmd(JCR *jcr)
471 {
472    BSOCK *dir = jcr->dir_bsock;
473
474    Dmsg1(100, "SessionCmd: %s", dir->msg);
475    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
476               &jcr->VolSessionId, &jcr->VolSessionTime,
477               &jcr->StartFile, &jcr->EndFile, 
478               &jcr->StartBlock, &jcr->EndBlock) != 7) {
479       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
480       return 0;
481    }
482
483    return bnet_fsend(dir, OKsession);
484 }
485
486 /*
487  * Get address of storage daemon from Director
488  *
489  */
490 static int storage_cmd(JCR *jcr)
491 {
492    int stored_port;                /* storage daemon port */
493    int enable_ssl;                 /* enable ssl to sd */
494    BSOCK *dir = jcr->dir_bsock;
495    BSOCK *sd;                         /* storage daemon bsock */
496
497    Dmsg1(100, "StorageCmd: %s", dir->msg);
498    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port, &enable_ssl) != 3) {
499       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
500       return 0;
501    }
502    Dmsg3(110, "Open storage: %s:%d ssl=%d\n", jcr->stored_addr, stored_port, enable_ssl);
503    /* Open command communications with Storage daemon */
504    /* Try to connect for 1 hour at 10 second intervals */
505    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
506                      jcr->stored_addr, NULL, stored_port, 1);
507    if (sd == NULL) {
508       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
509           jcr->stored_addr, stored_port);
510       return 0;
511    }
512
513    jcr->store_bsock = sd;
514
515    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
516    if (!authenticate_storagedaemon(jcr)) {
517       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
518       return 0;
519    }
520    Dmsg0(110, "Authenticated with SD.\n");
521
522    /* Send OK to Director */
523    return bnet_fsend(dir, OKstore);
524 }
525
526
527 /*  
528  * Do a backup. For now, we handle only Full and Incremental.
529  */
530 static int backup_cmd(JCR *jcr)
531
532    BSOCK *dir = jcr->dir_bsock;
533    BSOCK *sd = jcr->store_bsock;
534    int ok = 0;
535    int SDJobStatus;
536    char ed1[50], ed2[50];
537
538    set_jcr_job_status(jcr, JS_Blocked);
539    jcr->JobType = JT_BACKUP;
540    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
541
542    if (sd == NULL) {
543       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
544       goto cleanup;
545    }
546
547    bnet_fsend(dir, OKbackup);
548    Dmsg1(110, "bfiled>dird: %s", dir->msg);
549
550    /* 
551     * Send Append Open Session to Storage daemon
552     */
553    bnet_fsend(sd, append_open);
554    Dmsg1(110, ">stored: %s", sd->msg);
555    /* 
556     * Expect to receive back the Ticket number
557     */
558    if (bget_msg(sd) >= 0) {
559       Dmsg1(110, "<stored: %s", sd->msg);
560       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
561          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
562          goto cleanup;
563       }
564       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
565    } else {
566       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
567       goto cleanup;
568    }
569
570    /* 
571     * Send Append data command to Storage daemon
572     */
573    bnet_fsend(sd, append_data, jcr->Ticket);
574    Dmsg1(110, ">stored: %s", sd->msg);
575
576    /* 
577     * Expect to get OK data 
578     */
579    Dmsg1(110, "<stored: %s", sd->msg);
580    if (!response(jcr, sd, OK_data, "Append Data")) {
581       goto cleanup;
582    }
583       
584    /*
585     * Send Files to Storage daemon
586     */
587    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
588    if (!blast_data_to_storage_daemon(jcr, NULL)) {
589       set_jcr_job_status(jcr, JS_ErrorTerminated);
590       bnet_suppress_error_messages(sd, 1);
591    } else {
592       set_jcr_job_status(jcr, JS_Terminated);
593       if (jcr->JobStatus != JS_Terminated) {
594          bnet_suppress_error_messages(sd, 1);
595          goto cleanup;                /* bail out now */
596       }
597       /* 
598        * Expect to get response to append_data from Storage daemon
599        */
600       if (!response(jcr, sd, OK_append, "Append Data")) {
601          set_jcr_job_status(jcr, JS_ErrorTerminated);
602          goto cleanup;
603       }
604      
605       /* 
606        * Send Append End Data to Storage daemon
607        */
608       bnet_fsend(sd, append_end, jcr->Ticket);
609       /* Get end OK */
610       if (!response(jcr, sd, OK_end, "Append End")) {
611          set_jcr_job_status(jcr, JS_ErrorTerminated);
612          goto cleanup;
613       }
614
615       /*
616        * Send Append Close to Storage daemon
617        */
618       bnet_fsend(sd, append_close, jcr->Ticket);
619       while (bget_msg(sd) >= 0) {    /* stop on signal or error */
620          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
621             ok = 1;
622             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
623          }
624       }
625       if (!ok) {
626          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
627          goto cleanup;
628       }
629       if (SDJobStatus != JS_Terminated) {
630          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
631             SDJobStatus);
632       }
633    }
634
635 cleanup:
636
637    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
638       edit_uint64(jcr->ReadBytes, ed1), 
639       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
640
641    return 0;                          /* return and stop command loop */
642 }
643
644 /*  
645  * Do a Verify for Director
646  *
647  */
648 static int verify_cmd(JCR *jcr)
649
650    BSOCK *dir = jcr->dir_bsock;
651    BSOCK *sd  = jcr->store_bsock;
652    char level[100], ed1[50], ed2[50];
653
654    jcr->JobType = JT_VERIFY;
655    if (sscanf(dir->msg, verifycmd, level) != 1) {
656       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
657       return 0;   
658    }
659    if (strcasecmp(level, "init") == 0) {
660       jcr->JobLevel = L_VERIFY_INIT;
661    } else if (strcasecmp(level, "catalog") == 0){
662       jcr->JobLevel = L_VERIFY_CATALOG;
663    } else if (strcasecmp(level, "volume") == 0){
664       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
665    } else if (strcasecmp(level, "data") == 0){
666       jcr->JobLevel = L_VERIFY_DATA;
667    } else {   
668       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
669       return 0;   
670    }
671
672    bnet_fsend(dir, OKverify);
673    Dmsg1(110, "bfiled>dird: %s", dir->msg);
674
675    switch (jcr->JobLevel) {
676    case L_VERIFY_INIT:
677    case L_VERIFY_CATALOG:
678       do_verify(jcr);
679       break;
680    case L_VERIFY_VOLUME_TO_CATALOG:
681       if (!open_sd_read_session(jcr)) {
682          return 0;
683       }
684       start_dir_heartbeat(jcr);
685       do_verify_volume(jcr);
686       stop_dir_heartbeat(jcr);
687       /* 
688        * Send Close session command to Storage daemon
689        */
690       bnet_fsend(sd, read_close, jcr->Ticket);
691       Dmsg1(130, "bfiled>stored: %s", sd->msg);
692
693       /* ****FIXME**** check response */
694       bget_msg(sd);                      /* get OK */
695
696       /* Inform Storage daemon that we are done */
697       bnet_sig(sd, BNET_TERMINATE);
698
699       break;
700    default:
701       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
702       return 0; 
703    }
704
705    bnet_sig(dir, BNET_EOD);
706
707    /* Send termination status back to Dir */
708    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
709       edit_uint64(jcr->ReadBytes, ed1), 
710       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
711
712    /* Inform Director that we are done */
713    bnet_sig(dir, BNET_TERMINATE);
714    return 0;                          /* return and terminate command loop */
715 }
716
717 /*  
718  * Do a Restore for Director
719  *
720  */
721 static int restore_cmd(JCR *jcr)
722
723    BSOCK *dir = jcr->dir_bsock;
724    BSOCK *sd = jcr->store_bsock;
725    POOLMEM *where;
726    char replace;
727    char ed1[50], ed2[50];
728
729    /*
730     * Scan WHERE (base directory for restore) from command
731     */
732    Dmsg0(150, "restore command\n");
733    /* Pickup where string */
734    where = get_memory(dir->msglen+1);
735    *where = 0;
736
737    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
738       if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
739          Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
740          return 0;
741       }
742       *where = 0;
743    }
744    /* Turn / into nothing */
745    if (where[0] == '/' && where[1] == 0) {
746       where[0] = 0;
747    }
748       
749    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
750    unbash_spaces(where);
751    jcr->where = bstrdup(where);
752    free_pool_memory(where);
753    jcr->replace = replace;
754
755    bnet_fsend(dir, OKrestore);
756    Dmsg1(110, "bfiled>dird: %s", dir->msg);
757
758    jcr->JobType = JT_RESTORE;
759
760    set_jcr_job_status(jcr, JS_Blocked);
761    if (!open_sd_read_session(jcr)) {
762       set_jcr_job_status(jcr, JS_ErrorTerminated);
763       goto bail_out;
764    }
765
766    set_jcr_job_status(jcr, JS_Running);
767
768    /* 
769     * Do restore of files and data
770     */
771    start_dir_heartbeat(jcr);
772    do_restore(jcr);
773    stop_dir_heartbeat(jcr);
774    
775    set_jcr_job_status(jcr, JS_Terminated);
776    if (jcr->JobStatus != JS_Terminated) {
777       bnet_suppress_error_messages(sd, 1);
778    }
779
780    /* 
781     * Send Close session command to Storage daemon
782     */
783    bnet_fsend(sd, read_close, jcr->Ticket);
784    Dmsg1(130, "bfiled>stored: %s", sd->msg);
785
786    bget_msg(sd);                      /* get OK */
787
788    /* Inform Storage daemon that we are done */
789    bnet_sig(sd, BNET_TERMINATE);
790
791 bail_out:
792
793    /* Send termination status back to Dir */
794    bnet_fsend(dir, EndJob, jcr->JobStatus, jcr->JobFiles, 
795       edit_uint64(jcr->ReadBytes, ed1), 
796       edit_uint64(jcr->JobBytes, ed2), jcr->Errors);    
797
798    /* Inform Director that we are done */
799    bnet_sig(dir, BNET_TERMINATE);
800
801    Dmsg0(130, "Done in job.c\n");
802    return 0;                          /* return and terminate command loop */
803 }
804
805 static int open_sd_read_session(JCR *jcr)
806 {
807    BSOCK *sd = jcr->store_bsock;
808
809    if (!sd) {
810       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
811       return 0;
812    }
813    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
814       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
815    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
816    /* 
817     * Open Read Session with Storage daemon
818     */
819    bnet_fsend(sd, read_open, jcr->VolumeName,
820       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
821       jcr->StartBlock, jcr->EndBlock);
822    Dmsg1(110, ">stored: %s", sd->msg);
823
824    /* 
825     * Get ticket number
826     */
827    if (bget_msg(sd) >= 0) {
828       Dmsg1(110, "bfiled<stored: %s", sd->msg);
829       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
830          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
831          return 0;
832       }
833       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
834    } else {
835       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
836       return 0;
837    }
838
839    if (!send_bootstrap_file(jcr)) {
840       return 0;
841    }
842
843    /* 
844     * Start read of data with Storage daemon
845     */
846    bnet_fsend(sd, read_data, jcr->Ticket);
847    Dmsg1(110, ">stored: %s", sd->msg);
848
849    /* 
850     * Get OK data
851     */
852    if (!response(jcr, sd, OK_data, "Read Data")) {
853       return 0;
854    }
855    return 1;
856 }
857
858 /* 
859  * Destroy the Job Control Record and associated
860  * resources (sockets).
861  */
862 static void filed_free_jcr(JCR *jcr) 
863 {
864    if (jcr->store_bsock) {
865       bnet_close(jcr->store_bsock);
866    }
867    if (jcr->RestoreBootstrap) {
868       unlink(jcr->RestoreBootstrap);
869       free_pool_memory(jcr->RestoreBootstrap);
870    }
871    if (jcr->last_fname) {
872       free_pool_memory(jcr->last_fname);
873    }
874    return;
875 }
876
877 /*
878  * Get response from Storage daemon to a command we
879  * sent. Check that the response is OK.
880  *
881  *  Returns: 0 on failure
882  *           1 on success
883  */
884 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
885 {
886    int n;
887
888    if (sd->errors) {
889       return 0;
890    }
891    if ((n = bget_msg(sd)) > 0) {
892       Dmsg0(110, sd->msg);
893       if (strcmp(sd->msg, resp) == 0) {
894          return 1;
895       }
896    } 
897    if (job_canceled(jcr)) {
898       return 0;                       /* if canceled avoid useless error messages */
899    }
900    if (is_bnet_error(sd)) {
901       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
902          cmd, bnet_strerror(sd));
903    } else {
904       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
905          cmd, resp, sd->msg);
906    }
907    return 0;
908 }
909
910 static int send_bootstrap_file(JCR *jcr)
911 {
912    FILE *bs;
913    char buf[2000];
914    BSOCK *sd = jcr->store_bsock;
915    char *bootstrap = "bootstrap\n";
916
917    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
918    if (!jcr->RestoreBootstrap) {
919       return 1;
920    }
921    bs = fopen(jcr->RestoreBootstrap, "r");
922    if (!bs) {
923       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
924          jcr->RestoreBootstrap, strerror(errno));
925       set_jcr_job_status(jcr, JS_ErrorTerminated);
926       return 0;
927    }
928    strcpy(sd->msg, bootstrap);  
929    sd->msglen = strlen(sd->msg);
930    bnet_send(sd);
931    while (fgets(buf, sizeof(buf), bs)) {
932       sd->msglen = Mmsg(&sd->msg, "%s", buf);
933       bnet_send(sd);       
934    }
935    bnet_sig(sd, BNET_EOD);
936    fclose(bs);
937    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
938       set_jcr_job_status(jcr, JS_ErrorTerminated);
939       return 0;
940    }
941    return 1;
942 }