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 39 : struct dpth *dpth_alloc(void)
10 : {
11 39 : return (struct dpth *)calloc_w(1, sizeof(struct dpth), __func__);
12 : }
13 :
14 44 : void dpth_free(struct dpth **dpth)
15 : {
16 44 : if(!dpth || !*dpth) return;
17 39 : dpth_release_all(*dpth);
18 39 : fzp_close(&(*dpth)->cfile_fzp);
19 39 : free_w(&((*dpth)->base_path));
20 39 : free_v((void **)dpth);
21 : }
22 :
23 0 : int dpth_release_and_move_to_next_in_list(struct dpth *dpth)
24 : {
25 0 : int ret=0;
26 0 : 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 0 : if(fzp_close(&dpth->fzp)) ret=-1;
31 0 : if(lock_release(dpth->head->lock)) ret=-1;
32 0 : lock_free(&dpth->head->lock);
33 :
34 0 : next=dpth->head->next;
35 0 : if(dpth->head==dpth->tail) dpth->tail=next;
36 0 : free_v((void **)&dpth->head);
37 0 : dpth->head=next;
38 0 : return ret;
39 : }
40 :
41 39 : int dpth_release_all(struct dpth *dpth)
42 : {
43 39 : int ret=0;
44 39 : if(!dpth) return 0;
45 39 : if(dpth->fzp && fzp_close(&dpth->fzp)) ret=-1;
46 39 : while(dpth->head)
47 0 : 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 42 : if((*component)++<max) return 1;
56 18 : *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 26 : int dpth_incr(struct dpth *dpth)
65 : {
66 52 : if(incr(&dpth->comp[2], MAX_FILES_PER_DIR)
67 20 : || incr(&dpth->comp[1], dpth->max_storage_subdirs)
68 12 : || incr(&dpth->comp[0], dpth->max_storage_subdirs))
69 : return 0;
70 2 : logp("No free data file entries out of the %d*%d*%d available!\n",
71 : MAX_FILES_PER_DIR,
72 : dpth->max_storage_subdirs, dpth->max_storage_subdirs);
73 2 : logp("Maybe move the storage directory aside and start again.\n");
74 2 : return -1;
75 : }
|