Line data Source code
1 : #include "burp.h" 2 : #include "alloc.h" 3 : #include "bu.h" 4 : #include "prepend.h" 5 : // balh 6 : 7 755 : struct bu *bu_alloc(void) 8 : { 9 755 : return (struct bu *)calloc_w(1, sizeof(struct bu), __func__); 10 : } 11 : 12 640 : int bu_init(struct bu *bu, char *fullpath, char *basename, 13 : char *timestampstr, uint16_t flags) 14 : { 15 640 : if(!(bu->data=prepend_s(fullpath, "data")) 16 640 : || !(bu->delta=prepend_s(fullpath, "deltas.reverse"))) 17 0 : goto error; 18 640 : bu->path=fullpath; 19 640 : bu->basename=basename; 20 640 : bu->timestamp=timestampstr; 21 640 : bu->flags=flags; 22 640 : bu->bno=strtoul(timestampstr, NULL, 10); 23 640 : return 0; 24 0 : error: 25 0 : free_w(&bu->data); 26 0 : free_w(&bu->delta); 27 0 : return -1; 28 : } 29 : 30 755 : static void bu_free_content(struct bu *bu) 31 : { 32 755 : if(!bu) return; 33 755 : free_w(&bu->path); 34 755 : free_w(&bu->basename); 35 755 : free_w(&bu->data); 36 755 : free_w(&bu->delta); 37 755 : free_w(&bu->timestamp); 38 : } 39 : 40 755 : void bu_free(struct bu **bu) 41 : { 42 755 : if(!bu || !*bu) return; 43 755 : bu_free_content(*bu); 44 755 : free_v((void **)bu); 45 : } 46 : 47 295 : void bu_list_free(struct bu **bu_list) 48 : { 49 295 : struct bu *bu; 50 295 : struct bu *next; 51 295 : struct bu *prev=NULL; 52 295 : if(*bu_list) prev=(*bu_list)->prev; 53 947 : for(bu=*bu_list; bu; bu=next) 54 : { 55 652 : next=bu->next; 56 652 : bu_free(&bu); 57 : } 58 : // Do it in both directions. 59 338 : for(bu=prev; bu; bu=prev) 60 : { 61 43 : prev=bu->prev; 62 43 : bu_free(&bu); 63 : } 64 295 : *bu_list=NULL; 65 295 : } 66 : 67 66 : static struct bu *bu_find(struct bu *bu, uint16_t flag) 68 : { 69 66 : struct bu *cbu=NULL; 70 66 : if(!bu) return NULL; 71 57 : if(bu->flags & flag) return bu; 72 : // Search in both directions. 73 21 : if(bu->next) 74 0 : for(cbu=bu; cbu; cbu=cbu->next) 75 0 : if(cbu->flags & flag) return cbu; 76 21 : if(bu->prev) 77 60 : for(cbu=bu; cbu; cbu=cbu->prev) 78 51 : if(cbu->flags & flag) return cbu; 79 : return cbu; 80 : } 81 : 82 39 : struct bu *bu_find_current(struct bu *bu) 83 : { 84 39 : return bu_find(bu, BU_CURRENT); 85 : } 86 : 87 15 : struct bu *bu_find_working_or_finishing(struct bu *bu) 88 : { 89 15 : struct bu *cbu=NULL; 90 15 : if((cbu=bu_find(bu, BU_WORKING))) return cbu; 91 12 : return bu_find(bu, BU_FINISHING); 92 : }