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

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