]>
git.proxmox.com Git - libgit2.git/blob - tests/core/dirent.c
08e0b11cf16368be6551a7a6f63ac3d4b706cfc4
1 #include "clar_libgit2.h"
4 typedef struct name_data
{
5 int count
; /* return count */
6 char *name
; /* filename */
9 typedef struct walk_data
{
10 char *sub
; /* sub-directory name */
11 name_data
*names
; /* name state data */
16 static char *top_dir
= "dir-walk";
17 static walk_data
*state_loc
;
19 static void setup(walk_data
*d
)
23 cl_must_pass(p_mkdir(top_dir
, 0777));
25 cl_must_pass(p_chdir(top_dir
));
27 if (strcmp(d
->sub
, ".") != 0)
28 cl_must_pass(p_mkdir(d
->sub
, 0777));
30 cl_git_pass(git_buf_sets(&d
->path
, d
->sub
));
34 for (n
= d
->names
; n
->name
; n
++) {
35 git_file fd
= p_creat(n
->name
, 0666);
42 static void dirent_cleanup__cb(void *_d
)
47 for (n
= d
->names
; n
->name
; n
++) {
48 cl_must_pass(p_unlink(n
->name
));
51 if (strcmp(d
->sub
, ".") != 0)
52 cl_must_pass(p_rmdir(d
->sub
));
54 cl_must_pass(p_chdir(".."));
56 cl_must_pass(p_rmdir(top_dir
));
58 git_buf_dispose(&d
->path
);
61 static void check_counts(walk_data
*d
)
65 for (n
= d
->names
; n
->name
; n
++) {
66 cl_assert(n
->count
== 1);
70 static int update_count(name_data
*data
, const char *name
)
74 for (n
= data
; n
->name
; n
++) {
75 if (!strcmp(n
->name
, name
)) {
84 static int one_entry(void *state
, git_buf
*path
)
86 walk_data
*d
= (walk_data
*) state
;
88 if (state
!= state_loc
)
94 return update_count(d
->names
, path
->ptr
);
98 static name_data dot_names
[] = {
101 { 0, "./pack-foo.pack" },
104 static walk_data dot
= {
110 /* make sure that the '.' folder is not traversed */
111 void test_core_dirent__dont_traverse_dot(void)
113 cl_set_cleanup(&dirent_cleanup__cb
, &dot
);
116 cl_git_pass(git_path_direach(&dot
.path
, 0, one_entry
, &dot
));
122 static name_data sub_names
[] = {
125 { 0, "sub/pack-foo.pack" },
128 static walk_data sub
= {
134 /* traverse a subfolder */
135 void test_core_dirent__traverse_subfolder(void)
137 cl_set_cleanup(&dirent_cleanup__cb
, &sub
);
140 cl_git_pass(git_path_direach(&sub
.path
, 0, one_entry
, &sub
));
146 static walk_data sub_slash
= {
152 /* traverse a slash-terminated subfolder */
153 void test_core_dirent__traverse_slash_terminated_folder(void)
155 cl_set_cleanup(&dirent_cleanup__cb
, &sub_slash
);
158 cl_git_pass(git_path_direach(&sub_slash
.path
, 0, one_entry
, &sub_slash
));
160 check_counts(&sub_slash
);
164 static name_data empty_names
[] = {
167 static walk_data empty
= {
173 /* make sure that empty folders are not traversed */
174 void test_core_dirent__dont_traverse_empty_folders(void)
176 cl_set_cleanup(&dirent_cleanup__cb
, &empty
);
179 cl_git_pass(git_path_direach(&empty
.path
, 0, one_entry
, &empty
));
181 check_counts(&empty
);
183 /* make sure callback not called */
184 cl_assert(git_path_is_empty_dir(empty
.path
.ptr
));
187 static name_data odd_names
[] = {
190 /* the following don't work on cygwin/win32 */
191 /* { 0, "odd/.b." }, */
192 /* { 0, "odd/..d.." }, */
195 static walk_data odd
= {
201 /* make sure that strange looking filenames ('..c') are traversed */
202 void test_core_dirent__traverse_weird_filenames(void)
204 cl_set_cleanup(&dirent_cleanup__cb
, &odd
);
207 cl_git_pass(git_path_direach(&odd
.path
, 0, one_entry
, &odd
));
212 /* test filename length limits */
213 void test_core_dirent__length_limits(void)
215 char *big_filename
= (char *)git__malloc(FILENAME_MAX
+ 1);
216 memset(big_filename
, 'a', FILENAME_MAX
+ 1);
217 big_filename
[FILENAME_MAX
] = 0;
219 cl_must_fail(p_creat(big_filename
, 0666));
221 git__free(big_filename
);
224 void test_core_dirent__empty_dir(void)
226 cl_must_pass(p_mkdir("empty_dir", 0777));
227 cl_assert(git_path_is_empty_dir("empty_dir"));
229 cl_git_mkfile("empty_dir/content", "whatever\n");
230 cl_assert(!git_path_is_empty_dir("empty_dir"));
231 cl_assert(!git_path_is_empty_dir("empty_dir/content"));
233 cl_must_pass(p_unlink("empty_dir/content"));
235 cl_must_pass(p_mkdir("empty_dir/content", 0777));
236 cl_assert(!git_path_is_empty_dir("empty_dir"));
237 cl_assert(git_path_is_empty_dir("empty_dir/content"));
239 cl_must_pass(p_rmdir("empty_dir/content"));
241 cl_must_pass(p_rmdir("empty_dir"));
244 static void handle_next(git_path_diriter
*diriter
, walk_data
*walk
)
246 const char *fullpath
, *filename
;
247 size_t fullpath_len
, filename_len
;
249 cl_git_pass(git_path_diriter_fullpath(&fullpath
, &fullpath_len
, diriter
));
250 cl_git_pass(git_path_diriter_filename(&filename
, &filename_len
, diriter
));
252 cl_assert_equal_strn(fullpath
, "sub/", 4);
253 cl_assert_equal_s(fullpath
+4, filename
);
255 update_count(walk
->names
, fullpath
);
258 /* test directory iterator */
259 void test_core_dirent__diriter_with_fullname(void)
261 git_path_diriter diriter
= GIT_PATH_DIRITER_INIT
;
264 cl_set_cleanup(&dirent_cleanup__cb
, &sub
);
267 cl_git_pass(git_path_diriter_init(&diriter
, sub
.path
.ptr
, 0));
269 while ((error
= git_path_diriter_next(&diriter
)) == 0)
270 handle_next(&diriter
, &sub
);
272 cl_assert_equal_i(error
, GIT_ITEROVER
);
274 git_path_diriter_free(&diriter
);
279 void test_core_dirent__diriter_at_directory_root(void)
281 git_path_diriter diriter
= GIT_PATH_DIRITER_INIT
;
282 const char *sandbox_path
, *path
;
285 int root_offset
, error
;
287 sandbox_path
= clar_sandbox_path();
288 cl_assert((root_offset
= git_path_root(sandbox_path
)) >= 0);
290 cl_assert(root_path
= git__calloc(1, root_offset
+ 2));
291 strncpy(root_path
, sandbox_path
, root_offset
+ 1);
293 cl_git_pass(git_path_diriter_init(&diriter
, root_path
, 0));
295 while ((error
= git_path_diriter_next(&diriter
)) == 0) {
296 cl_git_pass(git_path_diriter_fullpath(&path
, &path_len
, &diriter
));
298 cl_assert(path_len
> (size_t)(root_offset
+ 1));
299 cl_assert(path
[root_offset
+1] != '/');
302 cl_assert_equal_i(error
, GIT_ITEROVER
);
304 git_path_diriter_free(&diriter
);
305 git__free(root_path
);