Line data Source code
1 : #include "../burp.h"
2 : #include "../alloc.h"
3 : #include "../fsops.h"
4 : #include "../fzp.h"
5 : #include "../lock.h"
6 : #include "../log.h"
7 : #include "dpth.h"
8 :
9 85 : struct dpth *dpth_alloc(void)
10 : {
11 85 : return (struct dpth *)calloc_w(1, sizeof(struct dpth), __func__);
12 : }
13 :
14 96 : void dpth_free(struct dpth **dpth)
15 : {
16 192 : if(!dpth || !*dpth) return;
17 85 : dpth_release_all(*dpth);
18 85 : fzp_close(&(*dpth)->cfile_fzp);
19 85 : free_w(&((*dpth)->base_path));
20 85 : free_v((void **)dpth);
21 : }
22 :
23 11 : int dpth_release_and_move_to_next_in_list(struct dpth *dpth)
24 : {
25 11 : int ret=0;
26 11 : struct dpth_lock *next=NULL;
27 :
28 : // Try to release (and unlink) the lock even if fzp_close failed, just
29 : // to be tidy.
30 11 : if(fzp_close(&dpth->fzp)) ret=-1;
31 11 : if(lock_release(dpth->head->lock)) ret=-1;
32 11 : lock_free(&dpth->head->lock);
33 :
34 11 : next=dpth->head->next;
35 11 : if(dpth->head==dpth->tail) dpth->tail=next;
36 11 : free_v((void **)&dpth->head);
37 11 : dpth->head=next;
38 11 : return ret;
39 : }
40 :
41 96 : int dpth_release_all(struct dpth *dpth)
42 : {
43 96 : int ret=0;
44 96 : if(!dpth) return 0;
45 96 : if(dpth->fzp && fzp_close(&dpth->fzp)) ret=-1;
46 106 : while(dpth->head)
47 10 : if(dpth_release_and_move_to_next_in_list(dpth)) ret=-1;
48 : return ret;
49 : }
50 :
51 : #define MAX_FILES_PER_DIR 0xFFFF
52 :
53 : static int incr(uint16_t *component, uint16_t max)
54 : {
55 79 : if((*component)++<max) return 1;
56 36 : *component=0;
57 : return 0;
58 : }
59 :
60 : // Three levels with 65535 entries each gives
61 : // 65535^3 = 281,462,092,005,375 data entries
62 : // recommend a filesystem with lots of inodes?
63 : // Hmm, but ext3 only allows 32000 subdirs, although that many files are OK.
64 47 : int dpth_incr(struct dpth *dpth)
65 : {
66 94 : if(incr(&dpth->comp[2], MAX_FILES_PER_DIR)
67 40 : || incr(&dpth->comp[1], dpth->max_storage_subdirs)
68 71 : || incr(&dpth->comp[0], dpth->max_storage_subdirs))
69 : return 0;
70 : logp("No free data file entries out of the %d*%d*%d available!\n",
71 : MAX_FILES_PER_DIR,
72 4 : dpth->max_storage_subdirs, dpth->max_storage_subdirs);
73 4 : logp("Maybe move the storage directory aside and start again.\n");
74 4 : return -1;
75 : }
|