/***************************************************************************
$Id: libmondo-verify.c 3613 2016-11-18 16:31:42Z bruno $
***************************************************************************/


/**
 * @file
 * Functions for verifying backups (booted from hard drive, not CD).
 */

#include "my-stuff.h"
#include "mr_mem.h"
#include "mondostructures.h"
#include "libmondo-verify.h"
#include "libmondo-gui-EXT.h"
#include "libmondo-files-EXT.h"
#include "libmondo-fork-EXT.h"
#include "libmondo-stream-EXT.h"
#include "libmondo-string-EXT.h"
#include "libmondo-devices-EXT.h"
#include "libmondo-tools-EXT.h"
#include "lib-common-externs.h"

/*@unused@*/
//static char cvsid[] = "$Id: libmondo-verify.c 3613 2016-11-18 16:31:42Z bruno $";

/**
 * The number of the most recently verified afioball.
 * @ingroup globalGroup
 */
int g_last_afioball_number = -1;

extern char *g_getfacl;
extern char *g_getfattr;
extern char *MONDO_LOGFILE;

/* Reference to global bkpinfo */
extern struct s_bkpinfo *bkpinfo;


/**
 * Generate the filename of a tarball to verify.
 * @param bkpinfo The backup information structure. @c bkpinfo->zip_suffix is the only field used.
 * @param mountpoint The directory where the CD/DVD/ISO is mounted.
 * @param setno The afioball number to get the location of.
 * @return The absolute path to the afioball.
 * @note The returned string points to static data that will be overwritten with each call.
 * @ingroup stringGroup
 */
char *vfy_tball_fname(char *mountpoint, int setno)
{
	/*@ buffers ******************************************************* */
	static char output[MAX_STR_LEN];

	assert(bkpinfo != NULL);
	assert_string_is_neither_NULL_nor_zerolength(mountpoint);
	sprintf(output, "%s/archives/%d.star.%s", mountpoint, setno, bkpinfo->zip_suffix);
	if (!does_file_exist(output)) {
		sprintf(output, "%s/archives/%d.afio.%s", mountpoint, setno, bkpinfo->zip_suffix);
	}
	return (output);
}


/**
 * Generate a list of the files that have changed, based on @c afio @c -r
 * messages.
 * @param changedfiles_fname Filename of the place to put a list of afio's reported changed.
 * @param ignorefiles_fname Filename of a list of files to ignore (use "" if none).
 * @param stderr_fname File containing afio's stderr output.
 * @return The number of differences found (0 for a perfect backup).
 * @bug This function seems orphaned.
 * @ingroup utilityGroup
 */
long
generate_list_of_changed_files(char *changedfiles_fname,
							   char *ignorefiles_fname, char *stderr_fname)
{
	/*@ buffer ********************************************************** */
	char *command = NULL;
	char *afio_found_changes = NULL;

	/*@ int ************************************************************* */
	int res = 0;

	/*@ long ************************************************************ */
	long afio_diffs = 0;

	assert_string_is_neither_NULL_nor_zerolength(changedfiles_fname);
	assert_string_is_neither_NULL_nor_zerolength(ignorefiles_fname);
	assert_string_is_neither_NULL_nor_zerolength(stderr_fname);

	mr_asprintf(afio_found_changes, "%s.afio", ignorefiles_fname);
	sync();

	log_msg(1, "Now scanning log file for 'afio: ' stuff");
	mr_asprintf(command, "grep \"afio: \" %s | sed 's/afio: //' | grep -vE '^/dev/.*$' >> %s", stderr_fname, afio_found_changes);
	log_msg(2, command);
	res = system(command);
	mr_free(command);

	if (res) {
		log_msg(2, "Warning - failed to think");
	}

	log_msg(1, "Now scanning log file for 'star: ' stuff");
	mr_asprintf(command, "grep \"star: \" %s | sed 's/star: //' | grep -vE '^/dev/.*$' >> %s", stderr_fname, afio_found_changes);
	log_msg(2, command);
	res = system(command);
	mr_free(command);

	if (res) {
		log_msg(2, "Warning - failed to think");
	}
	afio_diffs = count_lines_in_file(afio_found_changes);
	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);
	mr_free(afio_found_changes);
	log_msg(2, command);
	paranoid_system(command);
	mr_free(command);
	return (afio_diffs);
}


/**
 * @addtogroup LLverifyGroup
 * @{
 */
/**
 * Verify all afioballs stored on the inserted CD (or an ISO image).
 * @param bkpinfo The backup information structure. @c bkpinfo->backup_media_type
 * is used in this function, and the structure is also passed to verify_an_afioball_from_CD().
 * @param mountpoint The location the CD/DVD/ISO is mounted on.
 * @return The number of sets containing differences (0 for success).
 */
int verify_afioballs_on_CD(char *mountpoint)
{

	/*@ buffers ********************************************************* */
	char *tmp = NULL;
	char *mds = NULL;

	/*@ int ************************************************************* */
	int set_number = 0;
	int retval = 0;
	int total_sets = 0;
	int percentage = 0;

	assert_string_is_neither_NULL_nor_zerolength(mountpoint);
	assert(bkpinfo != NULL);

	for (set_number = 0;
		 set_number < 9999
		 &&
		 !does_file_exist(vfy_tball_fname(mountpoint, set_number));
		 set_number++);
	if (!does_file_exist(vfy_tball_fname(mountpoint, set_number))) {
		return (0);
	}

	if (g_last_afioball_number != set_number - 1) {
		if (set_number == 0) {
			log_msg(1, "Weird error in verify_afioballs_on_CD() but it's really a cosmetic error, nothing more");
		} else {
			retval++;
			log_to_screen("Warning - missing set(s) between %d and %d\n", g_last_afioball_number, set_number - 1);
		}
	}
	mds = media_descriptor_string(bkpinfo->backup_media_type);
	mr_asprintf(tmp, "Verifying %s #%d's tarballs", mds, g_current_media_number);
	mr_free(mds);

	open_evalcall_form(tmp);
	mr_free(tmp);

	for (total_sets = set_number;
		 does_file_exist(vfy_tball_fname(mountpoint, total_sets));
		 total_sets++) {
		log_msg(1, "total_sets = %d", total_sets);
	}
	for (;
		 does_file_exist(vfy_tball_fname(mountpoint, set_number));
		 set_number++) {
		percentage =
			(set_number - g_last_afioball_number) * 100 / (total_sets -
														   g_last_afioball_number);
		update_evalcall_form(percentage);
		log_msg(1, "set = %d", set_number);
		retval +=
			verify_an_afioball_from_CD(vfy_tball_fname(mountpoint, set_number));
	}
	g_last_afioball_number = set_number - 1;
	close_evalcall_form();
	return (retval);
}

/**
 * Verify all slices stored on the inserted CD (or a mounted ISO image).
 * @param bkpinfo The backup information structure. Fields used:
 * - @c compression_level
 * - @c restore_path
 * - @c use_lzo
 * - @c zip_exe
 * - @c zip_suffix
 * @param mtpt The mountpoint the CD/DVD/ISO is mounted on.
 * @return The number of differences (0 for perfect biggiefiles).
 */
int verify_all_slices_on_CD(char *mtpt)
{

	/*@ buffer ********************************************************** */
	char *tmp = NULL;
	char *mountpoint = NULL;
	char *command = NULL;
	char *mds = NULL;
	char *sz_exe = NULL;
	static char *bufblkA = NULL;
	static char *bufblkB = NULL;
	const long maxbufsize = 65536L;
	long currsizA = 0;
	long currsizB = 0;
	long j;

	/*@ long ************************************************************ */
	long bigfile_num = 0;
	long slice_num = -1;
	int res;

	static FILE *forig = NULL;
	static struct s_filename_and_lstat_info biggiestruct;
	static long last_bigfile_num = -1;
	static long last_slice_num = -1;
	FILE *pin;
	FILE *fin;
	int retval = 0;

	if (!bufblkA) {
		if (!(bufblkA = malloc(maxbufsize))) {
			fatal_error("Cannot malloc bufblkA");
		}
	}
	if (!bufblkB) {
		if (!(bufblkB = malloc(maxbufsize))) {
			fatal_error("Cannot malloc bufblkB");
		}
	}

	assert(bkpinfo != NULL);
	assert_string_is_neither_NULL_nor_zerolength(mtpt);

	if (bkpinfo->compression_level > 0) {
		if (bkpinfo->use_lzo) {
			mr_asprintf(sz_exe, "lzop");
		} else if (bkpinfo->use_gzip) {
			mr_asprintf(sz_exe, "gzip");
		} else if (bkpinfo->use_lzma) {
			mr_asprintf(sz_exe, "lzma");
		} else {
			mr_asprintf(sz_exe, "bzip2");
		}
	}

	log_it("before vsbf");
	mds = media_descriptor_string(bkpinfo->backup_media_type);
	mr_asprintf(tmp, "Verifying %s#%d's big files", mds, g_current_media_number);
	mr_free(mds);

	open_evalcall_form(tmp);
	mr_free(tmp);

	log_it("after vsbf");
	mr_asprintf(mountpoint, "%s/archives", mtpt);
	if (last_bigfile_num == -1) {
		bigfile_num = 0;
		slice_num = 0;
	} else if (slice_num == 0) {
		bigfile_num = last_bigfile_num + 1;
		slice_num = 0;
	} else {
		bigfile_num = last_bigfile_num;
		slice_num = last_slice_num + 1;
	}
	while (does_file_exist(slice_fname(bigfile_num, slice_num, mountpoint, bkpinfo->zip_suffix))
		   ||
		   does_file_exist(slice_fname(bigfile_num, slice_num, mountpoint, ""))) {
		// handle slices until end of CD
		if (slice_num == 0) {
			log_msg(2, "ISO=%d  bigfile=%ld --START--", g_current_media_number, bigfile_num);
			if (! (fin = fopen(slice_fname(bigfile_num, slice_num, mountpoint, ""), "r"))) {
				log_msg(2, "Cannot open bigfile's info file");
			} else {
				if (fread ((void *) &biggiestruct, 1, sizeof(biggiestruct), fin) < sizeof(biggiestruct)) {
					log_msg(2, "Unable to get biggiestruct");
				}
				paranoid_fclose(fin);
			}
			if (bkpinfo->restore_path) {
				mr_asprintf(tmp, "%s/%s", bkpinfo->restore_path, biggiestruct.filename);
				log_msg(2, "Opening biggiefile #%ld - '%s'", bigfile_num, tmp);
				forig = fopen(tmp, "r");
				mr_free(tmp);
	
				if (!forig) {
					log_msg(2, "Failed to open bigfile. Darn.");
					log_to_screen("%s/%s not found on live filesystem", bkpinfo->restore_path, biggiestruct.filename);
					mr_asprintf(tmp, "echo \"%s/%s not found\" >> %s/biggies.changed", bkpinfo->restore_path, biggiestruct.filename, bkpinfo->tmpdir);
					paranoid_system(tmp);
					mr_free(tmp);

					bigfile_num++;
					slice_num = 0;
					retval++;
				} else {
					slice_num++;
				}
			} else {
				log_it("Unable to open bigfile as restore_path is NULL");
			}
		} else if (does_file_exist(slice_fname(bigfile_num, slice_num, mountpoint, "")) &&
				   (length_of_file(slice_fname(bigfile_num, slice_num, mountpoint, "")) == 0)) {
			log_msg(2, "ISO=%d  bigfile=%ld ---END---",
					g_current_media_number, bigfile_num);
			bigfile_num++;
			paranoid_fclose(forig);
			slice_num = 0;
		} else {
			log_msg(2, "ISO=%d  bigfile=%ld  slice=%ld", g_current_media_number, bigfile_num, slice_num);
			if ((!does_file_exist(slice_fname(bigfile_num, slice_num, mountpoint, ""))) && (sz_exe != NULL)) {
				mr_asprintf(command, "%s -dc %s 2>> %s", sz_exe, slice_fname(bigfile_num, slice_num, mountpoint, bkpinfo->zip_suffix), MONDO_LOGFILE);
			} else {
				mr_asprintf(command, "cat %s 2>> %s", slice_fname(bigfile_num, slice_num, mountpoint, ""), MONDO_LOGFILE);
			}
			pin = popen(command, "r");
			mr_free(command);
			if (pin) {
				res = 0;
				while (!feof(pin)) {
					currsizA = fread(bufblkA, 1, maxbufsize, pin);
					if (currsizA <= 0) {
						break;
					}
					currsizB = fread(bufblkB, 1, currsizA, forig);
					if (currsizA != currsizB) {
						res++;
					} else {
						for (j = 0;
							 j < currsizA && bufblkA[j] == bufblkB[j];
							 j++);
						if (j < currsizA) {
							res++;
						}
					}
				}
				paranoid_pclose(pin);
				if (res && !strncmp(biggiestruct.filename, "/dev/", 5)) {
					log_msg(3,
							"Ignoring differences between %s and live filesystem because it's a device and therefore the archives are stored via ntfsclone, not dd.",
							biggiestruct.filename);
					log_msg(3,
							"If you really want verification for %s, please contact the devteam and offer an incentive.",
							biggiestruct.filename);
					res = 0;
				}
				if (res) {
					log_msg(0,
							"afio: \"%s\": Corrupt biggie file, says libmondo-archive.c",
							biggiestruct.filename);
					retval++;
				}
			}
			slice_num++;
		}
	}
	mr_free(mountpoint);
	mr_free(sz_exe);

	last_bigfile_num = bigfile_num;
	last_slice_num = slice_num - 1;
	if (last_slice_num < 0) {
		last_bigfile_num--;
	}
	close_evalcall_form();
	if (bufblkA) {
		paranoid_free(bufblkA);
	}
	if (bufblkB) {
		paranoid_free(bufblkB);
	}
	return (0);
}






/**
 * Verify one afioball from the CD.
 * You should be changed to the root directory (/) for this to work.
 * @param bkpinfo The backup information structure. Fields used:
 * - @c bkpinfo->use_lzo
 * - @c bkpinfo->tmpdir
 * - @c bkpinfo->zip_exe
 * - @c bkpinfo->zip_suffix
 * @param tarball_fname The filename of the afioball to verify.
 * @return 0, always.
 */
int verify_a_tarball(char *tarball_fname)
{
	/*@ buffers ********************************************************* */
	char *command = NULL;
	char *outlog = NULL;
	char *tmp = NULL;

	/*@ pointers ******************************************************* */
	FILE *pin;

	/*@ long *********************************************************** */
	long diffs = 0;

	assert(bkpinfo != NULL);
	assert_string_is_neither_NULL_nor_zerolength(tarball_fname);

	log_it("Verifying fileset '%s'", tarball_fname);
	mr_asprintf(outlog, "%s/afio.log", bkpinfo->tmpdir);
	/* if programmer forgot to say which compression thingy to use then find out */
	if (strstr(tarball_fname, ".lzo") && strcmp(bkpinfo->zip_suffix, "lzo")) {
		log_msg(2, "OK, I'm going to start using lzop.");
		strcpy(bkpinfo->zip_exe, "lzop");
		strcpy(bkpinfo->zip_suffix, "lzo");
		bkpinfo->use_lzo = TRUE;
		bkpinfo->use_gzip = FALSE;
		bkpinfo->use_lzma = FALSE;
	}
	if (strstr(tarball_fname, ".gz") && strcmp(bkpinfo->zip_suffix, "gz")) {
		log_msg(2, "OK, I'm going to start using gzip.");
		strcpy(bkpinfo->zip_exe, "gzip");
		strcpy(bkpinfo->zip_suffix, "gz");
		bkpinfo->use_lzo = FALSE;
		bkpinfo->use_gzip = TRUE;
		bkpinfo->use_lzma = FALSE;
	}
	if (strstr(tarball_fname, ".bz2") && strcmp(bkpinfo->zip_suffix, "bz2")) {
		log_msg(2, "OK, I'm going to start using bzip2.");
		strcpy(bkpinfo->zip_exe, "bzip2");
		strcpy(bkpinfo->zip_suffix, "bz2");
		bkpinfo->use_lzo = FALSE;
		bkpinfo->use_gzip = FALSE;
		bkpinfo->use_lzma = FALSE;
	}
	if (strstr(tarball_fname, ".lzma") && strcmp(bkpinfo->zip_suffix, "lzma")) {
		log_msg(2, "OK, I'm going to start using lzma.");
		strcpy(bkpinfo->zip_exe, "lzma");
		strcpy(bkpinfo->zip_suffix, "lzma");
		bkpinfo->use_lzo = FALSE;
		bkpinfo->use_gzip = FALSE;
		bkpinfo->use_lzma = TRUE;
	}
	if (bkpinfo->zip_exe == NULL) {
		strcpy(bkpinfo->zip_exe, "none");
	}
	if (bkpinfo->zip_suffix == NULL) {
		strcpy(bkpinfo->zip_suffix, "");
	}
	unlink(outlog);
	if (strstr(tarball_fname, ".star")) {
		bkpinfo->use_star = TRUE;
		if (strstr(tarball_fname, ".bz2"))
			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);
	} else {
		bkpinfo->use_star = FALSE;
		/* Here we suppose that there is always a compression program called */
		if (bkpinfo->zip_exe) {
			mr_asprintf(command, "afio -r -P %s -Z %s >> %s 2>> %s", bkpinfo->zip_exe, tarball_fname, outlog, outlog);
		} else {
			mr_asprintf(command, "afio -r -Z %s >> %s 2>> %s", tarball_fname, outlog, outlog);
		}
	}
	log_msg(6, "command=%s", command);
	paranoid_system(command);
	mr_free(command);

	if (length_of_file(outlog) < 10) {
		mr_asprintf(command, "cat %s >> %s", outlog, MONDO_LOGFILE);
	} else {
		mr_asprintf(command, "cut -d: -f%d %s | sort -u", (bkpinfo->use_star) ? 1 : 2, outlog);
		pin = popen(command, "r");
		if (pin) {
			for (mr_getline(tmp, pin); !feof(pin); mr_getline(tmp, pin)) {
				if (bkpinfo->use_star) {
					if (!strstr(tmp, "diffopts=")) {
						while (strlen(tmp) > 0 && tmp[strlen(tmp) - 1] < 32) {
							tmp[strlen(tmp) - 1] = '\0';
						}
						if (strchr(tmp, '/')) {
							if (!diffs) {
								log_msg(0, "'%s' - differences found", tarball_fname);
							}
							log_msg(0, "star: /%s", strip_afio_output_line(tmp));
							diffs++;
						}
					}
				} else {
					if (!diffs) {
						log_msg(0, "'%s' - differences found", tarball_fname);
					}
					log_msg(0, "afio: /%s", strip_afio_output_line(tmp));
					diffs++;
				}
				mr_free(tmp);
			}
			mr_free(tmp);
			paranoid_pclose(pin);
		} else {
			log_OS_error(command);
		}
	}
	mr_free(command);
	mr_free(outlog);
	return (0);
}






/**
 * Verify one afioball from the CD.
 * Checks for existence (calls fatal_error() if it does not exist) and 
 * then calls verify_an_afioball().
 * @param bkpinfo The backup information structure. Passed to verify_an_afioball().
 * @param tarball_fname The filename of the afioball to verify.
 * @return The return value of verify_an_afioball().
 * @see verify_an_afioball
 */
int
verify_an_afioball_from_CD(char *tarball_fname)
{

	/*@ int ************************************************************* */
	int res = 0;

	assert_string_is_neither_NULL_nor_zerolength(tarball_fname);

	log_msg(1, "Verifying %s", tarball_fname);
	if (!does_file_exist(tarball_fname)) {
		fatal_error("Cannot verify nonexistent afioball");
	}
	res = verify_a_tarball(tarball_fname);
	return (res);
}


/**
 * Verify one afioball from the opened tape/CD stream.
 * Copies the file from tape to tmpdir and then calls verify_an_afioball().
 * @param bkpinfo The backup information structure. Passed to verify_an_afioball().
 * @param orig_fname The original filename of the afioball to verify.
 * @param size The size of the afioball to verify.
 * @return The return value of verify_an_afioball().
 * @see verify_an_afioball
 */
int
verify_an_afioball_from_stream(char *orig_fname, long long size)
{

	/*@ int ************************************************************** */
	int retval = 0;
	int res = 0;

	/*@ buffers ********************************************************** */
	char *tmp = NULL;
	char *tarball_fname = NULL;

	/*@ pointers ********************************************************* */
	char *p;

	assert(bkpinfo != NULL);
	assert_string_is_neither_NULL_nor_zerolength(orig_fname);

	p = strrchr(orig_fname, '/');
	if (!p) {
		p = orig_fname;
	} else {
		p++;
	}
	mr_asprintf(tmp, "mkdir -p %s/tmpfs", bkpinfo->tmpdir);
	paranoid_system(tmp);
	mr_free(tmp);

	mr_asprintf(tarball_fname, "%s/tmpfs/temporary-%s", bkpinfo->tmpdir, p);
	read_file_from_stream_to_file(tarball_fname, size);
	res = verify_a_tarball(tarball_fname);
	if (res) {
		log_msg(0, "Afioball '%s' no longer matches your live filesystem", p);
		retval++;
	}
	unlink(tarball_fname);
	mr_free(tarball_fname);
	return (retval);
}


/**
 * Verify one biggiefile form the opened tape/CD stream.
 * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
 * @param biggie_fname The filename of the biggiefile to verify.
 * @param size The size in bytes of said biggiefile.
 * @return 0 for success (even if the file doesn't match); nonzero for a tape error.
 */
int
verify_a_biggiefile_from_stream(char *biggie_fname, long long size)
{

	/*@ int ************************************************************* */
	int retval = 0;
	int res = 0;
	int current_slice_number = 0;
	int ctrl_chr = '\0';

	/*@ char ************************************************************ */
	char *test_file = NULL;
	char *biggie_cksum = NULL;
	char *orig_cksum = NULL;
	char *tmp = NULL;
	char *slice_fnam = NULL;

	/*@ pointers ******************************************************** */
	char *p;

	/*@ long long ******************************************************* */
	long long slice_siz;

	malloc_string(slice_fnam);
	assert(bkpinfo != NULL);
	assert_string_is_neither_NULL_nor_zerolength(biggie_fname);

	p = strrchr(biggie_fname, '/');
	if (!p) {
		p = biggie_fname;
	} else {
		p++;
	}
	mr_asprintf(test_file, "%s/temporary-%s", bkpinfo->tmpdir, p);
	for (res =
		 read_header_block_from_stream(&slice_siz, slice_fnam, &ctrl_chr);
		 ctrl_chr != BLK_STOP_A_BIGGIE;
		 res =
		 read_header_block_from_stream(&slice_siz, slice_fnam, &ctrl_chr)) {
		if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
			wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
		}
		res = read_file_from_stream_to_file(test_file, slice_siz);
		unlink(test_file);
		res =
			read_header_block_from_stream(&slice_siz, slice_fnam, &ctrl_chr);
		if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
			log_msg(2, "test_file = %s", test_file);
			wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
		}
		current_slice_number++;
		retval += res;
	}
	mr_asprintf(biggie_cksum, "%s", slice_fnam);
	if (biggie_cksum[0] != '\0') {
		mr_asprintf(orig_cksum, "%s", calc_checksum_of_file(biggie_fname));
		if (strcmp(biggie_cksum, orig_cksum)) {
			log_msg(2, "orig cksum=%s; curr cksum=%s", biggie_cksum, orig_cksum);
			log_to_screen("%s has changed on live filesystem", biggie_fname);

			mr_asprintf(tmp, "echo \"%s\" >> %s/biggies.changed", biggie_fname, bkpinfo->tmpdir);
			paranoid_system(tmp);
			mr_free(tmp);
		}
		mr_free(orig_cksum);
	}
	mr_free(biggie_cksum);
	mr_free(test_file);
	paranoid_free(slice_fnam);
	return (retval);
}


/**
 * Verify all afioballs from the opened tape/CD stream.
 * @param bkpinfo The backup information structure. Fields used:
 * - @c bkpinfo->tmpdir
 *
 * @return 0 for success (even if there are differences); nonzero for a tape error.
 */
int verify_afioballs_from_stream()
{
	/*@ int ********************************************************** */
	int retval = 0;
	int res = 0;
	long current_afioball_number = 0;
	int ctrl_chr = 0;
	int total_afioballs = 0;

	/*@ buffers ***************************************************** */
	char *tmp = NULL;
	char *fname;
	char *curr_xattr_list_fname;
	char *curr_acl_list_fname;

	/*@ long long *************************************************** */
	long long size = 0;

	assert(bkpinfo != NULL);
	malloc_string(fname);
	malloc_string(curr_xattr_list_fname);
	malloc_string(curr_acl_list_fname);

	if (g_getfattr) {
		sprintf(curr_xattr_list_fname, XATTR_BIGGLST_FNAME_RAW_SZ,
			bkpinfo->tmpdir);
	}
	if (g_getfacl) {
		sprintf(curr_acl_list_fname, ACL_BIGGLST_FNAME_RAW_SZ,
			bkpinfo->tmpdir);
	}
	log_to_screen("Verifying regular archives on tape");
	total_afioballs = get_last_filelist_number() + 1;
	open_progress_form("Verifying filesystem",
					   "I am verifying archives against your live filesystem now.",
					   "Please wait. This may take a couple of hours.", "",
					   total_afioballs);
	res = read_header_block_from_stream(&size, fname, &ctrl_chr);
	if (ctrl_chr != BLK_START_AFIOBALLS) {
		log_it("YOU SHOULD NOT GET HERE");
		log_it("Grabbing the EXAT files");
		if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
			res = read_EXAT_files_from_tape(&size, fname, &ctrl_chr,
										  curr_xattr_list_fname,
										  curr_acl_list_fname);
		}
	}
	if (ctrl_chr != BLK_START_AFIOBALLS) {
		wrong_marker(BLK_START_AFIOBALLS, ctrl_chr);
	}

	for (res = read_header_block_from_stream(&size, fname, &ctrl_chr);
		 ctrl_chr != BLK_STOP_AFIOBALLS;
		 res = read_header_block_from_stream(&size, fname, &ctrl_chr)) {
		if (g_getfattr) {
			sprintf(curr_xattr_list_fname, XATTR_LIST_FNAME_RAW_SZ,
				bkpinfo->tmpdir, current_afioball_number);
		}
		if (g_getfacl) {
			sprintf(curr_acl_list_fname, ACL_LIST_FNAME_RAW_SZ,
				bkpinfo->tmpdir, current_afioball_number);
		}
		if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
			log_it("Reading EXAT files from tape");
			res = read_EXAT_files_from_tape(&size, fname, &ctrl_chr,
										  curr_xattr_list_fname,
										  curr_acl_list_fname);
		}
		if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
			wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
		}
		mr_asprintf(tmp, "Verifying fileset #%ld", current_afioball_number);
		update_progress_form(tmp);
		mr_free(tmp);

		res = verify_an_afioball_from_stream(fname, size);
		if (res) {
			log_to_screen("Afioball %ld differs from live filesystem", current_afioball_number);
		}
		retval += res;
		current_afioball_number++;
		g_current_progress++;
		res = read_header_block_from_stream(&size, fname, &ctrl_chr);
		if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
			wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
		}
	}
	log_msg(1, "All done with afioballs");
	close_progress_form();
	paranoid_free(fname);
	paranoid_free(curr_xattr_list_fname);
	paranoid_free(curr_acl_list_fname);
	return (retval);
}

/**
 * Verify all biggiefiles on the opened CD/tape stream.
 * @param bkpinfo The backup information structure. Fields used:
 * - @c bkpinfo->restore_path
 * - @c bkpinfo->tmpdir
 *
 * @return 0 for success (even if there are differences); nonzero for a tape error.
 */
int verify_biggiefiles_from_stream()
{

	/*@ int ************************************************************ */
	int retval = 0;
	int res = 0;
	int ctrl_chr = 0;

	/*@ long *********************************************************** */
	long noof_biggiefiles = 0;
	long current_biggiefile_number = 0;

	/*@ buffers ******************************************************** */
	char *orig_fname;
	char *logical_fname = NULL;
	char *comment = NULL;
	char *curr_xattr_list_fname = NULL;
	char *curr_acl_list_fname = NULL;
	/*@ pointers ******************************************************* */
	char *p;

	/*@ long long size ************************************************* */
	long long size = 0;

	assert(bkpinfo != NULL);
	malloc_string(orig_fname);
	malloc_string(curr_xattr_list_fname);
	malloc_string(curr_acl_list_fname);

	if (g_getfattr) {
		sprintf(curr_xattr_list_fname, XATTR_BIGGLST_FNAME_RAW_SZ,
			bkpinfo->tmpdir);
	}
	if (g_getfacl) {
		sprintf(curr_acl_list_fname, ACL_BIGGLST_FNAME_RAW_SZ,
			bkpinfo->tmpdir);
	}
	mr_asprintf(comment, "Verifying all bigfiles.");
	log_to_screen(comment);
	res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr);
	if (ctrl_chr != BLK_START_BIGGIEFILES) {
		if (ctrl_chr == BLK_START_EXTENDED_ATTRIBUTES) {
			log_it("Grabbing the EXAT biggiefiles");
			res = read_EXAT_files_from_tape(&size, orig_fname,
										  &ctrl_chr, curr_xattr_list_fname,
										  curr_acl_list_fname);
		}
	}
	if (ctrl_chr != BLK_START_BIGGIEFILES) {
		wrong_marker(BLK_START_BIGGIEFILES, ctrl_chr);
	}
	noof_biggiefiles = (long) size;
	log_msg(1, "noof_biggiefiles = %ld", noof_biggiefiles);
	open_progress_form("Verifying big files", comment,
					   "Please wait. This may take some time.", "",
					   noof_biggiefiles);
	mr_free(comment);

	for (res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr);
		 ctrl_chr != BLK_STOP_BIGGIEFILES;
		 res = read_header_block_from_stream(&size, orig_fname, &ctrl_chr))
	{
		if (ctrl_chr != BLK_START_A_NORMBIGGIE
			&& ctrl_chr != BLK_START_A_PIHBIGGIE) {
			wrong_marker(BLK_START_A_NORMBIGGIE, ctrl_chr);
		}
		p = strrchr(orig_fname, '/');
		if (!p) {
			p = orig_fname;
		} else {
			p++;
		}
		mr_asprintf(comment, "Verifying bigfile #%ld (%ld K)", current_biggiefile_number, (long) size >> 10);
		update_progress_form(comment);
		mr_free(comment);

		if (bkpinfo->restore_path) {
			mr_asprintf(logical_fname, "%s/%s", bkpinfo->restore_path, orig_fname);
			res = verify_a_biggiefile_from_stream(logical_fname, size);
			mr_free(logical_fname);
			retval += res;
		} else {
			log_it("Unable to verify bigfile as restore_path is NULL");
		}

		current_biggiefile_number++;
		g_current_progress++;
	}
	close_progress_form();
	paranoid_free(orig_fname);
	paranoid_free(curr_xattr_list_fname);
	paranoid_free(curr_acl_list_fname);
	return (retval);
}

/* @} - end of LLverifyGroup */


/**
 * Verify the USB device
 * @param bkpinfo The backup information structure. Fields used:
 * - @c bkpinfo->media_device
 * - @c bkpinfo->tmpdir
 * - @c bkpinfo->verify_data
 *
 * @return 0 for success (even if differences are found), nonzero for failure.
 * @ingroup verifyGroup
 */
int verify_usb_image()
{

	/*@ int ************************************************************ */
	int retval = 0;

	/*@ buffers ******************************************************** */
	char *mountpoint = NULL;
	char *tmp = NULL;
	char *tmp1 = NULL;
	char *fname = NULL;
	int ret = 0;
#ifdef __FreeBSD__
	char mdd[32];
	char *mddevice = mdd;
	int vndev = 2;
#else
//skip
#endif

	assert(bkpinfo != NULL);

	if (bkpinfo->media_device == NULL) {
		return(1);
	}

	mr_asprintf(fname, "%s1", bkpinfo->media_device);
	if (is_this_device_mounted(fname)) {
		log_msg(1, "USB device mounted. Remounting it at the right place");
		mr_asprintf(tmp, "umount %s", fname);
		run_program_and_log_output(tmp, FALSE);
		paranoid_free(tmp);
	}
	paranoid_free(fname);

	log_msg(1, "Mounting USB device.");
	mr_asprintf(mountpoint, "%s/usb", bkpinfo->tmpdir);
	mr_asprintf(tmp, "mkdir -p %s", mountpoint);
	run_program_and_log_output(tmp, FALSE);
	paranoid_free(tmp);
	/* Mindi always create one single parition on the USB dev */
	mr_asprintf(tmp, "mount %s1 %s", bkpinfo->media_device, mountpoint);
	ret = run_program_and_log_output(tmp, FALSE);
	paranoid_free(tmp);
	if (ret) {
		paranoid_free(mountpoint);
		return(ret);
	}

	sync();
	log_msg(2, "OK, I've mounted the USB Disk/Key");
	mr_asprintf(tmp, "%s/archives/NOT-THE-LAST", mountpoint);
	if (!does_file_exist(tmp)) {
		log_msg
			(2,
			 "This is the last USB device. I am therefore setting bkpinfo->verify_data to FALSE.");
		bkpinfo->verify_data = FALSE;
/*
   (a) It's an easy way to tell the calling subroutine that we've finished &
   there are no more device to be verified; (b) It stops the post-backup verifier
   from running after the per-device verifier has run too.
*/
	}
	paranoid_free(tmp);
	verify_afioballs_on_CD(mountpoint);
	log_it("before verify_all_slices");
	verify_all_slices_on_CD(mountpoint);

	mr_asprintf(tmp1, "umount -d %s", mountpoint);
#ifdef __FreeBSD__
	ret += system(tmp1);
	ret += kick_vn(mddevice);
	if (ret)
#else
	if (system(tmp1))
#endif
	{
		log_to_screen("%s failed; unable to unmount USB device\n", tmp1);
		retval++;
	} else {
		log_msg(2, "OK, I've unmounted the USB device");
	}
	mr_free(tmp1);
	mr_free(mountpoint);
	return (retval);
}


/**
 * Verify the CD indicated by @c g_current_media_number.
 * @param bkpinfo The backup information structure. Fields used:
 * - @c bkpinfo->isodir
 * - @c bkpinfo->prefix
 * - @c bkpinfo->manual_cd_tray
 * - @c bkpinfo->media_device
 * - @c bkpinfo->netfs_remote_dir
 * - @c bkpinfo->tmpdir
 * - @c bkpinfo->verify_data
 *
 * @return 0 for success (even if differences are found), nonzero for failure.
 * @ingroup verifyGroup
 */
int verify_cd_image()
{

	/*@ int ************************************************************ */
	int retval = 0;

	/*@ buffers ******************************************************** */
	char *mountpoint = NULL;
	char *command = NULL;
	char *tmp = NULL;
	char *fname = NULL;
#ifdef __FreeBSD__
	char mdd[32];
	char *mddevice = mdd;
	int ret = 0;
	int vndev = 2;
#else
//skip
#endif

	assert(bkpinfo != NULL);

	if (bkpinfo->media_device == NULL) {
		return(1);
	}

	mr_asprintf(mountpoint, "%s/cdrom", bkpinfo->tmpdir);
	if (((bkpinfo->isodir == NULL) && (bkpinfo->netfs_remote_dir == NULL)) || (bkpinfo->prefix == NULL)) {
		fatal_error("No iso filename preparation possible");
	}
	if (bkpinfo->netfs_remote_dir != NULL) {
		// NETFS
		mr_asprintf(fname, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->netfs_remote_dir, bkpinfo->prefix, g_current_media_number);
	} else {
		// ISO
		mr_asprintf(fname, "%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->prefix, g_current_media_number);
	}

	mkdir(mountpoint, 1777);
	sync();
	if (!does_file_exist(fname)) {
		log_msg(2, "%s not found; assuming you backed up to CD; verifying CD...", fname);
		if (bkpinfo->manual_cd_tray) {
			popup_and_OK("Please push CD tray closed.");
		}
		if (find_and_mount_actual_cd(mountpoint)) {
			log_to_screen("failed to mount actual CD");
			mr_free(mountpoint);
			mr_free(fname);
			return (1);
		}
	} else {
		log_msg(2, "%s found; verifying ISO...", fname);
#ifdef __FreeBSD__
		ret = 0;
		vndev = 2;
		mddevice = make_vn(fname);
		if (ret) {
			log_to_screen("make_vn of %s failed; unable to verify ISO\n", fname);
			mr_free(mountpoint);
			mr_free(fname);
			return (1);
		}
		mr_asprintf(command, "mount_cd9660 %s %s", mddevice, mountpoint);
#else
		mr_asprintf(command, "mount -o loop,ro -t iso9660 %s %s", fname, mountpoint);
#endif
		if (run_program_and_log_output(command, FALSE)) {
			log_to_screen("%s failed; unable to mount ISO image\n", command);
			mr_free(mountpoint);
			mr_free(command);
			mr_free(fname);
			return (1);
		}
		mr_free(command);
	}
	log_msg(2, "OK, I've mounted the ISO/CD");
	mr_asprintf(tmp, "%s/archives/NOT-THE-LAST", mountpoint);
	if (!does_file_exist(tmp)) {
		log_msg
			(2,
			 "This is the last CD. I am therefore setting bkpinfo->verify_data to FALSE.");
		bkpinfo->verify_data = FALSE;
/*
   (a) It's an easy way to tell the calling subroutine that we've finished &
   there are no more CD's to be verified; (b) It stops the post-backup verifier
   from running after the per-CD verifier has run too.
*/
	}
	mr_free(tmp);

	verify_afioballs_on_CD(mountpoint);
	log_it("before verify_all_slices");
	verify_all_slices_on_CD(mountpoint);

#ifdef __FreeBSD__
	ret = 0;
	mr_asprintf(command, "umount -d %s", mountpoint);
	ret += system(command);
	ret += kick_vn(mddevice);
	if (ret) {
#else
	mr_asprintf(command, "umount -d %s", mountpoint);
	if (system(command)) {
#endif
		log_to_screen("%s failed; unable to unmount ISO image\n", command);

		retval++;
	} else {
		log_msg(2, "OK, I've unmounted the ISO file");
	}
	mr_free(mountpoint);
	mr_free(command);

	if (!does_file_exist(fname)) {
		mr_asprintf(command, "umount -d %s", bkpinfo->media_device);
		run_program_and_log_output(command, 2);
		mr_free(command);

		if (!bkpinfo->please_dont_eject && eject_device(bkpinfo->media_device)) {
			log_msg(2, "Failed to eject CD-ROM drive");
		}
	}
	mr_free(fname);
	return (retval);
}

/**
 * Verify all backups on tape.
 * This should be done after the backup process has already closed the tape.
 * @param bkpinfo The backup information structure. Passed to various helper functions.
 * @return 0 for success (even if thee were differences), nonzero for failure.
 * @ingroup verifyGroup
 */
int verify_tape_backups()
{

	/*@ int ************************************************************ */
	int retval = 0;

	/*@ buffers ******************************************************** */
	char *tmp = NULL;
	char *changed_files_fname = NULL;

	/*@ long *********************************************************** */
	long diffs = 0;

	assert(bkpinfo != NULL);

	log_msg(3, "verify_tape_backups --- starting");
	log_to_screen("Verifying backups");
	openin_tape();
/* verify archives themselves */
	retval += verify_afioballs_from_stream();
	retval += verify_biggiefiles_from_stream();
/* find the final blocks */
	sync();
	sleep(2);
	closein_tape();
/* close tape; exit */
//  fclose(g_tape_stream); <-- not needed; is handled by closein_tape()
	mr_asprintf(tmp, "rm -f %s/biggies.changed %s/changed.files 2> /dev/null", bkpinfo->tmpdir, bkpinfo->tmpdir);
	paranoid_system(tmp);
	mr_free(tmp);

	mr_asprintf(changed_files_fname, "%s/changed.files", bkpinfo->tmpdir);
	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);
	log_msg(2, "Running command to derive list of changed files");
	log_msg(2, tmp);
	if (system(tmp)) {
		if (does_file_exist(changed_files_fname) && length_of_file(changed_files_fname) > 2) {
			log_to_screen("Warning - unable to check logfile to derive list of changed files");
		} else {
			log_to_screen("No differences found. Therefore, no 'changed.files' text file.");
		}
	}
	mr_free(tmp);

	mr_asprintf(tmp, "cat %s/biggies.changed >> %s", bkpinfo->tmpdir, changed_files_fname);
	paranoid_system(tmp);
	mr_free(tmp);

	diffs = count_lines_in_file(changed_files_fname);
	if (diffs > 0) {
		mr_asprintf(tmp, "cp -f %s %s/changed.files", changed_files_fname, MONDO_CACHE);
		run_program_and_log_output(tmp, FALSE);
		mr_free(tmp);

		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);
		log_to_screen("See "MONDO_CACHE"/changed.files for a list of nonmatching files.");
		log_to_screen("The files probably changed on filesystem, not on backup media.");
	}
	mr_free(changed_files_fname);
	return (retval);
}



