]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/verify.c
e4b9f623903f489a9cd1817627c2df221431866d
[bacula/bacula] / bacula / src / dird / verify.c
1 /*
2  *
3  *   Bacula Director -- verify.c -- responsible for running file verification
4  *
5  *     Kern Sibbald, October MM
6  *
7  *    This routine is run as a separate thread.  There may be more
8  *    work to be done to make it totally reentrant!!!!
9  * 
10  * Current implementation is Catalog verification only (i.e. no
11  *  verification versus tape).
12  *
13  *  Basic tasks done here:
14  *     Open DB
15  *     Open connection with File daemon and pass him commands
16  *       to do the verify.
17  *     When the File daemon sends the attributes, compare them to
18  *       what is in the DB.
19  *
20  *   Version $Id$
21  */
22
23 /*
24    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
25
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.
30
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.
35
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,
39    MA 02111-1307, USA.
40
41  */
42
43 #include "bacula.h"
44 #include "dird.h"
45
46 /* Imported Global Variables */
47 extern int debug_level;
48
49 /* Commands sent to File daemon */
50 static char verifycmd[]   = "verify";
51 static char levelcmd[]    = "level = %s%s\n";
52
53 /* Responses received from File daemon */
54 static char OKverify[]   = "2000 OK verify\n";
55 static char OKlevel[]    = "2000 OK level\n";
56
57 /* Forward referenced functions */
58 static void verify_cleanup(JCR *jcr, int TermCode);
59 static void prt_fname(JCR *jcr);
60 static int missing_handler(void *ctx, int num_fields, char **row);
61
62 /* 
63  * Do a verification of the specified files
64  *    
65  *  Returns:  0 on failure
66  *            1 on success
67  */
68 int do_verify(JCR *jcr) 
69 {
70    char *level;
71    BSOCK   *fd;
72    JOB_DBR jr;
73    int last_full_id;
74
75    if (!get_or_create_client_record(jcr)) {
76       goto bail_out;
77    }
78
79    Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
80
81    /* If we are doing a verify from the catalog,
82     * we must look up the time and date of the
83     * last full verify.
84     */
85    if (jcr->level == L_VERIFY_CATALOG) {
86       memcpy(&jr, &(jcr->jr), sizeof(jr));
87       if (!db_find_last_full_verify(jcr->db, &jr)) {
88          Jmsg(jcr, M_FATAL, 0, _("Unable to find last full verify. %s"),
89             db_strerror(jcr->db));
90          goto bail_out;
91       }
92       last_full_id = jr.JobId;
93       Dmsg1(20, "Last full id=%d\n", last_full_id);
94    }
95
96    jcr->jr.JobId = jcr->JobId;
97    jcr->jr.StartTime = jcr->start_time;
98    jcr->jr.Level = jcr->level;
99    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
100       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
101       goto bail_out;
102    }
103
104    if (!jcr->fname) {
105       jcr->fname = (char *) get_pool_memory(PM_FNAME);
106    }
107
108    jcr->jr.JobId = last_full_id;      /* save last full id */
109
110    /* Print Job Start message */
111    Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
112       jcr->JobId, jcr->Job);
113
114    if (jcr->level == L_VERIFY_CATALOG) {
115       memset(&jr, 0, sizeof(jr));
116       jr.JobId = last_full_id;
117       if (!db_get_job_record(jcr->db, &jr)) {
118          Jmsg(jcr, M_ERROR, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
119          goto bail_out;
120       }
121       Jmsg(jcr, M_INFO, 0, _("Verifying against Init JobId %d run %s\n"),
122          last_full_id, jr.cStartTime); 
123    }
124
125    /*
126     * OK, now connect to the File daemon
127     *  and ask him for the files.
128     */
129    jcr->sd_auth_key = bstrdup("dummy");    /* dummy Storage daemon key */
130    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
131       goto bail_out;
132    }
133
134    fd = jcr->file_bsock;
135
136    Dmsg0(30, ">filed: Send include list\n");
137    if (!send_include_list(jcr)) {
138       goto bail_out;
139    }
140
141    Dmsg0(30, ">filed: Send exclude list\n");
142    if (!send_exclude_list(jcr)) {
143       goto bail_out;
144    }
145
146    /* 
147     * Send Level command to File daemon
148     *
149     */
150    switch (jcr->level) {
151       case L_VERIFY_INIT:
152          level = "init";
153          break;
154       case L_VERIFY_CATALOG:
155          level = "catalog";
156          break;
157       case L_VERIFY_VOLUME:
158          level = "volume";
159          break;
160       case L_VERIFY_DATA:
161          level = "data";
162          break;
163       default:
164          Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
165          goto bail_out;
166    }
167    Dmsg1(20, ">filed: %s", fd->msg);
168    bnet_fsend(fd, levelcmd, level, " ");
169    if (!response(fd, OKlevel, "Level")) {
170       goto bail_out;
171    }
172
173    /* 
174     * Send verify command to File daemon
175     */
176    bnet_fsend(fd, verifycmd);
177    if (!response(fd, OKverify, "Verify")) {
178       goto bail_out;
179    }
180
181    /*
182     * Now get data back from File daemon and
183     *  compare it to the catalog or store it in the
184     *  catalog depending on the run type.
185     */
186    /* Compare to catalog */
187    switch (jcr->level) { 
188    case L_VERIFY_CATALOG:
189       Dmsg0(10, "Verify level=catalog\n");
190       get_attributes_and_compare_to_catalog(jcr, last_full_id);
191       break;
192
193    case L_VERIFY_INIT:
194       /* Build catalog */
195       Dmsg0(10, "Verify level=init\n");
196       get_attributes_and_put_in_catalog(jcr);
197       break;
198
199    default:
200       Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
201       goto bail_out;
202    }
203
204    verify_cleanup(jcr, JS_Terminated);
205    return 1;
206
207 bail_out:
208    verify_cleanup(jcr, JS_Terminated);
209    return 0;
210 }
211
212 /*
213  * Release resources allocated during backup.
214  *
215  */
216 static void verify_cleanup(JCR *jcr, int TermCode)
217 {
218    char sdt[50], edt[50];
219    char ec1[30];
220    char term_code[100];
221    char *term_msg;
222    int msg_type;
223    int last_full_id;
224
225    Dmsg0(100, "Enter verify_cleanup()\n");
226
227    last_full_id = jcr->jr.JobId;
228    jcr->JobStatus = TermCode;
229
230    update_job_end_record(jcr);
231
232    msg_type = M_INFO;                 /* by default INFO message */
233    switch (TermCode) {
234       case JS_Terminated:
235          term_msg = _("Verify OK");
236          break;
237       case JS_ErrorTerminated:
238          term_msg = _("*** Verify Error ***"); 
239          msg_type = M_ERROR;          /* Generate error message */
240          break;
241       case JS_Cancelled:
242          term_msg = _("Verify Cancelled");
243          break;
244       case JS_Differences:
245          term_msg = _("Verify Differences");
246          break;
247       default:
248          term_msg = term_code;
249          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
250          break;
251    }
252    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
253    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
254
255    Jmsg(jcr, msg_type, 0, _("%s\n\
256 JobId:                  %d\n\
257 Job:                    %s\n\
258 FileSet:                %s\n\
259 Verify Level:           %s\n\
260 Client:                 %s\n\
261 Start time:             %s\n\
262 End time:               %s\n\
263 Files Examined:         %s\n\
264 Termination:            %s\n\n"),
265         edt,
266         jcr->jr.JobId,
267         jcr->jr.Job,
268         jcr->fileset->hdr.name,
269         level_to_str(jcr->level),
270         jcr->client->hdr.name,
271         sdt,
272         edt,
273         edit_uint64_with_commas(jcr->jr.JobFiles, ec1),
274         term_msg);
275
276    Dmsg0(100, "Leave verify_cleanup()\n");
277    if (jcr->fname) {
278       free_memory(jcr->fname);
279       jcr->fname = NULL;
280    }
281 }
282
283 /*
284  * This routine is called only during a Verify
285  */
286 int get_attributes_and_compare_to_catalog(JCR *jcr, int last_full_id)
287 {
288    BSOCK   *fd;
289    int n, len;
290    FILE_DBR fdbr;
291    struct stat statf;                 /* file stat */
292    struct stat statc;                 /* catalog stat */
293    int stat = JS_Terminated;
294    char buf[MAXSTRING];
295    POOLMEM *fname = get_pool_memory(PM_MESSAGE);
296    int do_MD5 = FALSE;
297
298    memset(&fdbr, 0, sizeof(FILE_DBR));
299    fd = jcr->file_bsock;
300    fdbr.JobId = last_full_id;
301    
302    Dmsg0(20, "bdird: waiting to receive file attributes\n");
303    /*
304     * Get Attributes and MD5 Signature from File daemon
305     */
306    while ((n=bget_msg(fd, 0)) > 0) {
307       long file_index, attr_file_index;
308       int stream;
309       char *attr, *p;
310       char Opts_MD5[MAXSTRING];        /* Verify Opts or MD5 signature */
311
312       fname = check_pool_memory_size(fname, fd->msglen);
313       jcr->fname = check_pool_memory_size(jcr->fname, fd->msglen);
314       Dmsg1(50, "Atts+MD5=%s\n", fd->msg);
315       if ((len = sscanf(fd->msg, "%ld %d %100s %s", &file_index, &stream, 
316             Opts_MD5, fname)) != 4) {
317          Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 4 fields got %d\n\
318  mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
319          goto bail_out;
320       }
321       /*
322        * Got attributes stream, decode it
323        */
324       if (stream == STREAM_UNIX_ATTRIBUTES) {
325          jcr->jr.JobFiles++;
326          attr_file_index = file_index;    /* remember attribute file_index */
327          len = strlen(fd->msg);
328          attr = &fd->msg[len+1];
329          decode_stat(attr, &statf);  /* decode file stat packet */
330          do_MD5 = FALSE;
331          jcr->fn_printed = FALSE;
332          strip_trailing_junk(fname);
333          strcpy(jcr->fname, fname);  /* move filename into JCR */
334
335          Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
336          Dmsg1(20, "dird<filed: attr=%s\n", attr);
337
338          /* 
339           * Find equivalent record in the database 
340           */
341          fdbr.FileId = 0;
342          if (!db_get_file_attributes_record(jcr->db, jcr->fname, &fdbr)) {
343             Jmsg(jcr, M_INFO, 0, _("New file: %s\n"), jcr->fname);
344             Dmsg1(020, _("File not in catalog: %s\n"), jcr->fname);
345             stat = JS_Differences;
346             continue;
347          } else {
348             /* 
349              * mark file record as visited by stuffing the
350              * current JobId, which is unique, into the FileIndex
351              */
352             db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
353          }
354
355          Dmsg3(100, "Found %s in catalog. inx=%d Opts=%s\n", jcr->fname, 
356             file_index, Opts_MD5);
357          decode_stat(fdbr.LStat, &statc); /* decode catalog stat */
358          /*
359           * Loop over options supplied by user and verify the
360           * fields he requests.
361           */
362          for (p=Opts_MD5; *p; p++) {
363             switch (*p) {
364             case 'i':                /* compare INODEs */
365                if (statc.st_ino != statf.st_ino) {
366                   prt_fname(jcr);
367                   Jmsg(jcr, M_INFO, 0, _("      st_ino   differ. Cat: %x File: %x\n"), 
368                      statc.st_ino, statf.st_ino);
369                   stat = JS_Differences;
370                }
371                break;
372             case 'p':                /* permissions bits */
373                if (statc.st_mode != statf.st_mode) {
374                   prt_fname(jcr);
375                   Jmsg(jcr, M_INFO, 0, _("      st_mode  differ. Cat: %x File: %x\n"), 
376                      statc.st_mode, statf.st_mode);
377                   stat = JS_Differences;
378                }
379                break;
380             case 'n':                /* number of links */
381                if (statc.st_nlink != statf.st_nlink) {
382                   prt_fname(jcr);
383                   Jmsg(jcr, M_INFO, 0, _("      st_nlink differ. Cat: %d File: %d\n"), 
384                      statc.st_nlink, statf.st_nlink);
385                   stat = JS_Differences;
386                }
387                break;
388             case 'u':                /* user id */
389                if (statc.st_uid != statf.st_uid) {
390                   prt_fname(jcr);
391                   Jmsg(jcr, M_INFO, 0, _("      st_uid   differ. Cat: %d File: %d\n"), 
392                      statc.st_uid, statf.st_uid);
393                   stat = JS_Differences;
394                }
395                break;
396             case 'g':                /* group id */
397                if (statc.st_gid != statf.st_gid) {
398                   prt_fname(jcr);
399                   Jmsg(jcr, M_INFO, 0, _("      st_gid   differ. Cat: %d File: %d\n"), 
400                      statc.st_gid, statf.st_gid);
401                   stat = JS_Differences;
402                }
403                break;
404             case 's':                /* size */
405                if (statc.st_size != statf.st_size) {
406                   prt_fname(jcr);
407                   Jmsg(jcr, M_INFO, 0, _("      st_size  differ. Cat: %d File: %d\n"), 
408                      statc.st_size, statf.st_size);
409                   stat = JS_Differences;
410                }
411                break;
412             case 'a':                /* access time */
413                if (statc.st_atime != statf.st_atime) {
414                   prt_fname(jcr);
415                   Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
416                   stat = JS_Differences;
417                }
418                break;
419             case 'm':
420                if (statc.st_mtime != statf.st_mtime) {
421                   prt_fname(jcr);
422                   Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
423                   stat = JS_Differences;
424                }
425                break;
426             case 'c':                /* ctime */
427                if (statc.st_ctime != statf.st_ctime) {
428                   prt_fname(jcr);
429                   Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
430                   stat = JS_Differences;
431                }
432                break;
433             case 'd':                /* file size decrease */
434                if (statc.st_size > statf.st_size) {
435                   prt_fname(jcr);
436                   Jmsg(jcr, M_INFO, 0, _("      st_size  decrease. Cat: %d File: %d\n"), 
437                      statc.st_size, statf.st_size);
438                   stat = JS_Differences;
439                }
440                break;
441             case '5':                /* compare MD5 */
442                Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
443                do_MD5 = TRUE;
444                break;
445             case ':':
446             case 'V':
447             default:
448                break;
449             }
450          }
451       /*
452        * Got MD5 Signature from Storage daemon
453        *  It came across in the Opts_MD5 field.
454        */
455       } else if (stream == STREAM_MD5_SIGNATURE) {
456          Dmsg2(100, "stream=MD5 inx=%d fname=%s\n", file_index, jcr->fname);
457          /* 
458           * When ever we get an MD5 signature is MUST have been
459           * preceded by an attributes record, which sets attr_file_index
460           */
461          if (attr_file_index != file_index) {
462             Jmsg2(jcr, M_FATAL, 0, _("MD5 index %d not same as attributes %d\n"),
463                file_index, attr_file_index);
464             goto bail_out;
465          } 
466          if (do_MD5) {
467             db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
468             if (strcmp(buf, fdbr.MD5) != 0) {
469                prt_fname(jcr);
470                if (debug_level >= 10) {
471                   Jmsg(jcr, M_INFO, 0, _("      MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
472                } else {
473                   Jmsg(jcr, M_INFO, 0, _("      MD5 differs.\n"));
474                }
475                stat = JS_Differences;
476             }
477             do_MD5 = FALSE;
478          }
479       }
480       jcr->jr.JobFiles = file_index;
481    } 
482    if (n < 0) {
483       Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
484                         n, strerror(errno));
485       goto bail_out;
486    }
487
488    /* Now find all the files that are missing -- i.e. all files in
489     *  the database where the FileIndex != current JobId
490     */
491    jcr->fn_printed = FALSE;
492    sprintf(buf, 
493 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
494 "WHERE File.JobId=%d "
495 "AND File.FileIndex!=%d AND File.PathId=Path.PathId "
496 "AND File.FilenameId=Filename.FilenameId", 
497       last_full_id, jcr->JobId);
498    /* missing_handler is called for each file found */
499    db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
500    if (jcr->fn_printed) {
501       stat = JS_Differences;
502    }
503    free_pool_memory(fname);
504    jcr->JobStatus = stat;
505    return 1;
506     
507 bail_out:
508    free_pool_memory(fname);
509    jcr->JobStatus = JS_ErrorTerminated;
510    return 0;
511 }
512
513 /*
514  * We are called here for each record that matches the above
515  *  SQL query -- that is for each file contained in the Catalog
516  *  that was not marked earlier. This means that the file in
517  *  question is a missing file (in the Catalog but on on Disk).
518  */
519 static int missing_handler(void *ctx, int num_fields, char **row)
520 {
521    JCR *jcr = (JCR *)ctx;
522
523    if (!jcr->fn_printed) {
524       Jmsg(jcr, M_INFO, 0, "\n");
525       Jmsg(jcr, M_INFO, 0, _("The following files are missing:\n"));
526       jcr->fn_printed = TRUE;
527    }
528    Jmsg(jcr, M_INFO, 0, "      %s%s\n", row[0]?row[0]:"", row[1]?row[1]:"");
529    return 0;
530 }
531
532
533 /* 
534  * Print filename for verify
535  */
536 static void prt_fname(JCR *jcr)
537 {
538    if (!jcr->fn_printed) {
539       Jmsg(jcr, M_INFO, 0, _("File: %s\n"), jcr->fname);
540       jcr->fn_printed = TRUE;
541    }
542 }