1 /*
2 * libdpkg - Debian packaging suite library routines
3 * t-treewalk.c - test tree walk implementation
4 *
5 * Copyright © 2013-2016 Guillem Jover <guillem@debian.org>
6 *
7 * This is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <compat.h>
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #include <dpkg/ehandle.h>
29 #include <dpkg/treewalk.h>
30
31 static int
treenode_type(struct treenode * node)32 treenode_type(struct treenode *node)
33 {
34 switch (treenode_get_mode(node) & S_IFMT) {
35 case S_IFREG:
36 return 'f';
37 case S_IFDIR:
38 return 'd';
39 case S_IFLNK:
40 return 'l';
41 case S_IFIFO:
42 return 'p';
43 case S_IFBLK:
44 return 'b';
45 case S_IFCHR:
46 return 'c';
47 case S_IFSOCK:
48 return 's';
49 default:
50 return '?';
51 }
52 }
53
54 static int
treenode_visit_meta(struct treenode * node)55 treenode_visit_meta(struct treenode *node)
56 {
57 printf("T=%c N=%s V=%s R=%s\n",
58 treenode_type(node), treenode_get_name(node),
59 treenode_get_virtname(node), treenode_get_pathname(node));
60
61 return 0;
62 }
63
64 static int
treenode_visit_virt(struct treenode * node)65 treenode_visit_virt(struct treenode *node)
66 {
67 printf("%s\n", treenode_get_virtname(node));
68
69 return 0;
70 }
71
72 static int
treenode_visit_path(struct treenode * node)73 treenode_visit_path(struct treenode *node)
74 {
75 printf("%s\n", treenode_get_pathname(node));
76
77 return 0;
78 }
79
80 static const char *skipname;
81
82 static bool
treenode_skip(struct treenode * node)83 treenode_skip(struct treenode *node)
84 {
85 const char *absname = treenode_get_pathname(node);
86
87 if (strcmp(absname, skipname) == 0)
88 return true;
89
90 return false;
91 }
92
93 static void
test_treewalk_list(const char * rootdir)94 test_treewalk_list(const char *rootdir)
95 {
96 const char *visitname = getenv("TREEWALK_VISIT");
97 struct treewalk_funcs funcs = { NULL };
98
99 if (visitname == NULL || strcmp(visitname, "meta") == 0)
100 funcs.visit = treenode_visit_meta;
101 else if (strcmp(visitname, "virt") == 0)
102 funcs.visit = treenode_visit_virt;
103 else if (strcmp(visitname, "path") == 0)
104 funcs.visit = treenode_visit_path;
105 else
106 ohshit("unknown treewalk visit name");
107
108 skipname = getenv("TREEWALK_SKIP");
109 if (skipname)
110 funcs.skip = treenode_skip;
111
112 treewalk(rootdir, 0, &funcs);
113 }
114
115 int
main(int argc,char ** argv)116 main(int argc, char **argv)
117 {
118 const char *rootdir = argv[1];
119
120 setvbuf(stdout, NULL, _IOLBF, 0);
121
122 push_error_context();
123
124 if (rootdir == NULL)
125 ohshit("missing treewalk root dir");
126
127 test_treewalk_list(rootdir);
128
129 pop_error_context(ehflag_normaltidy);
130
131 return 0;
132 }
133