3 * Bacula Director -- verify.c -- responsible for running file verification
5 * Kern Sibbald, October MM
7 * This routine is run as a separate thread. There may be more
8 * work to be done to make it totally reentrant!!!!
10 * Current implementation is Catalog verification only (i.e. no
11 * verification versus tape).
13 * Basic tasks done here:
15 * Open connection with File daemon and pass him commands
17 * When the File daemon sends the attributes, compare them to
24 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
26 This program is free software; you can redistribute it and/or
27 modify it under the terms of the GNU General Public License as
28 published by the Free Software Foundation; either version 2 of
29 the License, or (at your option) any later version.
31 This program is distributed in the hope that it will be useful,
32 but WITHOUT ANY WARRANTY; without even the implied warranty of
33 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
34 General Public License for more details.
36 You should have received a copy of the GNU General Public
37 License along with this program; if not, write to the Free
38 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
46 /* Imported Global Variables */
47 extern int debug_level;
49 /* Commands sent to File daemon */
50 static char verifycmd[] = "verify level=%s\n";
51 static char storaddr[] = "storage address=%s port=%d\n";
52 static char sessioncmd[] = "session %s %ld %ld %ld %ld %ld %ld\n";
54 /* Responses received from File daemon */
55 static char OKverify[] = "2000 OK verify\n";
56 static char OKstore[] = "2000 OK storage\n";
57 static char OKsession[] = "2000 OK session\n";
59 /* Forward referenced functions */
60 static void verify_cleanup(JCR *jcr, int TermCode);
61 static void prt_fname(JCR *jcr);
62 static int missing_handler(void *ctx, int num_fields, char **row);
65 * Do a verification of the specified files against the Catlaog
67 * Returns: 0 on failure
70 int do_verify(JCR *jcr)
77 if (!get_or_create_client_record(jcr)) {
81 Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
83 /* If we are doing a verify from the catalog,
84 * we must look up the time and date of the
87 if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
88 memcpy(&jr, &(jcr->jr), sizeof(jr));
89 if (!db_find_last_jobid(jcr->db, &jr)) {
90 Jmsg(jcr, M_FATAL, 0, _(
91 "Unable to find JobId of previous InitCatalog Job.\n"
92 "Please run a Verify with Level=InitCatalog before\n"
93 "running the current Job.\n"));
97 Dmsg1(20, "Last full id=%d\n", JobId);
100 jcr->jr.JobId = jcr->JobId;
101 jcr->jr.StartTime = jcr->start_time;
102 jcr->jr.Level = jcr->JobLevel;
103 if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
104 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
109 jcr->fname = (char *) get_pool_memory(PM_FNAME);
112 jcr->jr.JobId = JobId; /* save target JobId */
114 /* Print Job Start message */
115 Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
116 jcr->JobId, jcr->Job);
118 if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
119 memset(&jr, 0, sizeof(jr));
121 if (!db_get_job_record(jcr->db, &jr)) {
122 Jmsg(jcr, M_FATAL, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
125 if (jr.JobStatus != 'T') {
126 Jmsg(jcr, M_FATAL, 0, _("Last Job %d did not terminate normally. JobStatus=%c\n"),
127 JobId, jr.JobStatus);
130 Jmsg(jcr, M_INFO, 0, _("Verifying against JobId=%d Job=%s\n"),
135 * If we are verifing a Volume, we need the Storage
136 * daemon, so open a connection, otherwise, just
137 * create a dummy authorization key (passed to
138 * File daemon but not used).
140 if (jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
142 * Now find the Volumes we will need for the Verify
144 jcr->VolumeName[0] = 0;
145 if (!db_get_job_volume_names(jcr->db, jr.JobId, &jcr->VolumeName) ||
146 jcr->VolumeName[0] == 0) {
147 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for verify JobId=%d. %s"),
148 jr.JobId, db_strerror(jcr->db));
151 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
153 * Start conversation with Storage daemon
155 jcr->JobStatus = JS_Blocked;
156 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
160 * Now start a job with the Storage daemon
162 if (!start_storage_daemon_job(jcr)) {
166 * Now start a Storage daemon message thread
168 if (!start_storage_daemon_message_thread(jcr)) {
171 Dmsg0(50, "Storage daemon connection OK\n");
173 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
176 * OK, now connect to the File daemon
177 * and ask him for the files.
179 jcr->JobStatus = JS_Blocked;
180 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
184 jcr->JobStatus = JS_Running;
185 fd = jcr->file_bsock;
187 Dmsg0(30, ">filed: Send include list\n");
188 if (!send_include_list(jcr)) {
192 Dmsg0(30, ">filed: Send exclude list\n");
193 if (!send_exclude_list(jcr)) {
198 * Send Level command to File daemon, as well
199 * as the Storage address if appropriate.
201 switch (jcr->JobLevel) {
205 case L_VERIFY_CATALOG:
208 case L_VERIFY_VOLUME_TO_CATALOG:
210 * send Storage daemon address to the File daemon
212 if (jcr->store->SDDport == 0) {
213 jcr->store->SDDport = jcr->store->SDport;
215 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
216 if (!response(fd, OKstore, "Storage")) {
220 * Pass the VolSessionId, VolSessionTime, Start and
221 * end File and Blocks on the session command.
223 bnet_fsend(fd, sessioncmd,
225 jr.VolSessionId, jr.VolSessionTime,
226 jr.StartFile, jr.EndFile, jr.StartBlock,
228 if (!response(fd, OKsession, "Session")) {
237 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
242 * Send verify command/level to File daemon
244 bnet_fsend(fd, verifycmd, level);
245 if (!response(fd, OKverify, "Verify")) {
250 * Now get data back from File daemon and
251 * compare it to the catalog or store it in the
252 * catalog depending on the run type.
254 /* Compare to catalog */
255 switch (jcr->JobLevel) {
256 case L_VERIFY_CATALOG:
257 Dmsg0(10, "Verify level=catalog\n");
258 get_attributes_and_compare_to_catalog(jcr, JobId);
261 case L_VERIFY_VOLUME_TO_CATALOG:
263 Dmsg0(10, "Verify level=volume\n");
264 get_attributes_and_compare_to_catalog(jcr, JobId);
265 stat = jcr->JobStatus;
266 jcr->JobStatus = JS_WaitSD;
267 wait_for_storage_daemon_termination(jcr);
268 /* If we terminate normally, use SD term code, else, use ours */
269 if (stat == JS_Terminated) {
270 jcr->JobStatus = jcr->SDJobStatus;
272 jcr->JobStatus = stat;
278 Dmsg0(10, "Verify level=init\n");
279 get_attributes_and_put_in_catalog(jcr);
283 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
287 verify_cleanup(jcr, jcr->JobStatus);
291 verify_cleanup(jcr, JS_ErrorTerminated);
296 * Release resources allocated during backup.
299 static void verify_cleanup(JCR *jcr, int TermCode)
301 char sdt[50], edt[50];
308 Dmsg0(100, "Enter verify_cleanup()\n");
310 JobId = jcr->jr.JobId;
311 jcr->JobStatus = TermCode;
313 update_job_end_record(jcr);
315 msg_type = M_INFO; /* by default INFO message */
318 term_msg = _("Verify OK");
320 case JS_ErrorTerminated:
321 term_msg = _("*** Verify Error ***");
322 msg_type = M_ERROR; /* Generate error message */
325 term_msg = _("Verify Cancelled");
328 term_msg = _("Verify Differences");
331 term_msg = term_code;
332 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
335 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
336 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
338 Jmsg(jcr, msg_type, 0, _("%s\n\
346 Files Examined: %s\n\
347 Termination: %s\n\n"),
351 jcr->fileset->hdr.name,
352 level_to_str(jcr->JobLevel),
353 jcr->client->hdr.name,
356 edit_uint64_with_commas(jcr->JobFiles, ec1),
359 Dmsg0(100, "Leave verify_cleanup()\n");
361 free_memory(jcr->fname);
367 * This routine is called only during a Verify
369 int get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
374 struct stat statf; /* file stat */
375 struct stat statc; /* catalog stat */
376 int stat = JS_Terminated;
378 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
380 long file_index = 0, attr_file_index = 0;
382 memset(&fdbr, 0, sizeof(FILE_DBR));
383 fd = jcr->file_bsock;
386 Dmsg0(20, "bdird: waiting to receive file attributes\n");
388 * Get Attributes and MD5 Signature from File daemon
397 while ((n=bget_msg(fd, 0)) > 0 && !job_cancelled(jcr)) {
400 char Opts_MD5[MAXSTRING]; /* Verify Opts or MD5 signature */
402 fname = check_pool_memory_size(fname, fd->msglen);
403 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
404 Dmsg1(400, "Atts+MD5=%s\n", fd->msg);
405 if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream,
407 Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
408 mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
412 skip_nonspaces(&p); /* skip FileIndex */
414 skip_nonspaces(&p); /* skip Stream */
416 skip_nonspaces(&p); /* skip Opts_MD5 */
417 p++; /* skip space */
420 *fn++ = *p++; /* copy filename */
422 *fn = *p++; /* term filename and point to attribs */
425 * Got attributes stream, decode it
427 if (stream == STREAM_UNIX_ATTRIBUTES) {
428 Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
430 attr_file_index = file_index; /* remember attribute file_index */
431 decode_stat(attr, &statf); /* decode file stat packet */
433 jcr->fn_printed = FALSE;
434 strcpy(jcr->fname, fname); /* move filename into JCR */
436 Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
437 Dmsg1(020, "dird<filed: attr=%s\n", attr);
440 * Find equivalent record in the database
443 if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
444 Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
445 Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
446 stat = JS_Differences;
450 * mark file record as visited by stuffing the
451 * current JobId, which is unique, into the MarkId field.
453 db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
456 Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname,
457 file_index, Opts_MD5);
458 decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
460 * Loop over options supplied by user and verify the
461 * fields he requests.
463 for (p=Opts_MD5; *p; p++) {
465 case 'i': /* compare INODEs */
466 if (statc.st_ino != statf.st_ino) {
468 Jmsg(jcr, M_INFO, 0, _(" st_ino differ. Cat: %x File: %x\n"),
469 statc.st_ino, statf.st_ino);
470 stat = JS_Differences;
473 case 'p': /* permissions bits */
474 if (statc.st_mode != statf.st_mode) {
476 Jmsg(jcr, M_INFO, 0, _(" st_mode differ. Cat: %x File: %x\n"),
477 statc.st_mode, statf.st_mode);
478 stat = JS_Differences;
481 case 'n': /* number of links */
482 if (statc.st_nlink != statf.st_nlink) {
484 Jmsg(jcr, M_INFO, 0, _(" st_nlink differ. Cat: %d File: %d\n"),
485 statc.st_nlink, statf.st_nlink);
486 stat = JS_Differences;
489 case 'u': /* user id */
490 if (statc.st_uid != statf.st_uid) {
492 Jmsg(jcr, M_INFO, 0, _(" st_uid differ. Cat: %d File: %d\n"),
493 statc.st_uid, statf.st_uid);
494 stat = JS_Differences;
497 case 'g': /* group id */
498 if (statc.st_gid != statf.st_gid) {
500 Jmsg(jcr, M_INFO, 0, _(" st_gid differ. Cat: %d File: %d\n"),
501 statc.st_gid, statf.st_gid);
502 stat = JS_Differences;
506 if (statc.st_size != statf.st_size) {
508 Jmsg(jcr, M_INFO, 0, _(" st_size differ. Cat: %d File: %d\n"),
509 statc.st_size, statf.st_size);
510 stat = JS_Differences;
513 case 'a': /* access time */
514 if (statc.st_atime != statf.st_atime) {
516 Jmsg(jcr, M_INFO, 0, _(" st_atime differs\n"));
517 stat = JS_Differences;
521 if (statc.st_mtime != statf.st_mtime) {
523 Jmsg(jcr, M_INFO, 0, _(" st_mtime differs\n"));
524 stat = JS_Differences;
527 case 'c': /* ctime */
528 if (statc.st_ctime != statf.st_ctime) {
530 Jmsg(jcr, M_INFO, 0, _(" st_ctime differs\n"));
531 stat = JS_Differences;
534 case 'd': /* file size decrease */
535 if (statc.st_size > statf.st_size) {
537 Jmsg(jcr, M_INFO, 0, _(" st_size decrease. Cat: %d File: %d\n"),
538 statc.st_size, statf.st_size);
539 stat = JS_Differences;
542 case '5': /* compare MD5 */
543 Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
553 * Got MD5 Signature from Storage daemon
554 * It came across in the Opts_MD5 field.
556 } else if (stream == STREAM_MD5_SIGNATURE) {
557 Dmsg2(400, "stream=MD5 inx=%d MD5=%s\n", file_index, Opts_MD5);
559 * When ever we get an MD5 signature is MUST have been
560 * preceded by an attributes record, which sets attr_file_index
562 if (attr_file_index != file_index) {
563 Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
564 file_index, attr_file_index);
568 db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
569 if (strcmp(buf, fdbr.MD5) != 0) {
571 if (debug_level >= 10) {
572 Jmsg(jcr, M_INFO, 0, _(" MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
574 Jmsg(jcr, M_INFO, 0, _(" MD5 differs.\n"));
576 stat = JS_Differences;
581 jcr->JobFiles = file_index;
584 Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
589 /* Now find all the files that are missing -- i.e. all files in
590 * the database where the FileIndex != current JobId
592 jcr->fn_printed = FALSE;
594 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
595 "WHERE File.JobId=%d "
596 "AND File.MarkedId!=%d AND File.PathId=Path.PathId "
597 "AND File.FilenameId=Filename.FilenameId",
599 /* missing_handler is called for each file found */
600 db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
601 if (jcr->fn_printed) {
602 stat = JS_Differences;
604 free_pool_memory(fname);
605 jcr->JobStatus = stat;
609 free_pool_memory(fname);
610 jcr->JobStatus = JS_ErrorTerminated;
615 * We are called here for each record that matches the above
616 * SQL query -- that is for each file contained in the Catalog
617 * that was not marked earlier. This means that the file in
618 * question is a missing file (in the Catalog but on on Disk).
620 static int missing_handler(void *ctx, int num_fields, char **row)
622 JCR *jcr = (JCR *)ctx;
624 if (!jcr->fn_printed) {
625 Jmsg(jcr, M_INFO, 0, "\n");
626 Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
627 jcr->fn_printed = TRUE;
629 Jmsg(jcr, M_INFO, 0, " %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
635 * Print filename for verify
637 static void prt_fname(JCR *jcr)
639 if (!jcr->fn_printed) {
640 Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
641 jcr->fn_printed = TRUE;