2 * Directory tree build/traverse routines
4 * Kern Sibbald, June MMII
8 Copyright (C) 2002 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"
30 #include "findlib/system.h"
33 #define MAXPATHLEN 1000
36 TREE_NODE *new_tree_node(int type)
41 if (type == TN_ROOT) {
42 size = sizeof(TREE_ROOT);
44 size = sizeof(TREE_NODE);
46 node = (TREE_NODE *)malloc(size);
47 memset(node, 0, size);
52 TREE_NODE *insert_tree_node(char *path, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
56 Dmsg1(100, "insert_tree_node: %s\n", path);
57 p = strrchr(path, '/');
59 Dmsg1(000, "No / found: %s\n", path);
65 parent = make_tree_path(path, root);
68 append_tree_node(fname, node, root, parent);
69 Dmsg1(100, "insert_tree_node: parent=%s\n", parent->fname);
73 TREE_NODE *make_tree_path(char *path, TREE_ROOT *root)
75 TREE_NODE *parent, *sibling;
78 Dmsg1(100, "make_tree_path: %s\n", path);
80 Dmsg0(100, "make_tree_path: parent=*root*\n");
81 return (TREE_NODE *)root;
83 p = strrchr(path, '/');
85 Dmsg1(000, "No / found: %s\n", path);
91 parent = make_tree_path(path, root);
93 /* Is it already a sibling? */
94 for (sibling=parent->sibling; sibling; sibling=sibling->sibling) {
95 if (strcmp(sibling->fname, fname) == 0) {
96 Dmsg1(100, "make_tree_path: found parent=%s\n", parent->fname);
101 sibling = new_tree_node(TN_NEWDIR);
102 append_tree_node(fname, sibling, root, parent);
104 Dmsg1(100, "make_tree_path: add parent=%s\n", parent->fname);
109 * Append sibling to parent's child chain
111 void append_tree_node(char *fname, TREE_NODE *node, TREE_ROOT *root, TREE_NODE *parent)
115 Dmsg1(100, "append_tree_node: %s\n", fname);
116 node->fname = bstrdup(fname);
117 node->parent = parent;
118 if (!parent->child) {
119 parent->child = node;
122 for (child=parent->child; child->sibling; child=child->sibling)
124 child->sibling = node;
131 root->last->next = node;
137 TREE_NODE *first_tree_node(TREE_ROOT *root)
142 TREE_NODE *next_tree_node(TREE_NODE *node)
148 void print_tree(char *path, TREE_NODE *tree)
150 char buf[MAXPATHLEN];
156 switch (tree->type) {
167 Dmsg3(-1, "%s/%s%s\n", path, tree->fname, termchr);
168 switch (tree->type) {
172 sprintf(buf, "%s/%s", path, tree->fname);
173 print_tree(buf, tree->child);
176 print_tree(path, tree->child);
179 sprintf(buf, "%s/%s", path, tree->fname);
180 print_tree(buf, tree->child);
183 Dmsg1(000, "Unknown node type %d\n", tree->type);
185 print_tree(path, tree->sibling);
189 void free_tree(TREE_NODE *node)
194 switch (node->type) {
200 free_tree(node->child);
203 Dmsg1(000, "Unknown node type %d\n", node->type);
206 free_tree(node->sibling);
212 int tree_getpath(TREE_NODE *node, char *buf, int buf_size)
218 tree_getpath(node->parent, buf, buf_size);
219 strcat(buf, node->fname);
220 if (node->type != TN_FILE) {
227 * Change to specified directory
229 TREE_NODE *tree_cwd(char *path, TREE_ROOT *root, TREE_NODE *node)
231 if (strcmp(path, ".") == 0) {
234 if (strcmp(path, "..") == 0) {
241 if (path[0] == '/') {
242 Dmsg0(100, "Doing absolute lookup.\n");
243 return tree_relcwd(path+1, root, (TREE_NODE *)root);
245 Dmsg0(100, "Doing relative lookup.\n");
246 return tree_relcwd(path, root, node);
250 TREE_NODE *tree_relcwd(char *path, TREE_ROOT *root, TREE_NODE *node)
259 p = strchr(path, '/');
263 for (cd=node->child; cd; cd=cd->sibling) {
264 if (strncmp(cd->fname, path, len) == 0) {
265 Dmsg1(100, "tree_relcwd: found cd=%s\n", cd->fname);
269 if (!cd || cd->type == TN_FILE) {
270 Dmsg1(100, "tree_relcwd: failed %s is a file.\n", cd->fname);
274 Dmsg0(100, "tree_relcwd: no more to lookup. found.\n");
277 Dmsg2(100, "recurse tree_relcwd with path=%s, cd=%s\n", p+1, cd->fname);
278 return tree_relcwd(p+1, root, cd);
283 #ifdef BUILD_TEST_PROGRAM
285 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent);
287 static uint32_t FileIndex = 0;
289 * Simple test program for tree routines
291 int main(int argc, char *argv[])
295 char buf[MAXPATHLEN];
297 root = (TREE_ROOT *)new_tree_node(TN_ROOT);
298 root->fname = bstrdup("");
300 FillDirectoryTree("/home/kern/bacula/k", root, NULL);
302 for (node = first_tree_node(root); node; node=next_tree_node(node)) {
303 tree_getpath(node, buf, sizeof(buf));
304 Dmsg2(100, "%d: %s\n", node->FileIndex, buf);
307 node = (TREE_NODE *)root;
308 Dmsg0(000, "doing cd /home/kern/bacula/k/techlogs\n");
309 node = tree_cwd("/home/kern/bacula/k/techlogs", root, node);
311 tree_getpath(node, buf, sizeof(buf));
312 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
315 Dmsg0(000, "doing cd /home/kern/bacula/k/src/testprogs\n");
316 node = tree_cwd("/home/kern/bacula/k/src/testprogs", root, node);
318 tree_getpath(node, buf, sizeof(buf));
319 Dmsg2(100, "findex=%d: cwd=%s\n", node->FileIndex, buf);
321 Dmsg0(100, "testprogs not found.\n");
324 free_tree((TREE_NODE *)root);
329 void FillDirectoryTree(char *path, TREE_ROOT *root, TREE_NODE *parent)
331 TREE_NODE *newparent = NULL;
336 char pathbuf[MAXPATHLEN];
337 char file[MAXPATHLEN];
341 Dmsg1(100, "FillDirectoryTree: %s\n", path);
346 while ((dir = readdir(dp))) {
347 if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
350 strcpy(file, dir->d_name);
351 snprintf(pathbuf, MAXPATHLEN-1, "%s/%s", path, file);
352 if (lstat(pathbuf, &statbuf) < 0) {
353 printf("lstat() failed. ERR=%s\n", strerror(errno));
356 // printf("got file=%s, pathbuf=%s\n", file, pathbuf);
358 if (S_ISLNK(statbuf.st_mode))
359 type = TN_FILE; /* link */
360 else if (S_ISREG(statbuf.st_mode))
362 else if (S_ISDIR(statbuf.st_mode)) {
364 } else if (S_ISCHR(statbuf.st_mode))
365 type = TN_FILE; /* char dev */
366 else if (S_ISBLK(statbuf.st_mode))
367 type = TN_FILE; /* block dev */
368 else if (S_ISFIFO(statbuf.st_mode))
369 type = TN_FILE; /* fifo */
370 else if (S_ISSOCK(statbuf.st_mode))
371 type = TN_FILE; /* sock */
374 printf("Unknown file type: 0x%x\n", statbuf.st_mode);
377 Dmsg2(100, "Doing: %d %s\n", type, pathbuf);
378 node = new_tree_node(type);
379 node->FileIndex = ++FileIndex;
380 parent = insert_tree_node(pathbuf, node, root, parent);
381 if (S_ISDIR(statbuf.st_mode) && !S_ISLNK(statbuf.st_mode)) {
382 Dmsg2(100, "calling fill. pathbuf=%s, file=%s\n", pathbuf, file);
383 FillDirectoryTree(pathbuf, root, node);