static int duplicate_file(const char *oldpath, const char *newpath) { int ret=-1; size_t s=0; size_t t=0; struct fzp *op=NULL; struct fzp *np=NULL; char buf[DUP_CHUNK]=""; if(!(op=fzp_open(oldpath, "rb")) || !(np=fzp_open(newpath, "wb"))) goto end; while((s=fzp_read(op, buf, DUP_CHUNK))>0) { t=fzp_write(np, buf, s); if(t!=s) { logp("could not write all bytes: %lu!=%lu\n", (unsigned long)s, (unsigned long)t); goto end; } } ret=0; end: fzp_close(&np); fzp_close(&op); if(ret) logp("could not duplicate %s to %s\n", oldpath, newpath); return ret; }
static int bcompress(const char *src, const char *dst, int compression) { int res; int got; struct fzp *sfzp=NULL; struct fzp *dfzp=NULL; char buf[ZCHUNK]; if(!(sfzp=fzp_open(src, "rb")) || !(dfzp=fzp_gzopen(dst, comp_level(compression)))) goto error; while((got=fzp_read(sfzp, buf, sizeof(buf)))>0) { res=fzp_write(dfzp, buf, got); if(res!=got) { logp("compressing %s - read %d but wrote %d\n", src, got, res); goto error; } } fzp_close(&sfzp); return fzp_close(&dfzp); error: fzp_close(&sfzp); fzp_close(&dfzp); return -1; }
int send_msg_fzp(struct fzp *fzp, enum cmd cmd, const char *buf, size_t s) { if(fzp_printf(fzp, "%c%04X", cmd, (unsigned int)s)!=5 || fzp_write(fzp, buf, s)!=s || fzp_printf(fzp, "\n")!=1) { logp("Unable to write message to file: %s\n", strerror(errno)); return -1; } return 0; }
int zlib_inflate(struct asfd *asfd, const char *source_path, const char *dest_path, struct conf **confs) { int ret=-1; size_t b=0; uint8_t in[ZCHUNK]; struct fzp *src=NULL; struct fzp *dst=NULL; if(!(src=fzp_gzopen(source_path, "rb"))) { logw(asfd, confs, "could not gzopen %s in %s: %s\n", source_path, __func__, strerror(errno)); goto end; } if(!(dst=fzp_open(dest_path, "wb"))) { logw(asfd, confs, "could not open %s in %s: %s\n", dest_path, __func__, strerror(errno)); goto end; } while((b=fzp_read(src, in, ZCHUNK))>0) { if(fzp_write(dst, in, b)!=b) { logw(asfd, confs, "error when writing to %s\n", dest_path); goto end; } } if(!fzp_eof(src)) { logw(asfd, confs, "error while reading %s in %s\n", source_path, __func__); goto end; } if(fzp_close(&dst)) { logw(asfd, confs, "error when closing %s in %s: %s\n", dest_path, __func__, strerror(errno)); goto end; } ret=0; end: fzp_close(&src); fzp_close(&dst); return ret; }
END_TEST START_TEST(test_protocol1_verify_file_gzip_read_failure) { struct asfd *asfd; struct cntr *cntr; struct sbuf *sb; const char *path="somepath"; const char *datapth="/datapth"; const char *endfile="0:0"; const char *best=BASE "/existent"; const char *plain_text="some plain text"; size_t s; struct fzp *fzp; s=strlen(plain_text); clean(); cntr=setup_cntr(); sb=setup_sbuf(path, datapth, endfile, 1/*compression*/); // Make a corrupt gzipped file. build_path_w(best); fail_unless((fzp=fzp_gzopen(best, "wb"))!=NULL); fail_unless(fzp_write(fzp, plain_text, s)==s); fail_unless(!fzp_close(&fzp)); fail_unless((fzp=fzp_open(best, "r+b"))!=NULL); fail_unless(!fzp_seek(fzp, 10, SEEK_SET)); fail_unless(fzp_write(fzp, "aa", 2)==2); fail_unless(!fzp_close(&fzp)); asfd=asfd_mock_setup(&areads, &awrites); setup_error_while_reading(asfd, best); // Returns 0 so that the parent process continues. fail_unless(!verify_file(asfd, sb, 0 /*patches*/, best, cntr)); fail_unless(cntr->ent[CMD_WARNING]->count==1); tear_down(&sb, &cntr, NULL, &asfd); }
static int deal_with_receive_append(struct asfd *asfd, struct sbuf *rb, struct conf **cconfs) { int app=0; static struct iobuf *rbuf; rbuf=asfd->rbuf; //logp("rbuf->len: %d\n", rbuf->len); cntr_add_recvbytes(get_cntr(cconfs[OPT_CNTR]), rbuf->len); if(rb->protocol1->fzp) app=fzp_write(rb->protocol1->fzp, rbuf->buf, rbuf->len); if(app>0) return 0; logp("error when appending: %d\n", app); asfd->write_str(asfd, CMD_ERROR, "write failed"); return -1; }
static int my_asfd_write(struct asfd *asfd, struct iobuf *wbuf) { fail_unless(fzp_write(output, wbuf->buf, wbuf->len)==wbuf->len); return 0; }
/* * The buf is already using BUF for an output buffer, and probably * contains some buffered output now. Write this out to F, and reset * the buffer cursor. */ rs_result rs_outfilebuf_drain(rs_job_t *job, rs_buffers_t *buf, void *opaque) { rs_filebuf_t *fb=(rs_filebuf_t *)opaque; int fd=fb->fd; size_t wlen; //logp("in rs_outfilebuf_drain\n"); /* This is only allowed if either the buf has no output buffer * yet, or that buffer could possibly be BUF. */ if(!buf->next_out) { if(buf->avail_out) { logp("buf->avail_out is %d in %s\n", buf->avail_out, __func__); return RS_IO_ERROR; } buf->next_out = fb->buf; buf->avail_out = fb->buf_len; return RS_DONE; } if(buf->avail_out > fb->buf_len) { logp("buf->avail_out > fb->buf_len (%d > %d) in %s\n", buf->avail_out, fb->buf_len, __func__); return RS_IO_ERROR; } if(buf->next_out < fb->buf) { logp("buf->next_out < fb->buf (%p < %p) in %s\n", buf->next_out, fb->buf, __func__); return RS_IO_ERROR; } if(buf->next_out > fb->buf + fb->buf_len) { logp("buf->next_out > fb->buf + fb->buf_len in %s\n", __func__); return RS_IO_ERROR; } if((wlen=buf->next_out-fb->buf)>0) { //logp("wlen: %d\n", wlen); if(fd>0) { size_t w=wlen; static struct iobuf *wbuf=NULL; if(!wbuf && !(wbuf=iobuf_alloc())) return RS_IO_ERROR; wbuf->cmd=CMD_APPEND; wbuf->buf=fb->buf; wbuf->len=wlen; switch(fb->asfd->append_all_to_write_buffer( fb->asfd, wbuf)) { case APPEND_OK: break; case APPEND_BLOCKED: return RS_BLOCKED; case APPEND_ERROR: default: return RS_IO_ERROR; } fb->bytes+=w; } else { size_t result=0; result=fzp_write(fb->fzp, fb->buf, wlen); if(wlen!=result) { logp("error draining buf to file: %s", strerror(errno)); return RS_IO_ERROR; } } } buf->next_out = fb->buf; buf->avail_out = fb->buf_len; return RS_DONE; }