/** * Run a job continuously, with input to/from the two specified files. * The job should already be set up, and must be free by the caller * after return. * * Buffers of ::rs_inbuflen and ::rs_outbuflen are allocated for * temporary storage. * * \param in_file Source of input bytes, or NULL if the input buffer * should not be filled. * * \return RS_DONE if the job completed, or otherwise an error result. */ rs_result rs_whole_run(rs_job_t *job, FILE *in_file, FILE *out_file) { rs_buffers_t buf; rs_result result; rs_filebuf_t *in_fb = NULL, *out_fb = NULL; if (in_file) in_fb = rs_filebuf_new(in_file, rs_inbuflen); if (out_file) out_fb = rs_filebuf_new(out_file, rs_outbuflen); result = rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, out_fb ? rs_outfilebuf_drain : NULL, out_fb); if (in_fb) rs_filebuf_free(in_fb); if (out_fb) rs_filebuf_free(out_fb); return result; }
static rs_result rs_whole_gzrun(rs_job_t *job, FILE *in_file, gzFile in_zfile, FILE *out_file, gzFile out_zfile, struct cntr *cntr) { rs_buffers_t buf; rs_result result; rs_filebuf_t *in_fb = NULL, *out_fb = NULL; if (in_file || in_zfile) in_fb = rs_filebuf_new(NULL, in_file, in_zfile, -1, ASYNC_BUF_LEN, -1, cntr); if (out_file || out_zfile) out_fb = rs_filebuf_new(NULL, out_file, out_zfile, -1, ASYNC_BUF_LEN, -1, cntr); //logp("before drive\n"); result = rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, out_fb ? rs_outfilebuf_drain : NULL, out_fb); //logp("after drive\n"); if (in_fb) rs_filebuf_free(in_fb); if (out_fb) rs_filebuf_free(out_fb); return result; }
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); }
rs_filebuf_t *rs_filebuf_new(struct asfd *asfd, BFILE *bfd, struct fzp *fzp, int fd, size_t buf_len, size_t data_len, struct cntr *cntr) { rs_filebuf_t *pf=NULL; if(!(pf=(struct rs_filebuf *)calloc_w(1, sizeof(struct rs_filebuf), __func__))) return NULL; if(!(pf->buf=(char *)calloc_w(1, buf_len, __func__))) goto error; pf->buf_len=buf_len; pf->fzp=fzp; pf->fd=fd; pf->bfd=bfd; pf->bytes=0; pf->data_len=data_len; if(data_len>0) pf->do_known_byte_count=1; else pf->do_known_byte_count=0; pf->cntr=cntr; if(!MD5_Init(&(pf->md5))) { logp("MD5_Init() failed\n"); goto error; } pf->asfd=asfd; return pf; error: rs_filebuf_free(&pf); return NULL; }
rs_filebuf_t *rs_filebuf_new(BFILE *bfd, FILE *fp, gzFile zp, int fd, size_t buf_len, size_t data_len, struct cntr *cntr) { rs_filebuf_t *pf=NULL; if(!(pf=rs_alloc_struct(rs_filebuf_t))) return NULL; if(!(pf->buf=(char *)rs_alloc(buf_len, "file buffer"))) { free(pf); return NULL; } pf->buf_len=buf_len; pf->fp=fp; pf->zp=zp; pf->fd=fd; pf->bfd=bfd; pf->bytes=0; pf->data_len=data_len; if(data_len>0) pf->do_known_byte_count=1; else pf->do_known_byte_count=0; pf->cntr=cntr; if(!MD5_Init(&(pf->md5))) { logp("MD5_Init() failed\n"); rs_filebuf_free(pf); return NULL; } return pf; }
rs_result do_rs_run(rs_job_t *job, BFILE *bfd, FILE *in_file, FILE *out_file, gzFile in_zfile, gzFile out_zfile, int infd, int outfd, struct cntr *cntr) { rs_buffers_t buf; rs_result result; rs_filebuf_t *in_fb=NULL; rs_filebuf_t *out_fb=NULL; if(in_file && infd>=0) { logp("do not specify both input file and input fd in do_rs_run()\n"); return RS_IO_ERROR; } if(out_file && outfd>=0) { logp("do not specify both output file and output fd in do_rs_run()\n"); return RS_IO_ERROR; } if((bfd || in_file || in_zfile || infd>=0) && !(in_fb=rs_filebuf_new(bfd, in_file, in_zfile, infd, ASYNC_BUF_LEN, -1, cntr))) return RS_MEM_ERROR; if((out_file || out_zfile || outfd>=0) && !(out_fb=rs_filebuf_new(NULL, out_file, out_zfile, outfd, ASYNC_BUF_LEN, -1, cntr))) { if(in_fb) rs_filebuf_free(in_fb); return RS_MEM_ERROR; } //logp("before rs_job_drive\n"); result = rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, out_fb ? rs_outfilebuf_drain : NULL, out_fb); //logp("after rs_job_drive\n"); if(in_fb) rs_filebuf_free(in_fb); if(out_fb) rs_filebuf_free(out_fb); return result; }
static rs_result rs_whole_gzrun(struct asfd *asfd, rs_job_t *job, struct fzp *in_file, struct fzp *out_file, struct cntr *cntr) { rs_buffers_t buf; rs_result result; rs_filebuf_t *in_fb=NULL; rs_filebuf_t *out_fb=NULL; if(in_file) in_fb=rs_filebuf_new(asfd, NULL, in_file, -1, ASYNC_BUF_LEN, -1, cntr); if(out_file) out_fb=rs_filebuf_new(asfd, NULL, out_file, -1, ASYNC_BUF_LEN, -1, cntr); result=rs_job_drive(job, &buf, in_fb ? rs_infilebuf_fill : NULL, in_fb, out_fb ? rs_outfilebuf_drain : NULL, out_fb); rs_filebuf_free(&in_fb); rs_filebuf_free(&out_fb); return result; }
rs_result rs_loadsig_network_run(struct asfd *asfd, rs_job_t *job, struct cntr *cntr) { rs_buffers_t buf; rs_result result; rs_filebuf_t *in_fb=NULL; if(!(in_fb=rs_filebuf_new(asfd, NULL, NULL, asfd->fd, ASYNC_BUF_LEN, -1, cntr))) { result=RS_MEM_ERROR; goto end; } result=rs_job_drive(job, &buf, rs_infilebuf_fill, in_fb, NULL, NULL); end: rs_filebuf_free(&in_fb); return result; }
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; }
static void tear_down(rs_filebuf_t **fb) { rs_filebuf_free(fb); alloc_check(); }
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; }