--- libaitsync/src/aitsync.c 2010/03/24 16:00:15 1.1 +++ libaitsync/src/aitsync.c 2012/07/22 22:09:18 1.2.2.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.2.2 2012/07/22 22:09:18 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, 2012 + 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" @@ -17,7 +54,8 @@ static int sync_Errno; static char sync_Error[STRSIZ]; -static inline int func_comp(sync_tag_t const *t1, sync_tag_t const *t2) +static inline int +func_comp(sync_tag_t const *t1, sync_tag_t const *t2) { return t1->st_tag - t2->st_tag; } @@ -27,19 +65,22 @@ static inline int func_comp(sync_tag_t const *t1, sync // // sync_GetErrno() Get error code of last operation -inline int sync_GetErrno() +inline int +sync_GetErrno() { return sync_Errno; } // sync_GetError() Get error text of last operation -inline const char *sync_GetError() +inline const char * +sync_GetError() { return sync_Error; } // sync_SetErr() Set error to variables for internal use!!! -inline void syncSetErr(int eno, char *estr, ...) +inline void +syncSetErr(int eno, char *estr, ...) { va_list lst; @@ -56,23 +97,29 @@ 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 +133,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,10 +170,11 @@ 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) +int +syncDelta(const char *csInput, const char *csSig, const char *csDelta, int compress) { int inf, outf, f, sigf, ret, cnt; size_t blk; @@ -120,17 +189,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 +230,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 +239,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 +280,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 +393,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 +410,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,10 +424,11 @@ 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) +int +syncPatch(const char *csInput, const char *csDelta, const char *csPatch, int compress) { int inf, outf, f, d, ret, readlen; char szTemp[MAXPATHLEN]; @@ -339,7 +440,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 +466,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 +474,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); }