libgit2/tests-clay/core/dirent.c
Brodie Rao ce8cd006ce fileops/repository: create (most) directories with 0777 permissions
To further match how Git behaves, this change makes most of the
directories libgit2 creates in a git repo have a file mode of
0777. Specifically:

- Intermediate directories created with git_futils_mkpath2file() have
  0777 permissions. This affects odb_loose, reflog, and refs.

- The top level folder for bare repos is created with 0777
  permissions.

- The top level folder for non-bare repos is created with 0755
  permissions.

- /objects/info/, /objects/pack/, /refs/heads/, and /refs/tags/ are
  created with 0777 permissions.

Additionally, the following changes have been made:

- fileops functions that create intermediate directories have grown a
  new dirmode parameter. The only exception to this is filebuf's
  lock_file(), which unconditionally creates intermediate directories
  with 0777 permissions when GIT_FILEBUF_FORCE is set.

- The test runner now sets the umask to 0 before running any
  tests. This ensurses all file mode checks are consistent across
  systems.

- t09-tree.c now does a directory permissions check. I've avoided
  adding this check to other tests that might reuse existing
  directories from the prefabricated test repos. Because they're
  checked into the repo, they have 0755 permissions.

- Other assorted directories created by tests have 0777 permissions.
2011-10-14 16:04:34 -07:00

223 lines
3.9 KiB
C

#include "clay_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 */
} walk_data;
static char path_buffer[GIT_PATH_MAX];
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));
strcpy(path_buffer, d->sub);
state_loc = d;
for (n = d->names; n->name; n++) {
git_file fd = p_creat(n->name, 0600);
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));
}
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, char *path)
{
walk_data *d = (walk_data *) state;
name_data *n;
if (state != state_loc)
return GIT_ERROR;
if (path != path_buffer)
return GIT_ERROR;
for (n = d->names; n->name; n++) {
if (!strcmp(n->name, path)) {
n->count++;
return 0;
}
}
return GIT_ERROR;
}
static int dont_call_me(void *GIT_UNUSED(state), char *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
};
/* 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_futils_direach(path_buffer,
sizeof(path_buffer),
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
};
/* traverse a subfolder */
void test_core_dirent__traverse_subfolder(void)
{
cl_set_cleanup(&dirent_cleanup__cb, &sub);
setup(&sub);
cl_git_pass(git_futils_direach(path_buffer,
sizeof(path_buffer),
one_entry,
&sub));
check_counts(&sub);
}
static walk_data sub_slash = {
"sub/",
sub_names
};
/* 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_futils_direach(path_buffer,
sizeof(path_buffer),
one_entry,
&sub_slash));
check_counts(&sub_slash);
}
static name_data empty_names[] = {
{ 0, NULL }
};
static walk_data empty = {
"empty",
empty_names
};
/* 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_futils_direach(path_buffer,
sizeof(path_buffer),
one_entry,
&empty));
check_counts(&empty);
/* make sure callback not called */
cl_git_pass(git_futils_direach(path_buffer,
sizeof(path_buffer),
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
};
/* 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_futils_direach(path_buffer,
sizeof(path_buffer),
one_entry,
&odd));
check_counts(&odd);
}