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
23 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25 This program is free software; you can redistribute it and/or
26 modify it under the terms of the GNU General Public License as
27 published by the Free Software Foundation; either version 2 of
28 the License, or (at your option) any later version.
30 This program is distributed in the hope that it will be useful,
31 but WITHOUT ANY WARRANTY; without even the implied warranty of
32 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
33 General Public License for more details.
35 You should have received a copy of the GNU General Public
36 License along with this program; if not, write to the Free
37 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
45 /* Imported Global Variables */
46 extern int debug_level;
48 /* Commands sent to File daemon */
49 static char verifycmd[] = "verify";
50 static char levelcmd[] = "level = %s%s\n";
52 /* Responses received from File daemon */
53 static char OKverify[] = "2000 OK verify\n";
54 static char OKlevel[] = "2000 OK level\n";
56 /* Forward referenced functions */
57 static void verify_cleanup(JCR *jcr);
58 static void prt_fname(JCR *jcr);
59 static int missing_handler(void *ctx, int num_fields, char **row);
62 * Do a verification of the specified files
64 * Returns: 0 on failure
67 int do_verify(JCR *jcr)
75 memset(&cr, 0, sizeof(cr));
76 strcpy(cr.Name, jcr->client->hdr.name);
77 if (jcr->client_name) {
78 free(jcr->client_name);
80 jcr->client_name = bstrdup(jcr->client->hdr.name);
81 if (!db_create_client_record(jcr->db, &cr)) {
82 Jmsg(jcr, M_ERROR, 0, _("Could not create Client record. %s"),
83 db_strerror(jcr->db));
84 jcr->JobStatus = JS_ErrorTerminated;
88 jcr->jr.ClientId = cr.ClientId;
90 Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
92 /* If we are doing a verify from the catalog,
93 * we must look up the time and date of the
96 if (jcr->level == L_VERIFY_CATALOG) {
97 memcpy(&jr, &(jcr->jr), sizeof(jr));
98 if (!db_find_last_full_verify(jcr->db, &jr)) {
99 Jmsg(jcr, M_FATAL, 0, _("Unable to find last full verify. %s"),
100 db_strerror(jcr->db));
101 jcr->JobStatus = JS_ErrorTerminated;
105 last_full_id = jr.JobId;
106 Dmsg1(20, "Last full id=%d\n", last_full_id);
109 jcr->jr.JobId = jcr->JobId;
110 jcr->jr.StartTime = jcr->start_time;
111 jcr->jr.Level = jcr->level;
112 if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
113 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
114 jcr->JobStatus = JS_ErrorTerminated;
120 jcr->fname = (char *) get_pool_memory(PM_FNAME);
123 jcr->jr.JobId = last_full_id; /* save last full id */
125 /* Print Job Start message */
126 Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
127 jcr->JobId, jcr->Job);
129 if (jcr->level == L_VERIFY_CATALOG) {
130 memset(&jr, 0, sizeof(jr));
131 jr.JobId = last_full_id;
132 if (!db_get_job_record(jcr->db, &jr)) {
133 Jmsg(jcr, M_ERROR, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
134 jcr->JobStatus = JS_ErrorTerminated;
138 Jmsg(jcr, M_INFO, 0, _("Verifying against Init JobId %d run %s\n"),
139 last_full_id, jr.cStartTime);
143 * OK, now connect to the File daemon
144 * and ask him for the files.
146 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
147 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
148 jcr->JobStatus = JS_ErrorTerminated;
153 fd = jcr->file_bsock;
155 Dmsg0(30, ">filed: Send include list\n");
156 if (!send_include_list(jcr)) {
157 jcr->JobStatus = JS_ErrorTerminated;
162 Dmsg0(30, ">filed: Send exclude list\n");
163 if (!send_exclude_list(jcr)) {
164 jcr->JobStatus = JS_ErrorTerminated;
170 * Send Level command to File daemon
173 switch (jcr->level) {
177 case L_VERIFY_CATALOG:
180 case L_VERIFY_VOLUME:
187 Emsg1(M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
188 jcr->JobStatus = JS_ErrorTerminated;
192 Dmsg1(20, ">filed: %s", fd->msg);
193 bnet_fsend(fd, levelcmd, level, " ");
194 if (!response(fd, OKlevel, "Level")) {
195 jcr->JobStatus = JS_ErrorTerminated;
201 * Send verify command to File daemon
203 bnet_fsend(fd, verifycmd);
204 if (!response(fd, OKverify, "Verify")) {
205 jcr->JobStatus = JS_ErrorTerminated;
211 * Now get data back from File daemon and
212 * compare it to the catalog or store it in the
213 * catalog depending on the run type.
215 /* Compare to catalog */
216 if (jcr->level == L_VERIFY_CATALOG) {
217 Dmsg0(10, "Verify level=catalog\n");
218 get_attributes_and_compare_to_catalog(jcr, last_full_id);
221 } else if (jcr->level == L_VERIFY_INIT) {
222 Dmsg0(10, "Verify level=init\n");
223 get_attributes_and_put_in_catalog(jcr);
226 Emsg1(M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
227 jcr->JobStatus = JS_ErrorTerminated;
237 * Release resources allocated during backup.
240 static void verify_cleanup(JCR *jcr)
242 char sdt[50], edt[50];
250 Dmsg0(100, "Enter verify_cleanup()\n");
252 last_full_id = jcr->jr.JobId;
255 if (jcr->jr.EndTime == 0) {
256 jcr->jr.EndTime = time(NULL);
258 jcr->end_time = jcr->jr.EndTime;
259 jcr->jr.JobId = jcr->JobId;
260 jcr->jr.JobStatus = jcr->JobStatus;
261 TermCode = jcr->JobStatus;
263 if (!db_update_job_end_record(jcr->db, &jcr->jr)) {
264 Jmsg(jcr, M_WARNING, 0, _("Error updating job record. %s"),
265 db_strerror(jcr->db));
268 msg_type = M_INFO; /* by default INFO message */
271 term_msg = _("Verify OK");
274 term_msg = _("*** Verify Error ***");
275 msg_type = M_ERROR; /* Generate error message */
278 term_msg = _("Verify Cancelled");
281 term_msg = _("Verify Differences");
284 term_msg = term_code;
285 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
288 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
289 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
291 Jmsg(jcr, msg_type, 0, _("%s\n\
299 Files Examined: %s\n\
304 jcr->fileset->hdr.name,
305 level_to_str(jcr->level),
306 jcr->client->hdr.name,
309 edit_uint_with_commas(jcr->jr.JobFiles, ec1),
312 Dmsg0(100, "Leave verify_cleanup()\n");
317 * This routine is called only during a Verify
319 int get_attributes_and_compare_to_catalog(JCR *jcr, int last_full_id)
324 struct stat statf; /* file stat */
325 struct stat statc; /* catalog stat */
326 int stat = JS_Terminated;
328 char *fname = (char *)get_pool_memory(PM_MESSAGE);
330 memset(&fdbr, 0, sizeof(FILE_DBR));
331 fd = jcr->file_bsock;
332 fdbr.JobId = last_full_id;
334 Dmsg0(20, "bdird: waiting to receive file attributes\n");
336 * Get Attributes and MD5 Signature from File daemon
338 while ((n=bget_msg(fd, 0)) > 0) {
339 long file_index, attr_file_index;
342 char Opts_MD5[MAXSTRING]; /* Verify Opts or MD5 signature */
345 fname = (char *)check_pool_memory_size(fname, fd->msglen);
346 jcr->fname = (char *)check_pool_memory_size(fname, fd->msglen);
347 Dmsg1(50, "Atts+MD5=%s\n", fd->msg);
348 if ((len = sscanf(fd->msg, "%ld %d %100s %s", &file_index, &stream,
349 Opts_MD5, fname)) != 4) {
350 Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 4 fields got %d\n\
351 msglen=%d msg=%s\n"), len, fd->msglen, fd->msg);
352 jcr->JobStatus = JS_ErrorTerminated;
356 * Got attributes stream, decode it
358 if (stream == STREAM_UNIX_ATTRIBUTES) {
359 attr_file_index = file_index; /* remember attribute file_index */
360 len = strlen(fd->msg);
361 attr = &fd->msg[len+1];
362 decode_stat(attr, &statf); /* decode file stat packet */
364 jcr->fn_printed = FALSE;
365 strcpy(jcr->fname, fname); /* move filename into JCR */
367 Dmsg2(11, "dird<filed: stream=%d %s\n", stream, jcr->fname);
368 Dmsg1(20, "dird<filed: attr=%s\n", attr);
371 * Find equivalent record in the database
374 db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr);
376 if (fdbr.FileId == 0) {
377 Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
378 Dmsg1(20, _("File not in catalog: %s\n"), jcr->fname);
379 stat = JS_Differences;
383 * mark file record as visited by stuffing the
384 * current JobId, which is unique, into the FileIndex
386 db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
389 Dmsg2(20, "Found %s in catalog. Opts=%s\n", jcr->fname, Opts_MD5);
390 decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
391 strip_trailing_junk(jcr->fname);
393 * Loop over options supplied by user and verify the
394 * fields he requests.
396 for (p=Opts_MD5; *p; p++) {
398 case 'i': /* compare INODEs */
399 if (statc.st_ino != statf.st_ino) {
401 Jmsg(jcr, M_INFO, 0, _(" st_ino differ. Cat: %x File: %x\n"),
402 statc.st_ino, statf.st_ino);
403 stat = JS_Differences;
406 case 'p': /* permissions bits */
407 if (statc.st_mode != statf.st_mode) {
409 Jmsg(jcr, M_INFO, 0, _(" st_mode differ. Cat: %x File: %x\n"),
410 statc.st_mode, statf.st_mode);
411 stat = JS_Differences;
414 case 'n': /* number of links */
415 if (statc.st_nlink != statf.st_nlink) {
417 Jmsg(jcr, M_INFO, 0, _(" st_nlink differ. Cat: %d File: %d\n"),
418 statc.st_nlink, statf.st_nlink);
419 stat = JS_Differences;
422 case 'u': /* user id */
423 if (statc.st_uid != statf.st_uid) {
425 Jmsg(jcr, M_INFO, 0, _(" st_uid differ. Cat: %d File: %d\n"),
426 statc.st_uid, statf.st_uid);
427 stat = JS_Differences;
430 case 'g': /* group id */
431 if (statc.st_gid != statf.st_gid) {
433 Jmsg(jcr, M_INFO, 0, _(" st_gid differ. Cat: %d File: %d\n"),
434 statc.st_gid, statf.st_gid);
435 stat = JS_Differences;
439 if (statc.st_size != statf.st_size) {
441 Jmsg(jcr, M_INFO, 0, _(" st_size differ. Cat: %d File: %d\n"),
442 statc.st_size, statf.st_size);
443 stat = JS_Differences;
446 case 'a': /* access time */
447 if (statc.st_atime != statf.st_atime) {
449 Jmsg(jcr, M_INFO, 0, _(" st_atime differs\n"));
450 stat = JS_Differences;
454 if (statc.st_mtime != statf.st_mtime) {
456 Jmsg(jcr, M_INFO, 0, _(" st_mtime differs\n"));
457 stat = JS_Differences;
460 case 'c': /* ctime */
461 if (statc.st_ctime != statf.st_ctime) {
463 Jmsg(jcr, M_INFO, 0, _(" st_ctime differs\n"));
464 stat = JS_Differences;
467 case 'd': /* file size decrease */
468 if (statc.st_size > statf.st_size) {
470 Jmsg(jcr, M_INFO, 0, _(" st_size decrease. Cat: %d File: %d\n"),
471 statc.st_size, statf.st_size);
472 stat = JS_Differences;
475 case '5': /* compare MD5 */
485 * Got MD5 Signature from Storage daemon
486 * It came across in the Opts_MD5 field.
488 } else if (stream == STREAM_MD5_SIGNATURE) {
489 if (attr_file_index != file_index) {
490 Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
491 file_index, attr_file_index);
492 jcr->JobStatus = JS_ErrorTerminated;
495 db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
496 if (strcmp(buf, fdbr.MD5) != 0) {
498 if (debug_level >= 10) {
499 Jmsg(jcr, M_INFO, 0, _(" MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
501 Jmsg(jcr, M_INFO, 0, _(" MD5 differs.\n"));
503 stat = JS_Differences;
507 jcr->jr.JobFiles = file_index;
511 Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
513 jcr->JobStatus = JS_ErrorTerminated;
517 /* Now find all the files that are missing -- i.e. all files in
518 * the database where the FileIndex != current JobId
520 jcr->fn_printed = FALSE;
522 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
523 "WHERE File.JobId=%d "
524 "AND File.FileIndex!=%d AND File.PathId=Path.PathId "
525 "AND File.FilenameId=Filename.FilenameId",
526 last_full_id, jcr->JobId);
527 /* missing_handler is called for each file found */
528 db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
529 if (jcr->fn_printed) {
530 stat = JS_Differences;
532 jcr->JobStatus = stat;
537 * We are called here for each record that matches the above
538 * SQL query -- that is for each file contained in the Catalog
539 * that was not marked earlier. This means that the file in
540 * question is a missing file (in the Catalog but on on Disk).
542 static int missing_handler(void *ctx, int num_fields, char **row)
544 JCR *jcr = (JCR *)ctx;
546 if (!jcr->fn_printed) {
547 Jmsg(jcr, M_INFO, 0, "\n");
548 Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
549 jcr->fn_printed = TRUE;
551 Jmsg(jcr, M_INFO, 0, " %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
557 * Print filename for verify
559 static void prt_fname(JCR *jcr)
561 if (!jcr->fn_printed) {
562 Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
563 jcr->fn_printed = TRUE;