5 typedef struct name_data
{
6 int count
; /* return count */
7 char *name
; /* filename */
10 typedef struct walk_data
{
11 char *sub
; /* sub-directory name */
12 name_data
*names
; /* name state data */
16 static char path_buffer
[GIT_PATH_MAX
];
17 static char *top_dir
= "dir-walk";
18 static walk_data
*state_loc
;
21 static int error(const char *fmt
, ...)
26 vfprintf(stderr
, fmt
, ap
);
28 fprintf(stderr
, "\n");
32 static int setup(walk_data
*d
)
36 if (gitfo_mkdir(top_dir
, 0755) < 0)
37 return error("can't mkdir(\"%s\")", top_dir
);
39 if (gitfo_chdir(top_dir
) < 0)
40 return error("can't chdir(\"%s\")", top_dir
);
42 if (strcmp(d
->sub
, ".") != 0)
43 if (gitfo_mkdir(d
->sub
, 0755) < 0)
44 return error("can't mkdir(\"%s\")", d
->sub
);
46 strcpy(path_buffer
, d
->sub
);
49 for (n
= d
->names
; n
->name
; n
++) {
50 git_file fd
= gitfo_creat(n
->name
, 0600);
51 must_be_true(fd
>= 0);
59 static int knockdown(walk_data
*d
)
63 for (n
= d
->names
; n
->name
; n
++) {
64 if (gitfo_unlink(n
->name
) < 0)
65 return error("can't unlink(\"%s\")", n
->name
);
68 if (strcmp(d
->sub
, ".") != 0)
69 if (gitfo_rmdir(d
->sub
) < 0)
70 return error("can't rmdir(\"%s\")", d
->sub
);
72 if (gitfo_chdir("..") < 0)
73 return error("can't chdir(\"..\")");
75 if (gitfo_rmdir(top_dir
) < 0)
76 return error("can't rmdir(\"%s\")", top_dir
);
81 static int check_counts(walk_data
*d
)
86 for (n
= d
->names
; n
->name
; n
++) {
88 ret
= error("count (%d, %s)", n
->count
, n
->name
);
93 static int one_entry(void *state
, char *path
)
95 walk_data
*d
= (walk_data
*) state
;
98 must_be_true(state
== state_loc
);
99 must_be_true(path
== path_buffer
);
100 for (n
= d
->names
; n
->name
; n
++) {
101 if (!strcmp(n
->name
, path
)) {
106 test_die("unexpected path \"%s\"", path
);
110 static name_data dot_names
[] = {
113 { 0, "./pack-foo.pack" },
116 static walk_data dot
= {
123 must_pass(setup(&dot
));
125 must_pass(gitfo_dirent(path_buffer
,
130 must_pass(check_counts(&dot
));
132 must_pass(knockdown(&dot
));
135 static name_data sub_names
[] = {
138 { 0, "sub/pack-foo.pack" },
141 static walk_data sub
= {
148 must_pass(setup(&sub
));
150 must_pass(gitfo_dirent(path_buffer
,
155 must_pass(check_counts(&sub
));
157 must_pass(knockdown(&sub
));
160 static walk_data sub_slash
= {
165 BEGIN_TEST(sub_slash
)
167 must_pass(setup(&sub_slash
));
169 must_pass(gitfo_dirent(path_buffer
,
174 must_pass(check_counts(&sub_slash
));
176 must_pass(knockdown(&sub_slash
));
179 static name_data empty_names
[] = {
182 static walk_data empty
= {
187 static int dont_call_me(void *GIT_UNUSED(state
), char *GIT_UNUSED(path
))
189 GIT_UNUSED_ARG(state
)
191 test_die("dont_call_me: unexpected callback!");
196 must_pass(setup(&empty
));
198 must_pass(gitfo_dirent(path_buffer
,
203 must_pass(check_counts(&empty
));
205 /* make sure callback not called */
206 must_pass(gitfo_dirent(path_buffer
,
211 must_pass(knockdown(&empty
));
214 static name_data odd_names
[] = {
217 /* the following don't work on cygwin/win32 */
218 /* { 0, "odd/.b." }, */
219 /* { 0, "odd/..d.." }, */
222 static walk_data odd
= {
229 must_pass(setup(&odd
));
231 must_pass(gitfo_dirent(path_buffer
,
236 must_pass(check_counts(&odd
));
238 must_pass(knockdown(&odd
));