]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/verify.c
Implement Auto Prune and Auto Recycle
[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       verify_cleanup(jcr, JS_ErrorTerminated);                    
77       return 0;
78    }
79
80    Dmsg1(9, "bdird: created client %s record\n", jcr->client->hdr.name);
81
82    /* If we are doing a verify from the catalog,
83     * we must look up the time and date of the
84     * last full verify.
85     */
86    if (jcr->level == L_VERIFY_CATALOG) {
87       memcpy(&jr, &(jcr->jr), sizeof(jr));
88       if (!db_find_last_full_verify(jcr->db, &jr)) {
89          Jmsg(jcr, M_FATAL, 0, _("Unable to find last full verify. %s"),
90             db_strerror(jcr->db));
91          verify_cleanup(jcr, JS_ErrorTerminated);                    
92          return 0;
93       }
94       last_full_id = jr.JobId;
95       Dmsg1(20, "Last full id=%d\n", last_full_id);
96    }
97
98    jcr->jr.JobId = jcr->JobId;
99    jcr->jr.StartTime = jcr->start_time;
100    jcr->jr.Level = jcr->level;
101    if (!db_update_job_start_record(jcr->db, &jcr->jr)) {
102       Jmsg(jcr, M_ERROR, 0, "%s", db_strerror(jcr->db));
103       verify_cleanup(jcr, JS_ErrorTerminated);                    
104       return 0;
105    }
106
107    if (!jcr->fname) {
108       jcr->fname = (char *) get_pool_memory(PM_FNAME);
109    }
110
111    jcr->jr.JobId = last_full_id;      /* save last full id */
112
113    /* Print Job Start message */
114    Jmsg(jcr, M_INFO, 0, _("Start Verify JobId %d Job=%s\n"),
115       jcr->JobId, jcr->Job);
116
117    if (jcr->level == L_VERIFY_CATALOG) {
118       memset(&jr, 0, sizeof(jr));
119       jr.JobId = last_full_id;
120       if (!db_get_job_record(jcr->db, &jr)) {
121          Jmsg(jcr, M_ERROR, 0, _("Could not get job record. %s"), db_strerror(jcr->db));
122          verify_cleanup(jcr, JS_ErrorTerminated);                    
123          return 0;
124       }
125       Jmsg(jcr, M_INFO, 0, _("Verifying against Init JobId %d run %s\n"),
126          last_full_id, jr.cStartTime); 
127    }
128
129    /*
130     * OK, now connect to the File daemon
131     *  and ask him for the files.
132     */
133    jcr->sd_auth_key = bstrdup("dummy");    /* dummy Storage daemon key */
134    if (!connect_to_file_daemon(jcr, 10, FDConnectTimeout, 1)) {
135       verify_cleanup(jcr, JS_ErrorTerminated);                    
136       return 0;
137    }
138
139    fd = jcr->file_bsock;
140
141    Dmsg0(30, ">filed: Send include list\n");
142    if (!send_include_list(jcr)) {
143       verify_cleanup(jcr, JS_ErrorTerminated);                    
144       return 0;
145    }
146
147    Dmsg0(30, ">filed: Send exclude list\n");
148    if (!send_exclude_list(jcr)) {
149       verify_cleanup(jcr, JS_ErrorTerminated);                    
150       return 0;
151    }
152
153    /* 
154     * Send Level command to File daemon
155     *
156     */
157    switch (jcr->level) {
158       case L_VERIFY_INIT:
159          level = "init";
160          break;
161       case L_VERIFY_CATALOG:
162          level = "catalog";
163          break;
164       case L_VERIFY_VOLUME:
165          level = "volume";
166          break;
167       case L_VERIFY_DATA:
168          level = "data";
169          break;
170       default:
171          Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
172          verify_cleanup(jcr, JS_ErrorTerminated);                    
173          return 0;
174    }
175    Dmsg1(20, ">filed: %s", fd->msg);
176    bnet_fsend(fd, levelcmd, level, " ");
177    if (!response(fd, OKlevel, "Level")) {
178       verify_cleanup(jcr, JS_ErrorTerminated);                    
179       return 0;
180    }
181
182    /* 
183     * Send verify command to File daemon
184     */
185    bnet_fsend(fd, verifycmd);
186    if (!response(fd, OKverify, "Verify")) {
187       verify_cleanup(jcr, JS_ErrorTerminated);                    
188       return 0;
189    }
190
191    /*
192     * Now get data back from File daemon and
193     *  compare it to the catalog or store it in the
194     *  catalog depending on the run type.
195     */
196    /* Compare to catalog */
197    if (jcr->level == L_VERIFY_CATALOG) {
198       Dmsg0(10, "Verify level=catalog\n");
199       get_attributes_and_compare_to_catalog(jcr, last_full_id);
200
201    /* Build catalog */
202    } else if (jcr->level == L_VERIFY_INIT) {
203       Dmsg0(10, "Verify level=init\n");
204       get_attributes_and_put_in_catalog(jcr);
205
206    } else {
207       Jmsg1(jcr, M_FATAL, 0, _("Unimplemented save level %d\n"), jcr->level);
208       verify_cleanup(jcr, JS_ErrorTerminated);                    
209       return 0;
210    }
211
212    verify_cleanup(jcr, JS_Terminated);
213    return 1;
214 }
215
216 /*
217  * Release resources allocated during backup.
218  *
219  */
220 static void verify_cleanup(JCR *jcr, int TermCode)
221 {
222    char sdt[50], edt[50];
223    char ec1[30];
224    char term_code[100];
225    char *term_msg;
226    int msg_type;
227    int last_full_id;
228
229    Dmsg0(100, "Enter verify_cleanup()\n");
230
231    last_full_id = jcr->jr.JobId;
232    jcr->JobStatus = TermCode;
233
234    update_job_end_record(jcr);
235
236    msg_type = M_INFO;                 /* by default INFO message */
237    switch (TermCode) {
238       case JS_Terminated:
239          term_msg = _("Verify OK");
240          break;
241       case JS_ErrorTerminated:
242          term_msg = _("*** Verify Error ***"); 
243          msg_type = M_ERROR;          /* Generate error message */
244          break;
245       case JS_Cancelled:
246          term_msg = _("Verify Cancelled");
247          break;
248       case JS_Differences:
249          term_msg = _("Verify Differences");
250          break;
251       default:
252          term_msg = term_code;
253          sprintf(term_code, _("Inappropriate term code: %c\n"), TermCode);
254          break;
255    }
256    bstrftime(sdt, sizeof(sdt), jcr->jr.StartTime);
257    bstrftime(edt, sizeof(edt), jcr->jr.EndTime);
258
259    Jmsg(jcr, msg_type, 0, _("%s\n\
260 JobId:                  %d\n\
261 Job:                    %s\n\
262 FileSet:                %s\n\
263 Verify Level:           %s\n\
264 Client:                 %s\n\
265 Start time:             %s\n\
266 End time:               %s\n\
267 Files Examined:         %s\n\
268 Termination:            %s\n\n"),
269         edt,
270         jcr->jr.JobId,
271         jcr->jr.Job,
272         jcr->fileset->hdr.name,
273         level_to_str(jcr->level),
274         jcr->client->hdr.name,
275         sdt,
276         edt,
277         edit_uint64_with_commas(jcr->jr.JobFiles, ec1),
278         term_msg);
279
280    Dmsg0(100, "Leave verify_cleanup()\n");
281    if (jcr->fname) {
282       free_memory(jcr->fname);
283       jcr->fname = NULL;
284    }
285 }
286
287 /*
288  * This routine is called only during a Verify
289  */
290 int get_attributes_and_compare_to_catalog(JCR *jcr, int last_full_id)
291 {
292    BSOCK   *fd;
293    int n, len;
294    FILE_DBR fdbr;
295    struct stat statf;                 /* file stat */
296    struct stat statc;                 /* catalog stat */
297    int stat = JS_Terminated;
298    char buf[MAXSTRING];
299    char *fname = (char *)get_pool_memory(PM_MESSAGE);
300    int do_MD5 = FALSE;
301
302    memset(&fdbr, 0, sizeof(FILE_DBR));
303    fd = jcr->file_bsock;
304    fdbr.JobId = last_full_id;
305    
306    Dmsg0(20, "bdird: waiting to receive file attributes\n");
307    /*
308     * Get Attributes and MD5 Signature from File daemon
309     */
310    while ((n=bget_msg(fd, 0)) > 0) {
311       long file_index, attr_file_index;
312       int stream;
313       char *attr, *p;
314       char Opts_MD5[MAXSTRING];        /* Verify Opts or MD5 signature */
315
316       fname = (char *)check_pool_memory_size(fname, fd->msglen);
317       jcr->fname = (char *)check_pool_memory_size(jcr->fname, fd->msglen);
318       Dmsg1(50, "Atts+MD5=%s\n", fd->msg);
319       if ((len = sscanf(fd->msg, "%ld %d %100s %s", &file_index, &stream, 
320             Opts_MD5, fname)) != 4) {
321          Jmsg3(jcr, M_FATAL, 0, _("bird<filed: bad attributes, expected 4 fields got %d\n\
322  mslen=%d msg=%s\n"), len, fd->msglen, fd->msg);
323          jcr->JobStatus = JS_ErrorTerminated;
324          return 0;
325       }
326       /*
327        * Got attributes stream, decode it
328        */
329       if (stream == STREAM_UNIX_ATTRIBUTES) {
330          attr_file_index = file_index;    /* remember attribute file_index */
331          len = strlen(fd->msg);
332          attr = &fd->msg[len+1];
333          decode_stat(attr, &statf);  /* decode file stat packet */
334          do_MD5 = FALSE;
335          jcr->fn_printed = FALSE;
336          strip_trailing_junk(fname);
337          strcpy(jcr->fname, fname);  /* move filename into JCR */
338
339          Dmsg2(040, "dird<filed: stream=%d %s\n", stream, jcr->fname);
340          Dmsg1(20, "dird<filed: attr=%s\n", attr);
341
342          /* 
343           * Find equivalent record in the database 
344           */
345          fdbr.FileId = 0;
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;
350             continue;
351          } else {
352             /* 
353              * mark file record as visited by stuffing the
354              * current JobId, which is unique, into the FileIndex
355              */
356             db_mark_file_record(jcr->db, fdbr.FileId, jcr->JobId);
357          }
358
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 */
362          /*
363           * Loop over options supplied by user and verify the
364           * fields he requests.
365           */
366          for (p=Opts_MD5; *p; p++) {
367             switch (*p) {
368             case 'i':                /* compare INODEs */
369                if (statc.st_ino != statf.st_ino) {
370                   prt_fname(jcr);
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;
374                }
375                break;
376             case 'p':                /* permissions bits */
377                if (statc.st_mode != statf.st_mode) {
378                   prt_fname(jcr);
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;
382                }
383                break;
384             case 'n':                /* number of links */
385                if (statc.st_nlink != statf.st_nlink) {
386                   prt_fname(jcr);
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;
390                }
391                break;
392             case 'u':                /* user id */
393                if (statc.st_uid != statf.st_uid) {
394                   prt_fname(jcr);
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;
398                }
399                break;
400             case 'g':                /* group id */
401                if (statc.st_gid != statf.st_gid) {
402                   prt_fname(jcr);
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;
406                }
407                break;
408             case 's':                /* size */
409                if (statc.st_size != statf.st_size) {
410                   prt_fname(jcr);
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;
414                }
415                break;
416             case 'a':                /* access time */
417                if (statc.st_atime != statf.st_atime) {
418                   prt_fname(jcr);
419                   Jmsg(jcr, M_INFO, 0, _("      st_atime differs\n"));
420                   stat = JS_Differences;
421                }
422                break;
423             case 'm':
424                if (statc.st_mtime != statf.st_mtime) {
425                   prt_fname(jcr);
426                   Jmsg(jcr, M_INFO, 0, _("      st_mtime differs\n"));
427                   stat = JS_Differences;
428                }
429                break;
430             case 'c':                /* ctime */
431                if (statc.st_ctime != statf.st_ctime) {
432                   prt_fname(jcr);
433                   Jmsg(jcr, M_INFO, 0, _("      st_ctime differs\n"));
434                   stat = JS_Differences;
435                }
436                break;
437             case 'd':                /* file size decrease */
438                if (statc.st_size > statf.st_size) {
439                   prt_fname(jcr);
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;
443                }
444                break;
445             case '5':                /* compare MD5 */
446                Dmsg1(500, "set Do_MD5 for %s\n", jcr->fname);
447                do_MD5 = TRUE;
448                break;
449             case ':':
450             case 'V':
451             default:
452                break;
453             }
454          }
455       /*
456        * Got MD5 Signature from Storage daemon
457        *  It came across in the Opts_MD5 field.
458        */
459       } else if (stream == STREAM_MD5_SIGNATURE) {
460          Dmsg2(100, "stream=MD5 inx=%d fname=%s\n", file_index, jcr->fname);
461          /* 
462           * When ever we get an MD5 signature is MUST have been
463           * preceded by an attributes record, which sets attr_file_index
464           */
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);
468             jcr->JobStatus = JS_ErrorTerminated;
469             return 0;
470          } 
471          if (do_MD5) {
472             db_escape_string(buf, Opts_MD5, strlen(Opts_MD5));
473             if (strcmp(buf, fdbr.MD5) != 0) {
474                prt_fname(jcr);
475                if (debug_level >= 10) {
476                   Jmsg(jcr, M_INFO, 0, _("      MD5 not same. File=%s Cat=%s\n"), buf, fdbr.MD5);
477                } else {
478                   Jmsg(jcr, M_INFO, 0, _("      MD5 differs.\n"));
479                }
480                stat = JS_Differences;
481             }
482             do_MD5 = FALSE;
483          }
484       }
485       jcr->jr.JobFiles = file_index;
486    } 
487    if (n < 0) {
488       Jmsg2(jcr, M_FATAL, 0, _("bdird<filed: bad attributes from filed n=%d : %s\n"),
489                         n, strerror(errno));
490       jcr->JobStatus = JS_ErrorTerminated;
491       return 0;
492    }
493
494    /* Now find all the files that are missing -- i.e. all files in
495     *  the database where the FileIndex != current JobId
496     */
497    jcr->fn_printed = FALSE;
498    sprintf(buf, 
499 "SELECT Path.Path,Filename.Name FROM File,Path,Filename "
500 "WHERE File.JobId=%d "
501 "AND File.FileIndex!=%d AND File.PathId=Path.PathId "
502 "AND File.FilenameId=Filename.FilenameId", 
503       last_full_id, jcr->JobId);
504    /* missing_handler is called for each file found */
505    db_sql_query(jcr->db, buf, missing_handler, (void *)jcr);
506    if (jcr->fn_printed) {
507       stat = JS_Differences;
508    }
509    jcr->JobStatus = stat;
510    return 1;
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 }