]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tree.c
Bug fixes + documentation -- kes06Aug02
[bacula/bacula] / bacula / src / lib / tree.c
1 /*
2  * Directory tree build/traverse routines
3  * 
4  *    Kern Sibbald, June MMII
5  *
6 */
7 /*
8    Copyright (C) 2002 Kern Sibbald and John Walker
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 as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
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 GNU
18    General Public License for more details.
19
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,
23    MA 02111-1307, USA.
24
25  */
26
27
28 #include "bacula.h"
29 #include "findlib/find.h"
30 #include "findlib/system.h"
31              
32 #ifndef MAXPATHLEN
33 #define MAXPATHLEN 1000
34 #endif
35
36 /*
37  * This subrouting gets a big buffer.
38  */
39 static void malloc_buf(TREE_ROOT *root, int size)
40 {
41    struct s_mem *mem;
42
43    mem = (struct s_mem *)malloc(size);
44    mem->next = root->mem;
45    root->mem = mem;
46    mem->mem = mem->first;
47    mem->rem = (char *)mem + size - mem->mem;
48    Dmsg2(400, "malloc buf size=%d rem=%d\n", size, mem->rem);
49 }
50
51
52 /*
53  * Note, we allocate a big buffer in the tree root
54  *  from which we allocate nodes. This runs more
55  *  than 100 times as fast as directly using malloc()
56  *  for each of the nodes.
57  */
58 TREE_ROOT *new_tree(int count)
59 {
60    TREE_ROOT *root;
61    uint32_t size;
62
63    if (count < 1000) {                /* minimum tree size */
64       count = 1000;
65    }
66    root = (TREE_ROOT *)malloc(sizeof(TREE_ROOT));
67    memset(root, 0, sizeof(TREE_ROOT));
68    root->type = TN_ROOT;
69    /* Assume filename = 20 characters average length */
70    size = count * (BALIGN(sizeof(TREE_NODE)) + 20);
71    if (size > 10000000) {
72       size = 10000000;
73    }
74    Dmsg2(400, "count=%d size=%d\n", count, size);
75    malloc_buf(root, size);
76    return root;
77 }
78
79 /* 
80  * Create a new tree node. Size depends on type.
81  */
82 TREE_NODE *new_tree_node(TREE_ROOT *root, int type)
83 {
84    TREE_NODE *node;
85    int size = BALIGN(sizeof(TREE_NODE));
86
87    if (root->mem->rem < size) {
88       malloc_buf(root, 20000);
89    }
90
91    root->mem->rem -= size;
92    node = (TREE_NODE *)root->mem->mem;
93    root->mem->mem += size;
94    memset(node, 0, sizeof(TREE_NODE));
95    node->type = type;
96    return node;
97 }
98
99
100 /*
101  * Allocate bytes for filename in tree structure.
102  *  Keep the pointers properly aligned by allocating
103  *  sizes that are aligned.
104  */
105 static char *tree_alloc(TREE_ROOT *root, int size)
106 {
107    char *buf;
108    int asize = BALIGN(size);
109
110    if (root->mem->rem < asize) {
111       malloc_buf(root, 20000+asize);
112    }
113    root->mem->rem -= asize;
114    buf = root->mem->mem;
115    root->mem->mem += asize;
116    return buf;
117 }
118
119
120 /* This routine frees the whole tree */
121 void free_tree(TREE_ROOT *root)
122 {
123    struct s_mem *mem, *rel;
124
125    for (mem=root->mem; mem; ) {
126       rel = mem;
127       mem = mem->next;
128       free(rel);
129    }
130    free(root);
131    return;
132 }
133
134
135
136 /* 
137  * Insert a node in the tree
138  *
139  */
140 TREE_NODE *insert_tree_node(char *path, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
141 {
142    TREE_NODE *sibling;
143    char *p, *q, *fname;
144    int len = strlen(path);
145
146    Dmsg1(100, "insert_tree_node: %s\n", path);
147    /*
148     * If trailing slash, strip it
149     */
150    if (len > 0) {
151       q = path + len - 1;
152       if (*q == '/') {
153          *q = 0;                      /* strip trailing slash */
154       } else {
155          q = NULL;                    /* no trailing slash */
156       }
157    } else {
158       q = NULL;                       /* no trailing slash */
159    }
160    p = strrchr(path, '/');            /* separate path and filename */
161    if (p) {
162       fname = p + 1;
163       if (!parent) {
164          *p = 0;                      /* terminate path */
165          Dmsg1(100, "make_tree_path for %s\n", path);
166          parent = make_tree_path(path, root);
167          Dmsg1(100, "parent=%s\n", parent->fname);
168          *p = '/';                    /* restore full name */
169       }
170    } else {
171       fname = path;
172       if (!parent) {
173          parent = (TREE_NODE *)root;
174       }
175       Dmsg1(100, "No / found: %s\n", path);
176    }
177
178    for (sibling=parent->child; sibling; sibling=sibling->sibling) {
179       Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
180       if (strcmp(sibling->fname, fname) == 0) {
181          Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
182          if (q) {                     /* if trailing slash on entry */
183             *q = '/';                 /*  restore it */
184          }
185          return sibling;
186       }
187    }
188
189
190    append_tree_node(fname, node, root, parent);
191    Dmsg1(100, "insert_tree_node: parent=%s\n", parent->fname);
192    if (q) {                           /* if trailing slash on entry */
193       *q = '/';                       /*  restore it */
194    }
195    return node;
196 }
197
198 /*
199  * Ensure that all appropriate nodes for a full path exist in
200  *  the tree.
201  */
202 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
203 {
204    TREE_NODE *parent, *sibling, *node;
205    char *fname, *p;
206
207    Dmsg1(100, "make_tree_path: %s\n", path);
208    if (*path == 0) {
209       Dmsg0(100, "make_tree_path: parent=*root*\n");
210       return (TREE_NODE *)root;
211    }
212    p = strrchr(path, '/');           /* separate path and filename */
213    if (p) {
214       fname = p + 1;
215       *p = 0;                         /* terminate path */
216       parent = make_tree_path(path, root);
217       *p = '/';                       /* restore full name */
218    } else {
219       fname = path;
220       parent = (TREE_NODE *)root;
221    }
222    /* Is it already a sibling? */
223    for (sibling=parent->child; sibling; sibling=sibling->sibling) {
224       Dmsg2(100, "sibling->fname=%s fname=%s\n", sibling->fname, fname);
225       if (strcmp(sibling->fname, fname) == 0) {
226          Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
227          return sibling;
228       }
229    }
230    /* Must add */
231    node = new_tree_node(root, TN_NEWDIR);
232    append_tree_node(fname, node, root, parent);
233    Dmsg1(100, "make_tree_path: add parent=%s\n", node->fname);
234    return node;
235 }  
236
237 /*
238  *  Append sibling to parent's child chain
239  */
240 void append_tree_node(char *fname, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
241 {
242    TREE_NODE *child;
243
244    Dmsg1(100, "append_tree_node: %s\n", fname);
245    node->fname = tree_alloc(root, strlen(fname) + 1);
246    strcpy(node->fname, fname);
247    node->parent = parent;
248    if (!parent->child) {
249       parent->child = node;
250       goto item_link;
251    }
252    /* Append to end of sibling chain */
253    for (child=parent->child; child->sibling; child=child->sibling)
254       { }
255    child->sibling = node;
256
257    /* Maintain a linear chain of nodes */
258 item_link:
259    if (!root->first) {
260       root->first = node;
261       root->last = node;
262    } else {
263       root->last->next = node;
264       root->last = node;
265    }
266    return;
267 }
268
269 TREE_NODE *first_tree_node(TREE_ROOT *root)
270 {
271    return root->first;
272 }
273
274 TREE_NODE *next_tree_node(TREE_NODE *node)
275 {
276    return node->next;
277 }
278
279
280 void print_tree(char *path, TREE_NODE *tree)
281 {
282    char buf[MAXPATHLEN];
283    char *termchr;
284
285    if (!tree) {
286       return;
287    }
288    switch (tree->type) {
289    case TN_DIR:
290    case TN_NEWDIR:  
291       termchr = "/";
292       break;
293    case TN_ROOT:
294    case TN_FILE:
295    default:
296       termchr = "";
297       break;
298    }
299    Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
300    switch (tree->type) {
301    case TN_FILE:
302       break;
303    case TN_DIR:
304       sprintf(buf, "%s/%s", path, tree->fname);
305       print_tree(buf, tree->child);
306       break;
307    case TN_ROOT:
308       print_tree(path, tree->child);
309       break;
310    case TN_NEWDIR:  
311       sprintf(buf, "%s/%s", path, tree->fname);
312       print_tree(buf, tree->child);
313       break;
314    default:
315       Dmsg1(000, "Unknown node type %d\n", tree->type);
316    }
317    print_tree(path, tree->sibling);
318    return;
319 }
320
321 int tree_getpath(TREE_NODE *node, char *buf, int buf_size)
322 {
323    if (!node) {
324       buf[0] = 0;
325       return 1;
326    }
327    tree_getpath(node->parent, buf, buf_size);
328    strcat(buf, node->fname);
329    if (node->type != TN_FILE) {
330       strcat(buf, "/");
331    }
332    return 1;
333 }
334
335 /* 
336  * Change to specified directory
337  */
338 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
339 {
340    if (strcmp(path, ".") == 0) {
341       return node;
342    }
343    if (strcmp(path, "..") == 0) {
344       if (node->parent) {
345          return node->parent;
346       } else {
347          return node;
348       }
349    }
350    if (path[0] == '/') {
351       Dmsg0(100, "Doing absolute lookup.\n");
352       return tree_relcwd(path+1, root, (TREE_NODE *)root);
353    }
354    Dmsg0(100, "Doing relative lookup.\n");
355    return tree_relcwd(path, root, node);
356 }
357
358
359 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
360 {
361    char *p;
362    int len;
363    TREE_NODE *cd;
364
365    if (*path == 0) {
366       return node;
367    }
368    p = strchr(path, '/');
369    if (p) {
370       len = p - path;
371    }
372    for (cd=node->child; cd; cd=cd->sibling) {
373       if (strncmp(cd->fname, path, len) == 0) {
374          Dmsg1(100, "tree_relcwd: found cd=%s\n", cd->fname);
375          break;
376       }
377    }
378    if (!cd || cd->type == TN_FILE) {
379       return NULL;
380    }
381    if (!p) {
382       Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
383       return cd;
384    }
385    Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
386    return tree_relcwd(p+1, root, cd);
387 }
388
389
390
391 #ifdef BUILD_TEST_PROGRAM
392
393 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
394
395 static uint32_t FileIndex = 0;
396 /*
397  * Simple test program for tree routines
398  */
399 int main(int argc, char *argv[])
400 {
401     TREE_ROOT *root;
402     TREE_NODE *node;
403     char buf[MAXPATHLEN];
404
405     root = new_tree();
406     root->fname = tree_alloc(root, 1);
407     *root->fname = 0;
408
409     FillDirectoryTree("/home/kern/bacula/k", root, NULL);
410
411     for (node = first_tree_node(root); node; node=next_tree_node(node)) {
412        tree_getpath(node, buf, sizeof(buf));
413        Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
414     }
415
416     node = (TREE_NODE *)root;
417     Dmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
418     node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
419     if (node) {
420        tree_getpath(node, buf, sizeof(buf));
421        Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
422     }
423
424     Dmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
425     node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
426     if (node) {
427        tree_getpath(node, buf, sizeof(buf));
428        Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
429     } else {
430        Dmsg0(100, "testprogs not found.\n");
431     }
432
433     free_tree((TREE_NODE *)root);
434
435     return 0;
436 }
437
438 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
439 {
440    TREE_NODE *newparent = NULL;
441    TREE_NODE *node;
442    struct stat statbuf;
443    DIR *dp;
444    struct dirent *dir;
445    char pathbuf[MAXPATHLEN];
446    char file[MAXPATHLEN];
447    int type;
448    int i;
449    
450    Dmsg1(100, "FillDirectoryTree: %s\n", path);
451    dp = opendir(path);
452    if (!dp) {
453       return;
454    }
455    while ((dir = readdir(dp))) {
456       if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
457          continue;
458       }
459       strcpy(file, dir->d_name);
460       snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
461       if (lstat(pathbuf, &statbuf) < 0) {
462          printf("lstat() failed. ERR=%s\n", strerror(errno));
463          continue;
464       }
465 //      printf("got file=%s, pathbuf=%s\n", file, pathbuf);
466       type = TN_FILE;
467       if (S_ISLNK(statbuf.st_mode))
468          type =  TN_FILE;  /* link */
469       else if (S_ISREG(statbuf.st_mode))
470          type = TN_FILE;
471       else if (S_ISDIR(statbuf.st_mode)) {
472          type = TN_DIR;
473       } else if (S_ISCHR(statbuf.st_mode))
474          type = TN_FILE; /* char dev */
475       else if (S_ISBLK(statbuf.st_mode))
476          type = TN_FILE; /* block dev */
477       else if (S_ISFIFO(statbuf.st_mode))
478          type = TN_FILE; /* fifo */
479       else if (S_ISSOCK(statbuf.st_mode))
480          type = TN_FILE; /* sock */
481       else {
482          type = TN_FILE;
483          printf("Unknown file type: 0x%x\n", statbuf.st_mode);
484       }
485
486       Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
487       node = new_tree_node(root, type);
488       node->FileIndex = ++FileIndex;
489       parent = insert_tree_node(pathbuf, node, root, parent);
490       if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
491          Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
492          FillDirectoryTree(pathbuf, root, node);
493       }
494    }
495    closedir(dp);
496 }
497 #endif