]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
06Mar05
[bacula/bacula] / bacula / src / dird / fd_cmds.c
1 /*
2  *
3  *   Bacula Director -- fd_cmds.c -- send commands to File daemon
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  *
10  *  Utility functions for sending info to File Daemon.
11  *   These functions are used by both backup and verify.
12  *
13  *   Version $Id$
14  */
15 /*
16    Copyright (C) 2000-2005 Kern Sibbald
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35 #include "bacula.h"
36 #include "dird.h"
37
38 /* Commands sent to File daemon */
39 static char inc[]         = "include\n";
40 static char exc[]         = "exclude\n";
41 static char fileset[]     = "fileset\n"; /* set full fileset */
42 static char jobcmd[]      = "JobId=%d Job=%s SDid=%u SDtime=%u Authorization=%s\n";
43 /* Note, mtime_only is not used here -- implemented as file option */
44 static char levelcmd[]    = "level = %s%s mtime_only=%d\n";
45 static char runbefore[]   = "RunBeforeJob %s\n";
46 static char runafter[]    = "RunAfterJob %s\n";
47
48
49 /* Responses received from File daemon */
50 static char OKinc[]       = "2000 OK include\n";
51 static char OKexc[]       = "2000 OK exclude\n";
52 static char OKjob[]       = "2000 OK Job";
53 static char OKbootstrap[] = "2000 OK bootstrap\n";
54 static char OKlevel[]     = "2000 OK level\n";
55 static char OKRunBefore[] = "2000 OK RunBefore\n";
56 static char OKRunAfter[]  = "2000 OK RunAfter\n";
57
58 /* Forward referenced functions */
59
60 /* External functions */
61 extern int debug_level;
62 extern DIRRES *director;
63 extern int FDConnectTimeout;
64
65 #define INC_LIST 0
66 #define EXC_LIST 1
67
68 /*
69  * Open connection with File daemon.
70  * Try connecting every retry_interval (default 10 sec), and
71  *   give up after max_retry_time (default 30 mins).
72  */
73
74 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
75                            int verbose)
76 {
77    BSOCK   *fd;
78
79    if (!jcr->file_bsock) {
80       fd = bnet_connect(jcr, retry_interval, max_retry_time,
81            _("File daemon"), jcr->client->address,
82            NULL, jcr->client->FDport, verbose);
83       if (fd == NULL) {
84          set_jcr_job_status(jcr, JS_ErrorTerminated);
85          return 0;
86       }
87       Dmsg0(10, "Opened connection with File daemon\n");
88    } else {
89       fd = jcr->file_bsock;           /* use existing connection */
90    }
91    fd->res = (RES *)jcr->client;      /* save resource in BSOCK */
92    jcr->file_bsock = fd;
93    set_jcr_job_status(jcr, JS_Running);
94
95    if (!authenticate_file_daemon(jcr)) {
96       set_jcr_job_status(jcr, JS_ErrorTerminated);
97       return 0;
98    }
99
100    /*
101     * Now send JobId and authorization key
102     */
103    bnet_fsend(fd, jobcmd, jcr->JobId, jcr->Job, jcr->VolSessionId,
104       jcr->VolSessionTime, jcr->sd_auth_key);
105    if (strcmp(jcr->sd_auth_key, "dummy") != 0) {
106       memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
107    }
108    Dmsg1(100, ">filed: %s", fd->msg);
109    if (bget_dirmsg(fd) > 0) {
110        Dmsg1(110, "<filed: %s", fd->msg);
111        if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
112           Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
113              jcr->client->hdr.name, fd->msg);
114           set_jcr_job_status(jcr, JS_ErrorTerminated);
115           return 0;
116        } else if (jcr->db) {
117           CLIENT_DBR cr;
118           memset(&cr, 0, sizeof(cr));
119           bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
120           cr.AutoPrune = jcr->client->AutoPrune;
121           cr.FileRetention = jcr->client->FileRetention;
122           cr.JobRetention = jcr->client->JobRetention;
123           bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
124           if (!db_update_client_record(jcr, jcr->db, &cr)) {
125              Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
126                 db_strerror(jcr->db));
127           }
128        }
129    } else {
130       Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
131          bnet_strerror(fd));
132       set_jcr_job_status(jcr, JS_ErrorTerminated);
133       return 0;
134    }
135    return 1;
136 }
137
138 /*
139  * This subroutine edits the last job start time into a
140  *   "since=date/time" buffer that is returned in the
141  *   variable since.  This is used for display purposes in
142  *   the job report.  The time in jcr->stime is later
143  *   passed to tell the File daemon what to do.
144  */
145 void get_level_since_time(JCR *jcr, char *since, int since_len)
146 {
147    int JobLevel;
148
149    since[0] = 0;
150    if (jcr->cloned) {
151       if ( jcr->stime && jcr->stime[0]) {
152          bstrncpy(since, ", since=", since_len);
153          bstrncat(since, jcr->stime, since_len);
154       }
155       return;
156    }
157    if (!jcr->stime) {
158       jcr->stime = get_pool_memory(PM_MESSAGE);
159    } 
160    jcr->stime[0] = 0;
161    /* Lookup the last FULL backup job to get the time/date for a
162     * differential or incremental save.
163     */
164    switch (jcr->JobLevel) {
165    case L_DIFFERENTIAL:
166    case L_INCREMENTAL:
167       /* Look up start time of last job */
168       jcr->jr.JobId = 0;     /* flag for db_find_job_start time */
169       if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
170          /* No job found, so upgrade this one to Full */
171          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
172          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
173          bsnprintf(since, since_len, " (upgraded from %s)",
174             level_to_str(jcr->JobLevel));
175          jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
176       } else {
177          if (jcr->job->rerun_failed_levels) {
178             if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
179                Jmsg(jcr, M_INFO, 0, _("Prior failed job found. Upgrading to %s.\n"),
180                   level_to_str(JobLevel));
181                bsnprintf(since, since_len, " (upgraded from %s)",
182                   level_to_str(jcr->JobLevel));
183                jcr->JobLevel = jcr->jr.JobLevel = JobLevel;
184                jcr->jr.JobId = jcr->JobId;
185                break;
186             }
187          }
188          bstrncpy(since, ", since=", since_len);
189          bstrncat(since, jcr->stime, since_len);
190       }
191       jcr->jr.JobId = jcr->JobId;
192       break;
193    }
194    Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
195 }
196
197 static void send_since_time(JCR *jcr)
198 {
199    BSOCK   *fd = jcr->file_bsock;
200    utime_t stime;
201    char ed1[50];
202
203    stime = str_to_utime(jcr->stime);
204    bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
205    while (bget_dirmsg(fd) >= 0) {  /* allow him to poll us to sync clocks */
206       Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
207    }
208 }
209
210
211 /*
212  * Send level command to FD.
213  * Used for backup jobs and estimate command.
214  */
215 int send_level_command(JCR *jcr)
216 {
217    BSOCK   *fd = jcr->file_bsock;
218    /*
219     * Send Level command to File daemon
220     */
221    switch (jcr->JobLevel) {
222    case L_BASE:
223       bnet_fsend(fd, levelcmd, "base", " ", 0);
224       break;
225    /* L_NONE is the console, sending something off to the FD */
226    case L_NONE:
227    case L_FULL:
228       bnet_fsend(fd, levelcmd, "full", " ", 0);
229       break;
230    case L_DIFFERENTIAL:
231       bnet_fsend(fd, levelcmd, "differential", " ", 0);
232       send_since_time(jcr);
233       break;
234    case L_INCREMENTAL:
235       bnet_fsend(fd, levelcmd, "incremental", " ", 0);
236       send_since_time(jcr);
237       break;
238    case L_SINCE:
239    default:
240       Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
241          jcr->JobLevel, jcr->JobLevel);
242       return 0;
243    }
244    Dmsg1(120, ">filed: %s", fd->msg);
245    if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
246       return 0;
247    }
248    return 1;
249 }
250
251
252 /*
253  * Send either an Included or an Excluded list to FD
254  */
255 static int send_list(JCR *jcr, int list)
256 {
257    FILESET *fileset;
258    BSOCK   *fd;
259    int num;
260
261    fd = jcr->file_bsock;
262    fileset = jcr->fileset;
263
264    if (list == INC_LIST) {
265       num = fileset->num_includes;
266    } else {
267       num = fileset->num_excludes;
268    }
269
270    for (int i=0; i<num; i++) {
271       BPIPE *bpipe;
272       FILE *ffd;
273       char buf[2000];
274       char *p;
275       int optlen, stat;
276       INCEXE *ie;
277
278
279       if (list == INC_LIST) {
280          ie = fileset->include_items[i];
281       } else {
282          ie = fileset->exclude_items[i];
283       }
284       for (int j=0; j<ie->name_list.size(); j++) {
285          p = (char *)ie->name_list.get(j);
286          switch (*p) {
287          case '|':
288             p++;                      /* skip over the | */
289             fd->msg = edit_job_codes(jcr, fd->msg, p, "");
290             bpipe = open_bpipe(fd->msg, 0, "r");
291             if (!bpipe) {
292                berrno be;
293                Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
294                   p, be.strerror());
295                goto bail_out;
296             }
297             /* Copy File options */
298             if (ie->num_opts) {
299                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
300                bstrncat(buf, " ", sizeof(buf));
301             } else {
302                bstrncpy(buf, "0 ", sizeof(buf));
303             }
304             Dmsg1(500, "Opts=%s\n", buf);
305             optlen = strlen(buf);
306             while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
307                fd->msglen = Mmsg(fd->msg, "%s", buf);
308                Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
309                if (!bnet_send(fd)) {
310                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
311                   goto bail_out;
312                }
313             }
314             if ((stat=close_bpipe(bpipe)) != 0) {
315                berrno be;
316                Jmsg(jcr, M_FATAL, 0, _("Error running program %p: ERR=%s\n"),
317                   p, be.strerror(stat));
318                goto bail_out;
319             }
320             break;
321          case '<':
322             p++;                      /* skip over < */
323             if ((ffd = fopen(p, "r")) == NULL) {
324                Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
325                   list==INC_LIST?"included":"excluded", p, strerror(errno));
326                goto bail_out;
327             }
328             /* Copy File options */
329             if (ie->num_opts) {
330                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
331                bstrncat(buf, " ", sizeof(buf));
332             } else {
333                bstrncpy(buf, "0 ", sizeof(buf));
334             }
335             Dmsg1(500, "Opts=%s\n", buf);
336             optlen = strlen(buf);
337             while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
338                fd->msglen = Mmsg(fd->msg, "%s", buf);
339                if (!bnet_send(fd)) {
340                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
341                   goto bail_out;
342                }
343             }
344             fclose(ffd);
345             break;
346          case '\\':
347             p++;                      /* skip over \ */
348             /* Note, fall through wanted */
349          default:
350             if (ie->num_opts) {
351                Dmsg2(500, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
352                pm_strcpy(fd->msg, ie->opts_list[0]->opts);
353                pm_strcat(fd->msg, " ");
354             } else {
355                pm_strcpy(fd->msg, "0 ");
356             }
357             fd->msglen = pm_strcat(fd->msg, p);
358             Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
359             if (!bnet_send(fd)) {
360                Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
361                goto bail_out;
362             }
363             break;
364          }
365       }
366    }
367    bnet_sig(fd, BNET_EOD);            /* end of data */
368    if (list == INC_LIST) {
369       if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
370          goto bail_out;
371       }
372    } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
373         goto bail_out;
374    }
375    return 1;
376
377 bail_out:
378    set_jcr_job_status(jcr, JS_ErrorTerminated);
379    return 0;
380
381 }
382
383
384 /*
385  * Send either an Included or an Excluded list to FD
386  */
387 static int send_fileset(JCR *jcr)
388 {
389    FILESET *fileset = jcr->fileset;
390    BSOCK   *fd = jcr->file_bsock;
391    int num;
392    bool include = true;
393
394    for ( ;; ) {
395       if (include) {
396          num = fileset->num_includes;
397       } else {
398          num = fileset->num_excludes;
399       }
400       for (int i=0; i<num; i++) {
401          BPIPE *bpipe;
402          FILE *ffd;
403          char buf[2000];
404          char *p;
405          int optlen, stat;
406          INCEXE *ie;
407          int j, k;
408
409          if (include) {
410             ie = fileset->include_items[i];
411             bnet_fsend(fd, "I\n");
412          } else {
413             ie = fileset->exclude_items[i];
414             bnet_fsend(fd, "E\n");
415          }
416          for (j=0; j<ie->num_opts; j++) {
417             FOPTS *fo = ie->opts_list[j];
418             bnet_fsend(fd, "O %s\n", fo->opts);
419             for (k=0; k<fo->regex.size(); k++) {
420                bnet_fsend(fd, "R %s\n", fo->regex.get(k));
421             }
422             for (k=0; k<fo->regexdir.size(); k++) {
423                bnet_fsend(fd, "RD %s\n", fo->regexdir.get(k));
424             }
425             for (k=0; k<fo->regexfile.size(); k++) {
426                bnet_fsend(fd, "RF %s\n", fo->regexfile.get(k));
427             }
428             for (k=0; k<fo->wild.size(); k++) {
429                bnet_fsend(fd, "W %s\n", fo->wild.get(k));
430             }
431             for (k=0; k<fo->wilddir.size(); k++) {
432                bnet_fsend(fd, "WD %s\n", fo->wilddir.get(k));
433             }
434             for (k=0; k<fo->wildfile.size(); k++) {
435                bnet_fsend(fd, "WF %s\n", fo->wildfile.get(k));
436             }
437             for (k=0; k<fo->base.size(); k++) {
438                bnet_fsend(fd, "B %s\n", fo->base.get(k));
439             }
440             for (k=0; k<fo->fstype.size(); k++) {
441                bnet_fsend(fd, "X %s\n", fo->fstype.get(k));
442             }
443             if (fo->reader) {
444                bnet_fsend(fd, "D %s\n", fo->reader);
445             }
446             if (fo->writer) {
447                bnet_fsend(fd, "T %s\n", fo->writer);
448             }
449             bnet_fsend(fd, "N\n");
450          }
451
452          for (j=0; j<ie->name_list.size(); j++) {
453             p = (char *)ie->name_list.get(j);
454             switch (*p) {
455             case '|':
456                p++;                      /* skip over the | */
457                fd->msg = edit_job_codes(jcr, fd->msg, p, "");
458                bpipe = open_bpipe(fd->msg, 0, "r");
459                if (!bpipe) {
460                   berrno be;
461                   Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
462                      p, be.strerror());
463                   goto bail_out;
464                }
465                bstrncpy(buf, "F ", sizeof(buf));
466                Dmsg1(500, "Opts=%s\n", buf);
467                optlen = strlen(buf);
468                while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
469                   fd->msglen = Mmsg(fd->msg, "%s", buf);
470                   Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
471                   if (!bnet_send(fd)) {
472                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
473                      goto bail_out;
474                   }
475                }
476                if ((stat=close_bpipe(bpipe)) != 0) {
477                   berrno be;
478                   Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
479                      p, be.strerror(stat));
480                   goto bail_out;
481                }
482                break;
483             case '<':
484                p++;                      /* skip over < */
485                if ((ffd = fopen(p, "r")) == NULL) {
486                   berrno be;
487                   Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
488                      p, be.strerror());
489                   goto bail_out;
490                }
491                bstrncpy(buf, "F ", sizeof(buf));
492                Dmsg1(500, "Opts=%s\n", buf);
493                optlen = strlen(buf);
494                while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
495                   fd->msglen = Mmsg(fd->msg, "%s", buf);
496                   if (!bnet_send(fd)) {
497                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
498                      goto bail_out;
499                   }
500                }
501                fclose(ffd);
502                break;
503             case '\\':
504                p++;                      /* skip over \ */
505                /* Note, fall through wanted */
506             default:
507                pm_strcpy(fd->msg, "F ");
508                fd->msglen = pm_strcat(fd->msg, p);
509                Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
510                if (!bnet_send(fd)) {
511                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
512                   goto bail_out;
513                }
514                break;
515             }
516          }
517          bnet_fsend(fd, "N\n");
518       }
519       if (!include) {                 /* If we just did excludes */
520          break;                       /*   all done */
521       }
522       include = false;                /* Now do excludes */
523    }
524
525    bnet_sig(fd, BNET_EOD);            /* end of data */
526    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
527       goto bail_out;
528    }
529    return 1;
530
531 bail_out:
532    set_jcr_job_status(jcr, JS_ErrorTerminated);
533    return 0;
534
535 }
536
537
538 /*
539  * Send include list to File daemon
540  */
541 int send_include_list(JCR *jcr)
542 {
543    BSOCK *fd = jcr->file_bsock;
544    if (jcr->fileset->new_include) {
545       bnet_fsend(fd, fileset);
546       return send_fileset(jcr);
547    } else {
548       bnet_fsend(fd, inc);
549    }
550    return send_list(jcr, INC_LIST);
551 }
552
553
554 /*
555  * Send exclude list to File daemon
556  */
557 int send_exclude_list(JCR *jcr)
558 {
559    BSOCK *fd = jcr->file_bsock;
560    if (jcr->fileset->new_include) {
561       return 1;
562    }
563    bnet_fsend(fd, exc);
564    return send_list(jcr, EXC_LIST);
565 }
566
567
568 /*
569  * Send bootstrap file if any to the File daemon.
570  *  This is used for restore and verify VolumeToCatalog
571  */
572 int send_bootstrap_file(JCR *jcr)
573 {
574    FILE *bs;
575    char buf[1000];
576    BSOCK *fd = jcr->file_bsock;
577    const char *bootstrap = "bootstrap\n";
578
579    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
580    if (!jcr->RestoreBootstrap) {
581       return 1;
582    }
583    bs = fopen(jcr->RestoreBootstrap, "r");
584    if (!bs) {
585       berrno be;
586       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
587          jcr->RestoreBootstrap, be.strerror());
588       set_jcr_job_status(jcr, JS_ErrorTerminated);
589       return 0;
590    }
591    bnet_fsend(fd, bootstrap);
592    while (fgets(buf, sizeof(buf), bs)) {
593       bnet_fsend(fd, "%s", buf);
594    }
595    bnet_sig(fd, BNET_EOD);
596    fclose(bs);
597    if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
598       set_jcr_job_status(jcr, JS_ErrorTerminated);
599       return 0;
600    }
601    return 1;
602 }
603
604 /*
605  * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
606  */
607 int send_run_before_and_after_commands(JCR *jcr)
608 {
609    POOLMEM *msg = get_pool_memory(PM_FNAME);
610    BSOCK *fd = jcr->file_bsock;
611    if (jcr->job->ClientRunBeforeJob) {
612       pm_strcpy(msg, jcr->job->ClientRunBeforeJob);
613       bash_spaces(msg);
614       bnet_fsend(fd, runbefore, msg);
615       if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
616          set_jcr_job_status(jcr, JS_ErrorTerminated);
617          free_pool_memory(msg);
618          return 0;
619       }
620    }
621    if (jcr->job->ClientRunAfterJob) {
622       fd->msglen = pm_strcpy(msg, jcr->job->ClientRunAfterJob);
623       bash_spaces(msg);
624       bnet_fsend(fd, runafter, msg);
625       if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
626          set_jcr_job_status(jcr, JS_ErrorTerminated);
627          free_pool_memory(msg);
628          return 0;
629       }
630    }
631    free_pool_memory(msg);
632    return 1;
633 }
634
635
636 /*
637  * Read the attributes from the File daemon for
638  * a Verify job and store them in the catalog.
639  */
640 int get_attributes_and_put_in_catalog(JCR *jcr)
641 {
642    BSOCK   *fd;
643    int n = 0;
644    ATTR_DBR ar;
645
646    fd = jcr->file_bsock;
647    jcr->jr.FirstIndex = 1;
648    memset(&ar, 0, sizeof(ar));
649    jcr->FileIndex = 0;
650
651    Dmsg0(120, "bdird: waiting to receive file attributes\n");
652    /* Pickup file attributes and signature */
653    while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
654
655    /*****FIXME****** improve error handling to stop only on
656     * really fatal problems, or the number of errors is too
657     * large.
658     */
659       long file_index;
660       int stream, len;
661       char *attr, *p, *fn;
662       char Opts_SIG[MAXSTRING];      /* either Verify opts or MD5/SHA1 signature */
663       char SIG[MAXSTRING];
664
665       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
666       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
667          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
668 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
669          set_jcr_job_status(jcr, JS_ErrorTerminated);
670          return 0;
671       }
672       p = fd->msg;
673       skip_nonspaces(&p);             /* skip FileIndex */
674       skip_spaces(&p);
675       skip_nonspaces(&p);             /* skip Stream */
676       skip_spaces(&p);
677       skip_nonspaces(&p);             /* skip Opts_SHA1 */
678       p++;                            /* skip space */
679       fn = jcr->fname;
680       while (*p != 0) {
681          *fn++ = *p++;                /* copy filename */
682       }
683       *fn = *p++;                     /* term filename and point to attribs */
684       attr = p;
685
686       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
687          jcr->JobFiles++;
688          jcr->FileIndex = file_index;
689          ar.attr = attr;
690          ar.fname = jcr->fname;
691          ar.FileIndex = file_index;
692          ar.Stream = stream;
693          ar.link = NULL;
694          ar.JobId = jcr->JobId;
695          ar.ClientId = jcr->ClientId;
696          ar.PathId = 0;
697          ar.FilenameId = 0;
698
699          Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
700          Dmsg1(120, "dird<filed: attr=%s\n", attr);
701
702          if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
703             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
704             set_jcr_job_status(jcr, JS_Error);
705             continue;
706          }
707          jcr->FileId = ar.FileId;
708       } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
709          if (jcr->FileIndex != (uint32_t)file_index) {
710             Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
711                file_index, jcr->FileIndex);
712             set_jcr_job_status(jcr, JS_Error);
713             continue;
714          }
715          db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
716          Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
717          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
718                    stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
719             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
720             set_jcr_job_status(jcr, JS_Error);
721          }
722       }
723       jcr->jr.JobFiles = jcr->JobFiles = file_index;
724       jcr->jr.LastIndex = file_index;
725    }
726    if (is_bnet_error(fd)) {
727       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
728                         bnet_strerror(fd));
729       set_jcr_job_status(jcr, JS_ErrorTerminated);
730       return 0;
731    }
732
733    set_jcr_job_status(jcr, JS_Terminated);
734    return 1;
735 }