]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/job.c
BFILE I/O, new console @ commands, regression, file mode restore fixes
[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\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 EndRestore[]  = "2800 End Job TermCode=%d JobFiles=%u JobBytes=%" lld "\n";
114 static char EndBackup[]   = "2801 End Backup Job TermCode=%d JobFiles=%u ReadBytes=%" lld " JobBytes=%" lld "\n";
115
116 /* Responses received from Storage Daemon */
117 static char OK_end[]       = "3000 OK end\n";
118 static char OK_close[]     = "3000 OK close Status = %d\n";
119 static char OK_open[]      = "3000 OK open ticket = %d\n";
120 static char OK_data[]      = "3000 OK data\n";
121 static char OK_append[]    = "3000 OK append data\n";
122 static char OKSDbootstrap[] = "3000 OK bootstrap\n";
123
124
125 /* Commands sent to Storage Daemon */
126 static char append_open[]  = "append open session\n";
127 static char append_data[]  = "append data %d\n";
128 static char append_end[]   = "append end session %d\n";
129 static char append_close[] = "append close session %d\n";
130 static char read_open[]    = "read open session = %s %ld %ld %ld %ld %ld %ld\n";
131 static char read_data[]    = "read data %d\n";
132 static char read_close[]   = "read close session %d\n";
133
134 /* 
135  * Accept requests from a Director
136  *
137  * NOTE! We are running as a separate thread
138  *
139  * Send output one line
140  * at a time followed by a zero length transmission.
141  *
142  * Return when the connection is terminated or there
143  * is an error.
144  *
145  * Basic task here is:
146  *   Authenticate Director (during Hello command).
147  *   Accept commands one at a time from the Director
148  *     and execute them.
149  *
150  */
151 void *handle_client_request(void *dirp)
152 {
153    int i, found, quit;
154    JCR *jcr;
155    BSOCK *dir = (BSOCK *)dirp;
156
157    jcr = new_jcr(sizeof(JCR), filed_free_jcr); /* create JCR */
158    jcr->dir_bsock = dir;
159    jcr->ff = init_find_files();
160    jcr->start_time = time(NULL);
161    jcr->last_fname = get_pool_memory(PM_FNAME);
162    jcr->last_fname[0] = 0;
163    jcr->client_name = get_memory(strlen(my_name) + 1);
164    pm_strcpy(&jcr->client_name, my_name);
165    dir->jcr = (void *)jcr;
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                Pmsg0(20, "Command error\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    Dmsg0(100, "Calling term_find_files\n");
199    term_find_files((FF_PKT *)jcr->ff);
200    Dmsg0(100, "Done with term_find_files\n");
201    free_jcr(jcr);                     /* destroy JCR record */
202    Dmsg0(100, "Done with free_jcr\n");
203    return NULL;
204 }
205
206 /*
207  * Hello from Director he must identify himself and provide his 
208  *  password.
209  */
210 static int hello_cmd(JCR *jcr)
211 {
212    Dmsg0(120, "Calling Authenticate\n");
213    if (!authenticate_director(jcr)) {
214       return 0;
215    }
216    Dmsg0(120, "OK Authenticate\n");
217    jcr->authenticated = TRUE;
218    return 1;
219 }
220
221 /*
222  * Cancel a Job
223  */
224 static int cancel_cmd(JCR *jcr)
225 {
226    BSOCK *dir = jcr->dir_bsock;
227    char Job[MAX_NAME_LENGTH];
228    JCR *cjcr;
229
230    if (sscanf(dir->msg, "cancel Job=%127s", Job) == 1) {
231       if (!(cjcr=get_jcr_by_full_name(Job))) {
232          bnet_fsend(dir, "2901 Job %s not found.\n", Job);
233       } else {
234          set_jcr_job_status(cjcr, JS_Canceled);
235          free_jcr(cjcr);
236          bnet_fsend(dir, "2001 Job %s marked to be canceled.\n", Job);
237       }
238    } else {
239       bnet_fsend(dir, "2902 Error scanning cancel command.\n");
240    }
241    bnet_sig(dir, BNET_EOD);
242    return 1;
243 }
244
245
246 /*
247  * Set debug level as requested by the Director
248  *
249  */
250 static int setdebug_cmd(JCR *jcr)
251 {
252    BSOCK *dir = jcr->dir_bsock;
253    int level;
254
255    Dmsg1(110, "setdebug_cmd: %s", dir->msg);
256    if (sscanf(dir->msg, "setdebug=%d", &level) != 1 || level < 0) {
257       bnet_fsend(dir, "2991 Bad setdebug command: %s\n", dir->msg);
258       return 0;   
259    }
260    debug_level = level;
261    return bnet_fsend(dir, OKsetdebug, level);
262 }
263
264
265 static int estimate_cmd(JCR *jcr)
266 {
267    BSOCK *dir = jcr->dir_bsock;
268    make_estimate(jcr);
269    return bnet_fsend(dir, OKest, jcr->num_files_examined, jcr->JobBytes);
270 }
271
272 /*
273  * Get JobId and Storage Daemon Authorization key from Director
274  */
275 static int job_cmd(JCR *jcr)
276 {
277    BSOCK *dir = jcr->dir_bsock;
278    POOLMEM *sd_auth_key;
279
280    sd_auth_key = get_memory(dir->msglen);
281    if (sscanf(dir->msg, jobcmd,  &jcr->JobId, jcr->Job,  
282               &jcr->VolSessionId, &jcr->VolSessionTime,
283               sd_auth_key) != 5) {
284       bnet_fsend(dir, BADjob);
285       Jmsg(jcr, M_FATAL, 0, _("Bad Job Command: %s\n"), dir->msg);
286       free_pool_memory(sd_auth_key);
287       return 0;
288    }
289    jcr->sd_auth_key = bstrdup(sd_auth_key);
290    free_pool_memory(sd_auth_key);
291    Dmsg2(120, "JobId=%d Auth=%s\n", jcr->JobId, jcr->sd_auth_key);
292    return bnet_fsend(dir, OKjob);
293 }
294
295 #define INC_LIST 0
296 #define EXC_LIST 1
297
298 static void add_fname_to_list(JCR *jcr, char *fname, int list)
299 {
300    char *p;  
301    if (list == INC_LIST) {
302       add_fname_to_include_list((FF_PKT *)jcr->ff, 1, fname);
303    } else {
304       /* Skip leading options -- currently ignored */
305       for (p=fname; *p && *p != ' '; p++)
306          { }
307       /* Skip spaces */
308       for ( ; *p && *p == ' '; p++)
309          { }
310       add_fname_to_exclude_list((FF_PKT *)jcr->ff, p);
311    }
312 }
313
314 /* 
315  * 
316  * Get list of files/directories to include from Director
317  *
318  */
319 static int include_cmd(JCR *jcr)
320 {
321    BSOCK *dir = jcr->dir_bsock;
322
323    while (bnet_recv(dir) >= 0) {
324       dir->msg[dir->msglen] = 0;
325       strip_trailing_junk(dir->msg);
326       Dmsg1(010, "include file: %s\n", dir->msg);
327       add_fname_to_list(jcr, dir->msg, INC_LIST);
328    }
329
330    return bnet_fsend(dir, OKinc);
331 }
332
333 /*
334  * Get list of files to exclude from Director
335  *
336  */
337 static int exclude_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       add_fname_to_list(jcr, dir->msg, EXC_LIST);
345       Dmsg1(110, "<dird: exclude file %s\n", dir->msg);
346    }
347
348    return bnet_fsend(dir, OKexc);
349 }
350
351
352 static int bootstrap_cmd(JCR *jcr)
353 {
354    BSOCK *dir = jcr->dir_bsock;
355    POOLMEM *fname = get_pool_memory(PM_FNAME);
356    FILE *bs;
357
358    if (jcr->RestoreBootstrap) {
359       unlink(jcr->RestoreBootstrap);
360       free_pool_memory(jcr->RestoreBootstrap);
361    }
362    Mmsg(&fname, "%s/%s.%s.bootstrap", me->working_directory, me->hdr.name,
363       jcr->Job);
364    Dmsg1(400, "bootstrap=%s\n", fname);
365    jcr->RestoreBootstrap = fname;
366    bs = fopen(fname, "a+");           /* create file */
367    if (!bs) {
368       Jmsg(jcr, M_FATAL, 0, _("Could not create bootstrap file %s: ERR=%s\n"),
369          jcr->RestoreBootstrap, strerror(errno));
370       free_pool_memory(jcr->RestoreBootstrap);
371       jcr->RestoreBootstrap = NULL;
372       set_jcr_job_status(jcr, JS_ErrorTerminated);
373       return 0;
374    }
375
376    while (bnet_recv(dir) >= 0) {
377        Dmsg1(200, "filed<dird: bootstrap file %s\n", dir->msg);
378        fputs(dir->msg, bs);
379    }
380    fclose(bs);
381
382    return bnet_fsend(dir, OKbootstrap);
383 }
384
385
386 /*
387  * Get backup level from Director
388  *
389  */
390 static int level_cmd(JCR *jcr)
391 {
392    BSOCK *dir = jcr->dir_bsock;
393    POOLMEM *level;
394    struct tm tm;
395    time_t mtime;
396
397    level = get_memory(dir->msglen+1);
398    Dmsg1(110, "level_cmd: %s", dir->msg);
399    if (sscanf(dir->msg, "level = %s ", level) != 1) {
400       Jmsg1(jcr, M_FATAL, 0, _("Bad level command: %s\n"), dir->msg);
401       free_memory(level);
402       return 0;
403    }
404    /*
405     * Full backup requested
406     */
407    if (strcmp(level, "full") == 0) {
408       jcr->save_level = L_FULL;
409    /* 
410     * Backup requested since <date> <time>
411     *  This form is also used for incremental and differential
412     */
413    } else if (strcmp(level, "since") == 0) {
414       jcr->save_level = L_SINCE;
415       if (sscanf(dir->msg, "level = since %d-%d-%d %d:%d:%d", 
416                  &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
417                  &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
418          Jmsg1(jcr, M_FATAL, 0, _("Bad scan of date/time: %s\n"), dir->msg);
419          free_memory(level);
420          return 0;
421       }
422       tm.tm_year -= 1900;
423       tm.tm_mon  -= 1;
424       tm.tm_wday = tm.tm_yday = 0;              
425       tm.tm_isdst = -1;
426       mtime = mktime(&tm);
427       Dmsg1(100, "Got since time: %s", ctime(&mtime));
428       jcr->incremental = 1;
429       jcr->mtime = mtime;
430    } else {
431       Jmsg1(jcr, M_FATAL, 0, "Unknown backup level: %s\n", level);
432       free_memory(level);
433       return 0;
434    }
435    free_memory(level);
436    return bnet_fsend(dir, OKlevel);
437 }
438
439 /*
440  * Get session parameters from Director -- this is for a Restore command
441  */
442 static int session_cmd(JCR *jcr)
443 {
444    BSOCK *dir = jcr->dir_bsock;
445
446    Dmsg1(100, "SessionCmd: %s", dir->msg);
447    if (sscanf(dir->msg, sessioncmd, jcr->VolumeName,
448               &jcr->VolSessionId, &jcr->VolSessionTime,
449               &jcr->StartFile, &jcr->EndFile, 
450               &jcr->StartBlock, &jcr->EndBlock) != 7) {
451       Jmsg(jcr, M_FATAL, 0, "Bad session command: %s", dir->msg);
452       return 0;
453    }
454
455    return bnet_fsend(dir, OKsession);
456 }
457
458 /*
459  * Get address of storage daemon from Director
460  *
461  */
462 static int storage_cmd(JCR *jcr)
463 {
464    int stored_port;                /* storage daemon port */
465    BSOCK *dir = jcr->dir_bsock;
466    BSOCK *sd;                         /* storage daemon bsock */
467
468    Dmsg1(100, "StorageCmd: %s", dir->msg);
469    if (sscanf(dir->msg, storaddr, &jcr->stored_addr, &stored_port) != 2) {
470       Jmsg(jcr, M_FATAL, 0, _("Bad storage command: %s"), dir->msg);
471       return 0;
472    }
473    Dmsg2(110, "Open storage: %s:%d\n", jcr->stored_addr, stored_port);
474    /* Open command communications with Storage daemon */
475    /* Try to connect for 1 hour at 10 second intervals */
476    sd = bnet_connect(jcr, 10, 3600, _("Storage daemon"), 
477                      jcr->stored_addr, NULL, stored_port, 1);
478    if (sd == NULL) {
479       Jmsg(jcr, M_FATAL, 0, _("Failed to connect to Storage daemon: %s:%d\n"),
480           jcr->stored_addr, stored_port);
481       return 0;
482    }
483
484    jcr->store_bsock = sd;
485
486    bnet_fsend(sd, "Hello Start Job %s\n", jcr->Job);
487    if (!authenticate_storagedaemon(jcr)) {
488       Jmsg(jcr, M_FATAL, 0, _("Failed to authenticate Storage daemon.\n"));
489       return 0;
490    }
491    Dmsg0(110, "Authenticated with SD.\n");
492
493    /* Send OK to Director */
494    return bnet_fsend(dir, OKstore);
495 }
496
497
498 /*  
499  * Do a backup. For now, we handle only Full and Incremental.
500  */
501 static int backup_cmd(JCR *jcr)
502
503    BSOCK *dir = jcr->dir_bsock;
504    BSOCK *sd = jcr->store_bsock;
505    int ok = 0;
506    int SDJobStatus;
507
508    set_jcr_job_status(jcr, JS_Blocked);
509    jcr->JobType = JT_BACKUP;
510    Dmsg1(100, "begin backup ff=%p\n", (FF_PKT *)jcr->ff);
511
512    if (sd == NULL) {
513       Jmsg(jcr, M_FATAL, 0, _("Cannot contact Storage daemon\n"));
514       set_jcr_job_status(jcr, JS_ErrorTerminated);
515       goto cleanup;
516    }
517
518    bnet_fsend(dir, OKbackup);
519    Dmsg1(110, "bfiled>dird: %s", dir->msg);
520
521    /* 
522     * Send Append Open Session to Storage daemon
523     */
524    bnet_fsend(sd, append_open);
525    Dmsg1(110, ">stored: %s", sd->msg);
526    /* 
527     * Expect to receive back the Ticket number
528     */
529    if (bnet_recv(sd) >= 0) {
530       Dmsg1(110, "<stored: %s", sd->msg);
531       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
532          Jmsg(jcr, M_FATAL, 0, _("Bad response to append open: %s\n"), sd->msg);
533          set_jcr_job_status(jcr, JS_ErrorTerminated);
534          goto cleanup;
535       }
536       Dmsg1(110, "Got Ticket=%d\n", jcr->Ticket);
537    } else {
538       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to open command\n"));
539       set_jcr_job_status(jcr, JS_ErrorTerminated);
540       goto cleanup;
541    }
542
543    /* 
544     * Send Append data command to Storage daemon
545     */
546    bnet_fsend(sd, append_data, jcr->Ticket);
547    Dmsg1(110, ">stored: %s", sd->msg);
548
549    /* 
550     * Expect to get OK data 
551     */
552    Dmsg1(110, "<stored: %s", sd->msg);
553    if (!response(jcr, sd, OK_data, "Append Data")) {
554       set_jcr_job_status(jcr, JS_ErrorTerminated);
555       goto cleanup;
556    }
557       
558    /*
559     * Send Files to Storage daemon
560     */
561    Dmsg1(110, "begin blast ff=%p\n", (FF_PKT *)jcr->ff);
562    if (!blast_data_to_storage_daemon(jcr, NULL)) {
563       set_jcr_job_status(jcr, JS_ErrorTerminated);
564       bnet_suppress_error_messages(sd, 1);
565    } else {
566       set_jcr_job_status(jcr, JS_Terminated);
567       if (jcr->JobStatus != JS_Terminated) {
568          bnet_suppress_error_messages(sd, 1);
569          goto cleanup;                /* bail out now */
570       }
571       /* 
572        * Expect to get response to append_data from Storage daemon
573        */
574       if (!response(jcr, sd, OK_append, "Append Data")) {
575          set_jcr_job_status(jcr, JS_ErrorTerminated);
576          goto cleanup;
577       }
578      
579       /* 
580        * Send Append End Data to Storage daemon
581        */
582       bnet_fsend(sd, append_end, jcr->Ticket);
583       /* Get end OK */
584       if (!response(jcr, sd, OK_end, "Append End")) {
585          set_jcr_job_status(jcr, JS_ErrorTerminated);
586          goto cleanup;
587       }
588
589       /*
590        * Send Append Close to Storage daemon
591        */
592       bnet_fsend(sd, append_close, jcr->Ticket);
593       while (bnet_recv(sd) >= 0) {    /* stop on signal or error */
594          if (sscanf(sd->msg, OK_close, &SDJobStatus) == 1) {
595             ok = 1;
596             Dmsg2(200, "SDJobStatus = %d %c\n", SDJobStatus, (char)SDJobStatus);
597          }
598       }
599       if (!ok) {
600          Jmsg(jcr, M_FATAL, 0, _("Append Close with SD failed.\n"));
601          set_jcr_job_status(jcr, JS_ErrorTerminated);
602          goto cleanup;
603       }
604       if (SDJobStatus != JS_Terminated) {
605          Jmsg(jcr, M_FATAL, 0, _("Bad status %d returned from Storage Daemon.\n"),
606             SDJobStatus);
607          set_jcr_job_status(jcr, JS_ErrorTerminated);
608       }
609    }
610
611 cleanup:
612
613    /* Inform Storage daemon that we are done */
614    if (sd) {
615       bnet_sig(sd, BNET_TERMINATE);
616    }
617
618    bnet_fsend(dir, EndBackup, jcr->JobStatus, jcr->JobFiles, jcr->ReadBytes, jcr->JobBytes);
619
620    /* Inform Director that we are done */
621    bnet_sig(dir, BNET_TERMINATE);
622
623    return 0;                          /* return and stop command loop */
624 }
625
626 /*  
627  * Do a Verify for Director
628  *
629  */
630 static int verify_cmd(JCR *jcr)
631
632    BSOCK *dir = jcr->dir_bsock;
633    BSOCK *sd  = jcr->store_bsock;
634    char level[100];
635
636    jcr->JobType = JT_VERIFY;
637    if (sscanf(dir->msg, verifycmd, level) != 1) {
638       bnet_fsend(dir, "2994 Bad verify command: %s\n", dir->msg);
639       return 0;   
640    }
641    if (strcasecmp(level, "init") == 0) {
642       jcr->JobLevel = L_VERIFY_INIT;
643    } else if (strcasecmp(level, "catalog") == 0){
644       jcr->JobLevel = L_VERIFY_CATALOG;
645    } else if (strcasecmp(level, "volume") == 0){
646       jcr->JobLevel = L_VERIFY_VOLUME_TO_CATALOG;
647    } else if (strcasecmp(level, "data") == 0){
648       jcr->JobLevel = L_VERIFY_DATA;
649    } else {   
650       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
651       return 0;   
652    }
653
654    bnet_fsend(dir, OKverify);
655    Dmsg1(110, "bfiled>dird: %s", dir->msg);
656
657    switch (jcr->JobLevel) {
658    case L_VERIFY_INIT:
659    case L_VERIFY_CATALOG:
660       do_verify(jcr);
661       break;
662    case L_VERIFY_VOLUME_TO_CATALOG:
663       if (!open_sd_read_session(jcr)) {
664          return 0;
665       }
666       do_verify_volume(jcr);
667       /* 
668        * Send Close session command to Storage daemon
669        */
670       bnet_fsend(sd, read_close, jcr->Ticket);
671       Dmsg1(130, "bfiled>stored: %s", sd->msg);
672
673       /* ****FIXME**** check response */
674       bnet_recv(sd);                     /* get OK */
675
676       /* Inform Storage daemon that we are done */
677       bnet_sig(sd, BNET_TERMINATE);
678
679       break;
680    default:
681       bnet_fsend(dir, "2994 Bad verify level: %s\n", dir->msg);
682       return 0; 
683    }
684
685    /* Inform Director that we are done */
686    bnet_sig(dir, BNET_TERMINATE);
687    return 0;                          /* return and terminate command loop */
688 }
689
690 /*  
691  * Do a Restore for Director
692  *
693  */
694 static int restore_cmd(JCR *jcr)
695
696    BSOCK *dir = jcr->dir_bsock;
697    BSOCK *sd = jcr->store_bsock;
698    POOLMEM *where;
699    char replace;
700
701    /*
702     * Scan WHERE (base directory for restore) from command
703     */
704    Dmsg0(150, "restore command\n");
705    /* Pickup where string */
706    where = get_memory(dir->msglen+1);
707    *where = 0;
708
709    if (sscanf(dir->msg, restorecmd, &replace, where) != 2) {
710       if (sscanf(dir->msg, restorecmd1, &replace) != 1) {
711          Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), dir->msg);
712          return 0;
713       }
714       *where = 0;
715    }
716    /* Turn / into nothing */
717    if (where[0] == '/' && where[1] == 0) {
718       where[0] = 0;
719    }
720       
721    Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
722    unbash_spaces(where);
723    jcr->where = where;
724    jcr->replace = replace;
725
726    bnet_fsend(dir, OKrestore);
727    Dmsg1(110, "bfiled>dird: %s", dir->msg);
728
729    jcr->JobType = JT_RESTORE;
730
731    set_jcr_job_status(jcr, JS_Blocked);
732    if (!open_sd_read_session(jcr)) {
733       set_jcr_job_status(jcr, JS_ErrorTerminated);
734       goto bail_out;
735    }
736
737    set_jcr_job_status(jcr, JS_Running);
738
739    /* 
740     * Do restore of files and data
741     */
742    do_restore(jcr);
743
744    set_jcr_job_status(jcr, JS_Terminated);
745    if (jcr->JobStatus != JS_Terminated) {
746       bnet_suppress_error_messages(sd, 1);
747    }
748
749    /* 
750     * Send Close session command to Storage daemon
751     */
752    bnet_fsend(sd, read_close, jcr->Ticket);
753    Dmsg1(130, "bfiled>stored: %s", sd->msg);
754
755    bnet_recv(sd);                     /* get OK */
756
757    /* Inform Storage daemon that we are done */
758    bnet_sig(sd, BNET_TERMINATE);
759
760 bail_out:
761    /* Send termination status back to Dir */
762    bnet_fsend(dir, EndRestore, jcr->JobStatus, jcr->num_files_examined, 
763               jcr->JobBytes);
764
765    /* Inform Director that we are done */
766    bnet_sig(dir, BNET_TERMINATE);
767
768    Dmsg0(130, "Done in job.c\n");
769    return 0;                          /* return and terminate command loop */
770 }
771
772 static int open_sd_read_session(JCR *jcr)
773 {
774    BSOCK *sd = jcr->store_bsock;
775
776    if (!sd) {
777       Jmsg(jcr, M_FATAL, 0, _("Improper calling sequence.\n"));
778       return 0;
779    }
780    Dmsg4(120, "VolSessId=%ld VolsessT=%ld SF=%ld EF=%ld\n",
781       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile);
782    Dmsg2(120, "JobId=%d vol=%s\n", jcr->JobId, "DummyVolume");
783    /* 
784     * Open Read Session with Storage daemon
785     */
786    bnet_fsend(sd, read_open, jcr->VolumeName,
787       jcr->VolSessionId, jcr->VolSessionTime, jcr->StartFile, jcr->EndFile, 
788       jcr->StartBlock, jcr->EndBlock);
789    Dmsg1(110, ">stored: %s", sd->msg);
790
791    /* 
792     * Get ticket number
793     */
794    if (bnet_recv(sd) >= 0) {
795       Dmsg1(110, "bfiled<stored: %s", sd->msg);
796       if (sscanf(sd->msg, OK_open, &jcr->Ticket) != 1) {
797          Jmsg(jcr, M_FATAL, 0, _("Bad response to SD read open: %s\n"), sd->msg);
798          return 0;
799       }
800       Dmsg1(110, "bfiled: got Ticket=%d\n", jcr->Ticket);
801    } else {
802       Jmsg(jcr, M_FATAL, 0, _("Bad response from stored to read open command\n"));
803       return 0;
804    }
805
806    if (!send_bootstrap_file(jcr)) {
807       return 0;
808    }
809
810    /* 
811     * Start read of data with Storage daemon
812     */
813    bnet_fsend(sd, read_data, jcr->Ticket);
814    Dmsg1(110, ">stored: %s", sd->msg);
815
816    /* 
817     * Get OK data
818     */
819    if (!response(jcr, sd, OK_data, "Read Data")) {
820       return 0;
821    }
822    return 1;
823 }
824
825 /* 
826  * Destroy the Job Control Record and associated
827  * resources (sockets).
828  */
829 static void filed_free_jcr(JCR *jcr) 
830 {
831    if (jcr->store_bsock) {
832       bnet_close(jcr->store_bsock);
833    }
834    if (jcr->where) {
835       free_pool_memory(jcr->where);
836    }
837    if (jcr->RestoreBootstrap) {
838       unlink(jcr->RestoreBootstrap);
839       free_pool_memory(jcr->RestoreBootstrap);
840    }
841    if (jcr->last_fname) {
842       free_pool_memory(jcr->last_fname);
843    }
844    return;
845 }
846
847 /*
848  * Get response from Storage daemon to a command we
849  * sent. Check that the response is OK.
850  *
851  *  Returns: 0 on failure
852  *           1 on success
853  */
854 int response(JCR *jcr, BSOCK *sd, char *resp, char *cmd)
855 {
856    int n;
857
858    if (sd->errors) {
859       return 0;
860    }
861    if ((n = bnet_recv(sd)) > 0) {
862       Dmsg0(110, sd->msg);
863       if (strcmp(sd->msg, resp) == 0) {
864          return 1;
865       }
866    } 
867    if (is_bnet_error(sd)) {
868       Jmsg2(jcr, M_FATAL, 0, _("Comm error with SD. bad response to %s. ERR=%s\n"),
869          cmd, bnet_strerror(sd));
870    } else {
871       Jmsg3(jcr, M_FATAL, 0, _("Bad response to %s command. Wanted %s, got %s\n"),
872          cmd, resp, sd->msg);
873    }
874    return 0;
875 }
876
877 static int send_bootstrap_file(JCR *jcr)
878 {
879    FILE *bs;
880    char buf[2000];
881    BSOCK *sd = jcr->store_bsock;
882    char *bootstrap = "bootstrap\n";
883
884    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
885    if (!jcr->RestoreBootstrap) {
886       return 1;
887    }
888    bs = fopen(jcr->RestoreBootstrap, "r");
889    if (!bs) {
890       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"), 
891          jcr->RestoreBootstrap, strerror(errno));
892       set_jcr_job_status(jcr, JS_ErrorTerminated);
893       return 0;
894    }
895    strcpy(sd->msg, bootstrap);  
896    sd->msglen = strlen(sd->msg);
897    bnet_send(sd);
898    while (fgets(buf, sizeof(buf), bs)) {
899       sd->msglen = Mmsg(&sd->msg, "%s", buf);
900       bnet_send(sd);       
901    }
902    bnet_sig(sd, BNET_EOD);
903    fclose(bs);
904    if (!response(jcr, sd, OKSDbootstrap, "Bootstrap")) {
905       set_jcr_job_status(jcr, JS_ErrorTerminated);
906       return 0;
907    }
908    return 1;
909 }