2 * Directory tree build/traverse routines
4 * Kern Sibbald, June MMII
8 Copyright (C) 2002-2004 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
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 GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29 #include "findlib/find.h"
32 /* Forward referenced subroutines */
33 static TREE_NODE *search_and_insert_tree_node(char *fname, int type,
34 TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent);
35 static char *tree_alloc(TREE_ROOT *root, int size);
38 * NOTE !!!!! we turn off Debug messages for performance reasons.
46 #define Dmsg2(n,f,a1,a2)
47 #define Dmsg3(n,f,a1,a2,a3)
50 * This subroutine gets a big buffer.
52 static void malloc_buf(TREE_ROOT *root, int size)
56 mem = (struct s_mem *)malloc(size);
57 root->total_size += size;
59 mem->next = root->mem;
61 mem->mem = mem->first;
62 mem->rem = (char *)mem + size - mem->mem;
63 Dmsg2(200, "malloc buf size=%d rem=%d\n", size, mem->rem);
68 * Note, we allocate a big buffer in the tree root
69 * from which we allocate nodes. This runs more
70 * than 100 times as fast as directly using malloc()
71 * for each of the nodes.
73 TREE_ROOT *new_tree(int count)
78 if (count < 1000) { /* minimum tree size */
81 root = (TREE_ROOT *)malloc(sizeof(TREE_ROOT));
82 memset(root, 0, sizeof(TREE_ROOT));
83 /* Assume filename + node = 40 characters average length */
84 size = count * (BALIGN(sizeof(TREE_NODE)) + 40);
85 if (count > 1000000 || size > 10000000) {
88 Dmsg2(400, "count=%d size=%d\n", count, size);
89 malloc_buf(root, size);
90 root->cached_path = get_pool_memory(PM_FNAME);
97 * Create a new tree node.
99 static TREE_NODE *new_tree_node(TREE_ROOT *root, int type)
102 int size = sizeof(TREE_NODE);
103 node = (TREE_NODE *)tree_alloc(root, size);
104 memset(node, 0, size);
111 * Allocate bytes for filename in tree structure.
112 * Keep the pointers properly aligned by allocating
113 * sizes that are aligned.
115 static char *tree_alloc(TREE_ROOT *root, int size)
118 int asize = BALIGN(size);
120 if (root->mem->rem < asize) {
122 if (root->total_size >= 1000000) {
127 malloc_buf(root, mb_size);
129 root->mem->rem -= asize;
130 buf = root->mem->mem;
131 root->mem->mem += asize;
136 /* This routine frees the whole tree */
137 void free_tree(TREE_ROOT *root)
139 struct s_mem *mem, *rel;
141 for (mem=root->mem; mem; ) {
146 if (root->cached_path) {
147 free_pool_memory(root->cached_path);
148 root->cached_path = NULL;
150 Dmsg2(400, "Total size=%u blocks=%d\n", root->total_size, root->blocks);
157 * Insert a node in the tree. This is the main subroutine
158 * called when building a tree.
161 TREE_NODE *insert_tree_node(char *path, char *fname, TREE_NODE *node,
162 TREE_ROOT *root, TREE_NODE *parent)
165 int path_len = strlen(path);
167 Dmsg1(100, "insert_tree_node: %s\n", path);
169 * If trailing slash on path, strip it
172 q = path + path_len - 1;
174 *q = 0; /* strip trailing slash */
176 q = NULL; /* no trailing slash */
179 q = NULL; /* no trailing slash */
181 /* If no filename, strip last component of path as "filename" */
183 p = strrchr(path, '/'); /* separate path and filename */
185 fname = p + 1; /* set new filename */
186 *p = 0; /* terminate new path */
192 if (!parent) { /* if no parent, we need to make one */
193 Dmsg1(100, "make_tree_path for %s\n", path);
194 path_len = strlen(path); /* get new length */
195 if (path_len == root->cached_path_len &&
196 strcmp(path, root->cached_path) == 0) {
197 parent = root->cached_parent;
199 root->cached_path_len = path_len;
200 pm_strcpy(&root->cached_path, path);
201 parent = make_tree_path(path, root);
202 root->cached_parent = parent;
204 Dmsg1(100, "parent=%s\n", parent->fname);
209 parent = (TREE_NODE *)root;
210 node->type = TN_DIR_NLS;
212 Dmsg1(100, "No / found: %s\n", path);
215 node = search_and_insert_tree_node(fname, 0, node, root, parent);
216 if (q) { /* if trailing slash on entry */
217 *q = '/'; /* restore it */
219 if (p) { /* if slash in path trashed */
220 *p = '/'; /* restore full path */
226 * Ensure that all appropriate nodes for a full path exist in
229 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
231 TREE_NODE *parent, *node;
233 int type = TN_NEWDIR;
235 Dmsg1(100, "make_tree_path: %s\n", path);
237 Dmsg0(100, "make_tree_path: parent=*root*\n");
238 return (TREE_NODE *)root;
240 p = strrchr(path, '/'); /* get last dir component of path */
243 *p = 0; /* terminate path */
244 parent = make_tree_path(path, root);
245 *p = '/'; /* restore full name */
248 parent = (TREE_NODE *)root;
251 node = search_and_insert_tree_node(fname, type, NULL, root, parent);
256 * See if the fname already exists. If not insert a new node for it.
258 static TREE_NODE *search_and_insert_tree_node(char *fname, int type,
259 TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
261 TREE_NODE *sibling, *last_sibling = NULL;
262 uint16_t fname_len = strlen(fname);
265 /* Is it already a sibling? */
266 foreach_child(sibling, parent) {
267 Dmsg2(000, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
268 if (fname[0] > sibling->fname[0] || (cmp=strcmp(fname, sibling->fname)) > 0) {
269 last_sibling = sibling;
273 /* Insert before current sibling */
275 node = new_tree_node(root, type);
277 node->sibling_ = sibling;
278 if (sibling == first_child(parent)) { /* if sibling was at head of list */
279 parent->child_ = NULL; /* force parent to be updated below */
281 Dmsg2(000, "insert before sibling->fname=%s fname=%s\n", sibling->fname, fname);
285 sibling->inserted = false; /* already in tree */
291 * At this point, the fname is not found. We must add it
294 node = new_tree_node(root, type);
296 Dmsg1(000, "append_tree_node: %s\n", fname);
297 node->fname_len = fname_len;
298 node->fname = tree_alloc(root, node->fname_len + 1);
299 strcpy(node->fname, fname);
300 node->parent = parent;
301 if (!tree_node_has_child(parent)) {
302 parent->child_ = node;
304 last_sibling->sibling_ = node;
307 /* Maintain a linear chain of nodes */
312 root->last->next = node;
315 node->inserted = true; /* inserted into tree */
320 /* Moved to tree.h to eliminate subroutine call */
321 TREE_NODE *first_tree_node(TREE_ROOT *root)
326 TREE_NODE *next_tree_node(TREE_NODE *node)
334 int tree_getpath(TREE_NODE *node, char *buf, int buf_size)
340 tree_getpath(node->parent, buf, buf_size);
342 * Fixup for Win32. If we have a Win32 directory and
343 * there is only a / in the buffer, remove it since
344 * win32 names don't generally start with /
346 if (node->type == TN_DIR_NLS && buf[0] == '/' && buf[1] == 0) {
349 bstrncat(buf, node->fname, buf_size);
350 /* Add a slash for all directories unless we are at the root,
351 * also add a slash to a soft linked file if it has children
352 * i.e. it is linked to a directory.
354 if ((node->type != TN_FILE && !(buf[0] == '/' && buf[1] == 0)) ||
355 (node->soft_link && tree_node_has_child(node))) {
356 bstrncat(buf, "/", buf_size);
362 * Change to specified directory
364 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
366 if (strcmp(path, ".") == 0) {
369 if (strcmp(path, "..") == 0) {
376 if (path[0] == '/') {
377 Dmsg0(100, "Doing absolute lookup.\n");
378 return tree_relcwd(path+1, root, (TREE_NODE *)root);
380 Dmsg0(100, "Doing relative lookup.\n");
381 return tree_relcwd(path, root, node);
386 * Do a relative cwd -- i.e. relative to current node rather than root node
388 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
397 /* Check the current segment only */
398 p = strchr(path, '/');
404 Dmsg2(100, "tree_relcwd: len=%d path=%s\n", len, path);
405 foreach_child(cd, node) {
406 Dmsg1(100, "tree_relcwd: test cd=%s\n", cd->fname);
407 if (cd->fname[0] == path[0] && len == (int)strlen(cd->fname)
408 && strncmp(cd->fname, path, len) == 0) {
412 if (!cd || (cd->type == TN_FILE && !tree_node_has_child(cd))) {
416 Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
419 Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
420 /* Check the next segment if any */
421 return tree_relcwd(p+1, root, cd);
426 #ifdef BUILD_TEST_PROGRAM
428 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
430 static uint32_t FileIndex = 0;
432 * Simple test program for tree routines
434 int main(int argc, char *argv[])
438 char buf[MAXPATHLEN];
441 root->fname = tree_alloc(root, 1);
445 FillDirectoryTree("/home/kern/bacula/k", root, NULL);
447 for (node = first_tree_node(root); node; node=next_tree_node(node)) {
448 tree_getpath(node, buf, sizeof(buf));
449 Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
452 node = (TREE_NODE *)root;
453 Pmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
454 node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
456 tree_getpath(node, buf, sizeof(buf));
457 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
460 Pmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
461 node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
463 tree_getpath(node, buf, sizeof(buf));
464 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
466 Dmsg0(100, "testprogs not found.\n");
469 free_tree((TREE_NODE *)root);
474 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
476 TREE_NODE *newparent = NULL;
481 char pathbuf[MAXPATHLEN];
482 char file[MAXPATHLEN];
486 Dmsg1(100, "FillDirectoryTree: %s\n", path);
491 while ((dir = readdir(dp))) {
492 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
495 bstrncpy(file, dir->d_name, sizeof(file));
496 snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
497 if (lstat(pathbuf, &statbuf) < 0) {
498 printf("lstat() failed. ERR=%s\n", strerror(errno));
501 // printf("got file=%s, pathbuf=%s\n", file, pathbuf);
503 if (S_ISLNK(statbuf.st_mode))
504 type = TN_FILE; /* link */
505 else if (S_ISREG(statbuf.st_mode))
507 else if (S_ISDIR(statbuf.st_mode)) {
509 } else if (S_ISCHR(statbuf.st_mode))
510 type = TN_FILE; /* char dev */
511 else if (S_ISBLK(statbuf.st_mode))
512 type = TN_FILE; /* block dev */
513 else if (S_ISFIFO(statbuf.st_mode))
514 type = TN_FILE; /* fifo */
515 else if (S_ISSOCK(statbuf.st_mode))
516 type = TN_FILE; /* sock */
519 printf("Unknown file type: 0x%x\n", statbuf.st_mode);
522 Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
523 node = new_tree_node(root, type);
524 node->FileIndex = ++FileIndex;
525 parent = insert_tree_node(pathbuf, node, root, parent);
526 if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
527 Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
528 FillDirectoryTree(pathbuf, root, node);
535 #define MAXPATHLEN 2000
538 void print_tree(char *path, TREE_NODE *tree)
540 char buf[MAXPATHLEN];
546 switch (tree->type) {
558 Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
559 switch (tree->type) {
564 bsnprintf(buf, sizeof(buf), "%s/%s", path, tree->fname);
565 print_tree(buf, first_child(tree));
568 print_tree(path, first_child(tree));
571 Pmsg1(000, "Unknown node type %d\n", tree->type);
573 print_tree(path, tree->sibling_);