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