static void do_fork(void) { switch(fork()) { case -1: fail_unless(0==1); break; case 0: // Child. { struct dpth *dpth; const char *savepath; dpth=dpth_alloc(); dpth_protocol2_init(dpth, lockpath, "testclient", cfiles, MAX_STORAGE_SUBDIRS); savepath=dpth_protocol2_mk(dpth); write_to_dpth(dpth, savepath); sleep(2); dpth_free(&dpth); exit(0); } default: break; } // Parent. }
struct dpth *dpth_alloc(const char *base_path) { struct dpth *dpth=NULL; if((dpth=(struct dpth *)calloc_w(1, sizeof(struct dpth), __func__)) && (dpth->base_path=strdup_w(base_path, __func__))) return dpth; dpth_free(&dpth); return NULL; }
static void tear_down(struct dpth **dpth) { dpth_free(dpth); fail_unless(recursive_delete(lockpath, "", 1)==0); fail_unless(free_count==alloc_count); }
static #endif int do_backup_phase2_server_protocol2(struct async *as, struct asfd *chfd, struct sdirs *sdirs, int resume, struct conf **confs) { int ret=-1; uint8_t end_flags=0; struct slist *slist=NULL; struct iobuf wbuf; struct dpth *dpth=NULL; man_off_t *p1pos=NULL; struct manios *manios=NULL; // This is used to tell the client that a number of consecutive blocks // have been found and can be freed. uint64_t wrap_up=0; struct asfd *asfd=NULL; struct cntr *cntr=NULL; struct sbuf *csb=NULL; uint64_t file_no=1; if(!as) { logp("async not provided to %s()\n", __func__); goto end; } if(!sdirs) { logp("sdirs not provided to %s()\n", __func__); goto end; } if(!confs) { logp("confs not provided to %s()\n", __func__); goto end; } asfd=as->asfd; if(!asfd) { logp("asfd not provided to %s()\n", __func__); goto end; } if(!chfd) { logp("chfd not provided to %s()\n", __func__); goto end; } cntr=get_cntr(confs); if(get_int(confs[OPT_BREAKPOINT])>=2000 && get_int(confs[OPT_BREAKPOINT])<3000) { breaking=get_int(confs[OPT_BREAKPOINT]); breakcount=breaking-2000; } logp("Phase 2 begin (recv backup data)\n"); if(!(dpth=dpth_alloc()) || dpth_protocol2_init(dpth, sdirs->data, get_string(confs[OPT_CNAME]), sdirs->cfiles, get_int(confs[OPT_MAX_STORAGE_SUBDIRS]))) goto end; if(resume && !(p1pos=do_resume(sdirs, dpth, confs))) goto end; if(!(manios=manios_open_phase2(sdirs, p1pos, PROTO_2)) || !(slist=slist_alloc()) || !(csb=sbuf_alloc(PROTO_2))) goto end; iobuf_free_content(asfd->rbuf); memset(&wbuf, 0, sizeof(struct iobuf)); while(!(end_flags&END_BACKUP)) { if(maybe_add_from_scan(manios, slist, chfd, &csb)) goto end; if(!wbuf.len) { if(get_wbuf_from_sigs(&wbuf, slist, &end_flags)) goto end; if(!wbuf.len) { get_wbuf_from_files(&wbuf, slist, manios, &end_flags, &file_no); } } if(wbuf.len && asfd->append_all_to_write_buffer(asfd, &wbuf)==APPEND_ERROR) goto end; if(append_for_champ_chooser(chfd, slist->blist, end_flags)) goto end; if(as->read_write(as)) { logp("error from as->read_write in %s\n", __func__); goto end; } while(asfd->rbuf->buf) { if(deal_with_read(asfd->rbuf, slist, cntr, &end_flags, dpth)) goto end; // Get as much out of the readbuf as possible. if(asfd->parse_readbuf(asfd)) goto end; } while(chfd->rbuf->buf) { if(deal_with_read_from_chfd(chfd, slist->blist, &wrap_up, dpth, cntr)) goto end; // Get as much out of the readbuf as possible. if(chfd->parse_readbuf(chfd)) goto end; } if(write_to_changed_file(asfd, chfd, manios, slist, end_flags)) goto end; } // Hack: If there are some entries left after the last entry that // contains block data, it will not be written to the changed file // yet because the last entry of block data has not had // sb->protocol2->bend set. if(slist->head && slist->head->next) { struct sbuf *sb=NULL; sb=slist->head; slist->head=sb->next; sbuf_free(&sb); if(write_to_changed_file(asfd, chfd, manios, slist, end_flags)) goto end; } if(manios_close(&manios)) goto end; if(check_for_missing_work_in_slist(slist)) goto end; // Need to release the last left. There should be one at most. if(dpth->head && dpth->head->next) { logp("ERROR: More data locks remaining after: %s\n", dpth->head->save_path); goto end; } if(dpth_release_all(dpth)) goto end; ret=0; end: logp("End backup\n"); sbuf_free(&csb); slist_free(&slist); if(asfd) iobuf_free_content(asfd->rbuf); if(chfd) iobuf_free_content(chfd->rbuf); dpth_free(&dpth); manios_close(&manios); man_off_t_free(&p1pos); return ret; }
static void tear_down(struct dpth **dpth) { dpth_free(dpth); fail_unless(recursive_delete(lockpath)==0); alloc_check(); }
int backup_phase2_server(struct async *as, struct sdirs *sdirs, const char *manifest_dir, int resume, struct conf *conf) { int ret=-1; int sigs_end=0; int backup_end=0; int requests_end=0; int blk_requests_end=0; struct slist *slist=NULL; struct blist *blist=NULL; struct iobuf *wbuf=NULL; struct dpth *dpth=NULL; struct manio *cmanio=NULL; // current manifest struct manio *p1manio=NULL; // phase1 scan manifest struct manio *chmanio=NULL; // changed manifest struct manio *unmanio=NULL; // unchanged manifest // This is used to tell the client that a number of consecutive blocks // have been found and can be freed. uint64_t wrap_up=0; // Main fd is first in the list. struct asfd *asfd=as->asfd; // Champ chooser fd is second in the list. struct asfd *chfd=asfd->next; logp("Phase 2 begin (recv backup data)\n"); //if(champ_chooser_init(sdirs->data, conf) if(!(cmanio=manio_alloc()) || !(p1manio=manio_alloc()) || !(chmanio=manio_alloc()) || !(unmanio=manio_alloc()) || manio_init_read(cmanio, sdirs->cmanifest) || manio_init_read(p1manio, sdirs->phase1data) || manio_init_write(chmanio, sdirs->changed) || manio_init_write(unmanio, sdirs->unchanged) || !(slist=slist_alloc()) || !(blist=blist_alloc()) || !(wbuf=iobuf_alloc()) || !(dpth=dpth_alloc(sdirs->data)) || dpth_init(dpth)) goto end; // The phase1 manifest looks the same as a burp1 one. manio_set_protocol(p1manio, PROTO_BURP1); while(!backup_end) { if(maybe_add_from_scan(asfd, p1manio, cmanio, unmanio, slist, conf)) goto end; if(!wbuf->len) { if(get_wbuf_from_sigs(wbuf, slist, blist, sigs_end, &blk_requests_end, dpth, conf)) goto end; if(!wbuf->len) { get_wbuf_from_files(wbuf, slist, p1manio, &requests_end); } } if(wbuf->len) asfd->append_all_to_write_buffer(asfd, wbuf); append_for_champ_chooser(chfd, blist, sigs_end); if(as->read_write(as)) { logp("error in %s\n", __func__); goto end; } while(asfd->rbuf->buf) { if(deal_with_read(asfd->rbuf, slist, blist, conf, &sigs_end, &backup_end, dpth)) goto end; // Get as much out of the // readbuf as possible. if(asfd->parse_readbuf(asfd)) goto end; } while(chfd->rbuf->buf) { if(deal_with_read_from_chfd(asfd, chfd, blist, &wrap_up, dpth)) goto end; // Get as much out of the // readbuf as possible. if(chfd->parse_readbuf(chfd)) goto end; } if(write_to_changed_file(asfd, chfd, chmanio, slist, blist, dpth, backup_end, conf)) goto end; } // Hack: If there are some entries left after the last entry that // contains block data, it will not be written to the changed file // yet because the last entry of block data has not had // sb->burp2->bend set. if(slist->head && slist->head->next) { slist->head=slist->head->next; if(write_to_changed_file(asfd, chfd, chmanio, slist, blist, dpth, backup_end, conf)) goto end; } if(manio_close(unmanio) || manio_close(chmanio)) goto end; if(blist->head) { logp("ERROR: finishing but still want block: %lu\n", blist->head->index); goto end; } // Need to release the last left. There should be one at most. if(dpth->head && dpth->head->next) { logp("ERROR: More data locks remaining after: %s\n", dpth->head->save_path); goto end; } if(dpth_release_all(dpth)) goto end; ret=0; end: logp("End backup\n"); slist_free(slist); blist_free(blist); iobuf_free_content(asfd->rbuf); iobuf_free_content(chfd->rbuf); // Write buffer did not allocate 'buf'. if(wbuf) wbuf->buf=NULL; iobuf_free(wbuf); dpth_release_all(dpth); dpth_free(&dpth); manio_free(&cmanio); manio_free(&p1manio); manio_free(&chmanio); manio_free(&unmanio); return ret; }
int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs, const char *incexc, int resume, struct conf **cconfs) { int ret=0; struct manio *p1manio=NULL; struct dpth *dpth=NULL; char *deltmppath=NULL; char *last_requested=NULL; // Where to write changed data. // Data is not getting written to a compressed file. // This is important for recovery if the power goes. struct fzp *chfp=NULL; struct fzp *ucfp=NULL; // unchanged data struct fzp *cmanfp=NULL; // previous (current) manifest. struct sbuf *cb=NULL; // file list in current manifest struct sbuf *p1b=NULL; // file list from client struct sbuf *rb=NULL; // receiving file from client struct asfd *asfd=as->asfd; int breaking=0; int breakcount=0; if(get_int(cconfs[OPT_BREAKPOINT])>=2000 && get_int(cconfs[OPT_BREAKPOINT])<3000) { breaking=get_int(cconfs[OPT_BREAKPOINT]); breakcount=breaking-2000; } logp("Begin phase2 (receive file data)\n"); if(!(dpth=dpth_alloc()) || dpth_protocol1_init(dpth, sdirs->currentdata, get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS]))) goto error; if(open_previous_manifest(&cmanfp, sdirs, incexc, cconfs)) goto error; if(get_int(cconfs[OPT_DIRECTORY_TREE])) { // Need to make sure we do not try to create a path that is // too long. if(build_path_w(sdirs->treepath)) goto error; treepathlen=strlen(sdirs->treepath); init_fs_max(sdirs->treepath); } if(!(p1manio=manio_alloc()) || manio_init_read(p1manio, sdirs->phase1data) || !(cb=sbuf_alloc(cconfs)) || !(p1b=sbuf_alloc(cconfs)) || !(rb=sbuf_alloc(cconfs))) goto error; manio_set_protocol(p1manio, PROTO_1); if(resume && do_resume(p1manio, sdirs, dpth, cconfs)) goto error; // Unchanged and changed should now be truncated correctly, we just // have to open them for appending. if(!(ucfp=fzp_open(sdirs->unchanged, "a+b")) || !(chfp=fzp_open(sdirs->changed, "a+b"))) goto error; if(manio_closed(p1manio) && manio_open_next_fpath(p1manio)) goto error; while(1) { if(breaking) { if(breakcount--==0) return breakpoint(cconfs, __func__); } //printf("in loop, %s %s %c\n", // cmanfp?"got cmanfp":"no cmanfp", // rb->path.buf?:"no rb->path", // rb->path.buf?'X':rb->path.cmd); if(write_status(CNTR_STATUS_BACKUP, rb->path.buf?rb->path.buf:p1b->path.buf, cconfs)) goto error; if(last_requested || manio_closed(p1manio) || asfd->writebuflen) { switch(do_stuff_to_receive(asfd, sdirs, cconfs, rb, chfp, dpth, &last_requested)) { case 0: break; case 1: goto end; // Finished ok. case -1: goto error; } } switch(do_stuff_to_send(asfd, p1b, &last_requested)) { case 0: break; case 1: continue; case -1: goto error; } if(manio_closed(p1manio)) continue; sbuf_free_content(p1b); switch(manio_sbuf_fill_phase1(p1manio, asfd, p1b, NULL, sdirs, cconfs)) { case 0: break; case 1: manio_close(p1manio); if(asfd->write_str(asfd, CMD_GEN, "backupphase2end")) goto error; break; case -1: goto error; } if(!cmanfp) { // No old manifest, need to ask for a new file. if(process_new(sdirs, cconfs, p1b, ucfp)) goto error; continue; } // Have an old manifest, look for it there. // Might already have it, or be ahead in the old // manifest. if(cb->path.buf) switch(maybe_process_file(asfd, sdirs, cb, p1b, ucfp, cconfs)) { case 0: break; case 1: continue; case -1: goto error; } while(cmanfp) { sbuf_free_content(cb); switch(sbufl_fill(cb, asfd, cmanfp, cconfs)) { case 0: break; case 1: fzp_close(&cmanfp); if(process_new(sdirs, cconfs, p1b, ucfp)) goto error; continue; case -1: goto error; } switch(maybe_process_file(asfd, sdirs, cb, p1b, ucfp, cconfs)) { case 0: continue; case 1: break; case -1: goto error; } break; } } error: ret=-1; end: if(fzp_close(&chfp)) { logp("error closing %s in %s\n", sdirs->changed, __func__); ret=-1; } if(fzp_close(&ucfp)) { logp("error closing %s in %s\n", sdirs->unchanged, __func__); ret=-1; } free_w(&deltmppath); sbuf_free(&cb); sbuf_free(&p1b); sbuf_free(&rb); manio_free(&p1manio); fzp_close(&cmanfp); dpth_free(&dpth); if(!ret) unlink(sdirs->phase1data); logp("End phase2 (receive file data)\n"); return ret; }