]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tree.h
- Correct typo in Copyright
[bacula/bacula] / bacula / src / lib / tree.h
1 /*
2  * Directory tree build/traverse routines
3  *
4  *    Kern Sibbald, June MMII
5  *
6 */
7 /*
8    Copyright (C) 2002-2005 Kern Sibbald
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    version 2 as amended with additional clauses defined in the
13    file LICENSE in the main source directory.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
18    the file LICENSE for additional details.
19
20  */
21
22 struct s_mem {
23    struct s_mem *next;                /* next buffer */
24    int rem;                           /* remaining bytes */
25    char *mem;                         /* memory pointer */
26    char first[1];                     /* first byte */
27 };
28
29 #define USE_DLIST
30
31 #define foreach_child(var, list) \
32     for((var)=NULL; (*((TREE_NODE **)&(var))=(TREE_NODE*)(list->child.next(var))); )
33
34 #define tree_node_has_child(node) \
35         ((node)->child.size() > 0)
36
37 #define first_child(node) \
38         ((TREE_NODE *)(node->child.first())
39
40
41 /*
42  * Keep this node as small as possible because
43  *   there is one for each file.
44  */
45 struct s_tree_node {
46    /* KEEP sibling as the first member to avoid having to
47     *  do initialization of child */
48    dlink sibling;
49    dlist child;
50    char *fname;                       /* file name */
51    int32_t FileIndex;                 /* file index */
52    uint32_t JobId;                    /* JobId */
53    uint16_t fname_len;                /* filename length */
54    int type: 8;                       /* node type */
55    unsigned int extract: 1;           /* extract item */
56    unsigned int extract_dir: 1;       /* extract dir entry only */
57    unsigned int hard_link: 1;         /* set if have hard link */
58    unsigned int soft_link: 1;         /* set if is soft link */
59    unsigned int inserted: 1;          /* set when newly inserted */
60    struct s_tree_node *parent;
61    struct s_tree_node *next;          /* next hash of FileIndex */
62 };
63 typedef struct s_tree_node TREE_NODE;
64
65 struct s_tree_root {
66    /* KEEP sibling as the first member to avoid having to
67     *  do initialization of child */
68    dlink sibling;
69    dlist child;
70    const char *fname;                 /* file name */
71    int32_t FileIndex;                 /* file index */
72    uint32_t JobId;                    /* JobId */
73    uint16_t fname_len;                /* filename length */
74    unsigned int type: 8;              /* node type */
75    unsigned int extract: 1;           /* extract item */
76    unsigned int extract_dir: 1;       /* extract dir entry only */
77    unsigned int have_link: 1;         /* set if have hard link */
78    unsigned int inserted: 1;          /* set when newly inserted */
79    struct s_tree_node *parent;
80    struct s_tree_node *next;          /* next hash of FileIndex */
81
82    /* The above ^^^ must be identical to a TREE_NODE structure */
83    struct s_tree_node *first;         /* first entry in the tree */
84    struct s_tree_node *last;          /* last entry in tree */
85    struct s_mem *mem;                 /* tree memory */
86    uint32_t total_size;               /* total bytes allocated */
87    uint32_t blocks;                   /* total mallocs */
88    int cached_path_len;               /* length of cached path */
89    char *cached_path;                 /* cached current path */
90    TREE_NODE *cached_parent;          /* cached parent for above path */
91 };
92 typedef struct s_tree_root TREE_ROOT;
93
94
95 /* type values */
96 #define TN_ROOT    1                  /* root node */
97 #define TN_NEWDIR  2                  /* created directory to fill path */
98 #define TN_DIR     3                  /* directory entry */
99 #define TN_DIR_NLS 4                  /* directory -- no leading slash -- win32 */
100 #define TN_FILE    5                  /* file entry */
101
102 /* External interface */
103 TREE_ROOT *new_tree(int count);
104 TREE_NODE *insert_tree_node(char *path, char *fname, int type,
105                             TREE_ROOT *root, TREE_NODE *parent);
106 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root);
107 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node);
108 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node);
109 void free_tree(TREE_ROOT *root);
110 int tree_getpath(TREE_NODE *node, char *buf, int buf_size);
111
112 /*
113  * Use the following for traversing the whole tree. It will be
114  *   traversed in the order the entries were inserted into the
115  *   tree.
116  */
117 #define first_tree_node(r) (r)->first
118 #define next_tree_node(n)  (n)->next