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-2003 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,
45 #include "findlib/find.h"
47 /* Imported Global Variables */
48 extern int debug_level;
50 /* Commands sent to File daemon */
51 static char verifycmd[] = "verify level=%s\n";
52 static char storaddr[] = "storage address=%s port=%d\n";
53 static char sessioncmd[] = "session %s %ld %ld %ld %ld %ld %ld\n";
55 /* Responses received from File daemon */
56 static char OKverify[] = "2000 OK verify\n";
57 static char OKstore[] = "2000 OK storage\n";
58 static char OKsession[] = "2000 OK session\n";
60 /* Forward referenced functions */
61 static void verify_cleanup(JCR *jcr, int TermCode);
62 static void prt_fname(JCR *jcr);
63 static int missing_handler(void *ctx, int num_fields, char **row);
66 * Do a verification of the specified files against the Catlaog
68 * Returns: 0 on failure
71 int do_verify(JCR *jcr)
78 if (!get_or_create_client_record(jcr)) {
82 Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
84 /* If we are doing a verify from the catalog,
85 * we must look up the time and date of the
88 if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
89 memcpy(&jr, &(jcr->jr), sizeof(jr));
90 if (!db_find_last_jobid(jcr, jcr->db, &jr)) {
91 Jmsg(jcr, M_FATAL, 0, _(
92 "Unable to find JobId of previous InitCatalog Job.\n"
93 "Please run a Verify with Level=InitCatalog before\n"
94 "running the current Job.\n"));
98 Dmsg1(20, "Last full id=%d\n", JobId);
101 jcr->jr.JobId = jcr->JobId;
102 jcr->jr.StartTime = jcr->start_time;
103 jcr->jr.Level = jcr->JobLevel;
104 if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
105 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
110 jcr->fname = (char *) get_pool_memory(PM_FNAME);
113 jcr->jr.JobId = JobId; /* save target JobId */
115 /* Print Job Start message */
116 Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
117 jcr->JobId, jcr->Job);
119 if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
120 memset(&jr, 0, sizeof(jr));
122 if (!db_get_job_record(jcr, jcr->db, &jr)) {
123 Jmsg(jcr, M_FATAL, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
126 if (jr.JobStatus != 'T') {
127 Jmsg(jcr, M_FATAL, 0, _("Last Job %d did not terminate normally. JobStatus=%c\n"),
128 JobId, jr.JobStatus);
131 Jmsg(jcr, M_INFO, 0, _("Verifying against JobId=%d Job=%s\n"),
136 * If we are verifing a Volume, we need the Storage
137 * daemon, so open a connection, otherwise, just
138 * create a dummy authorization key (passed to
139 * File daemon but not used).
141 if (jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
143 * Now find the Volumes we will need for the Verify
145 jcr->VolumeName[0] = 0;
146 if (!db_get_job_volume_names(jcr, jcr->db, jr.JobId, &jcr->VolumeName) ||
147 jcr->VolumeName[0] == 0) {
148 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for verify JobId=%d. %s"),
149 jr.JobId, db_strerror(jcr->db));
152 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
154 * Start conversation with Storage daemon
156 set_jcr_job_status(jcr, JS_Blocked);
157 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
161 * Now start a job with the Storage daemon
163 if (!start_storage_daemon_job(jcr)) {
167 * Now start a Storage daemon message thread
169 if (!start_storage_daemon_message_thread(jcr)) {
172 Dmsg0(50, "Storage daemon connection OK\n");
174 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
177 * OK, now connect to the File daemon
178 * and ask him for the files.
180 set_jcr_job_status(jcr, JS_Blocked);
181 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
185 set_jcr_job_status(jcr, JS_Running);
186 fd = jcr->file_bsock;
188 Dmsg0(30, ">filed: Send include list\n");
189 if (!send_include_list(jcr)) {
193 Dmsg0(30, ">filed: Send exclude list\n");
194 if (!send_exclude_list(jcr)) {
199 * Send Level command to File daemon, as well
200 * as the Storage address if appropriate.
202 switch (jcr->JobLevel) {
206 case L_VERIFY_CATALOG:
209 case L_VERIFY_VOLUME_TO_CATALOG:
211 * send Storage daemon address to the File daemon
213 if (jcr->store->SDDport == 0) {
214 jcr->store->SDDport = jcr->store->SDport;
216 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
217 if (!response(fd, OKstore, "Storage", 1)) {
221 * Pass the VolSessionId, VolSessionTime, Start and
222 * end File and Blocks on the session command.
224 bnet_fsend(fd, sessioncmd,
226 jr.VolSessionId, jr.VolSessionTime,
227 jr.StartFile, jr.EndFile, jr.StartBlock,
229 if (!response(fd, OKsession, "Session", 1)) {
238 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
243 * Send verify command/level to File daemon
245 bnet_fsend(fd, verifycmd, level);
246 if (!response(fd, OKverify, "Verify", 1)) {
251 * Now get data back from File daemon and
252 * compare it to the catalog or store it in the
253 * catalog depending on the run type.
255 /* Compare to catalog */
256 switch (jcr->JobLevel) {
257 case L_VERIFY_CATALOG:
258 Dmsg0(10, "Verify level=catalog\n");
259 get_attributes_and_compare_to_catalog(jcr, JobId);
262 case L_VERIFY_VOLUME_TO_CATALOG:
264 Dmsg0(10, "Verify level=volume\n");
265 get_attributes_and_compare_to_catalog(jcr, JobId);
266 stat = jcr->JobStatus;
267 set_jcr_job_status(jcr, JS_WaitSD);
268 wait_for_storage_daemon_termination(jcr);
269 /* If we terminate normally, use SD term code, else, use ours */
270 if (stat == JS_Terminated) {
271 set_jcr_job_status(jcr, jcr->SDJobStatus);
273 set_jcr_job_status(jcr, stat);
279 Dmsg0(10, "Verify level=init\n");
280 get_attributes_and_put_in_catalog(jcr);
284 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
288 verify_cleanup(jcr, jcr->JobStatus);
292 verify_cleanup(jcr, JS_ErrorTerminated);
297 * Release resources allocated during backup.
300 static void verify_cleanup(JCR *jcr, int TermCode)
302 char sdt[50], edt[50];
309 Dmsg0(100, "Enter verify_cleanup()\n");
311 JobId = jcr->jr.JobId;
312 set_jcr_job_status(jcr, TermCode);
314 update_job_end_record(jcr);
316 msg_type = M_INFO; /* by default INFO message */
319 term_msg = _("Verify OK");
321 case JS_ErrorTerminated:
322 term_msg = _("*** Verify Error ***");
323 msg_type = M_ERROR; /* Generate error message */
326 term_msg = _("Verify Cancelled");
329 term_msg = _("Verify Differences");
332 term_msg = term_code;
333 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
336 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
337 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
339 Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
347 Files Examined: %s\n\
348 Termination: %s\n\n"),
352 jcr->fileset->hdr.name,
353 level_to_str(jcr->JobLevel),
354 jcr->client->hdr.name,
357 edit_uint64_with_commas(jcr->JobFiles, ec1),
360 Dmsg0(100, "Leave verify_cleanup()\n");
362 free_memory(jcr->fname);
368 * This routine is called only during a Verify
370 int get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
375 struct stat statf; /* file stat */
376 struct stat statc; /* catalog stat */
377 int stat = JS_Terminated;
379 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
383 memset(&fdbr, 0, sizeof(FILE_DBR));
384 fd = jcr->file_bsock;
388 Dmsg0(20, "bdird: waiting to receive file attributes\n");
390 * Get Attributes and Signature from File daemon
394 * Options or SIG (MD5/SHA1)
399 while ((n=bget_msg(fd, 0)) >= 0 && !job_cancelled(jcr)) {
402 char Opts_SIG[MAXSTRING]; /* Verify Opts or MD5/SHA1 signature */
404 fname = check_pool_memory_size(fname, fd->msglen);
405 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
406 Dmsg1(400, "Atts+SIG=%s\n", fd->msg);
407 if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream,
409 Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
410 mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
414 * We read the Options or Signature into fname
415 * to prevent overrun, now copy it to proper location.
417 bstrncpy(Opts_SIG, fname, sizeof(Opts_SIG));
419 skip_nonspaces(&p); /* skip FileIndex */
421 skip_nonspaces(&p); /* skip Stream */
423 skip_nonspaces(&p); /* skip Opts_SIG */
424 p++; /* skip space */
427 *fn++ = *p++; /* copy filename */
429 *fn = *p++; /* term filename and point to attribs */
432 * Got attributes stream, decode it
434 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
435 uint32_t LinkFIf, LinkFIc;
436 Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
438 jcr->FileIndex = file_index; /* remember attribute file_index */
439 decode_stat(attr, &statf, &LinkFIf); /* decode file stat packet */
441 jcr->fn_printed = FALSE;
442 strcpy(jcr->fname, fname); /* move filename into JCR */
444 Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
445 Dmsg1(020, "dird<filed: attr=%s\n", attr);
448 * Find equivalent record in the database
451 if (!db_get_file_attributes_record(jcr, jcr->db, jcr->fname, &fdbr)) {
452 Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
453 Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
454 stat = JS_Differences;
458 * mark file record as visited by stuffing the
459 * current JobId, which is unique, into the MarkId field.
461 db_mark_file_record(jcr, jcr->db, fdbr.FileId, jcr->JobId);
464 Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname,
465 file_index, Opts_SIG);
466 decode_stat(fdbr.LStat, &statc, &LinkFIc); /* decode catalog stat */
468 * Loop over options supplied by user and verify the
469 * fields he requests.
471 for (p=Opts_SIG; *p; p++) {
472 char ed1[30], ed2[30];
474 case 'i': /* compare INODEs */
475 if (statc.st_ino != statf.st_ino) {
477 Jmsg(jcr, M_INFO, 0, _(" st_ino differ. Cat: %s File: %s\n"),
478 edit_uint64((uint64_t)statc.st_ino, ed1),
479 edit_uint64((uint64_t)statf.st_ino, ed2));
480 stat = JS_Differences;
483 case 'p': /* permissions bits */
484 if (statc.st_mode != statf.st_mode) {
486 Jmsg(jcr, M_INFO, 0, _(" st_mode differ. Cat: %x File: %x\n"),
487 (uint32_t)statc.st_mode, (uint32_t)statf.st_mode);
488 stat = JS_Differences;
491 case 'n': /* number of links */
492 if (statc.st_nlink != statf.st_nlink) {
494 Jmsg(jcr, M_INFO, 0, _(" st_nlink differ. Cat: %d File: %d\n"),
495 (uint32_t)statc.st_nlink, (uint32_t)statf.st_nlink);
496 stat = JS_Differences;
499 case 'u': /* user id */
500 if (statc.st_uid != statf.st_uid) {
502 Jmsg(jcr, M_INFO, 0, _(" st_uid differ. Cat: %u File: %u\n"),
503 (uint32_t)statc.st_uid, (uint32_t)statf.st_uid);
504 stat = JS_Differences;
507 case 'g': /* group id */
508 if (statc.st_gid != statf.st_gid) {
510 Jmsg(jcr, M_INFO, 0, _(" st_gid differ. Cat: %u File: %u\n"),
511 (uint32_t)statc.st_gid, (uint32_t)statf.st_gid);
512 stat = JS_Differences;
516 if (statc.st_size != statf.st_size) {
518 Jmsg(jcr, M_INFO, 0, _(" st_size differ. Cat: %s File: %s\n"),
519 edit_uint64((uint64_t)statc.st_size, ed1),
520 edit_uint64((uint64_t)statf.st_size, ed2));
521 stat = JS_Differences;
524 case 'a': /* access time */
525 if (statc.st_atime != statf.st_atime) {
527 Jmsg(jcr, M_INFO, 0, _(" st_atime differs\n"));
528 stat = JS_Differences;
532 if (statc.st_mtime != statf.st_mtime) {
534 Jmsg(jcr, M_INFO, 0, _(" st_mtime differs\n"));
535 stat = JS_Differences;
538 case 'c': /* ctime */
539 if (statc.st_ctime != statf.st_ctime) {
541 Jmsg(jcr, M_INFO, 0, _(" st_ctime differs\n"));
542 stat = JS_Differences;
545 case 'd': /* file size decrease */
546 if (statc.st_size > statf.st_size) {
548 Jmsg(jcr, M_INFO, 0, _(" st_size decrease. Cat: %s File: %s\n"),
549 edit_uint64((uint64_t)statc.st_size, ed1),
550 edit_uint64((uint64_t)statf.st_size, ed2));
551 stat = JS_Differences;
554 case '5': /* compare MD5 */
555 Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
558 case '1': /* compare SHA1 */
568 * Got SIG Signature from Storage daemon
569 * It came across in the Opts_SIG field.
571 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
572 Dmsg2(400, "stream=SIG inx=%d SIG=%s\n", file_index, Opts_SIG);
574 * When ever we get a signature is MUST have been
575 * preceded by an attributes record, which sets attr_file_index
577 if (jcr->FileIndex != (uint32_t)file_index) {
578 Jmsg2(jcr, M_FATAL, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
579 file_index, jcr->FileIndex);
583 db_escape_string(buf, Opts_SIG, strlen(Opts_SIG));
584 if (strcmp(buf, fdbr.SIG) != 0) {
586 if (debug_level >= 10) {
587 Jmsg(jcr, M_INFO, 0, _(" %s not same. File=%s Cat=%s\n"),
588 stream==STREAM_MD5_SIGNATURE?"MD5":"SHA1", buf, fdbr.SIG);
590 Jmsg(jcr, M_INFO, 0, _(" %s differs.\n"),
591 stream==STREAM_MD5_SIGNATURE?"MD5":"SHA1");
593 stat = JS_Differences;
598 jcr->JobFiles = file_index;
600 if (is_bnet_error(fd)) {
601 Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
606 /* Now find all the files that are missing -- i.e. all files in
607 * the database where the MarkedId != current JobId
609 jcr->fn_printed = FALSE;
611 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
612 "WHERE File.JobId=%d "
613 "AND File.MarkedId!=%d AND File.PathId=Path.PathId "
614 "AND File.FilenameId=Filename.FilenameId",
616 /* missing_handler is called for each file found */
617 db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
618 if (jcr->fn_printed) {
619 stat = JS_Differences;
621 free_pool_memory(fname);
622 set_jcr_job_status(jcr, stat);
626 free_pool_memory(fname);
627 set_jcr_job_status(jcr, JS_ErrorTerminated);
632 * We are called here for each record that matches the above
633 * SQL query -- that is for each file contained in the Catalog
634 * that was not marked earlier. This means that the file in
635 * question is a missing file (in the Catalog but on on Disk).
637 static int missing_handler(void *ctx, int num_fields, char **row)
639 JCR *jcr = (JCR *)ctx;
641 if (!jcr->fn_printed) {
642 Jmsg(jcr, M_INFO, 0, "\n");
643 Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
644 jcr->fn_printed = TRUE;
646 Jmsg(jcr, M_INFO, 0, " %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
652 * Print filename for verify
654 static void prt_fname(JCR *jcr)
656 if (!jcr->fn_printed) {
657 Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
658 jcr->fn_printed = TRUE;