static int deal_with_filedata(struct asfd *asfd, struct sdirs *sdirs, struct sbuf *rb, struct iobuf *rbuf, struct dpth *dpth, struct conf **cconfs) { iobuf_move(&rb->path, rbuf); if(rb->protocol1->datapth.buf) { // Receiving a delta. if(start_to_receive_delta(sdirs, cconfs, rb)) { logp("error in start_to_receive_delta\n"); return -1; } return 0; } // Receiving a whole new file. if(start_to_receive_new_file(asfd, sdirs, cconfs, rb, dpth)) { logp("error in start_to_receive_new_file\n"); return -1; } return 0; }
// returns 1 for finished ok. static int do_stuff_to_receive(struct sbuf *rb, FILE *p2fp, const char *datadirtmp, struct dpth *dpth, const char *working, char **last_requested, const char *deltmppath, struct cntr *cntr, struct config *cconf) { int ret=0; char rcmd; size_t rlen=0; size_t wlen=0; char *rbuf=NULL; // This also attempts to write anything in the write buffer. if(async_rw(&rcmd, &rbuf, &rlen, '\0', NULL, &wlen)) { logp("error in async_rw\n"); return -1; } if(rbuf) { if(rcmd==CMD_WARNING) { logp("WARNING: %s\n", rbuf); do_filecounter(cntr, rcmd, 0); } else if(rb->fp || rb->zp) { // Currently writing a file (or meta data) if(rcmd==CMD_APPEND) { int app; //logp("rlen: %d\n", rlen); if((rb->zp && (app=gzwrite(rb->zp, rbuf, rlen))<=0) || (rb->fp && (app=fwrite(rbuf, 1, rlen, rb->fp))<=0)) { logp("error when appending: %d\n", app); async_write_str(CMD_ERROR, "write failed"); ret=-1; } do_filecounter_recvbytes(cntr, rlen); } else if(rcmd==CMD_END_FILE) { // Finished the file. // Write it to the phase2 file, and free the // buffers. if(close_fp(&(rb->fp))) { logp("error closing delta for %s in receive\n", rb->path); ret=-1; } if(gzclose_fp(&(rb->zp))) { logp("error gzclosing delta for %s in receive\n", rb->path); ret=-1; } rb->endfile=rbuf; rb->elen=rlen; rbuf=NULL; if(!ret && rb->receivedelta && finish_delta(rb, working, deltmppath)) ret=-1; else if(!ret) { if(sbuf_to_manifest(rb, p2fp, NULL)) ret=-1; else { char cmd=rb->cmd; if(rb->receivedelta) do_filecounter_changed(cntr, cmd); else do_filecounter(cntr, cmd, 0); if(*last_requested && !strcmp(rb->path, *last_requested)) { free(*last_requested); *last_requested=NULL; } } } if(!ret) { char *cp=NULL; cp=strchr(rb->endfile, ':'); if(rb->endfile) do_filecounter_bytes(cntr, strtoull(rb->endfile, NULL, 10)); if(cp) { // checksum stuff goes here } } free_sbuf(rb); } else { logp("unexpected cmd from client while writing file: %c %s\n", rcmd, rbuf); ret=-1; } } // Otherwise, expecting to be told of a file to save. else if(rcmd==CMD_DATAPTH) { rb->datapth=rbuf; rbuf=NULL; } else if(rcmd==CMD_STAT) { rb->statbuf=rbuf; rb->slen=rlen; rbuf=NULL; } else if(filedata(rcmd)) { rb->cmd=rcmd; rb->plen=rlen; rb->path=rbuf; rbuf=NULL; if(rb->datapth) { // Receiving a delta. if(start_to_receive_delta(rb, working, deltmppath, cconf)) { logp("error in start_to_receive_delta\n"); ret=-1; } } else { // Receiving a whole new file. if(start_to_receive_new_file(rb, datadirtmp, dpth, cntr, cconf)) { logp("error in start_to_receive_new_file\n"); ret=-1; } } } else if(rcmd==CMD_GEN && !strcmp(rbuf, "okbackupphase2end")) { ret=1; //logp("got okbackupphase2end\n"); } else if(rcmd==CMD_INTERRUPT) { // Interrupt - forget about the last requested file // if it matches. Otherwise, we can get stuck on the // select in the async stuff, waiting for something // that will never arrive. if(*last_requested && !strcmp(rbuf, *last_requested)) { free(*last_requested); *last_requested=NULL; } } else { logp("unexpected cmd from client while expecting a file: %c %s\n", rcmd, rbuf); ret=-1; } if(rbuf) { free(rbuf); rbuf=NULL; } } //logp("returning: %d\n", ret); return ret; }
// returns 1 for finished ok. static int do_stuff_to_receive(struct asfd *asfd, struct sdirs *sdirs, struct conf *cconf, struct sbuf *rb, FILE *p2fp, struct dpthl *dpthl, char **last_requested) { struct iobuf *rbuf=asfd->rbuf; iobuf_free_content(rbuf); // This also attempts to write anything in the write buffer. if(asfd->as->rw(asfd->as)) { logp("error in async_rw\n"); return -1; } if(!rbuf->buf) return 0; if(rbuf->cmd==CMD_WARNING) { logp("WARNING: %s\n", rbuf->buf); cntr_add(cconf->cntr, rbuf->cmd, 0); } else if(rb->burp1->fp || rb->burp1->zp) { // Currently writing a file (or meta data) if(rbuf->cmd==CMD_APPEND) { if(deal_with_receive_append(asfd, rb, cconf)) goto error; } else if(rbuf->cmd==CMD_END_FILE) { if(deal_with_receive_end_file(asfd, sdirs, rb, p2fp, cconf, last_requested)) goto error; } else { iobuf_log_unexpected(rbuf, __func__); goto error; } } // Otherwise, expecting to be told of a file to save. else if(rbuf->cmd==CMD_DATAPTH) { iobuf_copy(&rb->burp1->datapth, rbuf); rbuf->buf=NULL; } else if(rbuf->cmd==CMD_ATTRIBS) { iobuf_copy(&rb->attr, rbuf); rbuf->buf=NULL; } else if(filedata(rbuf->cmd)) { iobuf_copy(&rb->path, rbuf); rbuf->buf=NULL; if(rb->burp1->datapth.buf) { // Receiving a delta. if(start_to_receive_delta(sdirs, cconf, rb)) { logp("error in start_to_receive_delta\n"); goto error; } } else { // Receiving a whole new file. if(start_to_receive_new_file(asfd, sdirs, cconf, rb, dpthl)) { logp("error in start_to_receive_new_file\n"); goto error; } } } else if(rbuf->cmd==CMD_GEN && !strcmp(rbuf->buf, "okbackupphase2end")) goto end_phase2; else if(rbuf->cmd==CMD_INTERRUPT) { // Interrupt - forget about the last requested file // if it matches. Otherwise, we can get stuck on the // select in the async stuff, waiting for something // that will never arrive. if(*last_requested && !strcmp(rbuf->buf, *last_requested)) { free(*last_requested); *last_requested=NULL; } } else { iobuf_log_unexpected(rbuf, __func__); goto error; } return 0; end_phase2: return 1; error: return -1; }