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 * This subroutine gets a big buffer.
38 static void malloc_buf(TREE_ROOT *root, int size)
42 mem = (struct s_mem *)malloc(size);
43 mem->next = root->mem;
45 mem->mem = mem->first;
46 mem->rem = (char *)mem + size - mem->mem;
47 Dmsg2(400, "malloc buf size=%d rem=%d\n", size, mem->rem);
52 * Note, we allocate a big buffer in the tree root
53 * from which we allocate nodes. This runs more
54 * than 100 times as fast as directly using malloc()
55 * for each of the nodes.
57 TREE_ROOT *new_tree(int count)
62 if (count < 1000) { /* minimum tree size */
65 root = (TREE_ROOT *)malloc(sizeof(TREE_ROOT));
66 memset(root, 0, sizeof(TREE_ROOT));
68 /* Assume filename = 20 characters average length */
69 size = count * (BALIGN(sizeof(TREE_NODE)) + 20);
70 if (size > 10000000) {
73 Dmsg2(400, "count=%d size=%d\n", count, size);
74 malloc_buf(root, size);
75 root->cached_path = get_pool_memory(PM_FNAME);
80 * Create a new tree node. Size depends on type.
82 TREE_NODE *new_tree_node(TREE_ROOT *root, int type)
85 int size = BALIGN(sizeof(TREE_NODE));
87 if (root->mem->rem < size) {
88 malloc_buf(root, 20000);
91 root->mem->rem -= size;
92 node = (TREE_NODE *)root->mem->mem;
93 root->mem->mem += size;
94 memset(node, 0, sizeof(TREE_NODE));
101 * Allocate bytes for filename in tree structure.
102 * Keep the pointers properly aligned by allocating
103 * sizes that are aligned.
105 static char *tree_alloc(TREE_ROOT *root, int size)
108 int asize = BALIGN(size);
110 if (root->mem->rem < asize) {
111 malloc_buf(root, 20000+asize);
113 root->mem->rem -= asize;
114 buf = root->mem->mem;
115 root->mem->mem += asize;
120 /* This routine frees the whole tree */
121 void free_tree(TREE_ROOT *root)
123 struct s_mem *mem, *rel;
125 for (mem=root->mem; mem; ) {
130 if (root->cached_path) {
131 free_pool_memory(root->cached_path);
140 * Insert a node in the tree
143 TREE_NODE *insert_tree_node(char *path, TREE_NODE *node,
144 TREE_ROOT *root, TREE_NODE *parent)
148 int len = strlen(path);
150 Dmsg1(100, "insert_tree_node: %s\n", path);
152 * If trailing slash, strip it
157 *q = 0; /* strip trailing slash */
159 q = NULL; /* no trailing slash */
162 q = NULL; /* no trailing slash */
164 p = strrchr(path, '/'); /* separate path and filename */
167 if (!parent) { /* if no parent, we need to make one */
168 *p = 0; /* terminate path */
169 Dmsg1(100, "make_tree_path for %s\n", path);
170 if ((int)strlen(path) == root->cached_path_len &&
171 strcmp(path, root->cached_path) == 0) {
172 parent = root->cached_parent;
174 root->cached_path_len = strlen(path);
175 pm_strcpy(&root->cached_path, path);
176 parent = make_tree_path(path, root);
177 root->cached_parent = parent;
179 Dmsg1(100, "parent=%s\n", parent->fname);
180 *p = '/'; /* restore full path */
185 parent = (TREE_NODE *)root;
186 node->type = TN_DIR_NLS;
188 Dmsg1(100, "No / found: %s\n", path);
191 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
192 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
193 if (strcmp(sibling->fname, fname) == 0) {
194 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
195 if (q) { /* if trailing slash on entry */
196 *q = '/'; /* restore it */
202 append_tree_node(fname, node, root, parent);
203 Dmsg1(100, "insert_tree_node: parent=%s\n", parent->fname);
204 if (q) { /* if trailing slash on entry */
205 *q = '/'; /* restore it */
211 * Ensure that all appropriate nodes for a full path exist in
214 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
216 TREE_NODE *parent, *sibling, *node;
218 int type = TN_NEWDIR;
220 Dmsg1(100, "make_tree_path: %s\n", path);
222 Dmsg0(100, "make_tree_path: parent=*root*\n");
223 return (TREE_NODE *)root;
225 p = strrchr(path, '/'); /* get last dir component of path */
228 *p = 0; /* terminate path */
229 parent = make_tree_path(path, root);
230 *p = '/'; /* restore full name */
233 parent = (TREE_NODE *)root;
236 /* Is it already a sibling? */
237 for (sibling=parent->child; sibling; sibling=sibling->sibling) {
238 Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
239 if (strcmp(sibling->fname, fname) == 0) {
240 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
245 node = new_tree_node(root, type);
246 append_tree_node(fname, node, root, parent);
247 Dmsg1(100, "make_tree_path: add parent=%s\n", node->fname);
252 * Append sibling to parent's child chain
254 void append_tree_node(char *fname, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
258 Dmsg1(100, "append_tree_node: %s\n", fname);
259 node->fname = tree_alloc(root, strlen(fname) + 1);
260 strcpy(node->fname, fname);
261 node->parent = parent;
262 if (!parent->child) {
263 parent->child = node;
266 /* Append to end of sibling chain */
267 for (child=parent->child; child->sibling; child=child->sibling)
269 child->sibling = node;
271 /* Maintain a linear chain of nodes */
277 root->last->next = node;
283 TREE_NODE *first_tree_node(TREE_ROOT *root)
288 TREE_NODE *next_tree_node(TREE_NODE *node)
294 void print_tree(char *path, TREE_NODE *tree)
296 char buf[MAXPATHLEN];
302 switch (tree->type) {
314 Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
315 switch (tree->type) {
321 bsnprintf(buf, sizeof(buf), "%s/%s", path, tree->fname);
322 print_tree(buf, tree->child);
325 print_tree(path, tree->child);
328 Pmsg1(000, "Unknown node type %d\n", tree->type);
330 print_tree(path, tree->sibling);
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 if (node->type != TN_FILE) {
351 bstrncat(buf, "/", buf_size);
357 * Change to specified directory
359 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
361 if (strcmp(path, ".") == 0) {
364 if (strcmp(path, "..") == 0) {
371 if (path[0] == '/') {
372 Dmsg0(100, "Doing absolute lookup.\n");
373 return tree_relcwd(path+1, root, (TREE_NODE *)root);
375 Dmsg0(100, "Doing relative lookup.\n");
376 return tree_relcwd(path, root, node);
381 * Do a relative cwd -- i.e. relative to current node rather than root node
383 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
392 /* Check the current segment only */
393 p = strchr(path, '/');
399 Dmsg2(100, "tree_relcwd: len=%d path=%s\n", len, path);
400 for (cd=node->child; cd; cd=cd->sibling) {
401 Dmsg1(100, "tree_relcwd: test cd=%s\n", cd->fname);
402 if (cd->fname[0] == path[0] && len == (int)strlen(cd->fname)
403 && strncmp(cd->fname, path, len) == 0) {
407 if (!cd || cd->type == TN_FILE) {
411 Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
414 Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
415 /* Check the next segment if any */
416 return tree_relcwd(p+1, root, cd);
421 #ifdef BUILD_TEST_PROGRAM
423 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
425 static uint32_t FileIndex = 0;
427 * Simple test program for tree routines
429 int main(int argc, char *argv[])
433 char buf[MAXPATHLEN];
436 root->fname = tree_alloc(root, 1);
439 FillDirectoryTree("/home/kern/bacula/k", root, NULL);
441 for (node = first_tree_node(root); node; node=next_tree_node(node)) {
442 tree_getpath(node, buf, sizeof(buf));
443 Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
446 node = (TREE_NODE *)root;
447 Pmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
448 node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
450 tree_getpath(node, buf, sizeof(buf));
451 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
454 Pmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
455 node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
457 tree_getpath(node, buf, sizeof(buf));
458 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
460 Dmsg0(100, "testprogs not found.\n");
463 free_tree((TREE_NODE *)root);
468 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
470 TREE_NODE *newparent = NULL;
475 char pathbuf[MAXPATHLEN];
476 char file[MAXPATHLEN];
480 Dmsg1(100, "FillDirectoryTree: %s\n", path);
485 while ((dir = readdir(dp))) {
486 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
489 bstrncpy(file, dir->d_name, sizeof(file));
490 snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
491 if (lstat(pathbuf, &statbuf) < 0) {
492 printf("lstat() failed. ERR=%s\n", strerror(errno));
495 // printf("got file=%s, pathbuf=%s\n", file, pathbuf);
497 if (S_ISLNK(statbuf.st_mode))
498 type = TN_FILE; /* link */
499 else if (S_ISREG(statbuf.st_mode))
501 else if (S_ISDIR(statbuf.st_mode)) {
503 } else if (S_ISCHR(statbuf.st_mode))
504 type = TN_FILE; /* char dev */
505 else if (S_ISBLK(statbuf.st_mode))
506 type = TN_FILE; /* block dev */
507 else if (S_ISFIFO(statbuf.st_mode))
508 type = TN_FILE; /* fifo */
509 else if (S_ISSOCK(statbuf.st_mode))
510 type = TN_FILE; /* sock */
513 printf("Unknown file type: 0x%x\n", statbuf.st_mode);
516 Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
517 node = new_tree_node(root, type);
518 node->FileIndex = ++FileIndex;
519 parent = insert_tree_node(pathbuf, node, root, parent);
520 if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
521 Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
522 FillDirectoryTree(pathbuf, root, node);