"Fossies" - the Fresh Open Source Software Archive

Member "zsync-0.6.2/librcksum/state.c" (16 Sep 2010, 4592 Bytes) of package /linux/privat/old/zsync-0.6.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "state.c" see the Fossies "Dox" file reference documentation.

    1 
    2 /*
    3  *   rcksum/lib - library for using the rsync algorithm to determine
    4  *               which parts of a file you have and which you need.
    5  *   Copyright (C) 2004,2005,2007,2009 Colin Phipps <cph@moria.org.uk>
    6  *
    7  *   This program is free software; you can redistribute it and/or modify
    8  *   it under the terms of the Artistic License v2 (see the accompanying 
    9  *   file COPYING for the full license terms), or, at your option, any later 
   10  *   version of the same license.
   11  *
   12  *   This program is distributed in the hope that it will be useful,
   13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  *   COPYING file for details.
   16  */
   17 
   18 /* Effectively the constructor and destructor for the rcksum object.
   19  * Also handles the file handles on the temporary store.
   20  */
   21 
   22 #include "zsglobal.h"
   23 
   24 #include <stdlib.h>
   25 #include <string.h>
   26 #include <unistd.h>
   27 
   28 #ifdef WITH_DMALLOC
   29 # include <dmalloc.h>
   30 #endif
   31 
   32 #include "rcksum.h"
   33 #include "internal.h"
   34 
   35 /* rcksum_init(num_blocks, block_size, rsum_bytes, checksum_bytes, require_consecutive_matches)
   36  * Creates and returns an rcksum_state with the given properties
   37  */
   38 struct rcksum_state *rcksum_init(zs_blockid nblocks, size_t blocksize,
   39                                  int rsum_bytes, int checksum_bytes,
   40                                  int require_consecutive_matches) {
   41     /* Allocate memory for the object */
   42     struct rcksum_state *z = malloc(sizeof(struct rcksum_state));
   43     if (z == NULL) return NULL;
   44 
   45     /* Enter supplied properties. */
   46     z->blocksize = blocksize;
   47     z->blocks = nblocks;
   48     z->rsum_a_mask = rsum_bytes < 3 ? 0 : rsum_bytes == 3 ? 0xff : 0xffff;
   49     z->checksum_bytes = checksum_bytes;
   50     z->seq_matches = require_consecutive_matches;
   51 
   52     /* require_consecutive_matches is 1 if true; and if true we need 1 block of
   53      * context to do block matching */
   54     z->context = blocksize * require_consecutive_matches;
   55 
   56     /* Temporary file to hold the target file as we get blocks for it */
   57     z->filename = strdup("rcksum-XXXXXX");
   58 
   59     /* Initialise to 0 various state & stats */
   60     z->gotblocks = 0;
   61     memset(&(z->stats), 0, sizeof(z->stats));
   62     z->ranges = NULL;
   63     z->numranges = 0;
   64 
   65     /* Hashes for looking up checksums are generated when needed.
   66      * So initially store NULL so we know there's nothing there yet.
   67      */
   68     z->rsum_hash = NULL;
   69     z->bithash = NULL;
   70 
   71     if (!(z->blocksize & (z->blocksize - 1)) && z->filename != NULL
   72             && z->blocks) {
   73         /* Create temporary file */
   74         z->fd = mkstemp(z->filename);
   75         if (z->fd == -1) {
   76             perror("open");
   77         }
   78         else {
   79             {   /* Calculate bit-shift for blocksize */
   80                 int i;
   81                 for (i = 0; i < 32; i++)
   82                     if (z->blocksize == (1u << i)) {
   83                         z->blockshift = i;
   84                         break;
   85                     }
   86             }
   87 
   88             z->blockhashes =
   89                 malloc(sizeof(z->blockhashes[0]) *
   90                         (z->blocks + z->seq_matches));
   91             if (z->blockhashes != NULL)
   92                 return z;
   93 
   94             /* All below is error handling */
   95         }
   96     }
   97     free(z->filename);
   98     free(z);
   99     return NULL;
  100 }
  101 
  102 /* rcksum_filename(self)
  103  * Returns temporary filename to caller as malloced string.
  104  * Ownership of the file passes to the caller - the function returns NULL if
  105  * called again, and it is up to the caller to deal with the file. */
  106 char *rcksum_filename(struct rcksum_state *rs) {
  107     char *p = rs->filename;
  108     rs->filename = NULL;
  109     return p;
  110 }
  111 
  112 /* rcksum_filehandle(self)
  113  * Returns the filehandle for the temporary file.
  114  * Ownership of the handle passes to the caller - the function returns -1 if
  115  * called again, and it is up to the caller to close it. */
  116 int rcksum_filehandle(struct rcksum_state *rs) {
  117     int h = rs->fd;
  118     rs->fd = -1;
  119     return h;
  120 }
  121 
  122 /* rcksum_end - destructor */
  123 void rcksum_end(struct rcksum_state *z) {
  124     /* Free temporary file resources */
  125     if (z->fd != -1)
  126         close(z->fd);
  127     if (z->filename) {
  128         unlink(z->filename);
  129         free(z->filename);
  130     }
  131 
  132     /* Free other allocated memory */
  133     free(z->rsum_hash);
  134     free(z->blockhashes);
  135     free(z->bithash);
  136     free(z->ranges);            // Should be NULL already
  137 #ifdef DEBUG
  138     fprintf(stderr, "hashhit %d, weakhit %d, checksummed %d, stronghit %d\n",
  139             z->stats.hashhit, z->stats.weakhit, z->stats.checksummed,
  140             z->stats.stronghit);
  141 #endif
  142     free(z);
  143 }