--- libaitsync/src/aitsync.c 2010/03/24 16:00:15 1.1 +++ libaitsync/src/aitsync.c 2011/05/09 14:36:33 1.2 @@ -3,9 +3,46 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsync.c,v 1.1 2010/03/24 16:00:15 misho Exp $ +* $Id: aitsync.c,v 1.2 2011/05/09 14:36:33 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 + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 + by Michael Pounov . 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 +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" @@ -56,23 +93,28 @@ inline void syncSetErr(int eno, char *estr, ...) * syncSignature() Calculate and create signature for diff * @csInput = Input patched 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 syncSignature(const char *csInput, const char *csSig, int compress) { - int inf, outf, ret; + 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]; inf = syncOpen(csInput, O_RDONLY); if (inf == -1) return inf; - outf = syncOpen(csSig, O_WRONLY); - if (outf == -1) { + if (compress & 2) + f = syncTemp(szTemp, MAXPATHLEN); + else + f = syncOpen(csSig, O_WRONLY); + if (f == -1) { syncClose(inf); - return outf; + return f; } for (i = 0, off = 0ll, ret = -1; ret; i++, off += ret) { @@ -86,13 +128,34 @@ int syncSignature(const char *csInput, const char *csS // fill chunk sync_mksig(i, off, buf, ret, &sc); - if (write(outf, &sc, sizeof sc) == -1) { + if (write(f, &sc, sizeof sc) == -1) { SETERR; break; } } - syncClose(outf); + /* Signatures is READY */ + + // build compressed delta file + if (compress & 2) { + outf = syncOpen(csSig, O_WRONLY); + if (outf == -1) { + ret = outf; + goto end; + } + if (sync_Deflate(f, outf, Z_DEFAULT_COMPRESSION) == -1) { + syncClose(outf); + unlink(csSig); + ret = -1; + goto end; + } + syncClose(outf); + } +end: + syncClose(f); + if (compress & 2) + unlink(szTemp); + syncClose(inf); return ret; } @@ -102,7 +165,7 @@ int syncSignature(const char *csInput, const char *csS * @csInput = Input original source file name for make delta patch file * @csSig = Input Signature file name * @csDelta = Output Delta patch file name - * @compress = Compress output, 0 not compressed + * @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) @@ -120,17 +183,40 @@ int syncDelta(const char *csInput, const char *csSig, /* load signatures */ - sigf = syncOpen(csSig, O_RDONLY); - if (sigf == -1) { - return sigf; + if (compress & 2) { + f = syncOpen(csSig, O_RDONLY); + if (-1 == f) + return f; + sigf = syncTemp(szTemp, MAXPATHLEN); + if (-1 == sigf) { + syncClose(f); + return sigf; + } + + if (sync_Inflate(f, sigf) == -1) { + syncClose(sigf); + syncClose(f); + unlink(szTemp); + return -1; + } else + syncClose(f); + } else { + sigf = syncOpen(csSig, O_RDONLY); + if (-1 == sigf) + return sigf; } + if (fstat(sigf, &sb) == -1) { SETERR; syncClose(sigf); + if (compress & 2) + unlink(szTemp); return -1; } else { if (!sb.st_size) { syncClose(sigf); + if (compress & 2) + unlink(szTemp); return 1; } @@ -138,6 +224,8 @@ int syncDelta(const char *csInput, const char *csSig, if (sb.st_size % sizeof(sync_chunk_t)) { syncSetErr(ENOEXEC, "Error:: signature file is broken!\n"); syncClose(sigf); + if (compress & 2) + unlink(szTemp); return -1; } } @@ -145,9 +233,14 @@ int syncDelta(const char *csInput, const char *csSig, if (MAP_FAILED == chunks) { SETERR; syncClose(sigf); + if (compress & 2) + unlink(szTemp); return -1; + } else { + syncClose(sigf); + if (compress & 2) + unlink(szTemp); } - syncClose(sigf); /* build from signatures sorted index and hashes */ @@ -181,7 +274,7 @@ int syncDelta(const char *csInput, const char *csSig, munmap(chunks, sb.st_size); return inf; } - if (compress) + if (compress & 1) f = syncTemp(szTemp, MAXPATHLEN); else f = syncOpen(csDelta, O_WRONLY); @@ -294,7 +387,7 @@ int syncDelta(const char *csInput, const char *csSig, /* Delta patch is READY */ // build compressed delta file - if (compress) { + if (compress & 1) { outf = syncOpen(csDelta, O_WRONLY); if (outf == -1) { ret = outf; @@ -311,7 +404,8 @@ int syncDelta(const char *csInput, const char *csSig, end: syncClose(f); - unlink(szTemp); + if (compress & 1) + unlink(szTemp); syncClose(inf); free(tag_table); @@ -324,7 +418,7 @@ end: * @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 = Compress output, 0 not compressed + * @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) @@ -339,7 +433,7 @@ int syncPatch(const char *csInput, const char *csDelta off_t off; sync_chunk_t sc, *suffix; - if (compress) { + if (compress & 1) { f = syncOpen(csDelta, O_RDONLY); if (f == -1) return f; @@ -365,7 +459,7 @@ int syncPatch(const char *csInput, const char *csDelta if (fstat(d, &sb) == -1) { SETERR; syncClose(d); - if (compress) + if (compress & 1) unlink(szTemp); return -1; } @@ -373,12 +467,12 @@ int syncPatch(const char *csInput, const char *csDelta if (MAP_FAILED == delta) { SETERR; syncClose(d); - if (compress) + if (compress & 1) unlink(szTemp); return -1; } else { syncClose(d); - if (compress) + if (compress & 1) unlink(szTemp); }