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