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