source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-verify.c@ 2276

Last change on this file since 2276 was 2276, checked in by Bruno Cornec, 15 years ago

r3262@localhost: bruno | 2009-07-13 02:18:17 +0200
Replace sprintf by mr_asprintf in libmondo-verify.c

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