2 * Directory tree build/traverse routines
4 * Kern Sibbald, June MMII
8 Copyright (C) 2002-2003 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 #define MAXPATHLEN 1000
41 #define Dmsg2(n,f,a1,a2)
42 #define Dmsg3(n,f,a1,a2,a3)
45 * This subroutine gets a big buffer.
47 static void malloc_buf(TREE_ROOT *root, int size)
51 mem = (struct s_mem *)malloc(size);
52 root->total_size += size;
54 mem->next = root->mem;
56 mem->mem = mem->first;
57 mem->rem = (char *)mem + size - mem->mem;
58 Dmsg2(200, "malloc buf size=%d rem=%d\n", size, mem->rem);
63 * Note, we allocate a big buffer in the tree root
64 * from which we allocate nodes. This runs more
65 * than 100 times as fast as directly using malloc()
66 * for each of the nodes.
68 TREE_ROOT *new_tree(int count)
73 if (count < 1000) { /* minimum tree size */
76 root = (TREE_ROOT *)malloc(sizeof(TREE_ROOT));
77 memset(root, 0, sizeof(TREE_ROOT));
79 /* Assume filename + node = 40 characters average length */
80 size = count * (BALIGN(sizeof(TREE_NODE)) + 40);
81 if (count > 1000000 || size > 10000000) {
84 Dmsg2(400, "count=%d size=%d\n", count, size);
85 malloc_buf(root, size);
86 root->cached_path = get_pool_memory(PM_FNAME);
91 * Create a new tree node. Size depends on type.
93 TREE_NODE *new_tree_node(TREE_ROOT *root, int type)
96 int asize = BALIGN(sizeof(TREE_NODE));
98 if (root->mem->rem < asize) {
100 if (root->total_size >= 1000000) {
105 malloc_buf(root, mb_size);
107 root->mem->rem -= asize;
108 node = (TREE_NODE *)root->mem->mem;
109 root->mem->mem += asize;
110 memset(node, 0, sizeof(TREE_NODE));
117 * Allocate bytes for filename in tree structure.
118 * Keep the pointers properly aligned by allocating
119 * sizes that are aligned.
121 static char *tree_alloc(TREE_ROOT *root, int size)
124 int asize = BALIGN(size);
126 if (root->mem->rem < asize) {
128 if (root->total_size >= 1000000) {
133 malloc_buf(root, mb_size);
135 root->mem->rem -= asize;
136 buf = root->mem->mem;
137 root->mem->mem += asize;
142 /* This routine frees the whole tree */
143 void free_tree(TREE_ROOT *root)
145 struct s_mem *mem, *rel;
147 for (mem=root->mem; mem; ) {
152 if (root->cached_path) {
153 free_pool_memory(root->cached_path);
155 Dmsg2(400, "Total size=%u blocks=%d\n", root->total_size, root->blocks);
163 * Insert a node in the tree. This is the main subroutine
164 * called when building a tree.
167 TREE_NODE *insert_tree_node(char *path, TREE_NODE *node,
168 TREE_ROOT *root, TREE_NODE *parent)
172 int path_len = strlen(path);
174 Dmsg1(100, "insert_tree_node: %s\n", path);
176 * If trailing slash, strip it
179 q = path + path_len - 1;
181 *q = 0; /* strip trailing slash */
183 q = NULL; /* no trailing slash */
186 q = NULL; /* no trailing slash */
188 p = strrchr(path, '/'); /* separate path and filename */
191 if (!parent) { /* if no parent, we need to make one */
192 *p = 0; /* terminate path */
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);
205 *p = '/'; /* restore full path */
210 parent = (TREE_NODE *)root;
211 node->type = TN_DIR_NLS;
213 Dmsg1(100, "No / found: %s\n", path);
216 uint16_t fname_len = strlen(fname);
217 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
218 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
219 if (sibling->fname_len == fname_len &&
220 strcmp(sibling->fname, fname) == 0) {
221 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
222 if (q) { /* if trailing slash on entry */
223 *q = '/'; /* restore it */
229 append_tree_node(fname, node, root, parent);
230 Dmsg1(100, "insert_tree_node: parent=%s\n", parent->fname);
231 if (q) { /* if trailing slash on entry */
232 *q = '/'; /* restore it */
238 * Ensure that all appropriate nodes for a full path exist in
241 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
243 TREE_NODE *parent, *sibling, *node;
245 int type = TN_NEWDIR;
247 Dmsg1(100, "make_tree_path: %s\n", path);
249 Dmsg0(100, "make_tree_path: parent=*root*\n");
250 return (TREE_NODE *)root;
252 p = strrchr(path, '/'); /* get last dir component of path */
255 *p = 0; /* terminate path */
256 parent = make_tree_path(path, root);
257 *p = '/'; /* restore full name */
260 parent = (TREE_NODE *)root;
263 /* Is it already a sibling? */
264 uint16_t fname_len = strlen(fname);
265 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
266 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
267 if (sibling->fname_len == fname_len &&
268 strcmp(sibling->fname, fname) == 0) {
269 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
274 node = new_tree_node(root, type);
275 append_tree_node(fname, node, root, parent);
276 Dmsg1(100, "make_tree_path: add parent=%s\n", node->fname);
281 * Append sibling to parent's child chain
283 void append_tree_node(char *fname, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
287 Dmsg1(100, "append_tree_node: %s\n", fname);
288 node->fname_len = strlen(fname);
289 node->fname = tree_alloc(root, node->fname_len + 1);
290 strcpy(node->fname, fname);
291 node->parent = parent;
292 if (!parent->child) {
293 parent->child = node;
296 /* Append to end of sibling chain */
297 for (child=parent->child; child->sibling; child=child->sibling)
299 child->sibling = node;
301 /* Maintain a linear chain of nodes */
307 root->last->next = node;
314 /* Moved to tree.h to eliminate subroutine call */
315 TREE_NODE *first_tree_node(TREE_ROOT *root)
320 TREE_NODE *next_tree_node(TREE_NODE *node)
327 void print_tree(char *path, TREE_NODE *tree)
329 char buf[MAXPATHLEN];
335 switch (tree->type) {
347 Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
348 switch (tree->type) {
354 bsnprintf(buf, sizeof(buf), "%s/%s", path, tree->fname);
355 print_tree(buf, tree->child);
358 print_tree(path, tree->child);
361 Pmsg1(000, "Unknown node type %d\n", tree->type);
363 print_tree(path, tree->sibling);
367 int tree_getpath(TREE_NODE *node, char *buf, int buf_size)
373 tree_getpath(node->parent, buf, buf_size);
375 * Fixup for Win32. If we have a Win32 directory and
376 * there is only a / in the buffer, remove it since
377 * win32 names don't generally start with /
379 if (node->type == TN_DIR_NLS && buf[0] == '/' && buf[1] == 0) {
382 bstrncat(buf, node->fname, buf_size);
383 if (node->type != TN_FILE) {
384 bstrncat(buf, "/", buf_size);
390 * Change to specified directory
392 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
394 if (strcmp(path, ".") == 0) {
397 if (strcmp(path, "..") == 0) {
404 if (path[0] == '/') {
405 Dmsg0(100, "Doing absolute lookup.\n");
406 return tree_relcwd(path+1, root, (TREE_NODE *)root);
408 Dmsg0(100, "Doing relative lookup.\n");
409 return tree_relcwd(path, root, node);
414 * Do a relative cwd -- i.e. relative to current node rather than root node
416 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
425 /* Check the current segment only */
426 p = strchr(path, '/');
432 Dmsg2(100, "tree_relcwd: len=%d path=%s\n", len, path);
433 for (cd=node->child; cd; cd=cd->sibling) {
434 Dmsg1(100, "tree_relcwd: test cd=%s\n", cd->fname);
435 if (cd->fname[0] == path[0] && len == (int)strlen(cd->fname)
436 && strncmp(cd->fname, path, len) == 0) {
440 if (!cd || cd->type == TN_FILE) {
444 Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
447 Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
448 /* Check the next segment if any */
449 return tree_relcwd(p+1, root, cd);
454 #ifdef BUILD_TEST_PROGRAM
456 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
458 static uint32_t FileIndex = 0;
460 * Simple test program for tree routines
462 int main(int argc, char *argv[])
466 char buf[MAXPATHLEN];
469 root->fname = tree_alloc(root, 1);
473 FillDirectoryTree("/home/kern/bacula/k", root, NULL);
475 for (node = first_tree_node(root); node; node=next_tree_node(node)) {
476 tree_getpath(node, buf, sizeof(buf));
477 Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
480 node = (TREE_NODE *)root;
481 Pmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
482 node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
484 tree_getpath(node, buf, sizeof(buf));
485 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
488 Pmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
489 node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
491 tree_getpath(node, buf, sizeof(buf));
492 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
494 Dmsg0(100, "testprogs not found.\n");
497 free_tree((TREE_NODE *)root);
502 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
504 TREE_NODE *newparent = NULL;
509 char pathbuf[MAXPATHLEN];
510 char file[MAXPATHLEN];
514 Dmsg1(100, "FillDirectoryTree: %s\n", path);
519 while ((dir = readdir(dp))) {
520 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
523 bstrncpy(file, dir->d_name, sizeof(file));
524 snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
525 if (lstat(pathbuf, &statbuf) < 0) {
526 printf("lstat() failed. ERR=%s\n", strerror(errno));
529 // printf("got file=%s, pathbuf=%s\n", file, pathbuf);
531 if (S_ISLNK(statbuf.st_mode))
532 type = TN_FILE; /* link */
533 else if (S_ISREG(statbuf.st_mode))
535 else if (S_ISDIR(statbuf.st_mode)) {
537 } else if (S_ISCHR(statbuf.st_mode))
538 type = TN_FILE; /* char dev */
539 else if (S_ISBLK(statbuf.st_mode))
540 type = TN_FILE; /* block dev */
541 else if (S_ISFIFO(statbuf.st_mode))
542 type = TN_FILE; /* fifo */
543 else if (S_ISSOCK(statbuf.st_mode))
544 type = TN_FILE; /* sock */
547 printf("Unknown file type: 0x%x\n", statbuf.st_mode);
550 Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
551 node = new_tree_node(root, type);
552 node->FileIndex = ++FileIndex;
553 parent = insert_tree_node(pathbuf, node, root, parent);
554 if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
555 Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
556 FillDirectoryTree(pathbuf, root, node);