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 : #include "strlist.h" 8 : 9 106 : struct cstat *cstat_alloc(void) 10 : { 11 106 : return (struct cstat *)calloc_w(1, sizeof(struct cstat), __func__); 12 : } 13 : 14 105 : int cstat_init(struct cstat *cstat, 15 : const char *name, const char *clientconfdir) 16 : { 17 105 : if((clientconfdir && !(cstat->conffile=prepend_s(clientconfdir, name))) 18 105 : || !(cstat->name=strdup_w(name, __func__))) 19 0 : return -1; 20 : return 0; 21 : } 22 : 23 106 : static void cstat_free_content(struct cstat *c) 24 : { 25 106 : if(!c) return; 26 106 : bu_list_free(&c->bu); 27 106 : free_w(&c->name); 28 106 : free_w(&c->conffile); 29 106 : strlists_free(&c->labels); 30 106 : cntrs_free(&c->cntrs); 31 106 : if(c->sdirs) logp("%s() called without freeing sdirs\n", __func__); 32 106 : c->clientdir_mtime=0; 33 : } 34 : 35 1 : void cstat_add_cntr_to_list(struct cstat *c, struct cntr *cntr) 36 : { 37 1 : cntr->next=c->cntrs; 38 1 : c->cntrs=cntr; 39 1 : } 40 : 41 0 : void cstat_remove_cntr_from_list(struct cstat *c, struct cntr *cntr) 42 : { 43 0 : struct cntr *x; 44 0 : if(!c || !cntr) 45 : return; 46 0 : if(c->cntrs==cntr) 47 : { 48 0 : c->cntrs=cntr->next; 49 0 : return; 50 : } 51 0 : for(x=c->cntrs; x; x=x->next) 52 : { 53 0 : if(x->next==cntr) 54 : { 55 0 : x->next=cntr->next; 56 0 : return; 57 : } 58 : } 59 : } 60 : 61 106 : void cstat_free(struct cstat **cstat) 62 : { 63 106 : if(!cstat || !*cstat) return; 64 106 : cstat_free_content(*cstat); 65 106 : free_v((void **)cstat); 66 : } 67 : 68 99 : void cstat_add_to_list(struct cstat **clist, struct cstat *cnew) 69 : { 70 99 : struct cstat *c=NULL; 71 99 : struct cstat *clast=NULL; 72 : 73 185 : for(c=*clist; c; c=c->next) 74 : { 75 110 : if(strcmp(cnew->name, c->name)<0) 76 : { 77 24 : c->prev=cnew; 78 24 : break; 79 : } 80 86 : c->prev=clast; 81 86 : clast=c; 82 : } 83 99 : if(clast) 84 : { 85 65 : cnew->next=clast->next; 86 65 : clast->next=cnew; 87 65 : cnew->prev=clast; 88 : } 89 : else 90 : { 91 34 : *clist=cnew; 92 34 : cnew->next=c; 93 : } 94 99 : } 95 : 96 50 : void cstat_list_free(struct cstat **clist) 97 : { 98 50 : struct cstat *c; 99 50 : struct cstat *next; 100 50 : struct cstat *prev=NULL; 101 50 : if(*clist) prev=(*clist)->prev; 102 141 : for(c=*clist; c; c=next) 103 : { 104 91 : next=c->next; 105 91 : cstat_free(&c); 106 : } 107 : // Do it in both directions. 108 50 : for(c=prev; c; c=prev) 109 : { 110 0 : prev=c->prev; 111 0 : cstat_free(&c); 112 : } 113 50 : *clist=NULL; 114 50 : } 115 : 116 45 : const char *run_status_to_str(struct cstat *cstat) 117 : { 118 45 : switch(cstat->run_status) 119 : { 120 : case RUN_STATUS_IDLE: 121 : return RUN_STATUS_STR_IDLE; 122 0 : case RUN_STATUS_RUNNING: 123 0 : return RUN_STATUS_STR_RUNNING; 124 45 : default: 125 45 : return "unknown"; 126 : } 127 : } 128 : 129 80 : enum run_status run_str_to_status(const char *str) 130 : { 131 80 : if(!strcmp(str, RUN_STATUS_STR_IDLE)) 132 : return RUN_STATUS_IDLE; 133 80 : else if(!strcmp(str, RUN_STATUS_STR_RUNNING)) 134 0 : return RUN_STATUS_RUNNING; 135 : return RUN_STATUS_UNSET; 136 : } 137 : 138 81 : struct cstat *cstat_get_by_name(struct cstat *clist, const char *name) 139 : { 140 81 : struct cstat *c; 141 157 : for(c=clist; c; c=c->next) if(!strcmp(c->name, name)) return c; 142 : return NULL; 143 : } 144 : 145 4 : int cstat_count(struct cstat *clist) 146 : { 147 4 : int count=0; 148 4 : struct cstat *c; 149 4 : for(c=clist; c; c=c->next) 150 0 : count++; 151 4 : return count; 152 : }