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
36 * NOTE !!!!! we turn off Debug messages for performance reasons.
44 #define Dmsg2(n,f,a1,a2)
45 #define Dmsg3(n,f,a1,a2,a3)
48 * This subroutine gets a big buffer.
50 static void malloc_buf(TREE_ROOT *root, int size)
54 mem = (struct s_mem *)malloc(size);
55 root->total_size += size;
57 mem->next = root->mem;
59 mem->mem = mem->first;
60 mem->rem = (char *)mem + size - mem->mem;
61 Dmsg2(200, "malloc buf size=%d rem=%d\n", size, mem->rem);
66 * Note, we allocate a big buffer in the tree root
67 * from which we allocate nodes. This runs more
68 * than 100 times as fast as directly using malloc()
69 * for each of the nodes.
71 TREE_ROOT *new_tree(int count)
76 if (count < 1000) { /* minimum tree size */
79 root = (TREE_ROOT *)malloc(sizeof(TREE_ROOT));
80 memset(root, 0, sizeof(TREE_ROOT));
82 /* Assume filename + node = 40 characters average length */
83 size = count * (BALIGN(sizeof(TREE_NODE)) + 40);
84 if (count > 1000000 || size > 10000000) {
87 Dmsg2(400, "count=%d size=%d\n", count, size);
88 malloc_buf(root, size);
89 root->cached_path = get_pool_memory(PM_FNAME);
94 * Create a new tree node. Size depends on type.
96 TREE_NODE *new_tree_node(TREE_ROOT *root, int type)
99 int asize = BALIGN(sizeof(TREE_NODE));
101 if (root->mem->rem < asize) {
103 if (root->total_size >= 1000000) {
108 malloc_buf(root, mb_size);
110 root->mem->rem -= asize;
111 node = (TREE_NODE *)root->mem->mem;
112 root->mem->mem += asize;
113 memset(node, 0, sizeof(TREE_NODE));
120 * Allocate bytes for filename in tree structure.
121 * Keep the pointers properly aligned by allocating
122 * sizes that are aligned.
124 static char *tree_alloc(TREE_ROOT *root, int size)
127 int asize = BALIGN(size);
129 if (root->mem->rem < asize) {
131 if (root->total_size >= 1000000) {
136 malloc_buf(root, mb_size);
138 root->mem->rem -= asize;
139 buf = root->mem->mem;
140 root->mem->mem += asize;
145 /* This routine frees the whole tree */
146 void free_tree(TREE_ROOT *root)
148 struct s_mem *mem, *rel;
150 for (mem=root->mem; mem; ) {
155 if (root->cached_path) {
156 free_pool_memory(root->cached_path);
158 Dmsg2(400, "Total size=%u blocks=%d\n", root->total_size, root->blocks);
166 * Insert a node in the tree. This is the main subroutine
167 * called when building a tree.
170 TREE_NODE *insert_tree_node(char *path, TREE_NODE *node,
171 TREE_ROOT *root, TREE_NODE *parent)
175 int path_len = strlen(path);
177 Dmsg1(100, "insert_tree_node: %s\n", path);
179 * If trailing slash, strip it
182 q = path + path_len - 1;
184 *q = 0; /* strip trailing slash */
186 q = NULL; /* no trailing slash */
189 q = NULL; /* no trailing slash */
191 p = strrchr(path, '/'); /* separate path and filename */
194 if (!parent) { /* if no parent, we need to make one */
195 *p = 0; /* terminate path */
196 Dmsg1(100, "make_tree_path for %s\n", path);
197 path_len = strlen(path); /* get new length */
198 if (path_len == root->cached_path_len &&
199 strcmp(path, root->cached_path) == 0) {
200 parent = root->cached_parent;
202 root->cached_path_len = path_len;
203 pm_strcpy(&root->cached_path, path);
204 parent = make_tree_path(path, root);
205 root->cached_parent = parent;
207 Dmsg1(100, "parent=%s\n", parent->fname);
208 *p = '/'; /* restore full path */
213 parent = (TREE_NODE *)root;
214 node->type = TN_DIR_NLS;
216 Dmsg1(100, "No / found: %s\n", path);
219 uint16_t fname_len = strlen(fname);
220 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
221 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
222 if (sibling->fname_len == fname_len &&
223 strcmp(sibling->fname, fname) == 0) {
224 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
225 if (q) { /* if trailing slash on entry */
226 *q = '/'; /* restore it */
232 append_tree_node(fname, node, root, parent);
233 Dmsg1(100, "insert_tree_node: parent=%s\n", parent->fname);
234 if (q) { /* if trailing slash on entry */
235 *q = '/'; /* restore it */
241 * Ensure that all appropriate nodes for a full path exist in
244 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
246 TREE_NODE *parent, *sibling, *node;
248 int type = TN_NEWDIR;
250 Dmsg1(100, "make_tree_path: %s\n", path);
252 Dmsg0(100, "make_tree_path: parent=*root*\n");
253 return (TREE_NODE *)root;
255 p = strrchr(path, '/'); /* get last dir component of path */
258 *p = 0; /* terminate path */
259 parent = make_tree_path(path, root);
260 *p = '/'; /* restore full name */
263 parent = (TREE_NODE *)root;
266 /* Is it already a sibling? */
267 uint16_t fname_len = strlen(fname);
268 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
269 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
270 if (sibling->fname_len == fname_len &&
271 strcmp(sibling->fname, fname) == 0) {
272 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
277 node = new_tree_node(root, type);
278 append_tree_node(fname, node, root, parent);
279 Dmsg1(100, "make_tree_path: add parent=%s\n", node->fname);
284 * Append sibling to parent's child chain
286 void append_tree_node(char *fname, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
290 Dmsg1(100, "append_tree_node: %s\n", fname);
291 node->fname_len = strlen(fname);
292 node->fname = tree_alloc(root, node->fname_len + 1);
293 strcpy(node->fname, fname);
294 node->parent = parent;
295 if (!parent->child) {
296 parent->child = node;
299 /* Append to end of sibling chain */
300 for (child=parent->child; child->sibling; child=child->sibling)
302 child->sibling = node;
304 /* Maintain a linear chain of nodes */
310 root->last->next = node;
317 /* Moved to tree.h to eliminate subroutine call */
318 TREE_NODE *first_tree_node(TREE_ROOT *root)
323 TREE_NODE *next_tree_node(TREE_NODE *node)
330 void print_tree(char *path, TREE_NODE *tree)
332 char buf[MAXPATHLEN];
338 switch (tree->type) {
350 Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
351 switch (tree->type) {
356 bsnprintf(buf, sizeof(buf), "%s/%s", path, tree->fname);
357 print_tree(buf, tree->child);
360 print_tree(path, tree->child);
363 Pmsg1(000, "Unknown node type %d\n", tree->type);
365 print_tree(path, tree->sibling);
369 int tree_getpath(TREE_NODE *node, char *buf, int buf_size)
375 tree_getpath(node->parent, buf, buf_size);
377 * Fixup for Win32. If we have a Win32 directory and
378 * there is only a / in the buffer, remove it since
379 * win32 names don't generally start with /
381 if (node->type == TN_DIR_NLS && buf[0] == '/' && buf[1] == 0) {
384 bstrncat(buf, node->fname, buf_size);
385 /* Add a slash for all directories unless we are at the root,
386 * also add a slash to a soft linked file if it has children
387 * i.e. it is linked to a directory.
389 if ((node->type != TN_FILE && !(buf[0] == '/' && buf[1] == 0)) ||
390 (node->soft_link && node->child)) {
391 bstrncat(buf, "/", buf_size);
397 * Change to specified directory
399 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
401 if (strcmp(path, ".") == 0) {
404 if (strcmp(path, "..") == 0) {
411 if (path[0] == '/') {
412 Dmsg0(100, "Doing absolute lookup.\n");
413 return tree_relcwd(path+1, root, (TREE_NODE *)root);
415 Dmsg0(100, "Doing relative lookup.\n");
416 return tree_relcwd(path, root, node);
421 * Do a relative cwd -- i.e. relative to current node rather than root node
423 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
432 /* Check the current segment only */
433 p = strchr(path, '/');
439 Dmsg2(100, "tree_relcwd: len=%d path=%s\n", len, path);
440 for (cd=node->child; cd; cd=cd->sibling) {
441 Dmsg1(100, "tree_relcwd: test cd=%s\n", cd->fname);
442 if (cd->fname[0] == path[0] && len == (int)strlen(cd->fname)
443 && strncmp(cd->fname, path, len) == 0) {
447 if (!cd || (cd->type == TN_FILE && !cd->child)) {
451 Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
454 Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
455 /* Check the next segment if any */
456 return tree_relcwd(p+1, root, cd);
461 #ifdef BUILD_TEST_PROGRAM
463 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
465 static uint32_t FileIndex = 0;
467 * Simple test program for tree routines
469 int main(int argc, char *argv[])
473 char buf[MAXPATHLEN];
476 root->fname = tree_alloc(root, 1);
480 FillDirectoryTree("/home/kern/bacula/k", root, NULL);
482 for (node = first_tree_node(root); node; node=next_tree_node(node)) {
483 tree_getpath(node, buf, sizeof(buf));
484 Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
487 node = (TREE_NODE *)root;
488 Pmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
489 node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
491 tree_getpath(node, buf, sizeof(buf));
492 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
495 Pmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
496 node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
498 tree_getpath(node, buf, sizeof(buf));
499 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
501 Dmsg0(100, "testprogs not found.\n");
504 free_tree((TREE_NODE *)root);
509 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
511 TREE_NODE *newparent = NULL;
516 char pathbuf[MAXPATHLEN];
517 char file[MAXPATHLEN];
521 Dmsg1(100, "FillDirectoryTree: %s\n", path);
526 while ((dir = readdir(dp))) {
527 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
530 bstrncpy(file, dir->d_name, sizeof(file));
531 snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
532 if (lstat(pathbuf, &statbuf) < 0) {
533 printf("lstat() failed. ERR=%s\n", strerror(errno));
536 // printf("got file=%s, pathbuf=%s\n", file, pathbuf);
538 if (S_ISLNK(statbuf.st_mode))
539 type = TN_FILE; /* link */
540 else if (S_ISREG(statbuf.st_mode))
542 else if (S_ISDIR(statbuf.st_mode)) {
544 } else if (S_ISCHR(statbuf.st_mode))
545 type = TN_FILE; /* char dev */
546 else if (S_ISBLK(statbuf.st_mode))
547 type = TN_FILE; /* block dev */
548 else if (S_ISFIFO(statbuf.st_mode))
549 type = TN_FILE; /* fifo */
550 else if (S_ISSOCK(statbuf.st_mode))
551 type = TN_FILE; /* sock */
554 printf("Unknown file type: 0x%x\n", statbuf.st_mode);
557 Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
558 node = new_tree_node(root, type);
559 node->FileIndex = ++FileIndex;
560 parent = insert_tree_node(pathbuf, node, root, parent);
561 if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
562 Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
563 FillDirectoryTree(pathbuf, root, node);