]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/fd_cmds.c
- Add back JobId index for MySQL as default -- speeds up
[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    /* Lookup the last FULL backup job to get the time/date for a
149     * differential or incremental save.
150     */
151    if (!jcr->stime) {
152       jcr->stime = get_pool_memory(PM_MESSAGE);
153    }
154    jcr->stime[0] = 0;
155    since[0] = 0;
156    switch (jcr->JobLevel) {
157    case L_DIFFERENTIAL:
158    case L_INCREMENTAL:
159       /* Look up start time of last job */
160       jcr->jr.JobId = 0;     /* flag for db_find_job_start time */
161       if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
162          /* No job found, so upgrade this one to Full */
163          Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
164          Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found. Doing FULL backup.\n"));
165          bsnprintf(since, since_len, " (upgraded from %s)",
166             level_to_str(jcr->JobLevel));
167          jcr->JobLevel = jcr->jr.JobLevel = L_FULL;
168       } else {
169          if (jcr->job->rerun_failed_levels) {
170             if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
171                Jmsg(jcr, M_INFO, 0, _("Prior failed job found. Upgrading to %s.\n"),
172                   level_to_str(JobLevel));
173                bsnprintf(since, since_len, " (upgraded from %s)",
174                   level_to_str(jcr->JobLevel));
175                jcr->JobLevel = jcr->jr.JobLevel = JobLevel;
176                jcr->jr.JobId = jcr->JobId;
177                break;
178             }
179          }
180          bstrncpy(since, ", since=", since_len);
181          bstrncat(since, jcr->stime, since_len);
182       }
183       jcr->jr.JobId = jcr->JobId;
184       break;
185    }
186    Dmsg2(100, "Level=%c last start time=%s\n", jcr->JobLevel, jcr->stime);
187 }
188
189 static void send_since_time(JCR *jcr)
190 {
191    BSOCK   *fd = jcr->file_bsock;
192    utime_t stime;
193    char ed1[50];
194
195    stime = str_to_utime(jcr->stime);
196    bnet_fsend(fd, levelcmd, "since_utime ", edit_uint64(stime, ed1), 0);
197    while (bget_dirmsg(fd) >= 0) {  /* allow him to poll us to sync clocks */
198       Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
199    }
200 }
201
202
203 /*
204  * Send level command to FD.
205  * Used for backup jobs and estimate command.
206  */
207 int send_level_command(JCR *jcr)
208 {
209    BSOCK   *fd = jcr->file_bsock;
210    /*
211     * Send Level command to File daemon
212     */
213    switch (jcr->JobLevel) {
214    case L_BASE:
215       bnet_fsend(fd, levelcmd, "base", " ", 0);
216       break;
217    /* L_NONE is the console, sending something off to the FD */
218    case L_NONE:
219    case L_FULL:
220       bnet_fsend(fd, levelcmd, "full", " ", 0);
221       break;
222    case L_DIFFERENTIAL:
223       bnet_fsend(fd, levelcmd, "differential", " ", 0);
224       send_since_time(jcr);
225       break;
226    case L_INCREMENTAL:
227       bnet_fsend(fd, levelcmd, "incremental", " ", 0);
228       send_since_time(jcr);
229       break;
230    case L_SINCE:
231    default:
232       Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
233          jcr->JobLevel, jcr->JobLevel);
234       return 0;
235    }
236    Dmsg1(120, ">filed: %s", fd->msg);
237    if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
238       return 0;
239    }
240    return 1;
241 }
242
243
244 /*
245  * Send either an Included or an Excluded list to FD
246  */
247 static int send_list(JCR *jcr, int list)
248 {
249    FILESET *fileset;
250    BSOCK   *fd;
251    int num;
252
253    fd = jcr->file_bsock;
254    fileset = jcr->fileset;
255
256    if (list == INC_LIST) {
257       num = fileset->num_includes;
258    } else {
259       num = fileset->num_excludes;
260    }
261
262    for (int i=0; i<num; i++) {
263       BPIPE *bpipe;
264       FILE *ffd;
265       char buf[2000];
266       char *p;
267       int optlen, stat;
268       INCEXE *ie;
269
270
271       if (list == INC_LIST) {
272          ie = fileset->include_items[i];
273       } else {
274          ie = fileset->exclude_items[i];
275       }
276       for (int j=0; j<ie->name_list.size(); j++) {
277          p = (char *)ie->name_list.get(j);
278          switch (*p) {
279          case '|':
280             p++;                      /* skip over the | */
281             fd->msg = edit_job_codes(jcr, fd->msg, p, "");
282             bpipe = open_bpipe(fd->msg, 0, "r");
283             if (!bpipe) {
284                berrno be;
285                Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
286                   p, be.strerror());
287                goto bail_out;
288             }
289             /* Copy File options */
290             if (ie->num_opts) {
291                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
292                bstrncat(buf, " ", sizeof(buf));
293             } else {
294                bstrncpy(buf, "0 ", sizeof(buf));
295             }
296             Dmsg1(500, "Opts=%s\n", buf);
297             optlen = strlen(buf);
298             while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
299                fd->msglen = Mmsg(fd->msg, "%s", buf);
300                Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
301                if (!bnet_send(fd)) {
302                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
303                   goto bail_out;
304                }
305             }
306             if ((stat=close_bpipe(bpipe)) != 0) {
307                berrno be;
308                Jmsg(jcr, M_FATAL, 0, _("Error running program %p: ERR=%s\n"),
309                   p, be.strerror(stat));
310                goto bail_out;
311             }
312             break;
313          case '<':
314             p++;                      /* skip over < */
315             if ((ffd = fopen(p, "r")) == NULL) {
316                Jmsg(jcr, M_FATAL, 0, _("Cannot open %s file: %s. ERR=%s\n"),
317                   list==INC_LIST?"included":"excluded", p, strerror(errno));
318                goto bail_out;
319             }
320             /* Copy File options */
321             if (ie->num_opts) {
322                bstrncpy(buf, ie->opts_list[0]->opts, sizeof(buf));
323                bstrncat(buf, " ", sizeof(buf));
324             } else {
325                bstrncpy(buf, "0 ", sizeof(buf));
326             }
327             Dmsg1(500, "Opts=%s\n", buf);
328             optlen = strlen(buf);
329             while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
330                fd->msglen = Mmsg(fd->msg, "%s", buf);
331                if (!bnet_send(fd)) {
332                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
333                   goto bail_out;
334                }
335             }
336             fclose(ffd);
337             break;
338          case '\\':
339             p++;                      /* skip over \ */
340             /* Note, fall through wanted */
341          default:
342             if (ie->num_opts) {
343                Dmsg2(500, "numopts=%d opts=%s\n", ie->num_opts, NPRT(ie->opts_list[0]->opts));
344                pm_strcpy(fd->msg, ie->opts_list[0]->opts);
345                pm_strcat(fd->msg, " ");
346             } else {
347                pm_strcpy(fd->msg, "0 ");
348             }
349             fd->msglen = pm_strcat(fd->msg, p);
350             Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
351             if (!bnet_send(fd)) {
352                Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
353                goto bail_out;
354             }
355             break;
356          }
357       }
358    }
359    bnet_sig(fd, BNET_EOD);            /* end of data */
360    if (list == INC_LIST) {
361       if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
362          goto bail_out;
363       }
364    } else if (!response(jcr, fd, OKexc, "Exclude", DISPLAY_ERROR)) {
365         goto bail_out;
366    }
367    return 1;
368
369 bail_out:
370    set_jcr_job_status(jcr, JS_ErrorTerminated);
371    return 0;
372
373 }
374
375
376 /*
377  * Send either an Included or an Excluded list to FD
378  */
379 static int send_fileset(JCR *jcr)
380 {
381    FILESET *fileset = jcr->fileset;
382    BSOCK   *fd = jcr->file_bsock;
383    int num;
384    bool include = true;
385
386    for ( ;; ) {
387       if (include) {
388          num = fileset->num_includes;
389       } else {
390          num = fileset->num_excludes;
391       }
392       for (int i=0; i<num; i++) {
393          BPIPE *bpipe;
394          FILE *ffd;
395          char buf[2000];
396          char *p;
397          int optlen, stat;
398          INCEXE *ie;
399          int j, k;
400
401          if (include) {
402             ie = fileset->include_items[i];
403             bnet_fsend(fd, "I\n");
404          } else {
405             ie = fileset->exclude_items[i];
406             bnet_fsend(fd, "E\n");
407          }
408          for (j=0; j<ie->num_opts; j++) {
409             FOPTS *fo = ie->opts_list[j];
410             bnet_fsend(fd, "O %s\n", fo->opts);
411             for (k=0; k<fo->regex.size(); k++) {
412                bnet_fsend(fd, "R %s\n", fo->regex.get(k));
413             }
414             for (k=0; k<fo->regexdir.size(); k++) {
415                bnet_fsend(fd, "RD %s\n", fo->regexdir.get(k));
416             }
417             for (k=0; k<fo->regexfile.size(); k++) {
418                bnet_fsend(fd, "RF %s\n", fo->regexfile.get(k));
419             }
420             for (k=0; k<fo->wild.size(); k++) {
421                bnet_fsend(fd, "W %s\n", fo->wild.get(k));
422             }
423             for (k=0; k<fo->wilddir.size(); k++) {
424                bnet_fsend(fd, "WD %s\n", fo->wilddir.get(k));
425             }
426             for (k=0; k<fo->wildfile.size(); k++) {
427                bnet_fsend(fd, "WF %s\n", fo->wildfile.get(k));
428             }
429             for (k=0; k<fo->base.size(); k++) {
430                bnet_fsend(fd, "B %s\n", fo->base.get(k));
431             }
432             for (k=0; k<fo->fstype.size(); k++) {
433                bnet_fsend(fd, "X %s\n", fo->fstype.get(k));
434             }
435             if (fo->reader) {
436                bnet_fsend(fd, "D %s\n", fo->reader);
437             }
438             if (fo->writer) {
439                bnet_fsend(fd, "T %s\n", fo->writer);
440             }
441             bnet_fsend(fd, "N\n");
442          }
443
444          for (j=0; j<ie->name_list.size(); j++) {
445             p = (char *)ie->name_list.get(j);
446             switch (*p) {
447             case '|':
448                p++;                      /* skip over the | */
449                fd->msg = edit_job_codes(jcr, fd->msg, p, "");
450                bpipe = open_bpipe(fd->msg, 0, "r");
451                if (!bpipe) {
452                   berrno be;
453                   Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
454                      p, be.strerror());
455                   goto bail_out;
456                }
457                bstrncpy(buf, "F ", sizeof(buf));
458                Dmsg1(500, "Opts=%s\n", buf);
459                optlen = strlen(buf);
460                while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
461                   fd->msglen = Mmsg(fd->msg, "%s", buf);
462                   Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
463                   if (!bnet_send(fd)) {
464                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
465                      goto bail_out;
466                   }
467                }
468                if ((stat=close_bpipe(bpipe)) != 0) {
469                   berrno be;
470                   Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
471                      p, be.strerror(stat));
472                   goto bail_out;
473                }
474                break;
475             case '<':
476                p++;                      /* skip over < */
477                if ((ffd = fopen(p, "r")) == NULL) {
478                   berrno be;
479                   Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
480                      p, be.strerror());
481                   goto bail_out;
482                }
483                bstrncpy(buf, "F ", sizeof(buf));
484                Dmsg1(500, "Opts=%s\n", buf);
485                optlen = strlen(buf);
486                while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
487                   fd->msglen = Mmsg(fd->msg, "%s", buf);
488                   if (!bnet_send(fd)) {
489                      Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
490                      goto bail_out;
491                   }
492                }
493                fclose(ffd);
494                break;
495             case '\\':
496                p++;                      /* skip over \ */
497                /* Note, fall through wanted */
498             default:
499                pm_strcpy(fd->msg, "F ");
500                fd->msglen = pm_strcat(fd->msg, p);
501                Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
502                if (!bnet_send(fd)) {
503                   Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
504                   goto bail_out;
505                }
506                break;
507             }
508          }
509          bnet_fsend(fd, "N\n");
510       }
511       if (!include) {                 /* If we just did excludes */
512          break;                       /*   all done */
513       }
514       include = false;                /* Now do excludes */
515    }
516
517    bnet_sig(fd, BNET_EOD);            /* end of data */
518    if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
519       goto bail_out;
520    }
521    return 1;
522
523 bail_out:
524    set_jcr_job_status(jcr, JS_ErrorTerminated);
525    return 0;
526
527 }
528
529
530 /*
531  * Send include list to File daemon
532  */
533 int send_include_list(JCR *jcr)
534 {
535    BSOCK *fd = jcr->file_bsock;
536    if (jcr->fileset->new_include) {
537       bnet_fsend(fd, fileset);
538       return send_fileset(jcr);
539    } else {
540       bnet_fsend(fd, inc);
541    }
542    return send_list(jcr, INC_LIST);
543 }
544
545
546 /*
547  * Send exclude list to File daemon
548  */
549 int send_exclude_list(JCR *jcr)
550 {
551    BSOCK *fd = jcr->file_bsock;
552    if (jcr->fileset->new_include) {
553       return 1;
554    }
555    bnet_fsend(fd, exc);
556    return send_list(jcr, EXC_LIST);
557 }
558
559
560 /*
561  * Send bootstrap file if any to the File daemon.
562  *  This is used for restore and verify VolumeToCatalog
563  */
564 int send_bootstrap_file(JCR *jcr)
565 {
566    FILE *bs;
567    char buf[1000];
568    BSOCK *fd = jcr->file_bsock;
569    const char *bootstrap = "bootstrap\n";
570
571    Dmsg1(400, "send_bootstrap_file: %s\n", jcr->RestoreBootstrap);
572    if (!jcr->RestoreBootstrap) {
573       return 1;
574    }
575    bs = fopen(jcr->RestoreBootstrap, "r");
576    if (!bs) {
577       berrno be;
578       Jmsg(jcr, M_FATAL, 0, _("Could not open bootstrap file %s: ERR=%s\n"),
579          jcr->RestoreBootstrap, be.strerror());
580       set_jcr_job_status(jcr, JS_ErrorTerminated);
581       return 0;
582    }
583    bnet_fsend(fd, bootstrap);
584    while (fgets(buf, sizeof(buf), bs)) {
585       bnet_fsend(fd, "%s", buf);
586    }
587    bnet_sig(fd, BNET_EOD);
588    fclose(bs);
589    if (!response(jcr, fd, OKbootstrap, "Bootstrap", DISPLAY_ERROR)) {
590       set_jcr_job_status(jcr, JS_ErrorTerminated);
591       return 0;
592    }
593    return 1;
594 }
595
596 /*
597  * Send ClientRunBeforeJob and ClientRunAfterJob to File daemon
598  */
599 int send_run_before_and_after_commands(JCR *jcr)
600 {
601    POOLMEM *msg = get_pool_memory(PM_FNAME);
602    BSOCK *fd = jcr->file_bsock;
603    if (jcr->job->ClientRunBeforeJob) {
604       pm_strcpy(msg, jcr->job->ClientRunBeforeJob);
605       bash_spaces(msg);
606       bnet_fsend(fd, runbefore, msg);
607       if (!response(jcr, fd, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR)) {
608          set_jcr_job_status(jcr, JS_ErrorTerminated);
609          free_pool_memory(msg);
610          return 0;
611       }
612    }
613    if (jcr->job->ClientRunAfterJob) {
614       fd->msglen = pm_strcpy(msg, jcr->job->ClientRunAfterJob);
615       bash_spaces(msg);
616       bnet_fsend(fd, runafter, msg);
617       if (!response(jcr, fd, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR)) {
618          set_jcr_job_status(jcr, JS_ErrorTerminated);
619          free_pool_memory(msg);
620          return 0;
621       }
622    }
623    free_pool_memory(msg);
624    return 1;
625 }
626
627
628 /*
629  * Read the attributes from the File daemon for
630  * a Verify job and store them in the catalog.
631  */
632 int get_attributes_and_put_in_catalog(JCR *jcr)
633 {
634    BSOCK   *fd;
635    int n = 0;
636    ATTR_DBR ar;
637
638    fd = jcr->file_bsock;
639    jcr->jr.FirstIndex = 1;
640    memset(&ar, 0, sizeof(ar));
641    jcr->FileIndex = 0;
642
643    Dmsg0(120, "bdird: waiting to receive file attributes\n");
644    /* Pickup file attributes and signature */
645    while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
646
647    /*****FIXME****** improve error handling to stop only on
648     * really fatal problems, or the number of errors is too
649     * large.
650     */
651       long file_index;
652       int stream, len;
653       char *attr, *p, *fn;
654       char Opts_SIG[MAXSTRING];      /* either Verify opts or MD5/SHA1 signature */
655       char SIG[MAXSTRING];
656
657       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
658       if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Opts_SIG)) != 3) {
659          Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
660 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
661          set_jcr_job_status(jcr, JS_ErrorTerminated);
662          return 0;
663       }
664       p = fd->msg;
665       skip_nonspaces(&p);             /* skip FileIndex */
666       skip_spaces(&p);
667       skip_nonspaces(&p);             /* skip Stream */
668       skip_spaces(&p);
669       skip_nonspaces(&p);             /* skip Opts_SHA1 */
670       p++;                            /* skip space */
671       fn = jcr->fname;
672       while (*p != 0) {
673          *fn++ = *p++;                /* copy filename */
674       }
675       *fn = *p++;                     /* term filename and point to attribs */
676       attr = p;
677
678       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
679          jcr->JobFiles++;
680          jcr->FileIndex = file_index;
681          ar.attr = attr;
682          ar.fname = jcr->fname;
683          ar.FileIndex = file_index;
684          ar.Stream = stream;
685          ar.link = NULL;
686          ar.JobId = jcr->JobId;
687          ar.ClientId = jcr->ClientId;
688          ar.PathId = 0;
689          ar.FilenameId = 0;
690
691          Dmsg2(111, "dird<filed: stream=%d %s\n", stream, jcr->fname);
692          Dmsg1(120, "dird<filed: attr=%s\n", attr);
693
694          if (!db_create_file_attributes_record(jcr, jcr->db, &ar)) {
695             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
696             set_jcr_job_status(jcr, JS_Error);
697             continue;
698          }
699          jcr->FileId = ar.FileId;
700       } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
701          if (jcr->FileIndex != (uint32_t)file_index) {
702             Jmsg2(jcr, M_ERROR, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
703                file_index, jcr->FileIndex);
704             set_jcr_job_status(jcr, JS_Error);
705             continue;
706          }
707          db_escape_string(SIG, Opts_SIG, strlen(Opts_SIG));
708          Dmsg2(120, "SIGlen=%d SIG=%s\n", strlen(SIG), SIG);
709          if (!db_add_SIG_to_file_record(jcr, jcr->db, jcr->FileId, SIG,
710                    stream==STREAM_MD5_SIGNATURE?MD5_SIG:SHA1_SIG)) {
711             Jmsg1(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
712             set_jcr_job_status(jcr, JS_Error);
713          }
714       }
715       jcr->jr.JobFiles = jcr->JobFiles = file_index;
716       jcr->jr.LastIndex = file_index;
717    }
718    if (is_bnet_error(fd)) {
719       Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
720                         bnet_strerror(fd));
721       set_jcr_job_status(jcr, JS_ErrorTerminated);
722       return 0;
723    }
724
725    set_jcr_job_status(jcr, JS_Terminated);
726    return 1;
727 }