]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tree.h
Backport from Bacula Enterprise
[bacula/bacula] / bacula / src / lib / tree.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2015 Kern Sibbald
5    Copyright (C) 2002-2014 Free Software Foundation Europe e.V.
6
7    The original author of Bacula is Kern Sibbald, with contributions
8    from many others, a complete list can be found in the file AUTHORS.
9
10    You may use this file and others of this release according to the
11    license defined in the LICENSE file, which includes the Affero General
12    Public License, v3.0 ("AGPLv3") and some additional permissions and
13    terms pursuant to its AGPLv3 Section 7.
14
15    This notice must be preserved when any source code is 
16    conveyed and/or propagated.
17
18    Bacula(R) is a registered trademark of Kern Sibbald.
19 */
20 /*
21  * Directory tree build/traverse routines
22  *
23  *    Kern Sibbald, June MMII
24  *
25 */
26
27 #include "htable.h"
28
29 struct s_mem {
30    struct s_mem *next;                /* next buffer */
31    int rem;                           /* remaining bytes */
32    char *mem;                         /* memory pointer */
33    char first[1];                     /* first byte */
34 };
35
36 #define USE_DLIST
37
38 #define foreach_child(var, list) \
39     for((var)=NULL; (*((TREE_NODE **)&(var))=(TREE_NODE*)(list->child.next(var))); )
40
41 #define tree_node_has_child(node) \
42         ((node)->child.size() > 0)
43
44 #define first_child(node) \
45         ((TREE_NODE *)(node->child.first())
46
47 struct delta_list {
48    struct delta_list *next;
49    JobId_t JobId;
50    int32_t FileIndex;
51 };
52
53 /*
54  * Keep this node as small as possible because
55  *   there is one for each file.
56  */
57 struct s_tree_node {
58    /* KEEP sibling as the first member to avoid having to
59     *  do initialization of child */
60    rblink sibling;
61    rblist child;
62    char *fname;                       /* file name */
63    int32_t FileIndex;                 /* file index */
64    uint32_t JobId;                    /* JobId */
65    int32_t delta_seq;                 /* current delta sequence */
66    uint16_t fname_len;                /* filename length */
67    int type: 8;                       /* node type */
68    unsigned int extract: 1;           /* extract item */
69    unsigned int extract_dir: 1;       /* extract dir entry only */
70    unsigned int hard_link: 1;         /* set if have hard link */
71    unsigned int soft_link: 1;         /* set if is soft link */
72    unsigned int inserted: 1;          /* set when node newly inserted */
73    unsigned int loaded: 1;            /* set when the dir is in the tree */
74    struct s_tree_node *parent;
75    struct s_tree_node *next;          /* next hash of FileIndex */
76    struct delta_list *delta_list;     /* delta parts for this node */
77 };
78 typedef struct s_tree_node TREE_NODE;
79
80 struct s_tree_root {
81    /* KEEP sibling as the first member to avoid having to
82     *  do initialization of child */
83    rblink sibling;
84    rblist child;
85    const char *fname;                 /* file name */
86    int32_t FileIndex;                 /* file index */
87    uint32_t JobId;                    /* JobId */
88    int32_t delta_seq;                 /* current delta sequence */
89    uint16_t fname_len;                /* filename length */
90    unsigned int type: 8;              /* node type */
91    unsigned int extract: 1;           /* extract item */
92    unsigned int extract_dir: 1;       /* extract dir entry only */
93    unsigned int have_link: 1;         /* set if have hard link */
94    unsigned int inserted: 1;          /* set when newly inserted */
95    unsigned int loaded: 1;            /* set when the dir is in the tree */
96    struct s_tree_node *parent;
97    struct s_tree_node *next;          /* next hash of FileIndex */
98    struct delta_list *delta_list;     /* delta parts for this node */
99
100    /* The above ^^^ must be identical to a TREE_NODE structure */
101    struct s_tree_node *first;         /* first entry in the tree */
102    struct s_tree_node *last;          /* last entry in tree */
103    struct s_mem *mem;                 /* tree memory */
104    uint32_t total_size;               /* total bytes allocated */
105    uint32_t blocks;                   /* total mallocs */
106    int cached_path_len;               /* length of cached path */
107    char *cached_path;                 /* cached current path */
108    TREE_NODE *cached_parent;          /* cached parent for above path */
109    htable hardlinks;                  /* references to first occurrence of hardlinks */
110 };
111 typedef struct s_tree_root TREE_ROOT;
112
113 /* hardlink hashtable entry */
114 struct s_hl_entry {
115    uint64_t key;
116    hlink link;
117    TREE_NODE *node;
118 };
119 typedef struct s_hl_entry HL_ENTRY;
120
121 /* type values */
122 #define TN_ROOT    1                  /* root node */
123 #define TN_NEWDIR  2                  /* created directory to fill path */
124 #define TN_DIR     3                  /* directory entry */
125 #define TN_DIR_NLS 4                  /* directory -- no leading slash -- win32 */
126 #define TN_FILE    5                  /* file entry */
127
128 /* External interface */
129 TREE_ROOT *new_tree(int count);
130 TREE_NODE *insert_tree_node(char *path, char *fname, int type,
131                             TREE_ROOT *root, TREE_NODE *parent);
132 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root);
133 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node);
134 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node);
135 void tree_add_delta_part(TREE_ROOT *root, TREE_NODE *node,
136                          JobId_t JobId, int32_t FileIndex);
137 void free_tree(TREE_ROOT *root);
138 int tree_getpath(TREE_NODE *node, char *buf, int buf_size);
139 void tree_remove_node(TREE_ROOT *root, TREE_NODE *node);
140
141 /*
142  * Use the following for traversing the whole tree. It will be
143  *   traversed in the order the entries were inserted into the
144  *   tree.
145  */
146 #define first_tree_node(r) (r)->first
147 #define next_tree_node(n)  (n)->next