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 : fzp_close(&(*dpth)->cfile_fzp);
19 84 : free_w(&((*dpth)->base_path));
20 84 : free_v((void **)dpth);
21 : }
22 :
23 10 : int dpth_release_and_move_to_next_in_list(struct dpth *dpth)
24 : {
25 10 : int ret=0;
26 10 : 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 10 : if(fzp_close(&dpth->fzp)) ret=-1;
31 10 : if(lock_release(dpth->head->lock)) ret=-1;
32 10 : lock_free(&dpth->head->lock);
33 :
34 10 : next=dpth->head->next;
35 10 : if(dpth->head==dpth->tail) dpth->tail=next;
36 10 : free_v((void **)&dpth->head);
37 10 : dpth->head=next;
38 10 : return ret;
39 : }
40 :
41 95 : int dpth_release_all(struct dpth *dpth)
42 : {
43 95 : int ret=0;
44 95 : if(!dpth) return 0;
45 95 : if(dpth->fzp && fzp_close(&dpth->fzp)) ret=-1;
46 103 : while(dpth->head)
47 8 : 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 76 : 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 44 : int dpth_incr(struct dpth *dpth)
65 : {
66 88 : if(incr(&dpth->comp[2], MAX_FILES_PER_DIR)
67 40 : || incr(&dpth->comp[1], dpth->max_storage_subdirs)
68 68 : || 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 : }
|