LCOV - code coverage report
Current view: top level - src/server/protocol1 - dpth.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 61 61 100.0 %
Date: 2018-05-06 01:36:20 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../alloc.h"
       3             : #include "../../cmd.h"
       4             : #include "../../conf.h"
       5             : #include "../../log.h"
       6             : #include "../../prepend.h"
       7             : #include "dpth.h"
       8             : 
       9          11 : char *dpth_protocol1_mk(struct dpth *dpth, int compression, enum cmd cmd)
      10             : {
      11             :         static char path[32];
      12             :         // File data.
      13          44 :         snprintf(path, sizeof(path), "%04X/%04X/%04X%s",
      14          33 :                 dpth->comp[0], dpth->comp[1], dpth->comp[2],
      15             :                 // Because of the way EFS works, it cannot be compressed.
      16          11 :                 (compression && cmd!=CMD_EFS_FILE)?".gz":"");
      17          11 :         return path;
      18             : }
      19             : 
      20             : static char *dpth_mk_prim(struct dpth *dpth)
      21             : {
      22             :         static char path[5];
      23          19 :         snprintf(path, sizeof(path), "%04X", dpth->comp[0]);
      24             :         return path;
      25             : }
      26             : 
      27             : static char *dpth_mk_seco(struct dpth *dpth)
      28             : {
      29             :         static char path[10];
      30          19 :         snprintf(path, sizeof(path), "%04X/%04X", dpth->comp[0], dpth->comp[1]);
      31             :         return path;
      32             : }
      33             : 
      34          57 : static void get_highest_entry(const char *path, uint16_t *max)
      35             : {
      36          57 :         int ent=0;
      37          57 :         DIR *d=NULL;
      38          57 :         struct dirent *dp=NULL;
      39             : 
      40          57 :         *max=0;
      41          57 :         if(!(d=opendir(path))) return;
      42          88 :         while((dp=readdir(d)))
      43             :         {
      44          66 :                 if(!dp->d_ino
      45          66 :                   || !strcmp(dp->d_name, ".")
      46          44 :                   || !strcmp(dp->d_name, ".."))
      47          44 :                         continue;
      48          22 :                 ent=strtol(dp->d_name, NULL, 16);
      49          22 :                 if(ent>*max) *max=ent;
      50             :         }
      51          22 :         closedir(d);
      52             : }
      53             : 
      54          57 : static int get_next_comp(const char *currentdata,
      55             :         const char *path, uint16_t *comp)
      56             : {
      57          57 :         int ret=-1;
      58          57 :         char *tmp=NULL;
      59          57 :         if(path)
      60          38 :                 tmp=prepend_s(currentdata, path);
      61             :         else
      62          19 :                 tmp=strdup_w(currentdata, __func__);
      63          57 :         if(!tmp) goto end;
      64             : 
      65          57 :         get_highest_entry(tmp, comp);
      66          57 :         ret=0;
      67             : end:
      68          57 :         free_w(&tmp);
      69          57 :         return ret;
      70             : }
      71             : 
      72          19 : int dpth_protocol1_init(struct dpth *dpth, const char *basepath,
      73             :         int max_storage_subdirs)
      74             : {
      75          19 :         int ret=0;
      76          19 :         dpth->savepath=0;
      77          19 :         dpth->max_storage_subdirs=max_storage_subdirs;
      78             : 
      79          19 :         if((ret=get_next_comp(basepath,
      80             :                 NULL, &dpth->comp[0]))) goto end;
      81             : 
      82          38 :         if((ret=get_next_comp(basepath,
      83          19 :                 dpth_mk_prim(dpth), &dpth->comp[1]))) goto end;
      84             : 
      85          38 :         if((ret=get_next_comp(basepath,
      86          19 :                 dpth_mk_seco(dpth), &dpth->comp[2]))) goto end;
      87             : 
      88             :         // At this point, we have the latest data file. Increment to get the
      89             :         // next free one.
      90          19 :         ret=dpth_incr(dpth);
      91             : 
      92             : end:
      93          19 :         switch(ret)
      94             :         {
      95             :                 case -1: return -1;
      96          18 :                 default: return 0;
      97             :         }
      98             : }
      99             : 
     100          12 : int dpth_protocol1_set_from_string(struct dpth *dpth, const char *datapath)
     101             : {
     102          12 :         unsigned int a=0;
     103          12 :         unsigned int b=0;
     104          12 :         unsigned int c=0;
     105             : 
     106          12 :         if(!datapath
     107          12 :           || *datapath=='t') // The path used the tree style structure.
     108             :                 return 0;
     109             : 
     110          11 :         if((sscanf(datapath, "%04X/%04X/%04X", &a, &b, &c))!=3)
     111             :                 return -1;
     112           9 :         if(dpth->comp[0]==(int)a
     113           4 :           && dpth->comp[1]==(int)b
     114           2 :           && dpth->comp[2] > (int)c)
     115             :                 return 0;
     116           8 :         if(dpth->comp[0]==(int)a
     117           3 :           && dpth->comp[1] > (int)b)
     118             :                 return 0;
     119           7 :         if(dpth->comp[0] > (int)a)
     120             :                 return 0;
     121             : 
     122           4 :         dpth->comp[0]=a;
     123           4 :         dpth->comp[1]=b;
     124           4 :         dpth->comp[2]=c;
     125           4 :         return 0;
     126             : }

Generated by: LCOV version 1.13