summaryrefslogtreecommitdiffstats
path: root/lib/dpkg/t/c-treewalk.c
blob: 4cd2b8cfc861a83028cf111cfee350ede6df6883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * libdpkg - Debian packaging suite library routines
 * c-treewalk.c - test tree walk implementation
 *
 * Copyright © 2013-2016 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <dpkg/ehandle.h>
#include <dpkg/treewalk.h>

static int
treenode_type(struct treenode *node)
{
	switch (treenode_get_mode(node) & S_IFMT) {
	case S_IFREG:
		return 'f';
	case S_IFDIR:
		return 'd';
	case S_IFLNK:
		return 'l';
	case S_IFIFO:
		return 'p';
	case S_IFBLK:
		return 'b';
	case S_IFCHR:
		return 'c';
	case S_IFSOCK:
		return 's';
	default:
		return '?';
	}
}

static int
treenode_visit_meta(struct treenode *node)
{
	printf("T=%c N=%s V=%s R=%s\n",
	       treenode_type(node), treenode_get_name(node),
	       treenode_get_virtname(node), treenode_get_pathname(node));

	return 0;
}

static int
treenode_visit_virt(struct treenode *node)
{
	printf("%s\n", treenode_get_virtname(node));

	return 0;
}

static int
treenode_visit_path(struct treenode *node)
{
	printf("%s\n", treenode_get_pathname(node));

	return 0;
}

static const char *skipname;

static bool
treenode_skip(struct treenode *node)
{
	const char *absname = treenode_get_pathname(node);

	if (strcmp(absname, skipname) == 0)
		return true;

	return false;
}

static void
test_treewalk_list(const char *rootdir)
{
	const char *visitname = getenv("TREEWALK_VISIT");
	struct treewalk_funcs funcs = { NULL };

	if (visitname == NULL || strcmp(visitname, "meta") == 0)
		funcs.visit = treenode_visit_meta;
	else if (strcmp(visitname, "virt") == 0)
		funcs.visit = treenode_visit_virt;
	else if (strcmp(visitname, "path") == 0)
		funcs.visit = treenode_visit_path;
	else
		ohshit("unknown treewalk visit name");

	skipname = getenv("TREEWALK_SKIP");
	if (skipname)
		funcs.skip = treenode_skip;

	treewalk(rootdir, 0, &funcs);
}

int
main(int argc, char **argv)
{
	const char *rootdir = argv[1];

	setvbuf(stdout, NULL, _IOLBF, 0);

	push_error_context();

	if (rootdir == NULL)
		ohshit("missing treewalk root dir");

	test_treewalk_list(rootdir);

	pop_error_context(ehflag_normaltidy);

	return 0;
}