aboutsummaryrefslogtreecommitdiff
path: root/lib/util/test/dir_tree_iterator2.c
blob: 4b2b6f1c05204fbbe08ffe5a4ee7cb74f0667d81 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * dir_tree_iterator2.c
 *
 * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "util/dir_tree_iterator.h"
#include "sqfs/error.h"
#include "util/test.h"
#include "compat.h"

static int compare_entries(const void *a, const void *b)
{
	const dir_entry_t *const *lhs = a;
	const dir_entry_t *const *rhs = b;

	return strcmp((*lhs)->name, (*rhs)->name);
}

int main(int argc, char **argv)
{
	dir_entry_t *ent[17];
	dir_iterator_t *dir;
	dir_tree_cfg_t cfg;
	size_t i;
	int ret;
	(void)argc; (void)argv;

	/********** without files **********/
	memset(&cfg, 0, sizeof(cfg));
	cfg.flags |= DIR_SCAN_NO_FILE;

	dir = dir_tree_iterator_create(TEST_PATH, &cfg);
	TEST_NOT_NULL(dir);

	for (i = 0; i < 4; ++i) {
		ret = dir->next(dir, &ent[i]);
		TEST_NOT_NULL(ent[i]);
		TEST_EQUAL_I(ret, 0);
		printf("READ %s\n", ent[i]->name);
	}

	ret = dir->next(dir, &ent[4]);
	TEST_NULL(ent[4]);
	TEST_ASSERT(ret > 0);

	dir = sqfs_drop(dir);

	qsort(ent, 4, sizeof(ent[0]), compare_entries);

	printf("After sort:\n");
	for (i = 0; i < 4; ++i)
		printf("%s\n", ent[i]->name);

	TEST_STR_EQUAL(ent[0]->name, "dira");
	TEST_ASSERT(S_ISDIR(ent[0]->mode));
	TEST_STR_EQUAL(ent[1]->name, "dirb");
	TEST_ASSERT(S_ISDIR(ent[1]->mode));
	TEST_STR_EQUAL(ent[2]->name, "dirb/dirx");
	TEST_ASSERT(S_ISDIR(ent[2]->mode));
	TEST_STR_EQUAL(ent[3]->name, "dirc");
	TEST_ASSERT(S_ISDIR(ent[3]->mode));

	for (i = 0; i < 4; ++i)
		free(ent[i]);

	/********** recursive but without dirs **********/
	memset(&cfg, 0, sizeof(cfg));
	cfg.flags |= DIR_SCAN_NO_DIR;

	dir = dir_tree_iterator_create(TEST_PATH, &cfg);
	TEST_NOT_NULL(dir);

	for (i = 0; i < 12; ++i) {
		ret = dir->next(dir, &ent[i]);
		TEST_NOT_NULL(ent[i]);
		TEST_EQUAL_I(ret, 0);
		printf("READ %s\n", ent[i]->name);
	}

	ret = dir->next(dir, &ent[12]);
	TEST_NULL(ent[12]);
	TEST_ASSERT(ret > 0);

	dir = sqfs_drop(dir);

	qsort(ent, 12, sizeof(ent[0]), compare_entries);

	printf("After sort:\n");
	for (i = 0; i < 12; ++i)
		printf("%s\n", ent[i]->name);

	TEST_STR_EQUAL(ent[0]->name, "dira/file_a0");
	TEST_ASSERT(S_ISREG(ent[0]->mode));
	TEST_STR_EQUAL(ent[1]->name, "dira/file_a1");
	TEST_ASSERT(S_ISREG(ent[1]->mode));
	TEST_STR_EQUAL(ent[2]->name, "dira/file_a2");
	TEST_ASSERT(S_ISREG(ent[2]->mode));
	TEST_STR_EQUAL(ent[3]->name, "dirb/dirx/file_x0");
	TEST_ASSERT(S_ISREG(ent[3]->mode));
	TEST_STR_EQUAL(ent[4]->name, "dirb/dirx/file_x1");
	TEST_ASSERT(S_ISREG(ent[4]->mode));
	TEST_STR_EQUAL(ent[5]->name, "dirb/dirx/file_x2");
	TEST_ASSERT(S_ISREG(ent[5]->mode));
	TEST_STR_EQUAL(ent[6]->name, "dirb/file_b0");
	TEST_ASSERT(S_ISREG(ent[6]->mode));
	TEST_STR_EQUAL(ent[7]->name, "dirb/file_b1");
	TEST_ASSERT(S_ISREG(ent[7]->mode));
	TEST_STR_EQUAL(ent[8]->name, "dirb/file_b2");
	TEST_ASSERT(S_ISREG(ent[8]->mode));
	TEST_STR_EQUAL(ent[9]->name, "dirc/file_c0");
	TEST_ASSERT(S_ISREG(ent[9]->mode));
	TEST_STR_EQUAL(ent[10]->name, "dirc/file_c1");
	TEST_ASSERT(S_ISREG(ent[10]->mode));
	TEST_STR_EQUAL(ent[11]->name, "dirc/file_c2");
	TEST_ASSERT(S_ISREG(ent[11]->mode));

	for (i = 0; i < 12; ++i)
		free(ent[i]);

	/********** non-recursive **********/
	memset(&cfg, 0, sizeof(cfg));
	cfg.flags |= DIR_SCAN_NO_RECURSION;

	dir = dir_tree_iterator_create(TEST_PATH, &cfg);
	TEST_NOT_NULL(dir);

	for (i = 0; i < 3; ++i) {
		ret = dir->next(dir, &ent[i]);
		TEST_NOT_NULL(ent[i]);
		TEST_EQUAL_I(ret, 0);
		printf("READ %s\n", ent[i]->name);
	}

	ret = dir->next(dir, &ent[3]);
	TEST_NULL(ent[3]);
	TEST_ASSERT(ret > 0);

	dir = sqfs_drop(dir);

	qsort(ent, 3, sizeof(ent[0]), compare_entries);

	printf("After sort:\n");
	for (i = 0; i < 3; ++i)
		printf("%s\n", ent[i]->name);

	TEST_STR_EQUAL(ent[0]->name, "dira");
	TEST_ASSERT(S_ISDIR(ent[0]->mode));
	TEST_STR_EQUAL(ent[1]->name, "dirb");
	TEST_ASSERT(S_ISDIR(ent[1]->mode));
	TEST_STR_EQUAL(ent[2]->name, "dirc");
	TEST_ASSERT(S_ISDIR(ent[2]->mode));

	for (i = 0; i < 3; ++i)
		free(ent[i]);

	return EXIT_SUCCESS;
}