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 ssl=0\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 if (jcr->JobLevel == L_VERIFY_CATALOG) {
92 Jmsg(jcr, M_FATAL, 0, _(
93 "Unable to find JobId of previous InitCatalog Job.\n"
94 "Please run a Verify with Level=InitCatalog before\n"
95 "running the current Job.\n"));
97 Jmsg(jcr, M_FATAL, 0, _(
98 "Unable to find JobId of previous Job for this client.\n"));
103 Dmsg1(20, "Last full id=%d\n", JobId);
106 jcr->jr.JobId = jcr->JobId;
107 jcr->jr.StartTime = jcr->start_time;
108 jcr->jr.Level = jcr->JobLevel;
109 if (!db_update_job_start_record(jcr, jcr->db, &jcr->jr)) {
110 Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
115 jcr->fname = (char *) get_pool_memory(PM_FNAME);
118 jcr->jr.JobId = JobId; /* save target JobId */
120 /* Print Job Start message */
121 Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
122 jcr->JobId, jcr->Job);
124 if (jcr->JobLevel == L_VERIFY_CATALOG || jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
125 memset(&jr, 0, sizeof(jr));
127 if (!db_get_job_record(jcr, jcr->db, &jr)) {
128 Jmsg(jcr, M_FATAL, 0, _("Could not get job record for previous Job. ERR=%s"),
129 db_strerror(jcr->db));
132 if (jr.JobStatus != 'T') {
133 Jmsg(jcr, M_FATAL, 0, _("Last Job %d did not terminate normally. JobStatus=%c\n"),
134 JobId, jr.JobStatus);
137 Jmsg(jcr, M_INFO, 0, _("Verifying against JobId=%d Job=%s\n"),
142 * If we are verifing a Volume, we need the Storage
143 * daemon, so open a connection, otherwise, just
144 * create a dummy authorization key (passed to
145 * File daemon but not used).
147 if (jcr->JobLevel == L_VERIFY_VOLUME_TO_CATALOG) {
149 * Now find the Volumes we will need for the Verify
151 jcr->VolumeName[0] = 0;
152 if (!db_get_job_volume_names(jcr, jcr->db, jr.JobId, &jcr->VolumeName) ||
153 jcr->VolumeName[0] == 0) {
154 Jmsg(jcr, M_FATAL, 0, _("Cannot find Volume Name for verify JobId=%u. ERR=%s"),
155 jr.JobId, db_strerror(jcr->db));
158 Dmsg1(20, "Got job Volume Names: %s\n", jcr->VolumeName);
160 * Start conversation with Storage daemon
162 set_jcr_job_status(jcr, JS_Blocked);
163 if (!connect_to_storage_daemon(jcr, 10, SDConnectTimeout, 1)) {
167 * Now start a job with the Storage daemon
169 if (!start_storage_daemon_job(jcr)) {
173 * Now start a Storage daemon message thread
175 if (!start_storage_daemon_message_thread(jcr)) {
178 Dmsg0(50, "Storage daemon connection OK\n");
180 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
183 * OK, now connect to the File daemon
184 * and ask him for the files.
186 set_jcr_job_status(jcr, JS_Blocked);
187 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
191 set_jcr_job_status(jcr, JS_Running);
192 fd = jcr->file_bsock;
194 Dmsg0(30, ">filed: Send include list\n");
195 if (!send_include_list(jcr)) {
199 Dmsg0(30, ">filed: Send exclude list\n");
200 if (!send_exclude_list(jcr)) {
205 * Send Level command to File daemon, as well
206 * as the Storage address if appropriate.
208 switch (jcr->JobLevel) {
212 case L_VERIFY_CATALOG:
215 case L_VERIFY_VOLUME_TO_CATALOG:
217 * send Storage daemon address to the File daemon
219 if (jcr->store->SDDport == 0) {
220 jcr->store->SDDport = jcr->store->SDport;
222 bnet_fsend(fd, storaddr, jcr->store->address, jcr->store->SDDport);
223 if (!response(fd, OKstore, "Storage", 1)) {
227 * Pass the VolSessionId, VolSessionTime, Start and
228 * end File and Blocks on the session command.
230 bnet_fsend(fd, sessioncmd,
232 jr.VolSessionId, jr.VolSessionTime,
233 jr.StartFile, jr.EndFile, jr.StartBlock,
235 if (!response(fd, OKsession, "Session", 1)) {
244 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
249 * Send verify command/level to File daemon
251 bnet_fsend(fd, verifycmd, level);
252 if (!response(fd, OKverify, "Verify", 1)) {
257 * Now get data back from File daemon and
258 * compare it to the catalog or store it in the
259 * catalog depending on the run type.
261 /* Compare to catalog */
262 switch (jcr->JobLevel) {
263 case L_VERIFY_CATALOG:
264 Dmsg0(10, "Verify level=catalog\n");
265 get_attributes_and_compare_to_catalog(jcr, JobId);
268 case L_VERIFY_VOLUME_TO_CATALOG:
270 Dmsg0(10, "Verify level=volume\n");
271 get_attributes_and_compare_to_catalog(jcr, JobId);
272 stat = jcr->JobStatus;
273 set_jcr_job_status(jcr, JS_WaitSD);
274 wait_for_storage_daemon_termination(jcr);
275 /* If we terminate normally, use SD term code, else, use ours */
276 if (stat == JS_Terminated) {
277 set_jcr_job_status(jcr, jcr->SDJobStatus);
279 set_jcr_job_status(jcr, stat);
285 Dmsg0(10, "Verify level=init\n");
286 get_attributes_and_put_in_catalog(jcr);
290 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
294 verify_cleanup(jcr, jcr->JobStatus);
298 verify_cleanup(jcr, JS_ErrorTerminated);
303 * Release resources allocated during backup.
306 static void verify_cleanup(JCR *jcr, int TermCode)
308 char sdt[50], edt[50];
315 Dmsg0(100, "Enter verify_cleanup()\n");
317 JobId = jcr->jr.JobId;
318 set_jcr_job_status(jcr, TermCode);
320 update_job_end_record(jcr);
322 msg_type = M_INFO; /* by default INFO message */
325 term_msg = _("Verify OK");
327 case JS_ErrorTerminated:
328 term_msg = _("*** Verify Error ***");
329 msg_type = M_ERROR; /* Generate error message */
332 term_msg = _("Verify Canceled");
335 term_msg = _("Verify Differences");
338 term_msg = term_code;
339 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
342 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
343 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
345 Jmsg(jcr, msg_type, 0, _("Bacula " VERSION " (" LSMDATE "): %s\n\
353 Files Examined: %s\n\
354 Termination: %s\n\n"),
358 jcr->fileset->hdr.name,
359 level_to_str(jcr->JobLevel),
360 jcr->client->hdr.name,
363 edit_uint64_with_commas(jcr->JobFiles, ec1),
366 Dmsg0(100, "Leave verify_cleanup()\n");
368 free_memory(jcr->fname);
374 * This routine is called only during a Verify
376 int get_attributes_and_compare_to_catalog(JCR *jcr, JobId_t JobId)
381 struct stat statf; /* file stat */
382 struct stat statc; /* catalog stat */
383 int stat = JS_Terminated;
385 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
389 memset(&fdbr, 0, sizeof(FILE_DBR));
390 fd = jcr->file_bsock;
394 Dmsg0(20, "bdird: waiting to receive file attributes\n");
396 * Get Attributes and Signature from File daemon
400 * Options or SIG (MD5/SHA1)
405 while ((n=bget_dirmsg(fd)) >= 0 && !job_canceled(jcr)) {
408 char Opts_SIG[MAXSTRING]; /* Verify Opts or MD5/SHA1 signature */
410 fname = check_pool_memory_size(fname, fd->msglen);
411 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
412 Dmsg1(400, "Atts+SIG=%s\n", fd->msg);
413 if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream,
415 Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
416 mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
420 * We read the Options or Signature into fname
421 * to prevent overrun, now copy it to proper location.
423 bstrncpy(Opts_SIG, fname, sizeof(Opts_SIG));
425 skip_nonspaces(&p); /* skip FileIndex */
427 skip_nonspaces(&p); /* skip Stream */
429 skip_nonspaces(&p); /* skip Opts_SIG */
430 p++; /* skip space */
433 *fn++ = *p++; /* copy filename */
435 *fn = *p++; /* term filename and point to attribs */
438 * Got attributes stream, decode it
440 if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
441 uint32_t LinkFIf, LinkFIc;
442 Dmsg2(400, "file_index=%d attr=%s\n", file_index, attr);
444 jcr->FileIndex = file_index; /* remember attribute file_index */
445 decode_stat(attr, &statf, &LinkFIf); /* decode file stat packet */
447 jcr->fn_printed = FALSE;
448 strcpy(jcr->fname, fname); /* move filename into JCR */
450 Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
451 Dmsg1(020, "dird<filed: attr=%s\n", attr);
454 * Find equivalent record in the database
457 if (!db_get_file_attributes_record(jcr, jcr->db, jcr->fname, &fdbr)) {
458 Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
459 Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
460 stat = JS_Differences;
464 * mark file record as visited by stuffing the
465 * current JobId, which is unique, into the MarkId field.
467 db_mark_file_record(jcr, jcr->db, fdbr.FileId, jcr->JobId);
470 Dmsg3(400, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname,
471 file_index, Opts_SIG);
472 decode_stat(fdbr.LStat, &statc, &LinkFIc); /* decode catalog stat */
474 * Loop over options supplied by user and verify the
475 * fields he requests.
477 for (p=Opts_SIG; *p; p++) {
478 char ed1[30], ed2[30];
480 case 'i': /* compare INODEs */
481 if (statc.st_ino != statf.st_ino) {
483 Jmsg(jcr, M_INFO, 0, _(" st_ino differ. Cat: %s File: %s\n"),
484 edit_uint64((uint64_t)statc.st_ino, ed1),
485 edit_uint64((uint64_t)statf.st_ino, ed2));
486 stat = JS_Differences;
489 case 'p': /* permissions bits */
490 if (statc.st_mode != statf.st_mode) {
492 Jmsg(jcr, M_INFO, 0, _(" st_mode differ. Cat: %x File: %x\n"),
493 (uint32_t)statc.st_mode, (uint32_t)statf.st_mode);
494 stat = JS_Differences;
497 case 'n': /* number of links */
498 if (statc.st_nlink != statf.st_nlink) {
500 Jmsg(jcr, M_INFO, 0, _(" st_nlink differ. Cat: %d File: %d\n"),
501 (uint32_t)statc.st_nlink, (uint32_t)statf.st_nlink);
502 stat = JS_Differences;
505 case 'u': /* user id */
506 if (statc.st_uid != statf.st_uid) {
508 Jmsg(jcr, M_INFO, 0, _(" st_uid differ. Cat: %u File: %u\n"),
509 (uint32_t)statc.st_uid, (uint32_t)statf.st_uid);
510 stat = JS_Differences;
513 case 'g': /* group id */
514 if (statc.st_gid != statf.st_gid) {
516 Jmsg(jcr, M_INFO, 0, _(" st_gid differ. Cat: %u File: %u\n"),
517 (uint32_t)statc.st_gid, (uint32_t)statf.st_gid);
518 stat = JS_Differences;
522 if (statc.st_size != statf.st_size) {
524 Jmsg(jcr, M_INFO, 0, _(" st_size differ. Cat: %s File: %s\n"),
525 edit_uint64((uint64_t)statc.st_size, ed1),
526 edit_uint64((uint64_t)statf.st_size, ed2));
527 stat = JS_Differences;
530 case 'a': /* access time */
531 if (statc.st_atime != statf.st_atime) {
533 Jmsg(jcr, M_INFO, 0, _(" st_atime differs\n"));
534 stat = JS_Differences;
538 if (statc.st_mtime != statf.st_mtime) {
540 Jmsg(jcr, M_INFO, 0, _(" st_mtime differs\n"));
541 stat = JS_Differences;
544 case 'c': /* ctime */
545 if (statc.st_ctime != statf.st_ctime) {
547 Jmsg(jcr, M_INFO, 0, _(" st_ctime differs\n"));
548 stat = JS_Differences;
551 case 'd': /* file size decrease */
552 if (statc.st_size > statf.st_size) {
554 Jmsg(jcr, M_INFO, 0, _(" st_size decrease. Cat: %s File: %s\n"),
555 edit_uint64((uint64_t)statc.st_size, ed1),
556 edit_uint64((uint64_t)statf.st_size, ed2));
557 stat = JS_Differences;
560 case '5': /* compare MD5 */
561 Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
564 case '1': /* compare SHA1 */
574 * Got SIG Signature from Storage daemon
575 * It came across in the Opts_SIG field.
577 } else if (stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE) {
578 Dmsg2(400, "stream=SIG inx=%d SIG=%s\n", file_index, Opts_SIG);
580 * When ever we get a signature is MUST have been
581 * preceded by an attributes record, which sets attr_file_index
583 if (jcr->FileIndex != (uint32_t)file_index) {
584 Jmsg2(jcr, M_FATAL, 0, _("MD5/SHA1 index %d not same as attributes %d\n"),
585 file_index, jcr->FileIndex);
589 db_escape_string(buf, Opts_SIG, strlen(Opts_SIG));
590 if (strcmp(buf, fdbr.SIG) != 0) {
592 if (debug_level >= 10) {
593 Jmsg(jcr, M_INFO, 0, _(" %s not same. File=%s Cat=%s\n"),
594 stream==STREAM_MD5_SIGNATURE?"MD5":"SHA1", buf, fdbr.SIG);
596 Jmsg(jcr, M_INFO, 0, _(" %s differs.\n"),
597 stream==STREAM_MD5_SIGNATURE?"MD5":"SHA1");
599 stat = JS_Differences;
604 jcr->JobFiles = file_index;
606 if (is_bnet_error(fd)) {
607 Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
612 /* Now find all the files that are missing -- i.e. all files in
613 * the database where the MarkedId != current JobId
615 jcr->fn_printed = FALSE;
617 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
618 "WHERE File.JobId=%d "
619 "AND File.MarkedId!=%d AND File.PathId=Path.PathId "
620 "AND File.FilenameId=Filename.FilenameId",
622 /* missing_handler is called for each file found */
623 db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
624 if (jcr->fn_printed) {
625 stat = JS_Differences;
627 free_pool_memory(fname);
628 set_jcr_job_status(jcr, stat);
632 free_pool_memory(fname);
633 set_jcr_job_status(jcr, JS_ErrorTerminated);
638 * We are called here for each record that matches the above
639 * SQL query -- that is for each file contained in the Catalog
640 * that was not marked earlier. This means that the file in
641 * question is a missing file (in the Catalog but on on Disk).
643 static int missing_handler(void *ctx, int num_fields, char **row)
645 JCR *jcr = (JCR *)ctx;
647 if (!jcr->fn_printed) {
648 Jmsg(jcr, M_INFO, 0, "\n");
649 Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
650 jcr->fn_printed = TRUE;
652 Jmsg(jcr, M_INFO, 0, " %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
658 * Print filename for verify
660 static void prt_fname(JCR *jcr)
662 if (!jcr->fn_printed) {
663 Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
664 jcr->fn_printed = TRUE;