// When a backup is ongoing, use this to add newly complete candidates. int candidate_add_fresh(const char *path, struct conf *conf) { int ars; int ret=-1; gzFile zp=NULL; const char *cp=NULL; struct sbuf *sb=NULL; struct candidate *candidate=NULL; struct blk *blk=NULL; if(!(candidate=candidates_add_new())) goto end; cp=path+strlen(conf->directory); while(cp && *cp=='/') cp++; if(!(candidate->path=strdup(cp))) { log_out_of_memory(__func__); goto end; } if(!(sb=sbuf_alloc(conf)) || !(blk=blk_alloc()) || !(zp=gzopen_file(path, "rb"))) goto end; while(zp) { if((ars=sbuf_fill_from_gzfile(sb, NULL /* struct async */, zp, blk, NULL, conf))<0) goto end; else if(ars>0) { // Reached the end. break; } if(!*(blk->weak)) continue; if(is_hook(blk->weak)) { if(sparse_add_candidate(blk->weak, candidate)) goto end; } *blk->weak='\0'; } if(scores_grow(scores, candidates_len)) goto end; candidates_set_score_pointers(candidates, candidates_len, scores); scores_reset(scores); //printf("HERE: %d candidates\n", (int)candidates_len); ret=0; end: gzclose_fp(&zp); sbuf_free(sb); blk_free(blk); return ret; }
END_TEST START_TEST(test_sparse_add_one) { struct sparse *sparse; uint64_t f=0xFF11223344556677; struct candidate *candidate; fail_unless((candidate=candidate_alloc())!=NULL); fail_unless(!sparse_add_candidate(&f, candidate)); fail_unless((sparse=sparse_find(&f))!=NULL); fail_unless(sparse->size==1); fail_unless(sparse->candidates[0]==candidate); sparse_delete_all(); candidate_free(&candidate); fail_unless(!candidate); tear_down(); }
END_TEST START_TEST(test_sparse_add_many) { struct sparse *sparse; uint64_t f0=0xFF11223344556699; uint64_t f1=0xFF11223344556677; uint64_t f2=0xFF11223344556688; struct candidate *candidate1; struct candidate *candidate2; struct candidate *candidate3; struct candidate *candidate4; struct candidate *candidate5; fail_unless((candidate1=candidate_alloc())!=NULL); fail_unless((candidate2=candidate_alloc())!=NULL); fail_unless((candidate3=candidate_alloc())!=NULL); fail_unless((candidate4=candidate_alloc())!=NULL); fail_unless((candidate5=candidate_alloc())!=NULL); fail_unless(!sparse_add_candidate(&f1, candidate1)); fail_unless(!sparse_add_candidate(&f1, candidate1)); fail_unless(!sparse_add_candidate(&f1, candidate2)); fail_unless(!sparse_add_candidate(&f2, candidate3)); fail_unless(!sparse_add_candidate(&f2, candidate4)); fail_unless(!sparse_add_candidate(&f2, candidate5)); // Try same again. fail_unless((sparse=sparse_find(&f0))==NULL); fail_unless((sparse=sparse_find(&f1))!=NULL); fail_unless(sparse->size==2); fail_unless(sparse->candidates[0]==candidate1); fail_unless(sparse->candidates[1]==candidate2); fail_unless((sparse=sparse_find(&f2))!=NULL); fail_unless(sparse->size==3); fail_unless(sparse->candidates[0]==candidate3); fail_unless(sparse->candidates[1]==candidate4); fail_unless(sparse->candidates[2]==candidate5); sparse_delete_all(); candidate_free(&candidate1); candidate_free(&candidate2); candidate_free(&candidate3); candidate_free(&candidate4); candidate_free(&candidate5); tear_down(); }
// This deals with reading in the sparse index, as well as actual candidate // manifests. enum cand_ret candidate_load(struct candidate *candidate, const char *path, struct scores *scores) { enum cand_ret ret=CAND_RET_PERM; struct fzp *fzp=NULL; struct sbuf *sb=NULL; struct blk *blk=NULL; if(!(sb=sbuf_alloc(PROTO_2)) || !(blk=blk_alloc())) { ret=CAND_RET_PERM; goto error; } if(!(fzp=fzp_gzopen(path, "rb"))) { ret=CAND_RET_TEMP; goto error; } while(fzp) { sbuf_free_content(sb); switch(sbuf_fill_from_file(sb, fzp, blk, NULL)) { case 1: goto end; case -1: logp("Error reading %s in %s, pos %d\n", path, __func__, fzp_tell(fzp)); ret=CAND_RET_TEMP; goto error; } if(blk_fingerprint_is_hook(blk)) { if(sparse_add_candidate(&blk->fingerprint, candidate)) { ret=CAND_RET_PERM; goto error; } } else if(sb->path.cmd==CMD_MANIFEST) { if(!(candidate=candidates_add_new())) { ret=CAND_RET_PERM; goto error; } candidate->path=sb->path.buf; sb->path.buf=NULL; } blk->fingerprint=0; } end: if(scores_grow(scores, candidates_len)) { ret=CAND_RET_PERM; goto error; } candidates_set_score_pointers(candidates, candidates_len, scores); scores_reset(scores); //logp("Now have %d candidates\n", (int)candidates_len); ret=CAND_RET_OK; error: fzp_close(&fzp); sbuf_free(&sb); blk_free(&blk); return ret; }