示例#1
0
int file_test(badblocks_list bb)
{
	badblocks_list new_bb = 0;
	errcode_t	retval;
	FILE	*f;

	f = tmpfile();
	if (!f) {
		fprintf(stderr, "Error opening temp file: %s\n",
			error_message(errno));
		return 1;
	}
	retval = ext2fs_write_bb_FILE(bb, 0, f);
	if (retval) {
		com_err("file_test", retval, "while writing bad blocks");
		return 1;
	}

	rewind(f);
	retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
	if (retval) {
		com_err("file_test", retval, "while reading bad blocks");
		return 1;
	}
	fclose(f);

	if (ext2fs_badblocks_equal(bb, new_bb)) {
		printf("Block bitmap matched after reading and writing.\n");
	} else {
		printf("Block bitmap NOT matched.\n");
		test_fail++;
	}
	return 0;
}
int file_test(badblocks_list bb)
{
    char	tmp_filename[20] = "#testXXXXXX";
    badblocks_list new_bb = 0;
    errcode_t	retval;
    FILE	*f;

    mktemp(tmp_filename);

    unlink(tmp_filename);
    f = fopen(tmp_filename, "w");
    if (!f) {
        fprintf(stderr, "Error opening temp file %s: %s\n",
                tmp_filename, error_message(errno));
        return 1;
    }
    retval = ext2fs_write_bb_FILE(bb, 0, f);
    if (retval) {
        com_err("file_test", retval, "while writing bad blocks");
        return 1;
    }
    fclose(f);

    f = fopen(tmp_filename, "r");
    if (!f) {
        fprintf(stderr, "Error re-opening temp file %s: %s\n",
                tmp_filename, error_message(errno));
        return 1;
    }
    retval = ext2fs_read_bb_FILE2(0, f, &new_bb, 0, 0);
    if (retval) {
        com_err("file_test", retval, "while reading bad blocks");
        return 1;
    }
    fclose(f);

    if (ext2fs_badblocks_equal(bb, new_bb)) {
        printf("Block bitmap matched after reading and writing.\n");
    } else {
        printf("Block bitmap NOT matched.\n");
        test_fail++;
    }
    return 0;
}