source: MondoRescue/trunk/mondo/src/common/libmondo-verify.c@ 956

Last change on this file since 956 was 956, checked in by Bruno Cornec, 17 years ago

merge -r938:954 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 32.3 KB
Line 
1/* $Id: libmondo-verify.c 956 2006-11-21 00:42:46Z bruno $ */
2
3/**
4 * @file
5 * Functions for verifying backups (booted from hard drive, not CD).
6 */
7
8#include <unistd.h>
9
10#include "my-stuff.h"
11#include "mondostructures.h"
12#include "libmondo-verify.h"
13#include "newt-specific-EXT.h"
14#include "libmondo-files-EXT.h"
15#include "libmondo-fork-EXT.h"
16#include "libmondo-stream-EXT.h"
17#include "libmondo-string-EXT.h"
18#include "libmondo-devices-EXT.h"
19#include "libmondo-tools-EXT.h"
20#include "mr_mem.h"
21
22/*@unused@*/
23//static char cvsid[] = "$Id: libmondo-verify.c 956 2006-11-21 00:42:46Z bruno $";
24
25char *vfy_tball_fname(struct s_bkpinfo *, char *, int);
26
27
28/**
29 * The number of the most recently verified afioball.
30 * @ingroup globalGroup
31 */
32int g_last_afioball_number = -1;
33
34extern char *g_getfacl;
35extern char *g_getfattr;
36
37/**
38 * Generate a list of the files that have changed, based on @c afio @c -r
39 * messages.
40 * @param changedfiles_fname Filename of the place to put a list of afio's reported changed.
41 * @param ignorefiles_fname Filename of a list of files to ignore (use "" if none).
42 * @param stderr_fname File containing afio's stderr output.
43 * @return The number of differences found (0 for a perfect backup).
44 * @bug This function seems orphaned.
45 * @ingroup utilityGroup
46 */
47long
48generate_list_of_changed_files(char *changedfiles_fname,
49 char *ignorefiles_fname, char *stderr_fname)
50{
51 /*@ buffer ********************************************************** */
52 char *command;
53 char *afio_found_changes;
54
55 /*@ int ************************************************************* */
56 int res = 0;
57
58 /*@ long ************************************************************ */
59 long afio_diffs = 0;
60
61 assert_string_is_neither_NULL_nor_zerolength(changedfiles_fname);
62 assert_string_is_neither_NULL_nor_zerolength(ignorefiles_fname);
63 assert_string_is_neither_NULL_nor_zerolength(stderr_fname);
64
65 mr_asprintf(&afio_found_changes, "%s.afio", ignorefiles_fname);
66 sync();
67
68/* s-printf (command,
69 "grep \"afio: \" %s | awk '{j=substr($0,8); i=index(j,\": \");printf \"/%%s\\n\",substr(j,1,i-2);}' | sort -u | grep -v \"incheckentry.*xwait\" | grep -vx \"/afio:.*\" | grep -vx \"/dev/.*\" > %s",
70 stderr_fname, afio_found_changes);
71*/
72
73 log_msg(1, "Now scanning log file for 'afio: ' stuff");
74 mr_asprintf(&command,
75 "grep \"afio: \" %s | sed 's/afio: //' | grep -vE '^/dev/.*$' >> %s",
76 stderr_fname, afio_found_changes);
77 log_msg(2, command);
78 res = system(command);
79 mr_free(command);
80 if (res) {
81 log_msg(2, "Warning - failed to think");
82 }
83
84 log_msg(1, "Now scanning log file for 'star: ' stuff");
85 mr_asprintf(&command,
86 "grep \"star: \" %s | sed 's/star: //' | grep -vE '^/dev/.*$' >> %s",
87 stderr_fname, afio_found_changes);
88 log_msg(2, command);
89 res = system(command);
90 mr_free(command);
91 if (res) {
92 log_msg(2, "Warning - failed to think");
93 }
94// exclude_nonexistent_files (afio_found_changes);
95 afio_diffs = count_lines_in_file(afio_found_changes);
96 mr_asprintf(&command,
97 "sort %s %s %s | uniq -c | awk '{ if ($1==\"2\") {print $2;};}' | grep -v \"incheckentry xwait()\" > %s",
98 ignorefiles_fname, afio_found_changes, afio_found_changes,
99 changedfiles_fname);
100 log_msg(2, command);
101 paranoid_system(command);
102 mr_free(command);
103 mr_free(afio_found_changes);
104 return (afio_diffs);
105}
106
107
108/**
109 * @addtogroup LLverifyGroup
110 * @{
111 */
112/**
113 * Verify all afioballs stored on the inserted CD (or an ISO image).
114 * @param bkpinfo The backup information structure. @c bkpinfo->backup_media_type
115 * is used in this function, and the structure is also passed to verify_an_afioball_from_CD().
116 * @param mountpoint The location the CD/DVD/ISO is mounted on.
117 * @return The number of sets containing differences (0 for success).
118 */
119int verify_afioballs_on_CD(struct s_bkpinfo *bkpinfo, char *mountpoint)
120{
121
122 /*@ buffers ********************************************************* */
123 char *tmp;
124
125 /*@ int ************************************************************* */
126 int set_number = 0;
127 int retval = 0;
128 int total_sets = 0;
129 int percentage = 0;
130
131 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
132 assert(bkpinfo != NULL);
133
134 for (set_number = 0;
135 set_number < 9999
136 &&
137 !does_file_exist(vfy_tball_fname
138 (bkpinfo, mountpoint, set_number));
139 set_number++);
140 if (!does_file_exist(vfy_tball_fname(bkpinfo, mountpoint, set_number))) {
141 return (0);
142 }
143
144 if (g_last_afioball_number != set_number - 1) {
145 if (set_number == 0) {
146 log_msg(1,
147 "Weird error in verify_afioballs_on_CD() but it's really a cosmetic error, nothing more");
148 } else {
149 retval++;
150 mr_asprintf(&tmp, "Warning - missing set(s) between %d and %d\n",
151 g_last_afioball_number, set_number - 1);
152 log_to_screen(tmp);
153 mr_free(tmp);
154 }
155 }
156 mr_asprintf(&tmp, "Verifying %s #%d's tarballs",
157 bkpinfo->backup_media_string,
158 g_current_media_number);
159 open_evalcall_form(tmp);
160 mr_free(tmp);
161
162 for (total_sets = set_number;
163 does_file_exist(vfy_tball_fname(bkpinfo, mountpoint, total_sets));
164 total_sets++) {
165 log_msg(1, "total_sets = %d", total_sets);
166 }
167 for (;
168 does_file_exist(vfy_tball_fname(bkpinfo, mountpoint, set_number));
169 set_number++) {
170 percentage =
171 (set_number - g_last_afioball_number) * 100 / (total_sets -
172 g_last_afioball_number);
173 update_evalcall_form(percentage);
174 log_msg(1, "set = %d", set_number);
175 retval +=
176 verify_an_afioball_from_CD(bkpinfo,
177 vfy_tball_fname(bkpinfo, mountpoint,
178 set_number));
179 }
180 g_last_afioball_number = set_number - 1;
181 close_evalcall_form();
182 return (retval);
183}
184
185
186/**
187 * Verify all slices stored on the inserted CD (or a mounted ISO image).
188 * @param bkpinfo The backup information structure. Fields used:
189 * - @c compression_level
190 * - @c restore_path
191 * - @c use_lzo
192 * - @c zip_suffix
193 * @param mtpt The mountpoint the CD/DVD/ISO is mounted on.
194 * @return The number of differences (0 for perfect biggiefiles).
195 */
196int verify_all_slices_on_CD(struct s_bkpinfo *bkpinfo, char *mtpt)
197{
198
199 char *tmp = NULL;
200 char *tmp1 = NULL;
201 char *tmp2 = NULL;
202 char *mountpoint = NULL;
203 char *command = NULL;
204 char *sz_exe = NULL;
205 static char *bufblkA = NULL;
206 static char *bufblkB = NULL;
207 const long maxbufsize = 65536L;
208 long currsizA = 0L;
209 long currsizB = 0L;
210 long j = 0L;
211 long bigfile_num = 0L;
212 long slice_num = -1;
213 int res = 0;
214
215 static FILE *forig = NULL;
216 static struct s_filename_and_lstat_info biggiestruct;
217 static long last_bigfile_num = -1;
218 static long last_slice_num = -1;
219 FILE *pin = NULL;
220 FILE *fin = NULL;
221 int retval = 0;
222
223 if (!bufblkA) {
224 if (!(bufblkA = malloc(maxbufsize))) {
225 fatal_error("Cannot malloc bufblkA");
226 }
227 }
228 if (!bufblkB) {
229 if (!(bufblkB = malloc(maxbufsize))) {
230 fatal_error("Cannot malloc bufblkB");
231 }
232 }
233
234 assert(bkpinfo != NULL);
235 assert_string_is_neither_NULL_nor_zerolength(mtpt);
236
237 if (bkpinfo->compression_level > 0) {
238 if (bkpinfo->use_lzo) {
239 mr_asprintf(&sz_exe, "lzop");
240 } else {
241 mr_asprintf(&sz_exe, "bzip2");
242 }
243 } else {
244 mr_asprintf(&sz_exe, " ");
245 }
246
247 iamhere("before vsbf");
248 mr_asprintf(&tmp, "Verifying %s#%d's big files",
249 bkpinfo->backup_media_string,
250 g_current_media_number);
251 open_evalcall_form(tmp);
252 mr_free(tmp);
253
254 iamhere("after vsbf");
255 mr_asprintf(&mountpoint, "%s/archives", mtpt);
256 if (last_bigfile_num == -1) {
257 bigfile_num = 0;
258 slice_num = 0;
259 } else if (slice_num == 0) {
260 bigfile_num = last_bigfile_num + 1;
261 slice_num = 0;
262 } else {
263 bigfile_num = last_bigfile_num;
264 slice_num = last_slice_num + 1;
265 }
266
267 tmp = slice_fname(bigfile_num, slice_num, mountpoint, bkpinfo->zip_suffix);
268 tmp1 = slice_fname(bigfile_num, slice_num, mountpoint, "");
269 while (does_file_exist(tmp) || does_file_exist(tmp1)) {
270 // handle slices until end of CD
271 if (slice_num == 0) {
272 log_msg(2, "ISO=%d bigfile=%ld --START--",
273 g_current_media_number, bigfile_num);
274 if (!(fin = fopen(tmp1,"r"))) {
275 log_msg(2, "Cannot open bigfile's info file");
276 } else {
277 if (fread
278 ((void *) &biggiestruct, 1, sizeof(biggiestruct),
279 fin) < sizeof(biggiestruct)) {
280 log_msg(2, "Unable to get biggiestruct");
281 }
282 paranoid_fclose(fin);
283 }
284 mr_asprintf(&tmp2, "%s/%s", bkpinfo->restore_path,
285 biggiestruct.filename);
286 log_msg(2, "Opening biggiefile #%ld - '%s'", bigfile_num, tmp2);
287 if (!(forig = fopen(tmp2, "r"))) {
288 log_msg(2, "Failed to open bigfile. Darn.");
289 retval++;
290 }
291 mr_free(tmp2);
292
293 slice_num++;
294 } else if (does_file_exist(tmp1)) {
295 log_msg(2, "ISO=%d bigfile=%ld ---END---",
296 g_current_media_number, bigfile_num);
297 bigfile_num++;
298 paranoid_fclose(forig);
299 slice_num = 0;
300 } else {
301 log_msg(2, "ISO=%d bigfile=%ld slice=%ld \r",
302 g_current_media_number, bigfile_num, slice_num);
303 if (bkpinfo->compression_level > 0) {
304 mr_asprintf(&command, "%s -dc %s 2>> %s", sz_exe, tmp, MONDO_LOGFILE);
305 } else {
306 mr_asprintf(&command, "cat %s", tmp);
307 }
308 if ((pin = popen(command, "r"))) {
309 res = 0;
310 while (!feof(pin)) {
311 currsizA = fread(bufblkA, 1, maxbufsize, pin);
312 if (currsizA <= 0) {
313 break;
314 }
315 currsizB = fread(bufblkB, 1, currsizA, forig);
316 if (currsizA != currsizB) {
317 res++;
318 } else {
319 for (j = 0;
320 j < currsizA && bufblkA[j] == bufblkB[j];
321 j++);
322 if (j < currsizA) {
323 res++;
324 }
325 }
326 }
327 paranoid_pclose(pin);
328 if (res && !strncmp(biggiestruct.filename, " /dev/", 5)) {
329 log_msg(3,
330 "Ignoring differences between %s and live filesystem because it's a device and therefore the archives are stored via ntfsclone, not dd.",
331 biggiestruct.filename);
332 log_msg(3,
333 "If you really want verification for %s, please contact the devteam and offer an incentive.",
334 biggiestruct.filename);
335 res = 0;
336 }
337 if (res) {
338 log_msg(0,
339 "afio: \"%s\": Corrupt biggie file, says libmondo-archive.c",
340 biggiestruct.filename);
341 retval++;
342 }
343 }
344 mr_free(command);
345 slice_num++;
346 }
347 }
348 mr_free(tmp);
349 mr_free(tmp1);
350 mr_free(mountpoint);
351 mr_free(sz_exe);
352
353 last_bigfile_num = bigfile_num;
354 last_slice_num = slice_num - 1;
355 if (last_slice_num < 0) {
356 last_bigfile_num--;
357 }
358 close_evalcall_form();
359 if (bufblkA) {
360 mr_free(bufblkA);
361 }
362 if (bufblkB) {
363 mr_free(bufblkB);
364 }
365 return (0);
366}
367
368
369/**
370 * Verify one afioball from the CD.
371 * You should be changed to the root directory (/) for this to work.
372 * @param bkpinfo The backup information structure. Fields used:
373 * - @c bkpinfo->use_lzo
374 * - @c bkpinfo->tmpdir
375 * - @c bkpinfo->zip_exe
376 * - @c bkpinfo->zip_suffix
377 * @param tarball_fname The filename of the afioball to verify.
378 * @return 0, always.
379 */
380int verify_a_tarball(struct s_bkpinfo *bkpinfo, char *tarball_fname)
381{
382 /*@ buffers ********************************************************* */
383 char *command;
384 char *outlog;
385 char *tmp = NULL;
386 // char *p;
387
388 /*@ pointers ******************************************************* */
389 FILE *pin;
390
391 size_t n = 0;
392
393 /*@ long *********************************************************** */
394 long diffs = 0;
395
396 assert(bkpinfo != NULL);
397 assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
398
399 log_it("Verifying fileset '%s'", tarball_fname);
400
401 /* chdir("/"); */
402 mr_asprintf(&outlog, "%s/afio.log", bkpinfo->tmpdir);
403
404 /* if programmer forgot to say which compression thingy to use then find out */
405 if (strstr(tarball_fname, ".lzo")
406 && strcmp(bkpinfo->zip_suffix, "lzo")) {
407 log_msg(2, "OK, I'm going to start using lzop.");
408 mr_allocstr(bkpinfo->zip_exe, "lzop");
409 mr_allocstr(bkpinfo->zip_suffix, "lzo");
410 bkpinfo->use_lzo = TRUE;
411 }
412 if (strstr(tarball_fname, ".bz2")
413 && strcmp(bkpinfo->zip_suffix, "bz2")) {
414 log_msg(2, "OK, I'm going to start using bzip2.");
415 mr_allocstr(bkpinfo->zip_exe, "bzip2");
416 mr_allocstr(bkpinfo->zip_suffix, "bz2");
417 bkpinfo->use_lzo = FALSE;
418 }
419 unlink(outlog);
420 if (strstr(tarball_fname, ".star")) {
421 bkpinfo->use_star = TRUE;
422 if (strstr(tarball_fname, ".bz2"))
423 mr_asprintf(&command,
424 "star -diff diffopts=mode,size,data file=%s %s >> %s 2>> %s",
425 tarball_fname,
426 (strstr(tarball_fname, ".bz2")) ? "-bz" : " ", outlog,
427 outlog);
428 } else {
429 bkpinfo->use_star = FALSE;
430 mr_asprintf(&command, "afio -r -P %s -Z %s >> %s 2>> %s",
431 bkpinfo->zip_exe, tarball_fname, outlog, outlog);
432 }
433 log_msg(6, "command=%s", command);
434 paranoid_system(command);
435 mr_free(command);
436
437 if (length_of_file(outlog) < 10) {
438 mr_asprintf(&command, "cat %s >> %s", outlog, MONDO_LOGFILE);
439 } else {
440 mr_asprintf(&command, "cut -d':' -f%d %s | sort -u",
441 (bkpinfo->use_star) ? 1 : 2, outlog);
442 pin = popen(command, "r");
443 if (pin) {
444 for (mr_getline(&tmp, &n, pin); !feof(pin);
445 mr_getline(&tmp, &n, pin)) {
446 if (bkpinfo->use_star) {
447 if (!strstr(tmp, "diffopts=")) {
448 while (strlen(tmp) > 0
449 && tmp[strlen(tmp) - 1] < 32) {
450 tmp[strlen(tmp) - 1] = '\0';
451 }
452 if (strchr(tmp, '/')) {
453 if (!diffs) {
454 log_msg(0, "'%s' - differences found",
455 tarball_fname);
456 }
457 log_msg(0, "star: /%s",
458 strip_afio_output_line(tmp));
459 diffs++;
460 }
461 }
462 } else {
463 if (!diffs) {
464 log_msg(0, "'%s' - differences found",
465 tarball_fname);
466 }
467 log_msg(0, "afio: /%s", strip_afio_output_line(tmp));
468 diffs++;
469 }
470 }
471 paranoid_pclose(pin);
472 mr_free(tmp);
473 } else {
474 log_OS_error(command);
475 }
476 }
477 mr_free(outlog);
478 mr_free(command);
479
480 /* chdir(old_pwd); */
481 // s-printf (tmp, "uniq -u %s >> %s", "/tmp/mondo-verify.err", MONDO_LOGFILE);
482 // paranoid_system (tmp);
483 // unlink ("/tmp/mondo-verify.err");
484 return (0);
485}
486
487
488/**
489 * Verify one afioball from the CD.
490 * Checks for existence (calls fatal_error() if it does not exist) and
491 * then calls verify_an_afioball().
492 * @param bkpinfo The backup information structure. Passed to verify_an_afioball().
493 * @param tarball_fname The filename of the afioball to verify.
494 * @return The return value of verify_an_afioball().
495 * @see verify_an_afioball
496 */
497int
498verify_an_afioball_from_CD(struct s_bkpinfo *bkpinfo, char *tarball_fname)
499{
500
501 /*@ int ************************************************************* */
502 int res = 0;
503
504 assert(bkpinfo != NULL);
505 assert_string_is_neither_NULL_nor_zerolength(tarball_fname);
506
507 log_msg(1, "Verifying %s", tarball_fname);
508 if (!does_file_exist(tarball_fname)) {
509 fatal_error("Cannot verify nonexistent afioball");
510 }
511 res = verify_a_tarball(bkpinfo, tarball_fname);
512 return (res);
513}
514
515
516/**
517 * Verify one afioball from the opened tape/CD stream.
518 * Copies the file from tape to tmpdir and then calls verify_an_afioball().
519 * @param bkpinfo The backup information structure. Passed to verify_an_afioball().
520 * @param orig_fname The original filename of the afioball to verify.
521 * @param size The size of the afioball to verify.
522 * @return The return value of verify_an_afioball().
523 * @see verify_an_afioball
524 */
525int
526verify_an_afioball_from_stream(struct s_bkpinfo *bkpinfo, char *orig_fname,
527 long long size)
528{
529
530 /*@ int ************************************************************** */
531 int retval = 0;
532 int res = 0;
533
534 /*@ buffers ********************************************************** */
535 char *tmp;
536 char *tarball_fname;
537
538 /*@ pointers ********************************************************* */
539 char *p;
540
541 assert(bkpinfo != NULL);
542 assert_string_is_neither_NULL_nor_zerolength(orig_fname);
543
544 p = strrchr(orig_fname, '/');
545 if (!p) {
546 p = orig_fname;
547 } else {
548 p++;
549 }
550 mr_asprintf(&tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
551 paranoid_system(tmp);
552 mr_free(tmp);
553
554 mr_asprintf(&tarball_fname, "%s/tmpfs/temporary-%s", bkpinfo->tmpdir, p);
555 /* BERLIOS : useless
556 mr_asprintf(&tmp, "Temporarily copying file from tape to '%s'",
557 tarball_fname);
558 log_it(tmp);
559 mr_free(tmp);
560 */
561 read_file_from_stream_to_file(bkpinfo, tarball_fname, size);
562 res = verify_a_tarball(bkpinfo, tarball_fname);
563 if (res) {
564 mr_asprintf(&tmp,
565 "Afioball '%s' no longer matches your live filesystem",
566 p);
567 log_msg(0, tmp);
568 mr_free(tmp);
569 retval++;
570 }
571 unlink(tarball_fname);
572 mr_free(tarball_fname);
573 return (retval);
574}
575
576
577/**
578 * Verify one biggiefile form the opened tape/CD stream.
579 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
580 * @param biggie_fname The filename of the biggiefile to verify.
581 * @param size The size in bytes of said biggiefile.
582 * @return 0 for success (even if the file doesn't match); nonzero for a tape error.
583 */
584int
585verify_a_biggiefile_from_stream(struct s_bkpinfo *bkpinfo,
586 char *biggie_fname, long long size)
587{
588
589 /*@ int ************************************************************* */
590 int retval = 0;
591 int res = 0;
592 int current_slice_number = 0;
593 int ctrl_chr = '\0';
594
595 /*@ char ************************************************************ */
596 char *test_file;
597 char *biggie_cksum;
598 char *orig_cksum;
599 char *tmp;
600 char *slice_fnam = (char *) &res;
601
602 /*@ pointers ******************************************************** */
603 char *p;
604
605 /*@ long long ******************************************************* */
606 long long slice_siz;
607
608 assert(bkpinfo != NULL);
609 assert_string_is_neither_NULL_nor_zerolength(biggie_fname);
610
611 p = strrchr(biggie_fname, '/');
612 if (!p) {
613 p = biggie_fname;
614 } else {
615 p++;
616 }
617 mr_asprintf(&test_file, "%s/temporary-%s", bkpinfo->tmpdir, p);
618 mr_asprintf(&tmp,
619 "Temporarily copying biggiefile %s's slices from tape to '%s'",
620 p, test_file);
621 log_it(tmp);
622 mr_free(tmp);
623 for (res =
624 read_header_block_from_stream(&slice_siz, slice_fnam, &ctrl_chr);
625 ctrl_chr != BLK_STOP_A_BIGGIE;
626 res =
627 read_header_block_from_stream(&slice_siz, slice_fnam,
628 &ctrl_chr)) {
629 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
630 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
631 }
632 res = read_file_from_stream_to_file(bkpinfo, test_file, slice_siz);
633 unlink(test_file);
634 mr_free(slice_fnam);
635 slice_fnam = (char *) &res;
636 res =
637 read_header_block_from_stream(&slice_siz, slice_fnam,
638 &ctrl_chr);
639 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
640 log_msg(2, "test_file = %s", test_file);
641 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
642 }
643 current_slice_number++;
644 retval += res;
645 mr_free(slice_fnam);
646 slice_fnam = (char *) &res;
647 }
648 mr_free(test_file);
649
650 mr_asprintf(&biggie_cksum, slice_fnam);
651 mr_free(slice_fnam);
652
653 if (biggie_cksum[0] != '\0') {
654 orig_cksum = calc_checksum_of_file(biggie_fname);
655 if (strcmp(biggie_cksum, orig_cksum)) {
656 mr_asprintf(&tmp, "orig cksum=%s; curr cksum=%s", biggie_cksum,
657 orig_cksum);
658 log_msg(2, tmp);
659 mr_free(tmp);
660
661 mr_asprintf(&tmp, _("%s has changed on live filesystem"),
662 biggie_fname);
663 log_to_screen(tmp);
664 mr_free(tmp);
665
666 mr_asprintf(&tmp, "echo \"%s\" >> /tmp/biggies.changed",
667 biggie_fname);
668 system(tmp);
669 mr_free(tmp);
670 }
671 mr_free(orig_cksum);
672 }
673 mr_free(biggie_cksum);
674
675 return (retval);
676}
677
678
679/**
680 * Verify all afioballs from the opened tape/CD stream.
681 * @param bkpinfo The backup information structure. Fields used:
682 * - @c bkpinfo->restore_path
683 * - @c bkpinfo->tmpdir
684 *
685 * @return 0 for success (even if there are differences); nonzero for a tape error.
686 */
687int verify_afioballs_from_stream(struct s_bkpinfo *bkpinfo)
688{
689 /*@ int ********************************************************** */
690 int retval = 0;
691 int res = 0;
692 long current_afioball_number = 0;
693 int ctrl_chr = 0;
694 int total_afioballs = 0;
695
696 /*@ buffers ***************************************************** */
697 char *tmp;
698 char *fname = (char *) &res; /* Should NOT be NULL */
699 char *curr_xattr_list_fname;
700 char *curr_acl_list_fname;
701
702 /*@ long long *************************************************** */
703 long long size = 0;
704
705 assert(bkpinfo != NULL);
706
707 if (g_getfattr) {
708 mr_asprintf(&curr_xattr_list_fname, XATTR_BIGGLST_FNAME_RAW_SZ,
709 bkpinfo->tmpdir);
710 }
711 if (g_getfacl) {
712 mr_asprintf(&curr_acl_list_fname, ACL_BIGGLST_FNAME_RAW_SZ,
713 bkpinfo->tmpdir);
714 }
715 log_to_screen(_("Verifying regular archives on tape"));
716 total_afioballs = get_last_filelist_number(bkpinfo) + 1;
717 open_progress_form(_("Verifying filesystem"),
718 _("I am verifying archives against your live filesystem now."),
719 _("Please wait. This may take a couple of hours."), "",
720 total_afioballs);
721 res = read_header_block_from_stream(&size, NULL, &ctrl_chr);
722 if (ctrl_chr != BLK_START_AFIOBALLS) {
723 iamhere("YOU SHOULD NOT GET HERE");
724 iamhere("Grabbing the EXAT files");
725 if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
726 res =
727 read_EXAT_files_from_tape(bkpinfo, &size, NULL, &ctrl_chr,
728 curr_xattr_list_fname,
729 curr_acl_list_fname);
730 }
731 }
732 if (ctrl_chr != BLK_START_AFIOBALLS) {
733 wrong_marker(BLK_START_AFIOBALLS, ctrl_chr);
734 }
735 mr_free(curr_xattr_list_fname);
736 mr_free(curr_acl_list_fname);
737
738 for (res = read_header_block_from_stream(&size, fname, &ctrl_chr);
739 ctrl_chr != BLK_STOP_AFIOBALLS;
740 res = read_header_block_from_stream(&size, fname, &ctrl_chr)) {
741 if (g_getfattr) {
742 mr_asprintf(&curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
743 bkpinfo->tmpdir, current_afioball_number);
744 }
745 if (g_getfacl) {
746 mr_asprintf(&curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
747 bkpinfo->tmpdir, current_afioball_number);
748 }
749 if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
750 iamhere("Reading EXAT files from tape");
751 mr_free(fname);
752 fname = (char *) &res;
753 res =
754 read_EXAT_files_from_tape(bkpinfo, &size, fname, &ctrl_chr,
755 curr_xattr_list_fname,
756 curr_acl_list_fname);
757 }
758 mr_free(curr_xattr_list_fname);
759 mr_free(curr_acl_list_fname);
760
761 if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
762 wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
763 }
764 mr_asprintf(&tmp, "Verifying fileset #%ld", current_afioball_number);
765 /*log_it(tmp); */
766 update_progress_form(tmp);
767 mr_free(tmp);
768
769 res = verify_an_afioball_from_stream(bkpinfo, fname, size);
770 if (res) {
771 mr_asprintf(&tmp, _("Afioball %ld differs from live filesystem"),
772 current_afioball_number);
773 log_to_screen(tmp);
774 mr_free(tmp);
775 }
776 retval += res;
777 current_afioball_number++;
778 g_current_progress++;
779 mr_free(fname);
780 fname = (char *) &res;
781 res = read_header_block_from_stream(&size, fname, &ctrl_chr);
782 if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
783 wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
784 }
785 mr_free(fname);
786 fname = (char *) &res;
787 }
788 log_msg(1, "All done with afioballs");
789 close_progress_form();
790 mr_free(fname);
791 return (retval);
792}
793
794
795/**
796 * Verify all biggiefiles on the opened CD/tape stream.
797 * @param bkpinfo The backup information structure. Fields used:
798 * - @c bkpinfo->restore_path
799 * - @c bkpinfo->tmpdir
800 *
801 * @return 0 for success (even if there are differences); nonzero for a tape error.
802 */
803int verify_biggiefiles_from_stream(struct s_bkpinfo *bkpinfo)
804{
805
806 /*@ int ************************************************************ */
807 int retval = 0;
808 int res = 0;
809 int ctrl_chr = 0;
810
811 /*@ long *********************************************************** */
812 long noof_biggiefiles = 0;
813 long current_biggiefile_number = 0;
814
815 /*@ buffers ******************************************************** */
816 char *orig_fname = (char *) &ctrl_chr; /* Should NOT be NULL */
817 char *logical_fname;
818 char *comment;
819 char *curr_xattr_list_fname;
820 char *curr_acl_list_fname;
821 /*@ pointers ******************************************************* */
822 char *p;
823
824 /*@ long long size ************************************************* */
825 long long size = 0;
826
827 assert(bkpinfo != NULL);
828
829 if (g_getfattr) {
830 mr_asprintf(&curr_xattr_list_fname, XATTR_BIGGLST_FNAME_RAW_SZ,
831 bkpinfo->tmpdir);
832 }
833 if (g_getfacl) {
834 mr_asprintf(&curr_acl_list_fname, ACL_BIGGLST_FNAME_RAW_SZ,
835 bkpinfo->tmpdir);
836 }
837 mr_asprintf(&comment, "Verifying all bigfiles.");
838 log_to_screen(comment);
839 res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr);
840 if (ctrl_chr != BLK_START_BIGGIEFILES) {
841 if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
842 iamhere("Grabbing the EXAT biggiefiles");
843 mr_free(orig_fname);
844 orig_fname = (char *) &ctrl_chr;
845 res =
846 read_EXAT_files_from_tape(bkpinfo, &size, orig_fname,
847 &ctrl_chr, curr_xattr_list_fname,
848 curr_acl_list_fname);
849 }
850 }
851 mr_free(curr_xattr_list_fname);
852 mr_free(curr_acl_list_fname);
853 mr_free(orig_fname);
854 orig_fname = (char *) &ctrl_chr;
855
856 if (ctrl_chr != BLK_START_BIGGIEFILES) {
857 wrong_marker(BLK_START_BIGGIEFILES, ctrl_chr);
858 }
859 noof_biggiefiles = (long) size;
860 log_msg(1, "noof_biggiefiles = %ld", noof_biggiefiles);
861 open_progress_form(_("Verifying big files"), comment,
862 _("Please wait. This may take some time."), "",
863 noof_biggiefiles);
864 mr_free(comment);
865
866 for (res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr);
867 ctrl_chr != BLK_STOP_BIGGIEFILES;
868 res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr))
869 {
870 if (ctrl_chr != BLK_START_A_NORMBIGGIE
871 && ctrl_chr != BLK_START_A_PIHBIGGIE) {
872 wrong_marker(BLK_START_A_NORMBIGGIE, ctrl_chr);
873 }
874 p = strrchr(orig_fname, '/');
875 if (!p) {
876 p = orig_fname;
877 } else {
878 p++;
879 }
880 mr_asprintf(&comment, _("Verifying bigfile #%ld (%ld K)"),
881 current_biggiefile_number, (long) size >> 10);
882 update_progress_form(comment);
883 mr_free(comment);
884
885 mr_asprintf(&logical_fname, "%s/%s", bkpinfo->restore_path,
886 orig_fname);
887 res =
888 verify_a_biggiefile_from_stream(bkpinfo, logical_fname, size);
889 mr_free(logical_fname);
890 retval += res;
891 current_biggiefile_number++;
892 g_current_progress++;
893 mr_free(orig_fname);
894 orig_fname = (char *) &ctrl_chr;
895 }
896 close_progress_form();
897 return (retval);
898}
899
900/* @} - end of LLverifyGroup */
901
902
903/**
904 * Verify the CD indicated by @c g_current_media_number.
905 * @param bkpinfo The backup information structure. Fields used:
906 * - @c bkpinfo->isodir
907 * - @c bkpinfo->prefix
908 * - @c bkpinfo->manual_cd_tray
909 * - @c bkpinfo->media_device
910 * - @c bkpinfo->nfs_remote_dir
911 * - @c bkpinfo->tmpdir
912 * - @c bkpinfo->verify_data
913 *
914 * @return 0 for success (even if differences are found), nonzero for failure.
915 * @ingroup verifyGroup
916 */
917int verify_cd_image(struct s_bkpinfo *bkpinfo)
918{
919
920 /*@ int ************************************************************ */
921 int retval = 0;
922
923 /*@ buffers ******************************************************** */
924 char *mountpoint;
925 char *command;
926 char *tmp;
927 char *fname;
928#ifdef __FreeBSD__
929 char mdd[32];
930 char *mddevice = mdd;
931 int ret = 0;
932 int vndev = 2;
933#else
934//skip
935#endif
936
937 assert(bkpinfo != NULL);
938
939 mr_asprintf(&mountpoint, "%s/cdrom", bkpinfo->tmpdir);
940 mr_asprintf(&fname, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir,
941 bkpinfo->prefix, g_current_media_number);
942
943 mkdir(mountpoint, 1777);
944 sync();
945 if (!does_file_exist(fname)) {
946 mr_asprintf(&tmp,
947 "%s not found; assuming you backed up to CD; verifying CD...",
948 fname);
949 log_msg(2, tmp);
950 mr_free(tmp);
951
952 if (bkpinfo->manual_cd_tray) {
953 popup_and_OK(_("Please push CD tray closed."));
954 }
955 if (find_and_mount_actual_cd(bkpinfo, mountpoint)) {
956 log_to_screen(_("failed to mount actual CD"));
957 return (1);
958 }
959 } else {
960 mr_asprintf(&tmp, "%s found; verifying ISO...", fname);
961 log_to_screen(tmp);
962 mr_free(tmp);
963#ifdef __FreeBSD__
964 ret = 0;
965 vndev = 2;
966 mddevice = make_vn(fname);
967 if (ret) {
968 mr_asprintf(&tmp, _("make_vn of %s failed; unable to verify ISO\n"),
969 fname);
970 log_to_screen(tmp);
971 mr_free(tmp);
972 return (1);
973 }
974 mr_asprintf(&command, "mount_cd9660 %s %s", mddevice, mountpoint);
975#else
976 mr_asprintf(&command, "mount -o loop,ro -t iso9660 %s %s", fname,
977 mountpoint);
978#endif
979 if (run_program_and_log_output(command, FALSE)) {
980 mr_asprintf(&tmp, _("%s failed; unable to mount ISO image\n"),
981 command);
982 log_to_screen(tmp);
983 mr_free(tmp);
984 return (1);
985 }
986 mr_free(command);
987 }
988 log_msg(2, "OK, I've mounted the ISO/CD\n");
989 mr_asprintf(&tmp, "%s/archives/NOT-THE-LAST", mountpoint);
990 if (!does_file_exist(tmp)) {
991 log_msg
992 (2,
993 "This is the last CD. I am therefore setting bkpinfo->verify_data to FALSE.");
994 bkpinfo->verify_data = FALSE;
995/*
996 (a) It's an easy way to tell the calling subroutine that we've finished &
997 there are no more CD's to be verified; (b) It stops the post-backup verifier
998 from running after the per-CD verifier has run too.
999*/
1000 }
1001 mr_free(tmp);
1002
1003 verify_afioballs_on_CD(bkpinfo, mountpoint);
1004 iamhere("before verify_all_slices");
1005 verify_all_slices_on_CD(bkpinfo, mountpoint);
1006
1007#ifdef __FreeBSD__
1008 ret = 0;
1009 mr_asprintf(&command, "umount %s", mountpoint);
1010 ret += system(command);
1011
1012 ret += kick_vn(mddevice);
1013 if (ret)
1014#else
1015 mr_asprintf(&command, "umount %s", mountpoint);
1016
1017 if (system(command))
1018#endif
1019 {
1020 mr_asprintf(&tmp, "%s failed; unable to unmount ISO image\n",
1021 command);
1022 log_to_screen(tmp);
1023 mr_free(tmp);
1024 retval++;
1025 } else {
1026 log_msg(2, "OK, I've unmounted the ISO file\n");
1027 }
1028 mr_free(command);
1029 mr_free(mountpoint);
1030
1031 if (!does_file_exist(fname)) {
1032 mr_asprintf(&command, "umount %s", bkpinfo->media_device);
1033 run_program_and_log_output(command, 2);
1034 mr_free(command);
1035
1036 if (!bkpinfo->please_dont_eject
1037 && eject_device(bkpinfo->media_device)) {
1038 log_msg(2, "Failed to eject CD-ROM drive");
1039 }
1040 }
1041 mr_free(fname);
1042 return (retval);
1043}
1044
1045
1046/**
1047 * Verify all backups on tape.
1048 * This should be done after the backup process has already closed the tape.
1049 * @param bkpinfo The backup information structure. Passed to various helper functions.
1050 * @return 0 for success (even if thee were differences), nonzero for failure.
1051 * @ingroup verifyGroup
1052 */
1053int verify_tape_backups(struct s_bkpinfo *bkpinfo)
1054{
1055
1056 /*@ int ************************************************************ */
1057 int retval = 0;
1058
1059 /*@ buffers ******************************************************** */
1060 char *tmp;
1061 char *changed_files_fname;
1062
1063 /*@ long *********************************************************** */
1064 long diffs = 0;
1065
1066 assert(bkpinfo != NULL);
1067
1068 log_msg(3, "verify_tape_backups --- starting");
1069 log_to_screen(_("Verifying backups"));
1070 openin_tape(bkpinfo);
1071
1072 /* verify archives themselves */
1073 retval += verify_afioballs_from_stream(bkpinfo);
1074 retval += verify_biggiefiles_from_stream(bkpinfo);
1075
1076 /* find the final blocks */
1077 sync();
1078 sleep(2);
1079 closein_tape(bkpinfo);
1080
1081 /* close tape; exit */
1082 // fclose(g_tape_stream); <-- not needed; is handled by closein_tape()
1083 paranoid_system
1084 ("rm -f /tmp/biggies.changed /tmp/changed.files.[0-9]* 2> /dev/null");
1085 mr_asprintf(&changed_files_fname, "/tmp/changed.files.%d",
1086 (int) (random() % 32767));
1087 mr_asprintf(&tmp,
1088 "grep -E '^%s:.*$' %s | cut -d'\"' -f2 | sort -u | awk '{print \"/\"$0;};' | tr -s '/' '/' | grep -v \"(total of\" | grep -v \"incheckentry.*xwait\" | grep -vE '^/afio:.*$' | grep -vE '^dev/.*$' > %s",
1089 (bkpinfo->use_star) ? "star" : "afio", MONDO_LOGFILE,
1090 changed_files_fname);
1091 log_msg(2, "Running command to derive list of changed files");
1092 log_msg(2, tmp);
1093 if (system(tmp)) {
1094 if (does_file_exist(changed_files_fname)
1095 && length_of_file(changed_files_fname) > 2) {
1096 log_to_screen
1097 (_("Warning - unable to check logfile to derive list of changed files"));
1098 } else {
1099 log_to_screen
1100 (_("No differences found. Therefore, no 'changed.files' text file."));
1101 }
1102 }
1103 mr_free(tmp);
1104
1105 mr_asprintf(&tmp, "cat /tmp/biggies.changed >> %s", changed_files_fname);
1106 paranoid_system(tmp);
1107 mr_free(tmp);
1108
1109 diffs = count_lines_in_file(changed_files_fname);
1110 if (diffs > 0) {
1111 mr_asprintf(&tmp, "cp -f %s %s", changed_files_fname,
1112 "/tmp/changed.files");
1113 run_program_and_log_output(tmp, FALSE);
1114 mr_free(tmp);
1115
1116 mr_asprintf(&tmp,
1117 "%ld files differed from live filesystem; type less %s or less %s to see",
1118 diffs, changed_files_fname, "/tmp/changed.files");
1119 log_msg(0, tmp);
1120 mr_free(tmp);
1121
1122 log_to_screen
1123 (_("See /tmp/changed.files for a list of nonmatching files."));
1124 log_to_screen
1125 (_("The files probably changed on filesystem, not on backup media."));
1126 // retval++;
1127 }
1128 mr_free(changed_files_fname);
1129 return (retval);
1130}
1131
1132
1133/**
1134 * Generate the filename of a tarball to verify.
1135 * @param bkpinfo The backup information structure. @c bkpinfo->zip_suffix is the only field used.
1136 * @param mountpoint The directory where the CD/DVD/ISO is mounted.
1137 * @param setno The afioball number to get the location of.
1138 * @return The absolute path to the afioball.
1139 * @note The returned string points to static data that will be overwritten with each call.
1140 * @ingroup stringGroup
1141 */
1142char *vfy_tball_fname(struct s_bkpinfo *bkpinfo, char *mountpoint,
1143 int setno)
1144{
1145 /*@ buffers ******************************************************* */
1146 static char *output;
1147
1148 assert(bkpinfo != NULL);
1149 assert_string_is_neither_NULL_nor_zerolength(mountpoint);
1150 mr_asprintf(&output, "%s/archives/%d.star.%s", mountpoint, setno,
1151 bkpinfo->zip_suffix);
1152 if (!does_file_exist(output)) {
1153 mr_free(output);
1154 mr_asprintf(&output, "%s/archives/%d.afio.%s", mountpoint, setno,
1155 bkpinfo->zip_suffix);
1156 }
1157 return (output);
1158}
Note: See TracBrowser for help on using the repository browser.