source: MondoRescue/branches/3.3/mondo/src/common/libmondo-verify.c@ 3870

Last change on this file since 3870 was 3868, checked in by Bruno Cornec, 3 months ago

Remove useless headers

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