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

Last change on this file since 817 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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