6 typedef struct name_data
{
7 int count
; /* return count */
8 char *name
; /* filename */
11 typedef struct walk_data
{
12 char *sub
; /* sub-directory name */
13 name_data
*names
; /* name state data */
17 static char path_buffer
[GIT_PATH_MAX
];
18 static char *top_dir
= "dir-walk";
19 static walk_data
*state_loc
;
22 static int error(const char *fmt
, ...)
27 vfprintf(stderr
, fmt
, ap
);
29 fprintf(stderr
, "\n");
33 static int setup(walk_data
*d
)
37 if (gitfo_mkdir(top_dir
, 0755) < 0)
38 return error("can't mkdir(\"%s\")", top_dir
);
40 if (gitfo_chdir(top_dir
) < 0)
41 return error("can't chdir(\"%s\")", top_dir
);
43 if (strcmp(d
->sub
, ".") != 0)
44 if (gitfo_mkdir(d
->sub
, 0755) < 0)
45 return error("can't mkdir(\"%s\")", d
->sub
);
47 strcpy(path_buffer
, d
->sub
);
50 for (n
= d
->names
; n
->name
; n
++) {
51 git_file fd
= gitfo_creat(n
->name
, 0600);
52 must_be_true(fd
>= 0);
60 static int knockdown(walk_data
*d
)
64 for (n
= d
->names
; n
->name
; n
++) {
65 if (gitfo_unlink(n
->name
) < 0)
66 return error("can't unlink(\"%s\")", n
->name
);
69 if (strcmp(d
->sub
, ".") != 0)
70 if (gitfo_rmdir(d
->sub
) < 0)
71 return error("can't rmdir(\"%s\")", d
->sub
);
73 if (gitfo_chdir("..") < 0)
74 return error("can't chdir(\"..\")");
76 if (gitfo_rmdir(top_dir
) < 0)
77 return error("can't rmdir(\"%s\")", top_dir
);
82 static int check_counts(walk_data
*d
)
87 for (n
= d
->names
; n
->name
; n
++) {
89 ret
= error("count (%d, %s)", n
->count
, n
->name
);
94 static int one_entry(void *state
, char *path
)
96 walk_data
*d
= (walk_data
*) state
;
99 must_be_true(state
== state_loc
);
100 must_be_true(path
== path_buffer
);
101 for (n
= d
->names
; n
->name
; n
++) {
102 if (!strcmp(n
->name
, path
)) {
107 test_die("unexpected path \"%s\"", path
);
111 static name_data dot_names
[] = {
114 { 0, "./pack-foo.pack" },
117 static walk_data dot
= {
124 must_pass(setup(&dot
));
126 must_pass(gitfo_dirent(path_buffer
,
131 must_pass(check_counts(&dot
));
133 must_pass(knockdown(&dot
));
136 static name_data sub_names
[] = {
139 { 0, "sub/pack-foo.pack" },
142 static walk_data sub
= {
149 must_pass(setup(&sub
));
151 must_pass(gitfo_dirent(path_buffer
,
156 must_pass(check_counts(&sub
));
158 must_pass(knockdown(&sub
));
161 static walk_data sub_slash
= {
166 BEGIN_TEST(sub_slash
)
168 must_pass(setup(&sub_slash
));
170 must_pass(gitfo_dirent(path_buffer
,
175 must_pass(check_counts(&sub_slash
));
177 must_pass(knockdown(&sub_slash
));
180 static name_data empty_names
[] = {
183 static walk_data empty
= {
188 static int dont_call_me(void *state
, char *path
)
190 test_die("dont_call_me: unexpected callback!");
195 must_pass(setup(&empty
));
197 must_pass(gitfo_dirent(path_buffer
,
202 must_pass(check_counts(&empty
));
204 /* make sure callback not called */
205 must_pass(gitfo_dirent(path_buffer
,
210 must_pass(knockdown(&empty
));
213 static name_data odd_names
[] = {
216 /* the following don't work on cygwin/win32 */
217 /* { 0, "odd/.b." }, */
218 /* { 0, "odd/..d.." }, */
221 static walk_data odd
= {
228 must_pass(setup(&odd
));
230 must_pass(gitfo_dirent(path_buffer
,
235 must_pass(check_counts(&odd
));
237 must_pass(knockdown(&odd
));