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";
52 /* Responses received from File daemon */
53 static char OKverify[] = "2000 OK verify\n";
55 /* Forward referenced functions */
56 static void verify_cleanup(JCR *jcr, int TermCode);
57 static void prt_fname(JCR *jcr);
58 static int missing_handler(void *ctx, int num_fields, char **row);
61 * Do a verification of the specified files
63 * Returns: 0 on failure
66 int do_verify(JCR *jcr)
73 if (!get_or_create_client_record(jcr)) {
77 Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
79 /* If we are doing a verify from the catalog,
80 * we must look up the time and date of the
83 if (jcr->JobLevel == L_VERIFY_CATALOG) {
84 memcpy(&jr, &(jcr->jr), sizeof(jr));
85 if (!db_find_last_full_verify(jcr->db, &jr)) {
86 Jmsg(jcr, M_FATAL, 0, _("Unable to find last full verify. %s"),
87 db_strerror(jcr->db));
90 last_full_id = jr.JobId;
91 Dmsg1(20, "Last full id=%d\n", last_full_id);
94 jcr->jr.JobId = jcr->JobId;
95 jcr->jr.StartTime = jcr->start_time;
96 jcr->jr.Level = jcr->JobLevel;
97 if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
98 Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
103 jcr->fname = (char *) get_pool_memory(PM_FNAME);
106 jcr->jr.JobId = last_full_id; /* save last full id */
108 /* Print Job Start message */
109 Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
110 jcr->JobId, jcr->Job);
112 if (jcr->JobLevel == L_VERIFY_CATALOG) {
113 memset(&jr, 0, sizeof(jr));
114 jr.JobId = last_full_id;
115 if (!db_get_job_record(jcr->db, &jr)) {
116 Jmsg(jcr, M_ERROR, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
119 Jmsg(jcr, M_INFO, 0, _("Verifying against Init JobId %d run %s\n"),
120 last_full_id, jr.cStartTime);
124 * OK, now connect to the File daemon
125 * and ask him for the files.
127 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
128 if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
132 fd = jcr->file_bsock;
134 Dmsg0(30, ">filed: Send include list\n");
135 if (!send_include_list(jcr)) {
139 Dmsg0(30, ">filed: Send exclude list\n");
140 if (!send_exclude_list(jcr)) {
145 * Send Level command to File daemon
148 switch (jcr->JobLevel) {
152 case L_VERIFY_CATALOG:
155 case L_VERIFY_VOLUME:
162 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->JobLevel);
167 * Send verify command/level to File daemon
169 bnet_fsend(fd, verifycmd, level);
170 if (!response(fd, OKverify, "Verify")) {
175 * Now get data back from File daemon and
176 * compare it to the catalog or store it in the
177 * catalog depending on the run type.
179 /* Compare to catalog */
180 switch (jcr->JobLevel) {
181 case L_VERIFY_CATALOG:
182 Dmsg0(10, "Verify level=catalog\n");
183 get_attributes_and_compare_to_catalog(jcr, last_full_id);
188 Dmsg0(10, "Verify level=init\n");
189 get_attributes_and_put_in_catalog(jcr);
193 Jmsg1(jcr, M_FATAL, 0, _("Unimplemented verify level %d\n"), jcr->JobLevel);
197 verify_cleanup(jcr, jcr->JobStatus);
201 verify_cleanup(jcr, JS_ErrorTerminated);
206 * Release resources allocated during backup.
209 static void verify_cleanup(JCR *jcr, int TermCode)
211 char sdt[50], edt[50];
218 Dmsg0(100, "Enter verify_cleanup()\n");
220 last_full_id = jcr->jr.JobId;
221 jcr->JobStatus = TermCode;
223 update_job_end_record(jcr);
225 msg_type = M_INFO; /* by default INFO message */
228 term_msg = _("Verify OK");
230 case JS_ErrorTerminated:
231 term_msg = _("*** Verify Error ***");
232 msg_type = M_ERROR; /* Generate error message */
235 term_msg = _("Verify Cancelled");
238 term_msg = _("Verify Differences");
241 term_msg = term_code;
242 sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
245 bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
246 bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
248 Jmsg(jcr, msg_type, 0, _("%s\n\
256 Files Examined: %s\n\
257 Termination: %s\n\n"),
261 jcr->fileset->hdr.name,
262 level_to_str(jcr->JobLevel),
263 jcr->client->hdr.name,
266 edit_uint64_with_commas(jcr->JobFiles, ec1),
269 Dmsg0(100, "Leave verify_cleanup()\n");
271 free_memory(jcr->fname);
277 * This routine is called only during a Verify
279 int get_attributes_and_compare_to_catalog(JCR *jcr, int last_full_id)
284 struct stat statf; /* file stat */
285 struct stat statc; /* catalog stat */
286 int stat = JS_Terminated;
288 POOLMEM *fname = get_pool_memory(PM_MESSAGE);
291 memset(&fdbr, 0, sizeof(FILE_DBR));
292 fd = jcr->file_bsock;
293 fdbr.JobId = last_full_id;
295 Dmsg0(20, "bdird: waiting to receive file attributes\n");
297 * Get Attributes and MD5 Signature from File daemon
299 while ((n=bget_msg(fd, 0)) > 0) {
300 long file_index, attr_file_index;
303 char Opts_MD5[MAXSTRING]; /* Verify Opts or MD5 signature */
305 fname = check_pool_memory_size(fname, fd->msglen);
306 jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
307 Dmsg1(50, "Atts+MD5=%s\n", fd->msg);
308 if ((len = sscanf(fd->msg, "%ld %d %100s", &file_index, &stream,
310 Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 3 fields got %d\n\
311 mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
315 skip_nonspaces(&p); /* skip FileIndex */
317 skip_nonspaces(&p); /* skip Stream */
319 skip_nonspaces(&p); /* skip Opts_MD5 */
320 p++; /* skip space */
323 *fn++ = *p++; /* copy filename */
325 *fn = *p++; /* term filename and point to attribs */
328 * Got attributes stream, decode it
330 if (stream == STREAM_UNIX_ATTRIBUTES) {
332 attr_file_index = file_index; /* remember attribute file_index */
333 decode_stat(attr, &statf); /* decode file stat packet */
335 jcr->fn_printed = FALSE;
336 strip_trailing_junk(fname);
337 strcpy(jcr->fname, fname); /* move filename into JCR */
339 Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
340 Dmsg1(20, "dird<filed: attr=%s\n", attr);
343 * Find equivalent record in the database
346 if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
347 Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
348 Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
349 stat = JS_Differences;
353 * mark file record as visited by stuffing the
354 * current JobId, which is unique, into the FileIndex
356 db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
359 Dmsg3(100, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname,
360 file_index, Opts_MD5);
361 decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
363 * Loop over options supplied by user and verify the
364 * fields he requests.
366 for (p=Opts_MD5; *p; p++) {
368 case 'i': /* compare INODEs */
369 if (statc.st_ino != statf.st_ino) {
371 Jmsg(jcr, M_INFO, 0, _(" st_ino differ. Cat: %x File: %x\n"),
372 statc.st_ino, statf.st_ino);
373 stat = JS_Differences;
376 case 'p': /* permissions bits */
377 if (statc.st_mode != statf.st_mode) {
379 Jmsg(jcr, M_INFO, 0, _(" st_mode differ. Cat: %x File: %x\n"),
380 statc.st_mode, statf.st_mode);
381 stat = JS_Differences;
384 case 'n': /* number of links */
385 if (statc.st_nlink != statf.st_nlink) {
387 Jmsg(jcr, M_INFO, 0, _(" st_nlink differ. Cat: %d File: %d\n"),
388 statc.st_nlink, statf.st_nlink);
389 stat = JS_Differences;
392 case 'u': /* user id */
393 if (statc.st_uid != statf.st_uid) {
395 Jmsg(jcr, M_INFO, 0, _(" st_uid differ. Cat: %d File: %d\n"),
396 statc.st_uid, statf.st_uid);
397 stat = JS_Differences;
400 case 'g': /* group id */
401 if (statc.st_gid != statf.st_gid) {
403 Jmsg(jcr, M_INFO, 0, _(" st_gid differ. Cat: %d File: %d\n"),
404 statc.st_gid, statf.st_gid);
405 stat = JS_Differences;
409 if (statc.st_size != statf.st_size) {
411 Jmsg(jcr, M_INFO, 0, _(" st_size differ. Cat: %d File: %d\n"),
412 statc.st_size, statf.st_size);
413 stat = JS_Differences;
416 case 'a': /* access time */
417 if (statc.st_atime != statf.st_atime) {
419 Jmsg(jcr, M_INFO, 0, _(" st_atime differs\n"));
420 stat = JS_Differences;
424 if (statc.st_mtime != statf.st_mtime) {
426 Jmsg(jcr, M_INFO, 0, _(" st_mtime differs\n"));
427 stat = JS_Differences;
430 case 'c': /* ctime */
431 if (statc.st_ctime != statf.st_ctime) {
433 Jmsg(jcr, M_INFO, 0, _(" st_ctime differs\n"));
434 stat = JS_Differences;
437 case 'd': /* file size decrease */
438 if (statc.st_size > statf.st_size) {
440 Jmsg(jcr, M_INFO, 0, _(" st_size decrease. Cat: %d File: %d\n"),
441 statc.st_size, statf.st_size);
442 stat = JS_Differences;
445 case '5': /* compare MD5 */
446 Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
456 * Got MD5 Signature from Storage daemon
457 * It came across in the Opts_MD5 field.
459 } else if (stream == STREAM_MD5_SIGNATURE) {
460 Dmsg2(100, "stream=MD5 inx=%d fname=%s\n", file_index, jcr->fname);
462 * When ever we get an MD5 signature is MUST have been
463 * preceded by an attributes record, which sets attr_file_index
465 if (attr_file_index != file_index) {
466 Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
467 file_index, attr_file_index);
471 db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
472 if (strcmp(buf, fdbr.MD5) != 0) {
474 if (debug_level >= 10) {
475 Jmsg(jcr, M_INFO, 0, _(" MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
477 Jmsg(jcr, M_INFO, 0, _(" MD5 differs.\n"));
479 stat = JS_Differences;
484 jcr->JobFiles = file_index;
487 Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
492 /* Now find all the files that are missing -- i.e. all files in
493 * the database where the FileIndex != current JobId
495 jcr->fn_printed = FALSE;
497 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
498 "WHERE File.JobId=%d "
499 "AND File.FileIndex!=%d AND File.PathId=Path.PathId "
500 "AND File.FilenameId=Filename.FilenameId",
501 last_full_id, jcr->JobId);
502 /* missing_handler is called for each file found */
503 db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
504 if (jcr->fn_printed) {
505 stat = JS_Differences;
507 free_pool_memory(fname);
508 jcr->JobStatus = stat;
512 free_pool_memory(fname);
513 jcr->JobStatus = JS_ErrorTerminated;
518 * We are called here for each record that matches the above
519 * SQL query -- that is for each file contained in the Catalog
520 * that was not marked earlier. This means that the file in
521 * question is a missing file (in the Catalog but on on Disk).
523 static int missing_handler(void *ctx, int num_fields, char **row)
525 JCR *jcr = (JCR *)ctx;
527 if (!jcr->fn_printed) {
528 Jmsg(jcr, M_INFO, 0, "\n");
529 Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
530 jcr->fn_printed = TRUE;
532 Jmsg(jcr, M_INFO, 0, " %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
538 * Print filename for verify
540 static void prt_fname(JCR *jcr)
542 if (!jcr->fn_printed) {
543 Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
544 jcr->fn_printed = TRUE;