]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/tree.c
Fix tree relcd + add rate
[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       Pmsg1(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 /*
360  * Do a relative cwd -- i.e. relative to current node rather than root node
361  */
362 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
363 {
364    char *p;
365    int len;
366    TREE_NODE *cd;
367
368    if (*path == 0) {
369       return node;
370    }
371    /* Check the current segment only */
372    p = strchr(path, '/');
373    if (p) {
374       len = p - path;
375    } else {
376       len = strlen(path);
377    }
378    Dmsg2(100, "tree_relcwd: len=%d path=%s\n", len, path);
379    for (cd=node->child; cd; cd=cd->sibling) {
380       Dmsg1(100, "tree_relcwd: test cd=%s\n", cd->fname);
381       if (cd->fname[0] == path[0] && len == (int)strlen(cd->fname)    
382           && strncmp(cd->fname, path, len) == 0) {
383          break;
384       }
385    }
386    if (!cd || cd->type == TN_FILE) {
387       return NULL;
388    }
389    if (!p) {
390       Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
391       return cd;
392    }
393    Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
394    /* Check the next segment if any */
395    return tree_relcwd(p+1, root, cd);
396 }
397
398
399
400 #ifdef BUILD_TEST_PROGRAM
401
402 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
403
404 static uint32_t FileIndex = 0;
405 /*
406  * Simple test program for tree routines
407  */
408 int main(int argc, char *argv[])
409 {
410     TREE_ROOT *root;
411     TREE_NODE *node;
412     char buf[MAXPATHLEN];
413
414     root = new_tree();
415     root->fname = tree_alloc(root, 1);
416     *root->fname = 0;
417
418     FillDirectoryTree("/home/kern/bacula/k", root, NULL);
419
420     for (node = first_tree_node(root); node; node=next_tree_node(node)) {
421        tree_getpath(node, buf, sizeof(buf));
422        Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
423     }
424
425     node = (TREE_NODE *)root;
426     Pmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
427     node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
428     if (node) {
429        tree_getpath(node, buf, sizeof(buf));
430        Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
431     }
432
433     Pmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
434     node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
435     if (node) {
436        tree_getpath(node, buf, sizeof(buf));
437        Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
438     } else {
439        Dmsg0(100, "testprogs not found.\n");
440     }
441
442     free_tree((TREE_NODE *)root);
443
444     return 0;
445 }
446
447 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
448 {
449    TREE_NODE *newparent = NULL;
450    TREE_NODE *node;
451    struct stat statbuf;
452    DIR *dp;
453    struct dirent *dir;
454    char pathbuf[MAXPATHLEN];
455    char file[MAXPATHLEN];
456    int type;
457    int i;
458    
459    Dmsg1(100, "FillDirectoryTree: %s\n", path);
460    dp = opendir(path);
461    if (!dp) {
462       return;
463    }
464    while ((dir = readdir(dp))) {
465       if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
466          continue;
467       }
468       strcpy(file, dir->d_name);
469       snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
470       if (lstat(pathbuf, &statbuf) < 0) {
471          printf("lstat() failed. ERR=%s\n", strerror(errno));
472          continue;
473       }
474 //      printf("got file=%s, pathbuf=%s\n", file, pathbuf);
475       type = TN_FILE;
476       if (S_ISLNK(statbuf.st_mode))
477          type =  TN_FILE;  /* link */
478       else if (S_ISREG(statbuf.st_mode))
479          type = TN_FILE;
480       else if (S_ISDIR(statbuf.st_mode)) {
481          type = TN_DIR;
482       } else if (S_ISCHR(statbuf.st_mode))
483          type = TN_FILE; /* char dev */
484       else if (S_ISBLK(statbuf.st_mode))
485          type = TN_FILE; /* block dev */
486       else if (S_ISFIFO(statbuf.st_mode))
487          type = TN_FILE; /* fifo */
488       else if (S_ISSOCK(statbuf.st_mode))
489          type = TN_FILE; /* sock */
490       else {
491          type = TN_FILE;
492          printf("Unknown file type: 0x%x\n", statbuf.st_mode);
493       }
494
495       Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
496       node = new_tree_node(root, type);
497       node->FileIndex = ++FileIndex;
498       parent = insert_tree_node(pathbuf, node, root, parent);
499       if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
500          Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
501          FillDirectoryTree(pathbuf, root, node);
502       }
503    }
504    closedir(dp);
505 }
506 #endif