source: MondoRescue/branches/3.0/mondo/src/common/libmondo-verify.c@ 3188

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