imagedelta.c 9.51 KB
Newer Older
1
/*
2
 * Copyright (c) 2000-2015 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
 */

#define FIXMAP_DEBUG

/*
27
 * imagedelta [ -S -f ] image1.ndz image2.ndz delta1to2.ndz
28 29 30
 *
 * Take two images (image1, image2) and produce a delta (delta1to2)
 * based on the differences. The -S option says to use the signature
31 32 33 34 35 36 37 38
 * files: image1.ndz.sig and image2.ndz.sig, if possible to determine
 * differences between the images. Signature files will be rejected
 * unless they can be positively matched with the image (right now,
 * via the modtime!) Using -f will force it to use a questionable
 * signature file.
 *
 * Without signature files, we compare the corresponding areas of both
 * images to determine if they are different.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
 *
 * Note that order matters here! We are generating a delta to get from
 * "image1" to "image2"; i.e., doing:
 *
 *  imageunzip image1.ndz /dev/da0
 *  imageunzip delta1to2.ndz /dev/da0
 *
 * would be identical to:
 *
 *  imageunzip image2.ndz /dev/da0
 *
 * Approach:
 *
 * We scan the chunks headers of both images (image1, image2) to produce
 * allocated range lists for both (R1, R2). We use these to produce a
 * range list for the delta (RD) as follows.
 *
 * - Anything that is in R1 but not R2 does not go in RD.
 * - Anything in R2 but not in R1 must go into RD.
 * - For overlapping areas, we read and hash or compare both and,
 *   if different, include in RD.
 * - Using RD, select data from image2 that need to be read, decompressed
 *   and then recompressed into the new image.
 *
 * There is the usual issue of dealing with the difference in granularity
 * and alignment of ranges (arbitrary multiples of 512 byte) vs. hash
 * blocks (64K byte), but that logic exists in imagezip today.
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <zlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#ifndef NOTHREADS
#include <pthread.h>
#endif

#include "imagehdr.h"
#include "libndz/libndz.h"

struct ifileinfo {
    struct ndz_file *ndz;
88
    char *sigfile;
89 90 91 92 93 94 95 96 97
    struct ndz_rangemap *map, *sigmap;
} ndz1, ndz2;

struct mergestate {
    unsigned int ochunkno;
    unsigned int nchunkno;
};

int usesigfiles = 0;
98
int forcesig = 0;
99 100 101 102 103 104 105

static int fixmap(struct ndz_rangemap *map, struct ndz_range *range, void *arg);

void
usage(void)
{
    fprintf(stderr,
106
	    "Usage: imagedelta [-Sf] image1.ndz image2.ndz delta1to2.ndz\n"
107 108 109 110
	    "\n"
	    "Produce a delta image (delta1to2) containing the changes\n"
	    "necessary to get from image1 to image2.\n"
	    "\n"
111 112
	    "  -S   Use signature files when computing differences.\n"
	    "  -f   With -S, force imagedelta to use a questionable sigfile.\n");
113 114 115 116 117 118 119 120 121 122 123
    exit(1);
}

/*
 * File must exist and be readable.
 * If usesigfiles is set, signature file must exist as well.
 * Reads in the range map and signature as well.
 */
void
openifile(char *file, struct ifileinfo *info)
{
124
    int sigfd;
125 126 127 128 129 130 131 132 133

    info->ndz = ndz_open(file, 0);
    if (info->ndz == NULL) {
	fprintf(stderr, "%s: could not open as NDZ file\n",
		ndz_filename(info->ndz));
	exit(1);
    }

    if (usesigfiles) {
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	struct stat sb1, sb2;

	info->sigfile = malloc(strlen(file) + 5);
	assert(info->sigfile != NULL);
	strcpy(info->sigfile, file);
	strcat(info->sigfile, ".sig");
	sigfd = open(info->sigfile, 0);
	if (sigfd < 0) {
	    fprintf(stderr, "%s: could not find signature file %s\n",
		    file, info->sigfile);
	    exit(1);
	}
	if (fstat(info->ndz->fd, &sb1) < 0 || fstat(sigfd, &sb2) < 0) {
	    fprintf(stderr, "%s: could stat image or signature file\n", file);
	    exit(1);
	}
	if (!forcesig && sb1.st_mtime != sb2.st_mtime) {
	    fprintf(stderr, "%s: image and signature disagree, "
		    "use -f to override.\n", file);
	    exit(1);
	}
	close(sigfd);
156
    }
157 158 159 160 161
}

void
readifile(struct ifileinfo *info)
{
162 163

    /* read range info from image */
164 165
    info->map = ndz_readranges(info->ndz);
    if (info->map == NULL) {
166 167 168 169 170 171 172
	fprintf(stderr, "%s: could not read ranges\n",
		ndz_filename(info->ndz));
	exit(1);
    }

    /* read signature info */
    if (usesigfiles) {
173 174 175 176 177 178 179 180
	info->sigmap = ndz_readhashinfo(info->ndz, info->sigfile);
	if (info->sigmap == NULL) {
	    fprintf(stderr, "%s: could not read signature info\n",
		    ndz_filename(info->ndz));
	    exit(1);
	}
    } else
	info->sigmap = NULL;
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}

static void
chunkfunc(void *ptr)
{
    unsigned int chunkno = (int)ptr;
    printf("chunkno=%u", chunkno);
}

static void
twochunkfunc(void *ptr)
{
    struct mergestate *state = (struct mergestate *)ptr;
    printf("ochunkno=%u, nchunkno=%u", state->ochunkno, state->nchunkno);
}

int
main(int argc, char **argv)
{
    int ch, version = 0;
    extern char build_info[];
202
    int delta;
203 204 205 206 207 208 209
    struct ndz_rangemap *mmap;

    while ((ch = getopt(argc, argv, "Sv")) != -1)
	switch(ch) {
	case 'S':
	    usesigfiles = 1;
	    break;
210 211 212
	case 'f':
	    forcesig = 1;
	    break;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	case 'v':
	    version++;
	    break;
	case 'h':
	case '?':
	default:
	    usage();
	}
    argc -= optind;
    argv += optind;

    if (argc < 3)
	usage();

    /*
     * Make sure we can open all the files
     */
    ndz1.ndz = ndz_open(argv[0], 0);
    if (ndz1.ndz == NULL)
	usage();

    ndz2.ndz = ndz_open(argv[1], 0);
    if (ndz2.ndz == NULL)
	usage();

    delta = open(argv[2], O_RDWR|O_CREAT|O_TRUNC, 0666);
    if (delta < 0) {
	perror(argv[2]);
	usage();
    }

    /*
     * Read in the range and signature info.
     */
    ndz1.map = ndz_readranges(ndz1.ndz);
    if (ndz1.map == NULL) {
	fprintf(stderr, "%s: could not read ranges\n",
		ndz_filename(ndz1.ndz));
	exit(1);
    }
#if 1
    printf("==== Old ");
    ndz_rangemap_dump(ndz1.map, chunkfunc);
    fflush(stdout);
#endif
    ndz2.map = ndz_readranges(ndz2.ndz);
    if (ndz2.map == NULL) {
	fprintf(stderr, "%s: could not read ranges\n",
		ndz_filename(ndz2.ndz));
	exit(1);
    }
#if 1
    printf("==== New ");
    ndz_rangemap_dump(ndz2.map, chunkfunc);
    fflush(stdout);
#endif

    /*
     * Roll through the new image map looking for overlaps with
     * the old image.
     */
    mmap = ndz_rangemap_init(ndz2.map->loaddr, ndz2.map->hiaddr);
    assert(mmap != NULL);

    (void) ndz_rangemap_iterate(ndz2.map, fixmap, mmap);
#if 1
    printf("==== Merged ");
    ndz_rangemap_dump(mmap, twochunkfunc);
    fflush(stdout);
#endif

284 285 286 287 288 289
    /*
     * Iterate through the produced map either copying the data
     * directly into the new image (data == NULL) or hashing the
     * two versions to determine if it should be copied.
     */

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    return 0;
}

static int
fixmap(struct ndz_rangemap *map, struct ndz_range *range, void *arg)
{
    struct ndz_rangemap *mmap = (struct ndz_rangemap *)arg;
    ndz_addr_t addr, eaddr, oaddr, oeaddr;
    struct ndz_range *orange, *oprev;
    int rv;

    addr = range->start;
    eaddr = range->end;

#ifdef FIXMAP_DEBUG
    fprintf(stderr, "fixmap [%lu - %lu]:\n", addr, eaddr);
#endif

    /* Allocate a range in the merge map */
    rv = ndz_rangemap_alloc(mmap, addr, eaddr-addr+1, NULL);
    assert(rv == 0);

    orange = ndz_rangemap_lookup(ndz1.map, addr, &oprev);

    /* Current map address in not in old range */
    if (orange == NULL) {
	if (oprev == NULL)
	    oprev = &ndz1.map->head;
	/* Nothing more in the old map, we are done */
	if ((orange = oprev->next) == NULL) {
#ifdef FIXMAP_DEBUG
	    fprintf(stderr, "  finished old map\n");
#endif
	    return 1;
	}
#ifdef FIXMAP_DEBUG
	fprintf(stderr, "  found oldrange [%lu - %lu]:\n", orange->start, orange->end);
#endif
	/* No overlap at all with current map range, move on */
	if (orange->start > eaddr) {
#ifdef FIXMAP_DEBUG
	    fprintf(stderr, "  no overlap in oldrange\n");
#endif
	    return 0;
	}
    }
#ifdef FIXMAP_DEBUG
    else
	fprintf(stderr, "  found oldrange [%lu - %lu]:\n", orange->start, orange->end);
#endif

    while ((oaddr = orange->start) <= eaddr) {
	struct mergestate *state;

	/* Determine the extent of the overlap */
	if (oaddr < addr)
	    oaddr = addr;
	if ((oeaddr = orange->end) > eaddr)
	    oeaddr = eaddr;

#ifdef FIXMAP_DEBUG
	fprintf(stderr, "    found overlap [%lu - %lu]:\n", oaddr, oeaddr);
#endif
	/* Dealloc this part of the range and realloc with flag set */
	rv = ndz_rangemap_dealloc(mmap, oaddr, oeaddr-oaddr+1);
	assert(rv == 0);
	state = calloc(1, sizeof *state);
	assert(state);
	state->ochunkno = (unsigned int)orange->data;
	state->nchunkno = (unsigned int)range->data;
	rv = ndz_rangemap_alloc(mmap, oaddr, oeaddr-oaddr+1, state);
	assert(rv == 0);

	/* on to the next old map entry */
	orange = orange->next;

	/* end of old map, we are done */
	if (orange == NULL) {
#ifdef FIXMAP_DEBUG
	    fprintf(stderr, "  finished old map\n");
#endif
	    return 1;
	}
#ifdef FIXMAP_DEBUG
	fprintf(stderr, "  next oldrange [%lu - %lu]:\n", orange->start, orange->end);
#endif

    }

    return 0;
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * End:
 */