LCOV - code coverage report
Current view: top level - src - cstat.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 60 62 96.8 %
Date: 2016-01-03 Functions: 10 10 100.0 %

          Line data    Source code
       1             : #include "burp.h"
       2             : #include "alloc.h"
       3             : #include "bu.h"
       4             : #include "cstat.h"
       5             : #include "log.h"
       6             : #include "prepend.h"
       7             : 
       8         105 : struct cstat *cstat_alloc(void)
       9             : {
      10         105 :         return (struct cstat *)calloc_w(1, sizeof(struct cstat), __func__);
      11             : }
      12             : 
      13         104 : int cstat_init(struct cstat *cstat,
      14             :         const char *name, const char *clientconfdir)
      15             : {
      16         176 :         if((clientconfdir && !(cstat->conffile=prepend_s(clientconfdir, name)))
      17         208 :           || !(cstat->name=strdup_w(name, __func__)))
      18             :                 return -1;
      19         104 :         return 0;
      20             : }
      21             : 
      22          85 : int cstat_init_with_cntr(struct cstat *cstat,
      23             :         const char *name, const char *clientconfdir)
      24             : {
      25         170 :         if(cstat_init(cstat, name, clientconfdir)
      26          85 :           || !(cstat->cntr=cntr_alloc())
      27         170 :           || cntr_init(cstat->cntr, name))
      28             :                 return -1;
      29          85 :         return 0;
      30             : }
      31             : 
      32         104 : static void cstat_free_content(struct cstat *c)
      33             : {
      34         208 :         if(!c) return;
      35         104 :         bu_list_free(&c->bu);
      36         104 :         free_w(&c->name);
      37         104 :         free_w(&c->conffile);
      38         104 :         cntr_free(&c->cntr);
      39         104 :         if(c->sdirs) logp("%s() called without freeing sdirs\n", __func__);
      40         104 :         c->clientdir_mtime=0;
      41         104 :         c->lockfile_mtime=0;
      42             : }
      43             : 
      44         104 : void cstat_free(struct cstat **cstat)
      45             : {
      46         208 :         if(!cstat || !*cstat) return;
      47         104 :         cstat_free_content(*cstat);
      48         104 :         free_v((void **)cstat);
      49             : }
      50             : 
      51          89 : void cstat_add_to_list(struct cstat **clist, struct cstat *cnew)
      52             : {
      53          89 :         struct cstat *c=NULL;
      54          89 :         struct cstat *clast=NULL;
      55             : 
      56         170 :         for(c=*clist; c; c=c->next)
      57             :         {
      58         102 :                 if(strcmp(cnew->name, c->name)<0)
      59             :                 {
      60          21 :                         c->prev=cnew;
      61          21 :                         break;
      62             :                 }
      63          81 :                 c->prev=clast;
      64          81 :                 clast=c;
      65             :         }
      66          89 :         if(clast)
      67             :         {
      68          52 :                 cnew->next=clast->next;
      69          52 :                 clast->next=cnew;
      70          52 :                 cnew->prev=clast;
      71             :         }
      72             :         else
      73             :         {
      74          37 :                 *clist=cnew;
      75          37 :                 cnew->next=c;
      76             :         }
      77          89 : }
      78             : 
      79          34 : void cstat_list_free(struct cstat **clist)
      80             : {
      81             :         struct cstat *c;
      82             :         struct cstat *next;
      83          34 :         struct cstat *prev=NULL;
      84          34 :         if(*clist) prev=(*clist)->prev;
      85         115 :         for(c=*clist; c; c=next)
      86             :         {
      87          81 :                 next=c->next;
      88          81 :                 cstat_free(&c);
      89             :         }
      90             :         // Do it in both directions.
      91          34 :         for(c=prev; c; c=prev)
      92             :         {
      93           0 :                 prev=c->prev;
      94           0 :                 cstat_free(&c);
      95             :         }
      96          34 :         *clist=NULL;
      97          34 : }
      98             : 
      99          16 : const char *run_status_to_str(struct cstat *cstat)
     100             : {
     101          16 :         switch(cstat->run_status)
     102             :         {
     103             :                 case RUN_STATUS_IDLE:
     104             :                         return RUN_STATUS_STR_IDLE;
     105             :                 case RUN_STATUS_CLIENT_CRASHED:
     106             :                         return RUN_STATUS_STR_CLIENT_CRASHED;
     107             :                 case RUN_STATUS_SERVER_CRASHED:
     108             :                         return RUN_STATUS_STR_SERVER_CRASHED;
     109             :                 case RUN_STATUS_RUNNING:
     110             :                         return RUN_STATUS_STR_RUNNING;
     111             :                 default: return "unknown";
     112             :         }
     113             : }
     114             : 
     115          80 : enum run_status run_str_to_status(const char *str)
     116             : {
     117          80 :         if(!strcmp(str, RUN_STATUS_STR_IDLE)) return RUN_STATUS_IDLE;
     118          80 :         else if(!strcmp(str, RUN_STATUS_STR_RUNNING)) return RUN_STATUS_RUNNING;
     119          80 :         else if(!strcmp(str, RUN_STATUS_STR_CLIENT_CRASHED))
     120             :                 return RUN_STATUS_CLIENT_CRASHED;
     121          80 :         else if(!strcmp(str, RUN_STATUS_STR_SERVER_CRASHED))
     122             :                 return RUN_STATUS_CLIENT_CRASHED;
     123          80 :         else if(!strcmp(str, RUN_STATUS_STR_RUNNING)) return RUN_STATUS_RUNNING;
     124          80 :         return RUN_STATUS_UNSET;
     125             : }
     126             : 
     127          81 : struct cstat *cstat_get_by_name(struct cstat *clist, const char *name)
     128             : {
     129             :         struct cstat *c;
     130          81 :         for(c=clist; c; c=c->next) if(!strcmp(c->name, name)) return c;
     131             :         return NULL;
     132             : }

Generated by: LCOV version 1.10