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