]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tree.h
Switch from GPLv2 to AGPLv3
[bacula/bacula] / bacula / src / lib / tree.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2002-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Directory tree build/traverse routines
30  *
31  *    Kern Sibbald, June MMII
32  *
33 */
34
35 struct s_mem {
36    struct s_mem *next;                /* next buffer */
37    int rem;                           /* remaining bytes */
38    char *mem;                         /* memory pointer */
39    char first[1];                     /* first byte */
40 };
41
42 #define USE_DLIST
43
44 #define foreach_child(var, list) \
45     for((var)=NULL; (*((TREE_NODE **)&(var))=(TREE_NODE*)(list->child.next(var))); )
46
47 #define tree_node_has_child(node) \
48         ((node)->child.size() > 0)
49
50 #define first_child(node) \
51         ((TREE_NODE *)(node->child.first())
52
53
54 /*
55  * Keep this node as small as possible because
56  *   there is one for each file.
57  */
58 struct s_tree_node {
59    /* KEEP sibling as the first member to avoid having to
60     *  do initialization of child */
61    rblink sibling;
62    rblist child;
63    char *fname;                       /* file name */
64    int32_t FileIndex;                 /* file index */
65    uint32_t JobId;                    /* JobId */
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 };
77 typedef struct s_tree_node TREE_NODE;
78
79 struct s_tree_root {
80    /* KEEP sibling as the first member to avoid having to
81     *  do initialization of child */
82    rblink sibling;
83    rblist child;
84    const char *fname;                 /* file name */
85    int32_t FileIndex;                 /* file index */
86    uint32_t JobId;                    /* JobId */
87    uint16_t fname_len;                /* filename length */
88    unsigned int type: 8;              /* node type */
89    unsigned int extract: 1;           /* extract item */
90    unsigned int extract_dir: 1;       /* extract dir entry only */
91    unsigned int have_link: 1;         /* set if have hard link */
92    unsigned int inserted: 1;          /* set when newly inserted */
93    unsigned int loaded: 1;            /* set when the dir is in the tree */
94    struct s_tree_node *parent;
95    struct s_tree_node *next;          /* next hash of FileIndex */
96
97    /* The above ^^^ must be identical to a TREE_NODE structure */
98    struct s_tree_node *first;         /* first entry in the tree */
99    struct s_tree_node *last;          /* last entry in tree */
100    struct s_mem *mem;                 /* tree memory */
101    uint32_t total_size;               /* total bytes allocated */
102    uint32_t blocks;                   /* total mallocs */
103    int cached_path_len;               /* length of cached path */
104    char *cached_path;                 /* cached current path */
105    TREE_NODE *cached_parent;          /* cached parent for above path */
106 };
107 typedef struct s_tree_root TREE_ROOT;
108
109
110 /* type values */
111 #define TN_ROOT    1                  /* root node */
112 #define TN_NEWDIR  2                  /* created directory to fill path */
113 #define TN_DIR     3                  /* directory entry */
114 #define TN_DIR_NLS 4                  /* directory -- no leading slash -- win32 */
115 #define TN_FILE    5                  /* file entry */
116
117 /* External interface */
118 TREE_ROOT *new_tree(int count);
119 TREE_NODE *insert_tree_node(char *path, char *fname, int type,
120                             TREE_ROOT *root, TREE_NODE *parent);
121 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root);
122 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node);
123 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node);
124 void free_tree(TREE_ROOT *root);
125 int tree_getpath(TREE_NODE *node, char *buf, int buf_size);
126
127 /*
128  * Use the following for traversing the whole tree. It will be
129  *   traversed in the order the entries were inserted into the
130  *   tree.
131  */
132 #define first_tree_node(r) (r)->first
133 #define next_tree_node(n)  (n)->next