static void
_librsync_patchmaker_dealloc(PyObject* self)
{
  _librsync_PatchMakerObject *pm = (_librsync_PatchMakerObject *)self;
  Py_DECREF(pm->basis_file);
  rs_job_free(pm->patch_job);
  PyObject_Del(self);
}
static void
_librsync_deltamaker_dealloc(PyObject* self)
{
  _librsync_DeltaMakerObject *dm = (_librsync_DeltaMakerObject *)self;
  rs_signature_t *sig_ptr = dm->sig_ptr;

  rs_free_sumset(sig_ptr);
  rs_job_free(dm->delta_job);
  PyObject_Del(self);
}
Beispiel #3
0
void sbuf_protocol1_free_content(struct protocol1 *protocol1)
{
	if(!protocol1) return;
	memset(&(protocol1->rsbuf), 0, sizeof(protocol1->rsbuf));
	if(protocol1->sigjob)
		{ rs_job_free(protocol1->sigjob); protocol1->sigjob=NULL; }
	rs_filebuf_free(&protocol1->infb);
	rs_filebuf_free(&protocol1->outfb);
	fzp_close(&protocol1->sigfzp);
	fzp_close(&protocol1->fzp);
	sbuf_protocol1_init(protocol1);
}
Beispiel #4
0
static void destroy_state(ErlNifEnv *env, void *data)
{
  state_t *state = (state_t *) data;
  if (state) {
    if (state->job) rs_job_free(state->job);
    if (state->out) free(state->out);
    if (state->in) free(state->in);
    if (state->sig) rs_free_sumset(state->sig);
    if (state->bin.data) enif_release_binary(&state->bin);
    memset(state, 0, sizeof(state_t));
  }
}
Beispiel #5
0
rs_result rs_patch_gzfile(struct asfd *asfd, struct fzp *basis_file,
	struct fzp *delta_file, struct fzp *new_file,
	rs_stats_t *stats, struct cntr *cntr)
{
	rs_job_t *job;
	rs_result r;

	job=rs_patch_begin(rs_file_copy_cb, basis_file);
	r=rs_whole_gzrun(asfd, job, delta_file, new_file, cntr);
	rs_job_free(job);

	return r;
}
Beispiel #6
0
rs_result rs_delta_gzfile(struct asfd *asfd,
	rs_signature_t *sig, struct fzp *new_file,
	struct fzp *delta_file,
	rs_stats_t *stats, struct cntr *cntr)
{
	rs_job_t *job;
	rs_result r;

	job=rs_delta_begin(sig);
	r=rs_whole_gzrun(asfd, job, new_file, delta_file, cntr);
	rs_job_free(job);

	return r;
}
Beispiel #7
0
/**
 * Load signatures from a signature file into memory.  Return a
 * pointer to the newly allocated structure in SUMSET.
 *
 * \sa rs_readsig_begin()
 */
rs_result
rs_loadsig_file(FILE *sig_file, rs_signature_t **sumset, rs_stats_t *stats)
{
    rs_job_t            *job;
    rs_result           r;

    job = rs_loadsig_begin(sumset);
    r = rs_whole_run(job, sig_file, NULL);
    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);
    rs_job_free(job);

    return r;
}
Beispiel #8
0
/**
 * Generate the signature of a basis file, and write it out to
 * another.
 *
 * \param new_block_len block size for signature generation, in bytes
 *
 * \param strong_len truncated length of strong checksums, in bytes
 *
 * \sa rs_sig_begin()
 */
rs_result
rs_sig_file(FILE *old_file, FILE *sig_file, size_t new_block_len,
            size_t strong_len, rs_stats_t *stats)
{
    rs_job_t        *job;
    rs_result       r;

    job = rs_sig_begin(new_block_len, strong_len);
    r = rs_whole_run(job, old_file, sig_file);
    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);
    rs_job_free(job);

    return r;
}
Beispiel #9
0
rs_result rs_sig_gzfile(FILE *old_file, gzFile old_zfile, FILE *sig_file, size_t new_block_len, size_t strong_len, rs_stats_t *stats, struct cntr *cntr)
{
    rs_job_t        *job;
    rs_result       r;

    job = rs_sig_begin(new_block_len, strong_len);
    r = rs_whole_gzrun(job, old_file, old_zfile, sig_file, NULL, cntr);
/*
    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);
*/
    rs_job_free(job);

    return r;
}
Beispiel #10
0
rs_result rs_delta_gzfile(rs_signature_t *sig, FILE *new_file, gzFile new_zfile, FILE *delta_file, gzFile delta_zfile, rs_stats_t *stats, struct cntr *cntr)
{
    rs_job_t            *job;
    rs_result           r;

    job = rs_delta_begin(sig);

    r = rs_whole_gzrun(job, new_file, new_zfile, delta_file, delta_zfile, cntr);
/*
    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);
*/

    rs_job_free(job);

    return r;
}
Beispiel #11
0
rs_result rs_patch_gzfile(FILE *basis_file, FILE *delta_file, gzFile delta_zfile, FILE *new_file, gzFile new_zfile, rs_stats_t *stats, struct cntr *cntr)
{
	rs_job_t            *job;
	rs_result           r;

	job = rs_patch_begin(rs_file_copy_cb, basis_file);

	r = rs_whole_gzrun(job, delta_file, delta_zfile, new_file, new_zfile, cntr);
/*
	if (stats)
		memcpy(stats, &job->stats, sizeof *stats);
*/

	rs_job_free(job);

	return r;
}
Beispiel #12
0
executer_result
_executer_send_signature (executer_job_t * job)
{

  rs_job_t *sigjob;
  executer_rs inrs;
  executer_rs outrs;
  rs_buffers_t rsbuf;

  utils_debug ("Sending signature");

  int openfile = open (job->realpath, O_RDONLY);

  inrs.sock = openfile;
  inrs.type = _EXECUTER_FILE;
  inrs.size = RS_DEFAULT_BLOCK_LEN;
  inrs.buf = malloc (inrs.size);

  outrs.sock = job->peer_sock;
  outrs.type = _EXECUTER_SOCKET;
  outrs.size = _EXECUTER_DEFAULT_OUT;
  outrs.buf = malloc (outrs.size);

  sigjob = rs_sig_begin (RS_DEFAULT_BLOCK_LEN, RS_DEFAULT_STRONG_LEN);
  rs_result result =
    rs_job_drive (sigjob, &rsbuf, _executer_in_callback, &inrs,
		  _executer_out_callback, &outrs);

  // Free the memory
  rs_job_free (sigjob);
  free (inrs.buf);
  free (outrs.buf);
  close (openfile);

  if (result != RS_DONE)
    {
      utils_error ("Error sending signature");
      return EXECUTER_ERROR;
    }

  _executer_send_finish (job->peer_sock);

  job->next_operation = _executer_recive_delta;
  return EXECUTER_RUNNING;

}
Beispiel #13
0
static int load_signature(struct asfd *asfd,
	rs_signature_t **sumset, struct conf *conf)
{
	rs_result r;
	rs_job_t *job;

	job = rs_loadsig_begin(sumset);
	if((r=do_rs_run(asfd, job,
		NULL, NULL, NULL, NULL, NULL, asfd->fd, -1, conf->cntr)))
	{
		rs_free_sumset(*sumset);
		return r;
	}
	if((r=rs_build_hash_table(*sumset))) return r;
	rs_job_free(job);
	return r;
}
Beispiel #14
0
rs_result rs_patch_file(FILE *basis_file, FILE *delta_file, FILE *new_file,
                        rs_stats_t *stats)
{
    rs_job_t            *job;
    rs_result           r;

    job = rs_patch_begin(rs_file_copy_cb, basis_file);

    r = rs_whole_run(job, delta_file, new_file);
    
    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);

    rs_job_free(job);

    return r;
}
Beispiel #15
0
rs_result
rs_delta_file(rs_signature_t *sig, FILE *new_file, FILE *delta_file,
              rs_stats_t *stats)
{
    rs_job_t            *job;
    rs_result           r;

    job = rs_delta_begin(sig);

    r = rs_whole_run(job, new_file, delta_file);

    if (stats)
        memcpy(stats, &job->stats, sizeof *stats);

    rs_job_free(job);

    return r;
}
Beispiel #16
0
executer_result
_executer_recive_signature (executer_job_t * job)
{
  rs_signature_t *signature;
  rs_job_t *signature_job;
  executer_rs inrs;
  rs_result result;
  rs_buffers_t rsbuf;

  utils_debug ("Receiving signature");

  //Load the signature
  signature_job = rs_loadsig_begin (&signature);
  inrs.type = _EXECUTER_SOCKET;
  inrs.sock = job->peer_sock;
  inrs.size = RS_DEFAULT_BLOCK_LEN;
  inrs.buf = malloc (inrs.size);

  // Run librsync job
  result =
    rs_job_drive (signature_job, &rsbuf, _executer_in_callback, &inrs, NULL,
		  NULL);

  // Free the memory
  rs_job_free (signature_job);
  free (inrs.buf);

  if (result != RS_DONE)
    {
      utils_error ("Error carculating singnature");
      return EXECUTER_ERROR;
    }
  else
    {
      //After receive signature, we need send delta
      job->next_operation = _executer_send_delta;
      job->signature = signature;
      return EXECUTER_RUNNING;
    }

}
Beispiel #17
0
rs_result rs_sig_gzfile(struct asfd *asfd,
	struct fzp *old_file, struct fzp *sig_file,
	size_t new_block_len, size_t strong_len,
	rs_stats_t *stats, struct conf **confs)
{
	rs_job_t *job;
	rs_result r;
	job=
		rs_sig_begin(new_block_len, strong_len
#ifndef RS_DEFAULT_STRONG_LEN
                  	, rshash_to_magic_number(
				get_e_rshash(confs[OPT_RSHASH]))
#endif
		);

	r=rs_whole_gzrun(asfd, job, old_file, sig_file,
		get_cntr(confs[OPT_CNTR]));
	rs_job_free(job);

	return r;
}
/* Call with the entire signature loaded into one big string */
static PyObject*
_librsync_new_deltamaker(PyObject* self, PyObject* args)
{
  _librsync_DeltaMakerObject* dm;
  char *sig_string, outbuf[RS_JOB_BLOCKSIZE];
  int sig_length;
  rs_job_t *sig_loader;
  rs_signature_t *sig_ptr;
  rs_buffers_t buf;
  rs_result result;

  if (!PyArg_ParseTuple(args,"s#:new_deltamaker", &sig_string, &sig_length))
	return NULL;

  dm = PyObject_New(_librsync_DeltaMakerObject, &_librsync_DeltaMakerType);
  if (dm == NULL) return NULL;
  dm->x_attr = NULL;

  /* Put signature at sig_ptr and build hash */
  sig_loader = rs_loadsig_begin(&sig_ptr);
  buf.next_in = sig_string;
  buf.avail_in = (size_t)sig_length;
  buf.next_out = outbuf;
  buf.avail_out = (size_t)RS_JOB_BLOCKSIZE;
  buf.eof_in = 1;
  result = rs_job_iter(sig_loader, &buf);
  rs_job_free(sig_loader);
  if (result != RS_DONE) {
	_librsync_seterror(result, "delta rs_signature_t builder");
	return NULL;
  }
  if ((result = rs_build_hash_table(sig_ptr)) != RS_DONE) {
	_librsync_seterror(result, "delta rs_build_hash_table");
	return NULL;
  }

  dm->sig_ptr = sig_ptr;
  dm->delta_job = rs_delta_begin(sig_ptr);
  return (PyObject*)dm;
}
Beispiel #19
0
static ERL_NIF_TERM delta_init(ErlNifEnv* env, int argc,
			       const ERL_NIF_TERM argv[])
{
  state_t *state = NULL;
  rs_result res;
  
  if (!enif_get_resource(env, argv[0], state_r, (void *) &state))
    return enif_make_badarg(env);

  if (state->type != LOADSIG)
    return enif_make_badarg(env);
  
  res = rs_build_hash_table(state->sig);
  if (res != RS_DONE)
    return mk_error(env, res);
  if (state->job) rs_job_free(state->job);
  state->job = rs_delta_begin(state->sig);
  state->in_size = 0;
  state->out_size = 0;
  state->type = DELTA;
  return enif_make_atom(env, "ok");
}
Beispiel #20
0
static int load_signature(rs_signature_t **sumset, struct cntr *cntr)
{
	rs_result r;
	rs_job_t *job;
//logp("loadsig %s\n", rpath);

	job = rs_loadsig_begin(sumset);
	if((r=do_rs_run(job,
		NULL, NULL, NULL, NULL, NULL, async_get_fd(), -1, cntr)))
	{
		rs_free_sumset(*sumset);
		return r;
	}
	if((r=rs_build_hash_table(*sumset)))
	{
		return r;
	}
	rs_job_free(job);
//logp("end loadsig\n");
//logp("\n");
	return r;
}
Beispiel #21
0
static int load_signature_and_send_delta(BFILE *bfd, FILE *in, unsigned long long *bytes, unsigned long long *sentbytes, struct cntr *cntr, size_t datalen)
{
	rs_job_t *job;
	rs_result r;
	rs_signature_t *sumset=NULL;
	unsigned char checksum[MD5_DIGEST_LENGTH+1];
	rs_filebuf_t *infb=NULL;
	rs_filebuf_t *outfb=NULL;
	rs_buffers_t rsbuf;
	memset(&rsbuf, 0, sizeof(rsbuf));

	if(load_signature(&sumset, cntr)) return -1;

//logp("start delta\n");

	if(!(job=rs_delta_begin(sumset)))
	{
		logp("could not start delta job.\n");
		rs_free_sumset(sumset);
		return RS_IO_ERROR;
	}

	if(!(infb=rs_filebuf_new(bfd,
		in, NULL, -1, ASYNC_BUF_LEN, datalen, cntr))
	  || !(outfb=rs_filebuf_new(NULL, NULL,
		NULL, async_get_fd(), ASYNC_BUF_LEN, -1, cntr)))
	{
		logp("could not rs_filebuf_new for delta\n");
		if(infb) rs_filebuf_free(infb);
		return -1;
	}
//logp("start delta loop\n");

	while(1)
	{
		size_t wlen=0;
		rs_result delresult;
		delresult=rs_async(job, &rsbuf, infb, outfb);
		if(delresult==RS_DONE)
		{
			r=delresult;
//			logp("delresult done\n");
			break;
		}
		else if(delresult==RS_BLOCKED || delresult==RS_RUNNING)
		{
//			logp("delresult running/blocked: %d\n", delresult);
			// Keep going
		}
		else
		{
			logp("error in rs_async for delta: %d\n", delresult);
			r=delresult;
			break;
		}
		// FIX ME: get it to read stuff (errors, for example) here too.
		if(async_rw(NULL, NULL, '\0', '\0', NULL, &wlen))
			return -1;
	}

	if(r!=RS_DONE)
		logp("delta loop returned: %d\n", r);

//logp("after delta loop: %d\n", r);
//logp("\n");

	if(r==RS_DONE)
	{
		*bytes=infb->bytes;
		*sentbytes=outfb->bytes;
		if(!MD5_Final(checksum, &(infb->md5)))
		{
			logp("MD5_Final() failed\n");
			r=RS_IO_ERROR;
		}
	}
	rs_filebuf_free(infb);
	rs_filebuf_free(outfb);
	rs_job_free(job);
	rs_free_sumset(sumset);

	if(r==RS_DONE && write_endfile(*bytes, checksum)) // finish delta file
			return -1;

	//logp("end of load_sig_send_delta\n");

	return r;
}
Beispiel #22
0
executer_result
_executer_recive_delta (executer_job_t * job)
{
  char *temppath;
  rs_buffers_t rsbuf;
  executer_rs inrs;
  executer_rs outrs;
  int result;

  utils_debug ("Receving delta.");

  int tempfile = _executer_create_temp (job->realpath, &temppath);
  if (tempfile == 0)
    {
      utils_error ("Error creating temp file for file '%s': %s",
		   job->realpath, strerror (errno));
      return EXECUTER_ERROR;
    }

  FILE *origfile = fopen (job->realpath, "rb");
  if (origfile == NULL)
    {
      utils_error ("Error opening file '%s': %s", job->realpath,
		   strerror (errno));
      return EXECUTER_ERROR;
    }

  rs_job_t *patchjob = rs_patch_begin (rs_file_copy_cb, origfile);

  inrs.sock = job->peer_sock;
  inrs.type = _EXECUTER_SOCKET;
  inrs.size = _EXECUTER_DEFAULT_OUT;
  inrs.buf = malloc (inrs.size);

  outrs.sock = tempfile;
  outrs.type = _EXECUTER_FILE;
  outrs.size = RS_DEFAULT_BLOCK_LEN;
  outrs.buf = malloc (outrs.size);

  rsbuf.avail_in = 0;
  rsbuf.avail_out = 0;
  rsbuf.next_in = 0;
  rsbuf.next_out = 0;
  rsbuf.eof_in = false;

  result =
    rs_job_drive (patchjob, &rsbuf, _executer_in_callback, &inrs,
		  _executer_out_callback, &outrs);
  if (result != RS_DONE)
    {
      utils_error ("Error patching file");
      return EXECUTER_ERROR;
    }

  //Check if there are any data
  if (rsbuf.avail_out > 0)
    {
      _executer_in_callback (patchjob, &rsbuf, &inrs);
      _executer_out_callback (patchjob, &rsbuf, &outrs);
    }

  rs_job_free (patchjob);

  close (tempfile);
  fclose (origfile);
  free (outrs.buf);
  free (inrs.buf);

  job->next_operation = _executer_receive_stat;
  job->tempfile = temppath;

  return EXECUTER_RUNNING;
}
Beispiel #23
0
executer_result
_executer_send_delta (executer_job_t * job)
{
  // The opaque pointer is to signature
  rs_signature_t *signature = job->signature;
  rs_job_t *deltajob;
  char *realpath;
  executer_rs inrs;
  executer_rs outrs;
  rs_result result;
  rs_buffers_t rsbuf;

  utils_debug ("Sending delta data");
  deltajob = rs_delta_begin (signature);

  _executer_real_path (job->file, &realpath);
  int file2 = open (realpath, O_RDONLY);

  inrs.type = _EXECUTER_FILE;
  inrs.sock = file2;
  inrs.size = RS_DEFAULT_BLOCK_LEN;
  inrs.buf = malloc (inrs.size);
  outrs.type = _EXECUTER_SOCKET;
  outrs.sock = job->peer_sock;
  outrs.size = _EXECUTER_DEFAULT_OUT;
  outrs.buf = malloc (outrs.size);
  if (rs_build_hash_table (signature) != RS_DONE)
    {
      utils_error ("Error building hash table");
      return EXECUTER_ERROR;
    }
  result =
    rs_job_drive (deltajob, &rsbuf, _executer_in_callback, &inrs,
		  _executer_out_callback, &outrs);

  // Free the memory
  free (realpath);
  rs_job_free (deltajob);
  free (inrs.buf);
  free (outrs.buf);

  // Send finish to the socket
  if (!_executer_send_finish (job->peer_sock))
    {
      utils_error ("Error sending finish message: %s", strerror (errno));
      return EXECUTER_ERROR;
    }
  if (result != RS_DONE)
    {
      utils_error ("Error calculating delta");
      return EXECUTER_ERROR;
    }
  else
    {
      // Ok, send the stat
      job->next_operation = _executer_send_stat;
      // Ok, the delta is send, no need more process
      return EXECUTER_RUNNING;
    }

}
static void
_librsync_sigmaker_dealloc(PyObject* self)
{
  rs_job_free(((_librsync_SigMakerObject *)self)->sig_job);
  PyObject_Del(self);
}
Beispiel #25
0
static int load_signature_and_send_delta(struct asfd *asfd,
	BFILE *bfd, FILE *in,
	unsigned long long *bytes, unsigned long long *sentbytes,
	struct conf *conf, size_t datalen)
{
	rs_job_t *job;
	rs_result r;
	rs_signature_t *sumset=NULL;
	unsigned char checksum[MD5_DIGEST_LENGTH+1];
	rs_filebuf_t *infb=NULL;
	rs_filebuf_t *outfb=NULL;
	rs_buffers_t rsbuf;
	memset(&rsbuf, 0, sizeof(rsbuf));

	if(load_signature(asfd, &sumset, conf)) return -1;

	if(!(job=rs_delta_begin(sumset)))
	{
		logp("could not start delta job.\n");
		rs_free_sumset(sumset);
		return RS_IO_ERROR;
	}

	if(!(infb=rs_filebuf_new(asfd, bfd,
		in, NULL, -1, ASYNC_BUF_LEN, datalen, conf->cntr))
	  || !(outfb=rs_filebuf_new(asfd, NULL, NULL,
		NULL, asfd->fd, ASYNC_BUF_LEN, -1, conf->cntr)))
	{
		logp("could not rs_filebuf_new for delta\n");
		if(infb) rs_filebuf_free(infb);
		return -1;
	}

	while(1)
	{
		rs_result delresult;
		delresult=rs_async(job, &rsbuf, infb, outfb);
		if(delresult==RS_DONE)
		{
			r=delresult;
			break;
		}
		else if(delresult==RS_BLOCKED || delresult==RS_RUNNING)
		{
			// Keep going
		}
		else
		{
			logp("error in rs_async for delta: %d\n", delresult);
			r=delresult;
			break;
		}
		// FIX ME: get it to read stuff (errors, for example) here too.
		if(asfd->as->rw(asfd->as)) return -1;
	}

	if(r!=RS_DONE)
		logp("delta loop returned: %d\n", r);

	if(r==RS_DONE)
	{
		*bytes=infb->bytes;
		*sentbytes=outfb->bytes;
		if(!MD5_Final(checksum, &(infb->md5)))
		{
			logp("MD5_Final() failed\n");
			r=RS_IO_ERROR;
		}
	}
	rs_filebuf_free(infb);
	rs_filebuf_free(outfb);
	rs_job_free(job);
	rs_free_sumset(sumset);

	if(r==RS_DONE && write_endfile(asfd, *bytes, checksum))
		return -1;

	return r;
}