--- libaitsync/src/aitsync.c 2012/11/13 10:30:44 1.3.2.2 +++ libaitsync/src/aitsync.c 2012/11/13 13:19:15 1.3.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsync.c,v 1.3.2.2 2012/11/13 10:30:44 misho Exp $ +* $Id: aitsync.c,v 1.3.2.3 2012/11/13 13:19:15 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -96,7 +96,7 @@ sync_SetErr(int eno, char *estr, ...) /* * syncSignature() - Calculate and create signature for diff * - * @csInput = Input file name for calculating check sums + * @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 @@ -141,7 +141,7 @@ syncSignature(const char *csInput, const char *csSig, } } - /* Signatures is READY */ + /* Signatures are READY */ if (compress & 2) { /* build compressed delta file */ @@ -167,9 +167,10 @@ end: } /* - * syncDelta() Create Delta patch file + * syncDelta() - Create Delta patch file + * * @csInput = Input original source file name for make delta patch file - * @csSig = Input Signature file name + * @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 @@ -251,11 +252,11 @@ syncDelta(const char *csInput, const char *csSig, cons /* build from signatures sorted index and hashes */ - // init tags array + /* init first stage tags array index */ for (i = 0; i < TABLESIZ; i++) tags[i] = NULL_TAG; - // build index from signature blocks + /* build second index from signature blocks */ tag_table = (sync_tag_t*) calloc(cnt, sizeof(sync_tag_t)); if (!tag_table) { LOGERR; @@ -267,12 +268,15 @@ syncDelta(const char *csInput, const char *csSig, cons 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); + qsort(tag_table, cnt, sizeof(sync_tag_t), + (int (*)(const void *, const void *)) func_comp); } - // assign less id position in tag_table to tags + /* 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); @@ -292,24 +296,29 @@ syncDelta(const char *csInput, const char *csSig, cons return f; } - for (i = 0, off = 0ll, ret = -1, blk = 0; (ret = read(inf, buf, CHUNK_MAX)); i++, off += ret) { + 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; - // printf("+ find=%p off=%llu i=%d blk=%d\n", find, off, i, blk); +#if 0 + printf("+ find=%p off=%llu i=%d blk=%d\n", find, off, i, blk); +#endif - // check chunk for differences with signature + /* check chunk for differences with signature */ sync_mksig(i, off, buf, ret, &sc); cx = GETTAG(sc.sc_roll); - // find in hash -> hash_sorted_table + /* 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; + /* 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 && + 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]; @@ -318,33 +327,35 @@ syncDelta(const char *csInput, const char *csSig, cons } } - // printf("+ find=%p off=%llu i=%d blk=%d\n", find, off, i, blk); +#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 match chunk, check for previous match */ if (!blk && find) continue; - // if not find chunk in signature skip write to delta patch + /* if not find chunk in signature skip write to delta patch */ if (!find) { - /* different piece, write it! */ - // write signature of current chunk + /* 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 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 ... + /* 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 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; @@ -354,18 +365,18 @@ syncDelta(const char *csInput, const char *csSig, cons continue; } - // match 1st block after difference and copy signature from B + /* 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 + /* 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 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; @@ -375,7 +386,7 @@ syncDelta(const char *csInput, const char *csSig, cons blk ^= blk; } - // check for error or empty delta file + /* check for error or empty delta file */ if (ret == -1) goto end; fsync(f); @@ -385,7 +396,7 @@ syncDelta(const char *csInput, const char *csSig, cons goto end; } - // No deferences, not needed delta.patch !!! + /* No deferences, not needed delta.patch !!! */ if (!sb_f.st_size) { ret = 1; goto end; @@ -393,7 +404,7 @@ syncDelta(const char *csInput, const char *csSig, cons /* Delta patch is READY */ - // build compressed delta file + /* build compressed delta file */ if (compress & 1) { outf = sync_Open(csDelta, O_WRONLY, 0); if (outf == -1) { @@ -413,7 +424,6 @@ end: sync_Close(f); if (compress & 1) unlink(szTemp); - sync_Close(inf); free(tag_table); munmap(chunks, sb.st_size); @@ -421,7 +431,8 @@ end: } /* - * syncPatch() Apply delta patch file to target + * 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 @@ -519,11 +530,13 @@ syncPatch(const char *csInput, const char *csDelta, co ret = readlen = 0; buffer = NULL; - for (i = 0, off = 0ll, suffix = NULL, piece = pieces; piece->pfx; i++, off += readlen) { + 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 - // printf("i=%d off=%llu sfx=%p piece=%p\n", i, off, suffix, piece); - - // if input offset is less then input file size + /* if input offset is less then input file size */ if (off < sb.st_size) { readlen = read(inf, buf, CHUNK_MAX); if (readlen == -1) { @@ -531,17 +544,21 @@ syncPatch(const char *csInput, const char *csDelta, co ret = -1; break; } - // if suffix find, check for correct patch + /* 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"); + 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"); + 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; } @@ -552,13 +569,17 @@ syncPatch(const char *csInput, const char *csDelta, co buffer = buf; } - // 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); +#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 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"); + sync_SetErr(ENOEXEC, "Error:: patch file is broken! " + "(missing data)\n"); ret = -1; break; } @@ -570,19 +591,20 @@ syncPatch(const char *csInput, const char *csDelta, co piece++; if (suffix && off >= sb.st_size) { - sync_SetErr(ENOEXEC, "Error:: patch file is broken! (after eof find suffix)\n"); + 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; + } 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) {