source: MondoRescue/branches/stable/mondo/src/common/libmondo-verify.c @ 948

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