"Fossies" - the Fresh Open Source Software Archive

Member "burp-2.3.6/utest/builders/server/build_manifest.c" (28 Apr 2019, 8595 Bytes) of package /linux/privat/burp-2.3.6.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "build_manifest.c" see the Fossies "Dox" file reference documentation.

    1 #include "../../test.h"
    2 #include "../build.h"
    3 #include "../../prng.h"
    4 #include "../../../src/alloc.h"
    5 #include "../../../src/attribs.h"
    6 #include "../../../src/fsops.h"
    7 #include "../../../src/fzp.h"
    8 #include "../../../src/handy.h"
    9 #include "../../../src/hexmap.h"
   10 #include "../../../src/msg.h"
   11 #include "../../../src/pathcmp.h"
   12 #include "../../../src/protocol1/handy.h"
   13 #include "../../../src/protocol2/blist.h"
   14 #include "../../../src/sbuf.h"
   15 #include "../../../src/slist.h"
   16 #include "../../../src/server/manio.h"
   17 #include "../../../src/server/sdirs.h"
   18 
   19 // Deal with a hack where the index is stripped off the beginning of the
   20 // attributes when protocol2 saves to the manifest.
   21 static void hack_protocol2_attr(struct iobuf *attr)
   22 {
   23     char *cp=NULL;
   24     char *copy=NULL;
   25     size_t newlen;
   26     fail_unless((cp=strchr(attr->buf, ' '))!=NULL);
   27     fail_unless((copy=strdup_w(cp, __func__))!=NULL);
   28     newlen=attr->buf-cp+attr->len;
   29     iobuf_free_content(attr);
   30     iobuf_set(attr, CMD_ATTRIBS, copy, newlen);
   31 }
   32 
   33 static struct slist *build_manifest_phase1(const char *path,
   34     enum protocol protocol, int entries)
   35 {
   36     struct sbuf *sb;
   37     struct slist *slist=NULL;
   38     struct manio *manio=NULL;
   39 
   40     slist=build_slist_phase1(NULL /*prefix*/, protocol, entries);
   41 
   42     fail_unless((manio=manio_open_phase1(path, "wb", protocol))!=NULL);
   43 
   44     for(sb=slist->head; sb; sb=sb->next)
   45     {
   46         fail_unless(!manio_write_sbuf(manio, sb));
   47         if(protocol==PROTO_2) hack_protocol2_attr(&sb->attr);
   48     }
   49 
   50     fail_unless(!send_msg_fzp(manio->fzp,
   51         CMD_GEN, "phase1end", strlen("phase1end")));
   52 
   53     fail_unless(!manio_close(&manio));
   54     return slist;
   55 }
   56 
   57 static char *gen_endfile_str(void)
   58 {
   59     uint64_t bytes;
   60     uint8_t checksum[MD5_DIGEST_LENGTH];
   61     bytes=prng_next64();
   62     prng_md5sum(checksum);
   63     return get_endfile_str(bytes, checksum);
   64 }
   65 
   66 static void set_sbuf_protocol1(struct sbuf *sb)
   67 {
   68     if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
   69     {
   70         char *endfile=gen_endfile_str();
   71         iobuf_free_content(&sb->endfile);
   72         sb->endfile.cmd=CMD_END_FILE;
   73         sb->endfile.len=strlen(endfile);
   74         fail_unless((sb->endfile.buf
   75             =strdup_w(endfile, __func__))!=NULL);
   76     }
   77 
   78     if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
   79     {
   80         char *datapth;
   81         fail_unless((datapth=prepend_s(TREE_DIR, sb->path.buf))!=NULL);
   82         iobuf_free_content(&sb->protocol1->datapth);
   83         iobuf_from_str(&sb->protocol1->datapth, CMD_DATAPTH, datapth);
   84     }
   85 }
   86 
   87 static void set_sbuf_protocol2(struct slist *slist, struct sbuf *sb,
   88     int with_data_files)
   89 {
   90     struct blk *tail=NULL;
   91     struct blist *blist=slist->blist;
   92     if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
   93     {
   94         if(blist->tail) tail=blist->tail;
   95         build_blks(blist, prng_next()%50, with_data_files);
   96         if(tail)
   97             sb->protocol2->bstart=tail->next;
   98         else
   99             sb->protocol2->bstart=blist->head; // first one
  100 
  101         if(sb->protocol2->bstart)
  102             sb->protocol2->bend=slist->blist->tail;
  103     }
  104 }
  105 
  106 static void set_sbuf(struct slist *slist, struct sbuf *sb, int with_data_files)
  107 {
  108     if(sb->protocol1) set_sbuf_protocol1(sb);
  109     else set_sbuf_protocol2(slist, sb, with_data_files);
  110 }
  111 
  112 static struct slist *build_slist(enum protocol protocol, int entries,
  113     int with_data_files)
  114 {
  115     struct sbuf *sb;
  116     struct slist *slist;
  117     slist=build_slist_phase1(NULL /* prefix */, protocol, entries);
  118     for(sb=slist->head; sb; sb=sb->next)
  119         set_sbuf(slist, sb, with_data_files);
  120     return slist;
  121 }
  122 
  123 static struct slist *do_build_manifest(struct manio *manio,
  124     enum protocol protocol, int entries, int with_data_files)
  125 {
  126     struct sbuf *sb;
  127     struct slist *slist=NULL;
  128 
  129     slist=build_slist(protocol, entries, with_data_files);
  130 
  131     for(sb=slist->head; sb; sb=sb->next)
  132     {
  133         fail_unless(!manio_write_sbuf(manio, sb));
  134         if(protocol==PROTO_2)
  135         {
  136             struct blk *blk=NULL;
  137             for(blk=sb->protocol2->bstart;
  138                 blk && blk!=sb->protocol2->bend; blk=blk->next)
  139             {
  140                 fail_unless(!manio_write_sig_and_path(manio,
  141                     blk));
  142             }
  143             if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
  144             {
  145                 struct iobuf endfile;
  146                 iobuf_from_str(&endfile,
  147                     CMD_END_FILE, (char *)"0:0");
  148                 fail_unless(!iobuf_send_msg_fzp(&endfile,
  149                     manio->fzp));
  150             }
  151             hack_protocol2_attr(&sb->attr);
  152         }
  153     }
  154 
  155     return slist;
  156 }
  157 
  158 static struct slist *build_manifest_phase2(const char *path,
  159     enum protocol protocol, int entries)
  160 {
  161     struct slist *slist=NULL;
  162     struct manio *manio=NULL;
  163 
  164     fail_unless((manio=manio_open_phase2(path, "wb", protocol))!=NULL);
  165     slist=do_build_manifest(manio,
  166         protocol, entries, 0 /*with_data_files*/);
  167     fail_unless(!manio_close(&manio));
  168 
  169     return slist;
  170 }
  171 
  172 static struct slist *build_manifest_phase3(const char *path,
  173     enum protocol protocol, int entries)
  174 {
  175     struct slist *slist=NULL;
  176     struct manio *manio=NULL;
  177 
  178     fail_unless((manio=manio_open_phase3(path, "wb", protocol,
  179         RMANIFEST_RELATIVE))!=NULL);
  180     slist=do_build_manifest(manio,
  181         protocol, entries, 0 /*with_data_files*/);
  182     fail_unless(!manio_close(&manio));
  183 
  184     return slist;
  185 }
  186 
  187 static struct slist *build_manifest_final(const char *path,
  188     enum protocol protocol, int entries)
  189 {
  190     // Same as phase3.
  191     return build_manifest_phase3(path, protocol, entries);
  192 }
  193 
  194 struct slist *build_manifest(const char *path,
  195     enum protocol protocol, int entries, int phase)
  196 {
  197     switch(phase)
  198     {
  199         case 0: return build_manifest_final(path, protocol, entries);
  200         case 1: return build_manifest_phase1(path, protocol, entries);
  201         case 2: return build_manifest_phase2(path, protocol, entries);
  202         case 3: return build_manifest_phase3(path, protocol, entries);
  203         default:
  204             fprintf(stderr, "Do not know how to build_manifest phase %d\n", phase);
  205             fail_unless(0);
  206             return NULL;
  207     }
  208 }
  209 
  210 struct slist *build_manifest_with_data_files(const char *path,
  211     const char *datapath, int entries, int data_files)
  212 {
  213     struct blk *b=NULL;
  214     struct slist *slist=NULL;
  215     struct manio *manio=NULL;
  216     struct fzp *fzp=NULL;
  217     char spath[256]="";
  218     char cpath[256]="";
  219 
  220     fail_unless((manio=manio_open_phase3(path, "wb", PROTO_2,
  221         RMANIFEST_RELATIVE))!=NULL);
  222     slist=do_build_manifest(manio, PROTO_2, entries, data_files);
  223     fail_unless(!manio_close(&manio));
  224 
  225     for(b=slist->blist->head; b; b=b->next)
  226     {
  227         snprintf(spath, sizeof(spath), "%s/%s", datapath,
  228             uint64_to_savepathstr(b->savepath));
  229         if(strcmp(spath, cpath))
  230         {
  231             snprintf(cpath, sizeof(cpath), "%s", spath);
  232             fzp_close(&fzp);
  233         }
  234         if(!fzp)
  235         {
  236             fail_unless(!build_path_w(cpath));
  237             fail_unless((fzp=fzp_open(cpath, "wb"))!=NULL);
  238         }
  239         fzp_printf(fzp, "%c%04X%s", CMD_DATA, strlen("data"), "data");
  240     }
  241     fzp_close(&fzp);
  242 
  243     return slist;
  244 }
  245 
  246 void build_manifest_phase2_from_slist(const char *path,
  247     struct slist *slist, enum protocol protocol, int short_write)
  248 {
  249     struct sbuf *sb;
  250     struct manio *manio=NULL;
  251 
  252     for(sb=slist->head; sb; sb=sb->next)
  253         set_sbuf(slist, sb, 0 /* with_data_files */);
  254 
  255     fail_unless((manio=manio_open_phase2(path, "wb", protocol))!=NULL);
  256 
  257     for(sb=slist->head; sb; sb=sb->next)
  258     {
  259         fail_unless(!manio_write_sbuf(manio, sb));
  260         if(protocol==PROTO_2)
  261         {
  262             struct blk *blk=NULL;
  263             for(blk=sb->protocol2->bstart;
  264                 blk && blk!=sb->protocol2->bend; blk=blk->next)
  265             {
  266                 fail_unless(!manio_write_sig_and_path(manio,
  267                     blk));
  268             }
  269             if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
  270             {
  271                 struct iobuf endfile;
  272                 iobuf_from_str(&endfile,
  273                     CMD_END_FILE, (char *)"0:0");
  274                 fail_unless(!iobuf_send_msg_fzp(&endfile,
  275                     manio->fzp));
  276             }
  277             hack_protocol2_attr(&sb->attr);
  278         }
  279     }
  280 
  281     if(short_write)
  282     {
  283         man_off_t *pos;
  284         fail_unless((pos=manio_tell(manio))!=NULL);
  285         if(pos->offset>=short_write) pos->offset-=short_write;
  286         fail_unless(!manio_close_and_truncate(&manio,
  287             pos, 0 /* compression */));
  288         man_off_t_free(&pos);
  289     }
  290     fail_unless(!manio_close(&manio));
  291 }
  292 
  293 void build_manifest_phase1_from_slist(const char *path,
  294     struct slist *slist, enum protocol protocol)
  295 {
  296     struct sbuf *sb;
  297     struct manio *manio=NULL;
  298     struct iobuf datapth;
  299     struct iobuf endfile;
  300     iobuf_init(&datapth);
  301     iobuf_init(&endfile);
  302 
  303     for(sb=slist->head; sb; sb=sb->next)
  304         set_sbuf(slist, sb, 0 /* with_data_files */);
  305 
  306     fail_unless((manio=manio_open_phase1(path, "wb", protocol))!=NULL);
  307 
  308     for(sb=slist->head; sb; sb=sb->next)
  309     {
  310         // Might be given an slist that has datapth or endfile set,
  311         // which should not go into a phase1 scan. Deal with it.
  312         if(sb->protocol1
  313           && sb->protocol1->datapth.buf)
  314             iobuf_move(&datapth, &sb->protocol1->datapth);
  315         if(sb->endfile.buf)
  316             iobuf_move(&endfile, &sb->endfile);
  317         fail_unless(!manio_write_sbuf(manio, sb));
  318         if(datapth.buf)
  319             iobuf_move(&sb->protocol1->datapth, &datapth);
  320         if(endfile.buf)
  321             iobuf_move(&sb->endfile, &endfile);
  322     }
  323 
  324     fail_unless(!send_msg_fzp(manio->fzp,
  325         CMD_GEN, "phase1end", strlen("phase1end")));
  326 
  327     fail_unless(!manio_close(&manio));
  328 }