Hash :
2705576b
Author :
Date :
2012-01-24T14:06:42
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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#include "clar_libgit2.h"
#include "fileops.h"
typedef struct name_data {
int count; /* return count */
char *name; /* filename */
} name_data;
typedef struct walk_data {
char *sub; /* sub-directory name */
name_data *names; /* name state data */
git_buf path;
} walk_data;
static char *top_dir = "dir-walk";
static walk_data *state_loc;
static void setup(walk_data *d)
{
name_data *n;
cl_must_pass(p_mkdir(top_dir, 0777));
cl_must_pass(p_chdir(top_dir));
if (strcmp(d->sub, ".") != 0)
cl_must_pass(p_mkdir(d->sub, 0777));
cl_git_pass(git_buf_sets(&d->path, d->sub));
state_loc = d;
for (n = d->names; n->name; n++) {
git_file fd = p_creat(n->name, 0666);
cl_assert(fd >= 0);
p_close(fd);
n->count = 0;
}
}
static void dirent_cleanup__cb(void *_d)
{
walk_data *d = _d;
name_data *n;
for (n = d->names; n->name; n++) {
cl_must_pass(p_unlink(n->name));
}
if (strcmp(d->sub, ".") != 0)
cl_must_pass(p_rmdir(d->sub));
cl_must_pass(p_chdir(".."));
cl_must_pass(p_rmdir(top_dir));
git_buf_free(&d->path);
}
static void check_counts(walk_data *d)
{
name_data *n;
for (n = d->names; n->name; n++) {
cl_assert(n->count == 1);
}
}
static int one_entry(void *state, git_buf *path)
{
walk_data *d = (walk_data *) state;
name_data *n;
if (state != state_loc)
return GIT_ERROR;
if (path != &d->path)
return GIT_ERROR;
for (n = d->names; n->name; n++) {
if (!strcmp(n->name, path->ptr)) {
n->count++;
return 0;
}
}
return GIT_ERROR;
}
static int dont_call_me(void *GIT_UNUSED(state), git_buf *GIT_UNUSED(path))
{
GIT_UNUSED_ARG(state);
GIT_UNUSED_ARG(path);
return GIT_ERROR;
}
static name_data dot_names[] = {
{ 0, "./a" },
{ 0, "./asdf" },
{ 0, "./pack-foo.pack" },
{ 0, NULL }
};
static walk_data dot = {
".",
dot_names,
GIT_BUF_INIT
};
/* make sure that the '.' folder is not traversed */
void test_core_dirent__dont_traverse_dot(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &dot);
setup(&dot);
cl_git_pass(git_path_direach(&dot.path,
one_entry,
&dot));
check_counts(&dot);
}
static name_data sub_names[] = {
{ 0, "sub/a" },
{ 0, "sub/asdf" },
{ 0, "sub/pack-foo.pack" },
{ 0, NULL }
};
static walk_data sub = {
"sub",
sub_names,
GIT_BUF_INIT
};
/* traverse a subfolder */
void test_core_dirent__traverse_subfolder(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &sub);
setup(&sub);
cl_git_pass(git_path_direach(&sub.path,
one_entry,
&sub));
check_counts(&sub);
}
static walk_data sub_slash = {
"sub/",
sub_names,
GIT_BUF_INIT
};
/* traverse a slash-terminated subfolder */
void test_core_dirent__traverse_slash_terminated_folder(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &sub_slash);
setup(&sub_slash);
cl_git_pass(git_path_direach(&sub_slash.path,
one_entry,
&sub_slash));
check_counts(&sub_slash);
}
static name_data empty_names[] = {
{ 0, NULL }
};
static walk_data empty = {
"empty",
empty_names,
GIT_BUF_INIT
};
/* make sure that empty folders are not traversed */
void test_core_dirent__dont_traverse_empty_folders(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &empty);
setup(&empty);
cl_git_pass(git_path_direach(&empty.path,
one_entry,
&empty));
check_counts(&empty);
/* make sure callback not called */
cl_git_pass(git_path_direach(&empty.path,
dont_call_me,
&empty));
}
static name_data odd_names[] = {
{ 0, "odd/.a" },
{ 0, "odd/..c" },
/* the following don't work on cygwin/win32 */
/* { 0, "odd/.b." }, */
/* { 0, "odd/..d.." }, */
{ 0, NULL }
};
static walk_data odd = {
"odd",
odd_names,
GIT_BUF_INIT
};
/* make sure that strange looking filenames ('..c') are traversed */
void test_core_dirent__traverse_weird_filenames(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &odd);
setup(&odd);
cl_git_pass(git_path_direach(&odd.path,
one_entry,
&odd));
check_counts(&odd);
}