Diff for /libaitsync/src/aitsync.c between versions 1.1.1.1 and 1.3

version 1.1.1.1, 2010/03/24 16:00:15 version 1.3, 2012/07/22 22:09:47
Line 5 Line 5
 * $Author$  * $Author$
 * $Id$  * $Id$
 *  *
*************************************************************************/**************************************************************************
 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
         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 "global.h"
 #include "tool.h"  #include "tool.h"
 #include "zc.h"  #include "zc.h"
Line 17  static int sync_Errno; Line 54  static int sync_Errno;
 static char sync_Error[STRSIZ];  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;          return t1->st_tag - t2->st_tag;
 }  }
Line 27  static inline int func_comp(sync_tag_t const *t1, sync Line 65  static inline int func_comp(sync_tag_t const *t1, sync
 //  //
   
 // sync_GetErrno() Get error code of last operation  // sync_GetErrno() Get error code of last operation
inline int sync_GetErrno()inline int
 sync_GetErrno()
 {  {
         return sync_Errno;          return sync_Errno;
 }  }
   
 // sync_GetError() Get error text of last operation  // sync_GetError() Get error text of last operation
inline const char *sync_GetError()inline const char *
 sync_GetError()
 {  {
         return sync_Error;          return sync_Error;
 }  }
   
 // sync_SetErr() Set error to variables for internal use!!!  // 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;          va_list lst;
   
Line 56  inline void syncSetErr(int eno, char *estr, ...) Line 97  inline void syncSetErr(int eno, char *estr, ...)
  * syncSignature() Calculate and create signature for diff   * syncSignature() Calculate and create signature for diff
  * @csInput = Input patched file name for calculating check sums   * @csInput = Input patched file name for calculating check sums
  * @csSig = Output Signature file name   * @csSig = Output Signature file name
    * @compress = 2 compress signatures output, 0 not compressed
  * return: -1 error, 0 ok   * 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];          u_char buf[CHUNK_MAX];
         register int i = 0;          register int i = 0;
         off_t off = 0ll;          off_t off = 0ll;
         sync_chunk_t sc;          sync_chunk_t sc;
           char szTemp[MAXPATHLEN];
   
         inf = syncOpen(csInput, O_RDONLY);          inf = syncOpen(csInput, O_RDONLY);
         if (inf == -1)          if (inf == -1)
                 return inf;                  return inf;
        outf = syncOpen(csSig, O_WRONLY);        if (compress & 2)
        if (outf == -1) {                f = syncTemp(szTemp, MAXPATHLEN);
         else
                 f = syncOpen(csSig, O_WRONLY);
         if (f == -1) {
                 syncClose(inf);                  syncClose(inf);
                return outf;                return f;
         }          }
   
         for (i = 0, off = 0ll, ret = -1; ret; i++, off += ret) {          for (i = 0, off = 0ll, ret = -1; ret; i++, off += ret) {
Line 86  int syncSignature(const char *csInput, const char *csS Line 133  int syncSignature(const char *csInput, const char *csS
                 // fill chunk                  // fill chunk
                 sync_mksig(i, off, buf, ret, &sc);                  sync_mksig(i, off, buf, ret, &sc);
   
                if (write(outf, &sc, sizeof sc) == -1) {                if (write(f, &sc, sizeof sc) == -1) {
                         SETERR;                          SETERR;
                         break;                          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);          syncClose(inf);
         return ret;          return ret;
 }  }
Line 102  int syncSignature(const char *csInput, const char *csS Line 170  int syncSignature(const char *csInput, const char *csS
  * @csInput = Input original source file name for make delta patch file   * @csInput = Input original source file name for make delta patch file
  * @csSig = Input Signature file name   * @csSig = Input Signature file name
  * @csDelta = Output Delta patch 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   * 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;          int inf, outf, f, sigf, ret, cnt;
         size_t blk;          size_t blk;
Line 120  int syncDelta(const char *csInput, const char *csSig,  Line 189  int syncDelta(const char *csInput, const char *csSig, 
   
         /* load signatures */          /* load signatures */
   
        sigf = syncOpen(csSig, O_RDONLY);        if (compress & 2) {
        if (sigf == -1) {                f = syncOpen(csSig, O_RDONLY);
                return sigf;                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) {          if (fstat(sigf, &sb) == -1) {
                 SETERR;                  SETERR;
                 syncClose(sigf);                  syncClose(sigf);
                   if (compress & 2)
                           unlink(szTemp);
                 return -1;                  return -1;
         } else {          } else {
                 if (!sb.st_size) {                  if (!sb.st_size) {
                         syncClose(sigf);                          syncClose(sigf);
                           if (compress & 2)
                                   unlink(szTemp);
                         return 1;                          return 1;
                 }                  }
   
Line 138  int syncDelta(const char *csInput, const char *csSig,  Line 230  int syncDelta(const char *csInput, const char *csSig, 
                 if (sb.st_size % sizeof(sync_chunk_t)) {                  if (sb.st_size % sizeof(sync_chunk_t)) {
                         syncSetErr(ENOEXEC, "Error:: signature file is broken!\n");                          syncSetErr(ENOEXEC, "Error:: signature file is broken!\n");
                         syncClose(sigf);                          syncClose(sigf);
                           if (compress & 2)
                                   unlink(szTemp);
                         return -1;                          return -1;
                 }                  }
         }          }
Line 145  int syncDelta(const char *csInput, const char *csSig,  Line 239  int syncDelta(const char *csInput, const char *csSig, 
         if (MAP_FAILED == chunks) {          if (MAP_FAILED == chunks) {
                 SETERR;                  SETERR;
                 syncClose(sigf);                  syncClose(sigf);
                   if (compress & 2)
                           unlink(szTemp);
                 return -1;                  return -1;
           } else {
                   syncClose(sigf);
                   if (compress & 2)
                           unlink(szTemp);
         }          }
         syncClose(sigf);  
   
         /* build from signatures sorted index and hashes */          /* build from signatures sorted index and hashes */
   
Line 181  int syncDelta(const char *csInput, const char *csSig,  Line 280  int syncDelta(const char *csInput, const char *csSig, 
                 munmap(chunks, sb.st_size);                  munmap(chunks, sb.st_size);
                 return inf;                  return inf;
         }          }
        if (compress)        if (compress & 1)
                 f = syncTemp(szTemp, MAXPATHLEN);                  f = syncTemp(szTemp, MAXPATHLEN);
         else          else
                 f = syncOpen(csDelta, O_WRONLY);                  f = syncOpen(csDelta, O_WRONLY);
Line 294  int syncDelta(const char *csInput, const char *csSig,  Line 393  int syncDelta(const char *csInput, const char *csSig, 
         /* Delta patch is READY */          /* Delta patch is READY */
   
         // build compressed delta file          // build compressed delta file
        if (compress) {        if (compress & 1) {
                 outf = syncOpen(csDelta, O_WRONLY);                  outf = syncOpen(csDelta, O_WRONLY);
                 if (outf == -1) {                  if (outf == -1) {
                         ret = outf;                          ret = outf;
Line 311  int syncDelta(const char *csInput, const char *csSig,  Line 410  int syncDelta(const char *csInput, const char *csSig, 
   
 end:  end:
         syncClose(f);          syncClose(f);
        unlink(szTemp);        if (compress & 1)
                 unlink(szTemp);
   
         syncClose(inf);          syncClose(inf);
         free(tag_table);          free(tag_table);
Line 324  end: Line 424  end:
  * @csInput = Input target file name for patch   * @csInput = Input target file name for patch
  * @csDelta = Input Delta patch file name   * @csDelta = Input Delta patch file name
  * @csPatch = After applied patch create new alternate target file, if != NULL   * @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   * 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;          int inf, outf, f, d, ret, readlen;
         char szTemp[MAXPATHLEN];          char szTemp[MAXPATHLEN];
Line 339  int syncPatch(const char *csInput, const char *csDelta Line 440  int syncPatch(const char *csInput, const char *csDelta
         off_t off;          off_t off;
         sync_chunk_t sc, *suffix;          sync_chunk_t sc, *suffix;
   
        if (compress) {        if (compress & 1) {
                 f = syncOpen(csDelta, O_RDONLY);                  f = syncOpen(csDelta, O_RDONLY);
                 if (f == -1)                  if (f == -1)
                         return f;                          return f;
Line 365  int syncPatch(const char *csInput, const char *csDelta Line 466  int syncPatch(const char *csInput, const char *csDelta
         if (fstat(d, &sb) == -1) {          if (fstat(d, &sb) == -1) {
                 SETERR;                  SETERR;
                 syncClose(d);                  syncClose(d);
                if (compress)                if (compress & 1)
                         unlink(szTemp);                          unlink(szTemp);
                 return -1;                  return -1;
         }          }
Line 373  int syncPatch(const char *csInput, const char *csDelta Line 474  int syncPatch(const char *csInput, const char *csDelta
         if (MAP_FAILED == delta) {          if (MAP_FAILED == delta) {
                 SETERR;                  SETERR;
                 syncClose(d);                  syncClose(d);
                if (compress)                if (compress & 1)
                         unlink(szTemp);                          unlink(szTemp);
                 return -1;                  return -1;
         } else {          } else {
                 syncClose(d);                  syncClose(d);
                if (compress)                if (compress & 1)
                         unlink(szTemp);                          unlink(szTemp);
         }          }
   

Removed from v.1.1.1.1  
changed lines
  Added in v.1.3


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