2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2010 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director -- fd_cmds.c -- send commands to File daemon
32 * Kern Sibbald, October MM
34 * This routine is run as a separate thread. There may be more
35 * work to be done to make it totally reentrant!!!!
37 * Utility functions for sending info to File Daemon.
38 * These functions are used by both backup and verify.
44 #include "findlib/find.h"
46 const int dbglvl = 400;
48 /* Commands sent to File daemon */
49 static char filesetcmd[] = "fileset%s\n"; /* set full fileset */
50 static char jobcmd[] = "JobId=%s Job=%s SDid=%u SDtime=%u Authorization=%s\n";
51 /* Note, mtime_only is not used here -- implemented as file option */
52 static char levelcmd[] = "level = %s%s%s mtime_only=%d\n";
53 static char runscript[] = "Run OnSuccess=%u OnFailure=%u AbortOnError=%u When=%u Command=%s\n";
54 static char runbeforenow[]= "RunBeforeNow\n";
55 static char bandwidthcmd[] = "setbandwidth=%lld Job=%s\n";
57 /* Responses received from File daemon */
58 static char OKinc[] = "2000 OK include\n";
59 static char OKjob[] = "2000 OK Job";
60 static char OKlevel[] = "2000 OK level\n";
61 static char OKRunScript[] = "2000 OK RunScript\n";
62 static char OKRunBeforeNow[] = "2000 OK RunBeforeNow\n";
63 static char OKRestoreObject[] = "2000 OK ObjectRestored\n";
64 static char OKBandwidth[] = "2000 OK Bandwidth\n";
66 /* Forward referenced functions */
67 static bool send_list_item(JCR *jcr, const char *code, char *item, BSOCK *fd);
69 /* External functions */
70 extern DIRRES *director;
71 extern int FDConnectTimeout;
77 * Open connection with File daemon.
78 * Try connecting every retry_interval (default 10 sec), and
79 * give up after max_retry_time (default 30 mins).
82 int connect_to_file_daemon(JCR *jcr, int retry_interval, int max_retry_time,
85 BSOCK *fd = new_bsock();
89 if (jcr->client->heartbeat_interval) {
90 heart_beat = jcr->client->heartbeat_interval;
92 heart_beat = director->heartbeat_interval;
95 if (!jcr->file_bsock) {
96 char name[MAX_NAME_LENGTH + 100];
97 bstrncpy(name, _("Client: "), sizeof(name));
98 bstrncat(name, jcr->client->name(), sizeof(name));
100 fd->set_source_address(director->DIRsrc_addr);
101 if (!fd->connect(jcr,retry_interval,max_retry_time, heart_beat, name, jcr->client->address,
102 NULL, jcr->client->FDport, verbose)) {
108 set_jcr_job_status(jcr, JS_ErrorTerminated);
111 Dmsg0(10, "Opened connection with File daemon\n");
113 fd = jcr->file_bsock; /* use existing connection */
115 fd->res = (RES *)jcr->client; /* save resource in BSOCK */
116 jcr->file_bsock = fd;
117 set_jcr_job_status(jcr, JS_Running);
119 if (!authenticate_file_daemon(jcr)) {
120 set_jcr_job_status(jcr, JS_ErrorTerminated);
125 * Now send JobId and authorization key
127 if (jcr->sd_auth_key == NULL) {
128 jcr->sd_auth_key = bstrdup("dummy");
130 fd->fsend(jobcmd, edit_int64(jcr->JobId, ed1), jcr->Job, jcr->VolSessionId,
131 jcr->VolSessionTime, jcr->sd_auth_key);
132 if (!jcr->keep_sd_auth_key && strcmp(jcr->sd_auth_key, "dummy")) {
133 memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
135 Dmsg1(100, ">filed: %s", fd->msg);
136 if (bget_dirmsg(fd) > 0) {
137 Dmsg1(110, "<filed: %s", fd->msg);
138 if (strncmp(fd->msg, OKjob, strlen(OKjob)) != 0) {
139 Jmsg(jcr, M_FATAL, 0, _("File daemon \"%s\" rejected Job command: %s\n"),
140 jcr->client->hdr.name, fd->msg);
141 set_jcr_job_status(jcr, JS_ErrorTerminated);
143 } else if (jcr->db) {
145 memset(&cr, 0, sizeof(cr));
146 bstrncpy(cr.Name, jcr->client->hdr.name, sizeof(cr.Name));
147 cr.AutoPrune = jcr->client->AutoPrune;
148 cr.FileRetention = jcr->client->FileRetention;
149 cr.JobRetention = jcr->client->JobRetention;
150 bstrncpy(cr.Uname, fd->msg+strlen(OKjob)+1, sizeof(cr.Uname));
151 if (!db_update_client_record(jcr, jcr->db, &cr)) {
152 Jmsg(jcr, M_WARNING, 0, _("Error updating Client record. ERR=%s\n"),
153 db_strerror(jcr->db));
157 Jmsg(jcr, M_FATAL, 0, _("FD gave bad response to JobId command: %s\n"),
159 set_jcr_job_status(jcr, JS_ErrorTerminated);
166 * This subroutine edits the last job start time into a
167 * "since=date/time" buffer that is returned in the
168 * variable since. This is used for display purposes in
169 * the job report. The time in jcr->stime is later
170 * passed to tell the File daemon what to do.
172 void get_level_since_time(JCR *jcr, char *since, int since_len)
176 bool do_full = false;
177 bool do_diff = false;
179 utime_t last_full_time = 0;
180 utime_t last_diff_time;
183 /* If job cloned and a since time already given, use it */
184 if (jcr->cloned && jcr->stime && jcr->stime[0]) {
185 bstrncpy(since, _(", since="), since_len);
186 bstrncat(since, jcr->stime, since_len);
189 /* Make sure stime buffer is allocated */
191 jcr->stime = get_pool_memory(PM_MESSAGE);
195 * Lookup the last FULL backup job to get the time/date for a
196 * differential or incremental save.
198 switch (jcr->getJobLevel()) {
201 POOLMEM *stime = get_pool_memory(PM_MESSAGE);
202 /* Look up start time of last Full job */
203 now = (utime_t)time(NULL);
204 jcr->jr.JobId = 0; /* flag to return since time */
206 * This is probably redundant, but some of the code below
207 * uses jcr->stime, so don't remove unless you are sure.
209 if (!db_find_job_start_time(jcr, jcr->db, &jcr->jr, &jcr->stime)) {
212 have_full = db_find_last_job_start_time(jcr, jcr->db, &jcr->jr, &stime, L_FULL);
214 last_full_time = str_to_utime(stime);
216 do_full = true; /* No full, upgrade to one */
218 Dmsg4(50, "have_full=%d do_full=%d now=%lld full_time=%lld\n", have_full,
219 do_full, now, last_full_time);
220 /* Make sure the last diff is recent enough */
221 if (have_full && jcr->getJobLevel() == L_INCREMENTAL && jcr->job->MaxDiffInterval > 0) {
222 /* Lookup last diff job */
223 if (db_find_last_job_start_time(jcr, jcr->db, &jcr->jr, &stime, L_DIFFERENTIAL)) {
224 last_diff_time = str_to_utime(stime);
225 /* If no Diff since Full, use Full time */
226 if (last_diff_time < last_full_time) {
227 last_diff_time = last_full_time;
229 Dmsg2(50, "last_diff_time=%lld last_full_time=%lld\n", last_diff_time,
232 /* No last differential, so use last full time */
233 last_diff_time = last_full_time;
234 Dmsg1(50, "No last_diff_time setting to full_time=%lld\n", last_full_time);
236 do_diff = ((now - last_diff_time) >= jcr->job->MaxDiffInterval);
237 Dmsg2(50, "do_diff=%d diffInter=%lld\n", do_diff, jcr->job->MaxDiffInterval);
239 /* Note, do_full takes precedence over do_diff */
240 if (have_full && jcr->job->MaxFullInterval > 0) {
241 do_full = ((now - last_full_time) >= jcr->job->MaxFullInterval);
243 free_pool_memory(stime);
246 /* No recent Full job found, so upgrade this one to Full */
247 Jmsg(jcr, M_INFO, 0, "%s", db_strerror(jcr->db));
248 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Full backup found in catalog. Doing FULL backup.\n"));
249 bsnprintf(since, since_len, _(" (upgraded from %s)"),
250 level_to_str(jcr->getJobLevel()));
251 jcr->set_JobLevel(jcr->jr.JobLevel = L_FULL);
252 } else if (do_diff) {
253 /* No recent diff job found, so upgrade this one to Diff */
254 Jmsg(jcr, M_INFO, 0, _("No prior or suitable Differential backup found in catalog. Doing Differential backup.\n"));
255 bsnprintf(since, since_len, _(" (upgraded from %s)"),
256 level_to_str(jcr->getJobLevel()));
257 jcr->set_JobLevel(jcr->jr.JobLevel = L_DIFFERENTIAL);
259 if (jcr->job->rerun_failed_levels) {
260 if (db_find_failed_job_since(jcr, jcr->db, &jcr->jr, jcr->stime, JobLevel)) {
261 Jmsg(jcr, M_INFO, 0, _("Prior failed job found in catalog. Upgrading to %s.\n"),
262 level_to_str(JobLevel));
263 bsnprintf(since, since_len, _(" (upgraded from %s)"),
264 level_to_str(jcr->getJobLevel()));
265 jcr->set_JobLevel(jcr->jr.JobLevel = JobLevel);
266 jcr->jr.JobId = jcr->JobId;
270 bstrncpy(since, _(", since="), since_len);
271 bstrncat(since, jcr->stime, since_len);
273 jcr->jr.JobId = jcr->JobId;
276 Dmsg2(100, "Level=%c last start time=%s\n", jcr->getJobLevel(), jcr->stime);
279 static void send_since_time(JCR *jcr)
281 BSOCK *fd = jcr->file_bsock;
285 stime = str_to_utime(jcr->stime);
286 fd->fsend(levelcmd, "", NT_("since_utime "), edit_uint64(stime, ed1), 0);
287 while (bget_dirmsg(fd) >= 0) { /* allow him to poll us to sync clocks */
288 Jmsg(jcr, M_INFO, 0, "%s\n", fd->msg);
292 bool send_bwlimit(JCR *jcr, const char *Job)
294 BSOCK *fd = jcr->file_bsock;
295 if (jcr->FDVersion >= 4) {
296 fd->fsend(bandwidthcmd, jcr->max_bandwidth, Job);
297 if (!response(jcr, fd, OKBandwidth, "Bandwidth", DISPLAY_ERROR)) {
298 jcr->max_bandwidth = 0; /* can't set bandwidth limit */
306 * Send level command to FD.
307 * Used for backup jobs and estimate command.
309 bool send_level_command(JCR *jcr)
311 BSOCK *fd = jcr->file_bsock;
312 const char *accurate = jcr->accurate?"accurate_":"";
313 const char *not_accurate = "";
314 const char *incomplete = jcr->incomplete?" incomplete ":" ";
316 * Send Level command to File daemon
318 switch (jcr->getJobLevel()) {
320 fd->fsend(levelcmd, not_accurate, "base", incomplete, 0);
322 /* L_NONE is the console, sending something off to the FD */
325 fd->fsend(levelcmd, not_accurate, "full", incomplete, 0);
328 fd->fsend(levelcmd, accurate, "differential", incomplete, 0);
329 send_since_time(jcr);
332 fd->fsend(levelcmd, accurate, "incremental", incomplete, 0);
333 send_since_time(jcr);
337 Jmsg2(jcr, M_FATAL, 0, _("Unimplemented backup level %d %c\n"),
338 jcr->getJobLevel(), jcr->getJobLevel());
341 Dmsg1(120, ">filed: %s", fd->msg);
342 if (!response(jcr, fd, OKlevel, "Level", DISPLAY_ERROR)) {
349 * Send either an Included or an Excluded list to FD
351 static bool send_fileset(JCR *jcr)
353 FILESET *fileset = jcr->fileset;
354 BSOCK *fd = jcr->file_bsock;
355 STORE *store = jcr->wstore;
361 num = fileset->num_includes;
363 num = fileset->num_excludes;
365 for (int i=0; i<num; i++) {
371 ie = fileset->include_items[i];
374 ie = fileset->exclude_items[i];
378 bnet_fsend(fd, "Z %s\n", ie->ignoredir);
380 for (j=0; j<ie->num_opts; j++) {
381 FOPTS *fo = ie->opts_list[j];
383 bool enhanced_wild = false;
384 for (k=0; fo->opts[k]!='\0'; k++) {
385 if (fo->opts[k]=='W') {
386 enhanced_wild = true;
391 /* Strip out compression option Zn if disallowed for this Storage */
392 if (store && !store->AllowCompress) {
393 char newopts[MAX_FOPTS];
394 bool done=false; /* print warning only if compression enabled in FS */
396 for (k=0; fo->opts[k]!='\0'; k++) {
397 /* Z compress option is followed by the single-digit compress level */
398 if (fo->opts[k]=='Z') {
400 k++; /* skip option and level */
402 newopts[j] = fo->opts[k];
410 _("FD compression disabled for this Job because AllowCompress=No in Storage resource.\n") );
412 /* Send the new trimmed option set without overwriting fo->opts */
413 fd->fsend("O %s\n", newopts);
415 /* Send the original options */
416 fd->fsend("O %s\n", fo->opts);
419 for (k=0; k<fo->regex.size(); k++) {
420 fd->fsend("R %s\n", fo->regex.get(k));
422 for (k=0; k<fo->regexdir.size(); k++) {
423 fd->fsend("RD %s\n", fo->regexdir.get(k));
425 for (k=0; k<fo->regexfile.size(); k++) {
426 fd->fsend("RF %s\n", fo->regexfile.get(k));
428 for (k=0; k<fo->wild.size(); k++) {
429 fd->fsend("W %s\n", fo->wild.get(k));
431 for (k=0; k<fo->wilddir.size(); k++) {
432 fd->fsend("WD %s\n", fo->wilddir.get(k));
434 for (k=0; k<fo->wildfile.size(); k++) {
435 fd->fsend("WF %s\n", fo->wildfile.get(k));
437 for (k=0; k<fo->wildbase.size(); k++) {
438 fd->fsend("W%c %s\n", enhanced_wild ? 'B' : 'F', fo->wildbase.get(k));
440 for (k=0; k<fo->base.size(); k++) {
441 fd->fsend("B %s\n", fo->base.get(k));
443 for (k=0; k<fo->fstype.size(); k++) {
444 fd->fsend("X %s\n", fo->fstype.get(k));
446 for (k=0; k<fo->drivetype.size(); k++) {
447 fd->fsend("XD %s\n", fo->drivetype.get(k));
450 fd->fsend("G %s\n", fo->plugin);
453 fd->fsend("D %s\n", fo->reader);
456 fd->fsend("T %s\n", fo->writer);
461 for (j=0; j<ie->name_list.size(); j++) {
462 item = (char *)ie->name_list.get(j);
463 if (!send_list_item(jcr, "F ", item, fd)) {
468 for (j=0; j<ie->plugin_list.size(); j++) {
469 item = (char *)ie->plugin_list.get(j);
470 if (!send_list_item(jcr, "P ", item, fd)) {
476 if (!include) { /* If we just did excludes */
477 break; /* all done */
479 include = false; /* Now do excludes */
482 fd->signal(BNET_EOD); /* end of data */
483 if (!response(jcr, fd, OKinc, "Include", DISPLAY_ERROR)) {
489 set_jcr_job_status(jcr, JS_ErrorTerminated);
494 static bool send_list_item(JCR *jcr, const char *code, char *item, BSOCK *fd)
504 p++; /* skip over the | */
505 fd->msg = edit_job_codes(jcr, fd->msg, p, "");
506 bpipe = open_bpipe(fd->msg, 0, "r");
509 Jmsg(jcr, M_FATAL, 0, _("Cannot run program: %s. ERR=%s\n"),
513 bstrncpy(buf, code, sizeof(buf));
514 Dmsg1(500, "code=%s\n", buf);
515 optlen = strlen(buf);
516 while (fgets(buf+optlen, sizeof(buf)-optlen, bpipe->rfd)) {
517 fd->msglen = Mmsg(fd->msg, "%s", buf);
518 Dmsg2(500, "Inc/exc len=%d: %s", fd->msglen, fd->msg);
519 if (!bnet_send(fd)) {
520 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
524 if ((stat=close_bpipe(bpipe)) != 0) {
526 Jmsg(jcr, M_FATAL, 0, _("Error running program: %s. ERR=%s\n"),
527 p, be.bstrerror(stat));
532 p++; /* skip over < */
533 if ((ffd = fopen(p, "rb")) == NULL) {
535 Jmsg(jcr, M_FATAL, 0, _("Cannot open included file: %s. ERR=%s\n"),
539 bstrncpy(buf, code, sizeof(buf));
540 Dmsg1(500, "code=%s\n", buf);
541 optlen = strlen(buf);
542 while (fgets(buf+optlen, sizeof(buf)-optlen, ffd)) {
543 fd->msglen = Mmsg(fd->msg, "%s", buf);
544 if (!bnet_send(fd)) {
545 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
552 p++; /* skip over \ */
553 /* Note, fall through wanted */
555 pm_strcpy(fd->msg, code);
556 fd->msglen = pm_strcat(fd->msg, p);
557 Dmsg1(500, "Inc/Exc name=%s\n", fd->msg);
559 Jmsg(jcr, M_FATAL, 0, _(">filed: write error on socket\n"));
569 * Send include list to File daemon
571 bool send_include_list(JCR *jcr)
573 BSOCK *fd = jcr->file_bsock;
574 if (jcr->fileset->new_include) {
575 fd->fsend(filesetcmd, jcr->fileset->enable_vss ? " vss=1" : "");
576 return send_fileset(jcr);
583 * Send exclude list to File daemon
584 * Under the new scheme, the Exclude list
585 * is part of the FileSet sent with the
586 * "include_list" above.
588 bool send_exclude_list(JCR *jcr)
593 /* TODO: drop this with runscript.old_proto in bacula 1.42 */
594 static char runbefore[] = "RunBeforeJob %s\n";
595 static char runafter[] = "RunAfterJob %s\n";
596 static char OKRunBefore[] = "2000 OK RunBefore\n";
597 static char OKRunAfter[] = "2000 OK RunAfter\n";
599 int send_runscript_with_old_proto(JCR *jcr, int when, POOLMEM *msg)
602 Dmsg1(120, "bdird: sending old runcommand to fd '%s'\n",msg);
603 if (when & SCRIPT_Before) {
604 bnet_fsend(jcr->file_bsock, runbefore, msg);
605 ret = response(jcr, jcr->file_bsock, OKRunBefore, "ClientRunBeforeJob", DISPLAY_ERROR);
607 bnet_fsend(jcr->file_bsock, runafter, msg);
608 ret = response(jcr, jcr->file_bsock, OKRunAfter, "ClientRunAfterJob", DISPLAY_ERROR);
614 * Send RunScripts to File daemon
615 * 1) We send all runscript to FD, they can be executed Before, After, or twice
616 * 2) Then, we send a "RunBeforeNow" command to the FD to tell him to do the
617 * first run_script() call. (ie ClientRunBeforeJob)
619 int send_runscripts_commands(JCR *jcr)
621 POOLMEM *msg = get_pool_memory(PM_FNAME);
622 BSOCK *fd = jcr->file_bsock;
624 bool launch_before_cmd = false;
625 POOLMEM *ehost = get_pool_memory(PM_FNAME);
628 Dmsg0(120, "bdird: sending runscripts to fd\n");
630 foreach_alist(cmd, jcr->job->RunScripts) {
631 if (cmd->can_run_at_level(jcr->getJobLevel()) && cmd->target) {
632 ehost = edit_job_codes(jcr, ehost, cmd->target, "");
633 Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
635 if (strcmp(ehost, jcr->client->name()) == 0) {
636 pm_strcpy(msg, cmd->command);
639 Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
641 /* TODO: remove this with bacula 1.42 */
642 if (cmd->old_proto) {
643 result = send_runscript_with_old_proto(jcr, cmd->when, msg);
646 fd->fsend(runscript, cmd->on_success,
652 result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
653 launch_before_cmd = true;
660 /* TODO : we have to play with other client */
663 send command to an other client
669 /* Tell the FD to execute the ClientRunBeforeJob */
670 if (launch_before_cmd) {
671 fd->fsend(runbeforenow);
672 if (!response(jcr, fd, OKRunBeforeNow, "RunBeforeNow", DISPLAY_ERROR)) {
676 free_pool_memory(msg);
677 free_pool_memory(ehost);
681 Jmsg(jcr, M_FATAL, 0, _("Client \"%s\" RunScript failed.\n"), ehost);
682 free_pool_memory(msg);
683 free_pool_memory(ehost);
692 static int restore_object_handler(void *ctx, int num_fields, char **row)
694 OBJ_CTX *octx = (OBJ_CTX *)ctx;
695 JCR *jcr = octx->jcr;
698 fd = jcr->file_bsock;
699 if (jcr->is_job_canceled()) {
702 /* Old File Daemon doesn't handle restore objects */
703 if (jcr->FDVersion < 3) {
704 Jmsg(jcr, M_WARNING, 0, _("Client \"%s\" may not be used to restore "
705 "this job. Please upgrade your client.\n"),
706 jcr->client->name());
710 fd->fsend("restoreobject JobId=%s %s,%s,%s,%s,%s,%s\n",
711 row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
713 Dmsg1(010, "Send obj hdr=%s", fd->msg);
715 fd->msglen = pm_strcpy(fd->msg, row[7]);
716 fd->send(); /* send Object name */
718 Dmsg1(010, "Send obj: %s\n", fd->msg);
720 // fd->msglen = str_to_uint64(row[1]); /* object length */
721 // Dmsg1(000, "obj size: %lld\n", (uint64_t)fd->msglen);
724 db_unescape_object(jcr, jcr->db,
726 str_to_uint64(row[1]), /* Object length */
727 &fd->msg, &fd->msglen);
728 fd->send(); /* send object */
732 for (int i=0; i < fd->msglen; i++)
735 Dmsg1(000, "Send obj: %s\n", fd->msg);
741 bool send_restore_objects(JCR *jcr)
743 POOL_MEM query(PM_MESSAGE);
747 if (!jcr->JobIds || !jcr->JobIds[0]) {
752 Mmsg(query, "SELECT JobId,ObjectLength,ObjectFullLength,ObjectIndex,"
753 "ObjectType,ObjectCompression,FileIndex,ObjectName,"
755 "FROM RestoreObject "
756 "WHERE JobId IN (%s) "
757 "ORDER BY ObjectIndex ASC", jcr->JobIds);
759 /* restore_object_handler is called for each file found */
760 db_sql_query(jcr->db, query.c_str(), restore_object_handler, (void *)&octx);
763 * Send to FD only if we have at least one restore object.
764 * This permits backward compatibility with older FDs.
766 if (octx.count > 0) {
767 fd = jcr->file_bsock;
768 fd->fsend("restoreobject end\n");
769 if (!response(jcr, fd, OKRestoreObject, "RestoreObject", DISPLAY_ERROR)) {
770 Jmsg(jcr, M_FATAL, 0, _("RestoreObject failed.\n"));
780 * Read the attributes from the File daemon for
781 * a Verify job and store them in the catalog.
783 int get_attributes_and_put_in_catalog(JCR *jcr)
788 char digest[MAXSTRING];
790 fd = jcr->file_bsock;
791 jcr->jr.FirstIndex = 1;
793 /* Start transaction allocates jcr->attr and jcr->ar if needed */
794 db_start_transaction(jcr, jcr->db); /* start transaction if not already open */
797 Dmsg0(120, "bdird: waiting to receive file attributes\n");
798 /* Pickup file attributes and digest */
799 while (!fd->errors && (n = bget_dirmsg(fd)) > 0) {
803 char Digest[MAXSTRING]; /* either Verify opts or MD5/SHA1 digest */
805 if ((len = sscanf(fd->msg, "%ld %d %s", &file_index, &stream, Digest)) != 3) {
806 Jmsg(jcr, M_FATAL, 0, _("<filed: bad attributes, expected 3 fields got %d\n"
807 "msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
808 set_jcr_job_status(jcr, JS_ErrorTerminated);
812 /* The following three fields were sscanf'ed above so skip them */
813 skip_nonspaces(&p); /* skip FileIndex */
815 skip_nonspaces(&p); /* skip Stream */
817 skip_nonspaces(&p); /* skip Opts_Digest */
818 p++; /* skip space */
819 Dmsg1(dbglvl, "Stream=%d\n", stream);
820 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_UNIX_ATTRIBUTES_EX) {
821 if (jcr->cached_attribute) {
822 Dmsg3(dbglvl, "Cached attr. Stream=%d fname=%s\n", ar->Stream, ar->fname,
824 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
825 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
828 /* Any cached attr is flushed so we can reuse jcr->attr and jcr->ar */
829 fn = jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
831 *fn++ = *p++; /* copy filename */
833 *fn = *p++; /* term filename and point p to attribs */
834 pm_strcpy(jcr->attr, p); /* save attributes */
836 jcr->FileIndex = file_index;
837 ar->attr = jcr->attr;
838 ar->fname = jcr->fname;
839 ar->FileIndex = file_index;
842 ar->JobId = jcr->JobId;
843 ar->ClientId = jcr->ClientId;
847 ar->DigestType = CRYPTO_DIGEST_NONE;
848 jcr->cached_attribute = true;
850 Dmsg2(dbglvl, "dird<filed: stream=%d %s\n", stream, jcr->fname);
851 Dmsg1(dbglvl, "dird<filed: attr=%s\n", ar->attr);
852 jcr->FileId = ar->FileId;
854 * First, get STREAM_UNIX_ATTRIBUTES and fill ATTR_DBR structure
855 * Next, we CAN have a CRYPTO_DIGEST, so we fill ATTR_DBR with it (or not)
856 * When we get a new STREAM_UNIX_ATTRIBUTES, we known that we can add file to the catalog
857 * At the end, we have to add the last file
859 } else if (crypto_digest_stream_type(stream) != CRYPTO_DIGEST_NONE) {
860 if (jcr->FileIndex != (uint32_t)file_index) {
861 Jmsg3(jcr, M_ERROR, 0, _("%s index %d not same as attributes %d\n"),
862 stream_to_ascii(stream), file_index, jcr->FileIndex);
866 ar->DigestType = crypto_digest_stream_type(stream);
867 db_escape_string(jcr, jcr->db, digest, Digest, strlen(Digest));
868 Dmsg4(dbglvl, "stream=%d DigestLen=%d Digest=%s type=%d\n", stream,
869 strlen(digest), digest, ar->DigestType);
871 jcr->jr.JobFiles = jcr->JobFiles = file_index;
872 jcr->jr.LastIndex = file_index;
874 if (is_bnet_error(fd)) {
875 Jmsg1(jcr, M_FATAL, 0, _("<filed: Network error getting attributes. ERR=%s\n"),
879 if (jcr->cached_attribute) {
880 Dmsg3(dbglvl, "Cached attr with digest. Stream=%d fname=%s attr=%s\n", ar->Stream,
881 ar->fname, ar->attr);
882 if (!db_create_file_attributes_record(jcr, jcr->db, ar)) {
883 Jmsg1(jcr, M_FATAL, 0, _("Attribute create error. %s"), db_strerror(jcr->db));
885 jcr->cached_attribute = false;
887 set_jcr_job_status(jcr, JS_Terminated);