File:  [ELWIX - Embedded LightWeight unIX -] / libaitsync / src / aitsync.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Tue Feb 4 16:58:17 2014 UTC (10 years, 3 months ago) by misho
Branches: MAIN
CVS tags: sync2_3, sync2_2, SYNC2_2, SYNC2_1, HEAD
version 2.1

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: aitsync.c,v 1.6 2014/02/04 16:58:17 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004 - 2014
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"
#include "tool.h"
#include "zc.h"
#include "patch.h"
#include "file.h"


static int sync_Errno;
static char sync_Error[STRSIZ];


static inline int
func_comp(sync_tag_t const *t1, sync_tag_t const *t2)
{
	return t1->st_tag - t2->st_tag;
}

/*
 * Error maintenance functions ...
 */

// sync_GetErrno() Get error code of last operation
int
sync_GetErrno()
{
	return sync_Errno;
}

// sync_GetError() Get error text of last operation
const char *
sync_GetError()
{
	return sync_Error;
}

// sync_SetErr() Set error to variables for internal use!!!
void
sync_SetErr(int eno, char *estr, ...)
{
	va_list lst;

	sync_Errno = eno;
	memset(sync_Error, 0, sizeof sync_Error);
	va_start(lst, estr);
	vsnprintf(sync_Error, sizeof sync_Error, estr, lst);
	va_end(lst);
}

/* ---------------------------------------------------------- */

/*
 * syncSignature() - Calculate and create signature for diff
 *
 * @csInput = Input target file name for calculating check sums
 * @csSig = Output Signature file name
 * @compress = 2 compress signatures output, 0 not compressed
 * return: -1 error, 0 ok
 */
int
syncSignature(const char *csInput, const char *csSig, int compress)
{
	int inf, outf, f, ret;
	u_char buf[CHUNK_MAX];
	register int i = 0;
	off_t off = 0ll;
	sync_chunk_t sc;
	char szTemp[MAXPATHLEN];

	/* open work files */
	inf = sync_Open(csInput, O_RDONLY, 0);
	if (inf == -1)
		return inf;
	if (compress & 2)
		f = sync_Temp(szTemp, sizeof szTemp);
	else
		f = sync_Open(csSig, O_WRONLY, 0);
	if (f == -1) {
		sync_Close(inf);
		return f;
	}

	for (i = 0, off = 0ll, ret = -1; ret; i++, off += ret) {
		memset(buf, 0, CHUNK_MAX);
		ret = read(inf, buf, CHUNK_MAX);
		if (ret == -1) {
			LOGERR;
			break;
		}

		/* fill chunk */
		sync_mksig(i, off, buf, ret, &sc);

		if (write(f, &sc, sizeof sc) == -1) {
			LOGERR;
			break;
		}
	}

	/* Signatures are READY */

	if (compress & 2) {
		/* build compressed delta file */
		outf = sync_Open(csSig, O_WRONLY, 0);
		if (outf == -1) {
			ret = outf;
			goto end;
		}
		if (sync_Deflate(f, outf, Z_DEFAULT_COMPRESSION) == -1) {
			sync_Close(outf);
			unlink(csSig);
			ret = -1;
			goto end;
		}
		sync_Close(outf);
	}
end:
	sync_Close(f);
	if (compress & 2)
		unlink(szTemp);
	sync_Close(inf);
	return ret;
}

/*
 * syncDelta() - Create Delta patch file
 *
 * @csInput = Input original source file name for make delta patch file
 * @csSig = Input target Signature file name
 * @csDelta = Output Delta patch file name
 * @compress = 3 everything compress, 2 compressed signatures, 1 compress delta output, 0 not compressed
 * return: -1 error, 0 ok
 */
int
syncDelta(const char *csInput, const char *csSig, const char *csDelta, int compress)
{
	int inf, outf, f, sigf, ret, cnt;
	size_t blk;
	register int i, j, c, cx;
	struct stat sb, sb_f;
	u_long tags[TABLESIZ];
	sync_tag_t *tag_table;
	sync_chunk_t *chunks, *find, sc;
	u_char buf[CHUNK_MAX];
	off_t off;
	char szTemp[MAXPATHLEN];

	/* load signatures */

	if (compress & 2) {
		f = sync_Open(csSig, O_RDONLY, 0);
		if (-1 == f)
			return f;
		sigf = sync_Temp(szTemp, sizeof szTemp);
		if (-1 == sigf) {
			sync_Close(f);
			return sigf;
		}

		if (sync_Inflate(f, sigf) == -1) {
			sync_Close(sigf);
			sync_Close(f);
			unlink(szTemp);
			return -1;
		} else
			sync_Close(f);
	} else {
		sigf = sync_Open(csSig, O_RDONLY, 0);
		if (-1 == sigf)
			return sigf;
	}

	if (fstat(sigf, &sb) == -1) {
		LOGERR;
		sync_Close(sigf);
		if (compress & 2)
			unlink(szTemp);
		return -1;
	} else {
		if (!sb.st_size) {
			sync_Close(sigf);
			if (compress & 2)
				unlink(szTemp);
			return 1;
		}

		cnt = sb.st_size / sizeof(sync_chunk_t);
		if (sb.st_size % sizeof(sync_chunk_t)) {
			sync_SetErr(ENOEXEC, "Error:: signature file is broken!\n");
			sync_Close(sigf);
			if (compress & 2)
				unlink(szTemp);
			return -1;
		}
	}
	chunks = (sync_chunk_t*) mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, sigf, 0);
	if (MAP_FAILED == chunks) {
		LOGERR;
		sync_Close(sigf);
		if (compress & 2)
			unlink(szTemp);
		return -1;
	} else {
		sync_Close(sigf);
		if (compress & 2)
			unlink(szTemp);
	}

	/* build from signatures sorted index and hashes */

	/* init first stage tags array index */
	for (i = 0; i < TABLESIZ; i++)
		tags[i] = NULL_TAG;

	/* build second index from signature blocks */
	tag_table = (sync_tag_t*) e_calloc(cnt, sizeof(sync_tag_t));
	if (!tag_table) {
		LOGERR;
		munmap(chunks, sb.st_size);
		return -1;
	} else {
		for (i = 0; i < cnt; i++) {
			tag_table[i].st_id = i;
			tag_table[i].st_tag = GETTAG(chunks[i].sc_roll);
		}

		qsort(tag_table, cnt, sizeof(sync_tag_t), 
				(int (*)(const void *, const void *)) func_comp);
	}
	/* assign less id position in tag_table to tags. 
	 * It made relation between 1st & 2nd indexes */
	for (i = cnt - 1; i > -1; i--)
		tags[tag_table[i].st_tag] = i;


	/* build delta patch */

	inf = sync_Open(csInput, O_RDONLY, 0);
	if (inf == -1) {
		e_free(tag_table);
		munmap(chunks, sb.st_size);
		return inf;
	}
	if (compress & 1)
		f = sync_Temp(szTemp, sizeof szTemp);
	else
		f = sync_Open(csDelta, O_WRONLY, 0);
	if (f == -1) {
		sync_Close(inf);
		e_free(tag_table);
		munmap(chunks, sb.st_size);
		return f;
	}

	for (i = 0, off = 0ll, ret = -1, blk = 0; 
			(ret = read(inf, buf, CHUNK_MAX)); i++, off += ret) {
		if (ret == -1) {
			LOGERR;
			break;
		}
		find = NULL;

#if 0
		printf("+ find=%p off=%llu i=%d blk=%d\n", find, off, i, blk);
#endif

		/* check chunk for differences with signature */
		sync_mksig(i, off, buf, ret, &sc);
		cx = GETTAG(sc.sc_roll);
		/* find in hash -> hash_sorted_table */
		if (NULL_TAG != tags[cx] && tag_table[tags[cx]].st_tag == cx) {
			/* find in hash_sorted_table crc == -> real chunks id */
			for (j = 0, c = tag_table[tags[cx]].st_id; 
					tag_table[tags[cx] + j].st_tag == cx; 
					j++, c = tag_table[tags[cx] + j].st_id) {
				if (chunks[c].sc_magic == sc.sc_magic && 
						chunks[c].sc_len == sc.sc_len && 
						chunks[c].sc_roll == sc.sc_roll && 
						!memcmp(chunks[c].sc_cksum, sc.sc_cksum, MD5_DIGEST_LENGTH)) {
					find = &chunks[c];
					break;
				}
			}
		}

#if 0
		printf("+ find=%p off=%llu i=%d blk=%d\n", find, off, i, blk);
#endif

		/* if match chunk, check for previous match */
		if (!blk && find)
			continue;
		/* if not find chunk in signature skip write to delta patch */
		if (!find) {
			/* different piece, write it! 
			 * Write signature of current chunk */
			ret = write(f, &sc, sizeof sc);
			if (-1 == ret) {
				LOGERR;
				break;
			}
			/* if write chunk len is differnt from requested len */
			if (ret != sizeof sc) {
				sync_SetErr(ENOEXEC, "Error:: delta file signature is broken!\n");
				ret = -1;
				break;
			}
			/* write current chunk data ... */
			ret = write(f, buf, sc.sc_len);
			if (-1 == ret) {
				LOGERR;
				break;
			}
			/* if write chunk len is differnt from requested len */
			if (ret != sc.sc_len) {
				sync_SetErr(ENOEXEC, "Error:: delta file data is broken!\n");
				ret = -1;
				break;
			}
			blk += sc.sc_len;

			continue;
		}
		/* match 1st block after difference and copy signature from B */
		memcpy(&sc, find, sizeof sc);
		sc.sc_magic = SIGSYNC_MAGIC;
		sc.sc_len = blk;

		/* write signature from chunk B */
		blk = write(f, &sc, sizeof sc);
		if (-1 == blk) {
			LOGERR;
			break;
		}
		/* if write chunk len is differnt from requested len */
		if (blk != sizeof sc) {
			sync_SetErr(ENOEXEC, "Error:: delta file end signature is broken!\n");
			ret = -1;
			break;
		}

		blk ^= blk;
	}

	/* check for error or empty delta file */
	if (ret == -1)
		goto end;
	fsync(f);
	if (fstat(f, &sb_f) == -1) {
		LOGERR;
		ret = -1;
		goto end;
	}

	/* No deferences, not needed delta.patch !!! */
	if (!sb_f.st_size) {
		ret = 1;
		goto end;
	}

	/* Delta patch is READY */

	/* build compressed delta file */
	if (compress & 1) {
		outf = sync_Open(csDelta, O_WRONLY, 0);
		if (outf == -1) {
			ret = outf;
			goto end;
		}
		if (sync_Deflate(f, outf, Z_DEFAULT_COMPRESSION) == -1) {
			sync_Close(outf);
			unlink(csDelta);
			ret = -1;
			goto end;
		}
		sync_Close(outf);
	}

end:
	sync_Close(f);
	if (compress & 1)
		unlink(szTemp);
	sync_Close(inf);
	e_free(tag_table);
	munmap(chunks, sb.st_size);
	return ret;
}

/*
 * syncPatch() - Apply delta patch file to target
 *
 * @csInput = Input target file name for patch
 * @csDelta = Input Delta patch file name
 * @csPatch = After applied patch create new alternate target file, if != NULL
 * @compress = 1 compress delta input, 0 not compressed
 * return: -1 error, 0 ok, create delta patch, 1 ok, no differences and not create patch
 */
int
syncPatch(const char *csInput, const char *csDelta, const char *csPatch, int compress)
{
	int inf, outf, f, d, ret, readlen;
	char szTemp[MAXPATHLEN];
	u_char *buffer, buf[CHUNK_MAX];
	struct stat sb;
	void *delta;
	struct tagPiece *piece, *pieces = NULL;
	register int i;
	off_t off;
	sync_chunk_t sc, *suffix;

	if (compress & 1) {
		f = sync_Open(csDelta, O_RDONLY, 0);
		if (f == -1)
			return f;
		d = sync_Temp(szTemp, sizeof szTemp);
		if (d == -1) {
			sync_Close(f);
			return d;
		}
		
		if (sync_Inflate(f, d) == -1) {
			sync_Close(d);
			sync_Close(f);
			unlink(szTemp);
			return -1;
		} else
			sync_Close(f);
	} else {
		d = sync_Open(csDelta, O_RDONLY, 0);
		if (d == -1)
			return d;
	}

	if (fstat(d, &sb) == -1) {
		LOGERR;
		sync_Close(d);
		if (compress & 1)
			unlink(szTemp);
		return -1;
	}
	delta = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, d, 0);
	if (MAP_FAILED == delta) {
		LOGERR;
		sync_Close(d);
		if (compress & 1)
			unlink(szTemp);
		return -1;
	} else {
		sync_Close(d);
		if (compress & 1)
			unlink(szTemp);
	}

	if (sync_buildPatch(delta, sb.st_size, &pieces) == -1 || !pieces) {
		sync_SetErr(ENOEXEC, "Error:: patch file is broken!\n");
		munmap(delta, sb.st_size);
		return -1;
	}

	inf = sync_Open(csInput, O_RDONLY, 0);
	if (inf == -1) {
		e_free(pieces);
		munmap(delta, sb.st_size);
		return inf;
	}
	outf = sync_Open(csPatch, O_WRONLY, 0);
	if (outf == -1) {
		sync_Close(inf);
		e_free(pieces);
		munmap(delta, sb.st_size);
		return outf;
	}

	if (fstat(inf, &sb) == -1) {
		LOGERR;
		ret = -1;
		goto end;
	} else {
		if (!sb.st_size) {
			ret = -1;
			goto end;
		}
	}

	ret = readlen = 0;
	buffer = NULL;
	for (i = 0, off = 0ll, suffix = NULL, piece = pieces; piece->pfx; 
			i++, off += readlen) {
#if 0
		printf("i=%d off=%llu sfx=%p piece=%p\n", i, off, suffix, piece);
#endif

		/* if input offset is less then input file size */
		if (off < sb.st_size) {
			readlen = read(inf, buf, CHUNK_MAX);
			if (readlen == -1) {
				LOGERR;
				ret = -1;
				break;
			}
			/* if suffix find, check for correct patch */
			if (suffix) {
				if (suffix->sc_len != readlen || 
						suffix->sc_off != off) {
					sync_SetErr(ENOEXEC, "Error:: patch file is broken! "
							"(wrong suffix pos)\n");
					ret = -1;
					break;
				}
				sync_mksig(i, off, buf, readlen, &sc);
				if (sc.sc_roll != suffix->sc_roll || 
						memcmp(sc.sc_cksum, suffix->sc_cksum, 
							MD5_DIGEST_LENGTH)) {
					sync_SetErr(ENOEXEC, "Error:: patch file is broken! "
							"(wrong suffix crc)\n");
					ret = -1;
					break;
				}

				suffix = NULL;
			}

			buffer = buf;
		}

#if 0
		printf("i=%d off=%llu sfx=%p piece=%p pfx=%p pfx_off=%llu\n", i, off, 
				suffix, piece, piece ? piece->pfx : 0l, 
				piece->pfx ? piece->pfx->sc_off : 0l);
#endif

		/* if delta chunk match! */
		if (piece->pfx && piece->pfx->sc_off == off) {
			if (!piece->buf) {
				sync_SetErr(ENOEXEC, "Error:: patch file is broken! "
						"(missing data)\n");
				ret = -1;
				break;
			}

			buffer = piece->buf;
			readlen = piece->pfx->sc_len;
			suffix = piece->sfx ? piece->sfx : NULL;

			piece++;

			if (suffix && off >= sb.st_size) {
				sync_SetErr(ENOEXEC, "Error:: patch file is broken! "
						"(after eof find suffix)\n");
				ret = -1;
				break;
			}
		} else if (off >= sb.st_size) {
		       if (piece->pfx) {
				sync_SetErr(ENOEXEC, "Error:: patch file is broken! "
						"(after eof find prefix)\n");
				ret = -1;
			}

			break;
		}

		ret = write(outf, buffer, readlen);
		if (ret == -1 || ret != readlen) {
			LOGERR;
			break;
		}
	}

end:
	sync_Close(inf);
	sync_Close(outf);
	e_free(pieces);
	munmap(delta, sb.st_size);
	return ret;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>