From ce8cd006ce3adcd012a38401b8a7555ba3307b3f Mon Sep 17 00:00:00 2001 From: Brodie Rao Date: Wed, 7 Sep 2011 15:32:44 -0700 Subject: 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. --- tests/test_helpers.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) (limited to 'tests/test_helpers.c') diff --git a/tests/test_helpers.c b/tests/test_helpers.c index 0900430e1..7074e4822 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -42,7 +42,7 @@ int write_object_data(char *file, void *data, size_t len) int write_object_files(const char *odb_dir, object_data *d) { - if (p_mkdir(odb_dir, 0755) < 0) { + if (p_mkdir(odb_dir, GIT_OBJECT_DIR_MODE) < 0) { int err = errno; fprintf(stderr, "can't make directory \"%s\"", odb_dir); if (err == EEXIST) @@ -51,7 +51,7 @@ int write_object_files(const char *odb_dir, object_data *d) return -1; } - if ((p_mkdir(d->dir, 0755) < 0) && (errno != EEXIST)) { + if ((p_mkdir(d->dir, GIT_OBJECT_DIR_MODE) < 0) && (errno != EEXIST)) { fprintf(stderr, "can't make object directory \"%s\"\n", d->dir); return -1; } @@ -82,7 +82,7 @@ int remove_object_files(const char *odb_dir, object_data *d) return 0; } -int remove_loose_object(const char *repository_folder, git_object *object) +void locate_loose_object(const char *repository_folder, git_object *object, char **out, char **out_folder) { static const char *objects_folder = "objects/"; @@ -104,6 +104,40 @@ int remove_loose_object(const char *repository_folder, git_object *object) ptr += GIT_OID_HEXSZ + 1; *ptr = 0; + *out = full_path; + + if (out_folder) + *out_folder = top_folder; +} + +int loose_object_dir_mode(const char *repository_folder, git_object *object) +{ + char *object_path; + size_t pos; + struct stat st; + + locate_loose_object(repository_folder, object, &object_path, NULL); + + pos = strlen(object_path); + while (pos--) { + if (object_path[pos] == '/') { + object_path[pos] = 0; + break; + } + } + + assert(p_stat(object_path, &st) == 0); + free(object_path); + + return st.st_mode; +} + +int remove_loose_object(const char *repository_folder, git_object *object) +{ + char *full_path, *top_folder; + + locate_loose_object(repository_folder, object, &full_path, &top_folder); + if (p_unlink(full_path) < 0) { fprintf(stderr, "can't delete object file \"%s\"\n", full_path); return -1; @@ -141,7 +175,7 @@ int copy_file(const char *src, const char *dst) if (git_futils_readbuffer(&source_buf, src) < GIT_SUCCESS) return GIT_ENOTFOUND; - dst_fd = git_futils_creat_withpath(dst, 0644); + dst_fd = git_futils_creat_withpath(dst, 0777, 0644); if (dst_fd < 0) goto cleanup; -- cgit v1.2.1 From 01ad7b3a9ec8f5e465f94c2704e1e96b84f941c7 Mon Sep 17 00:00:00 2001 From: Brodie Rao Date: Tue, 6 Sep 2011 15:48:45 -0700 Subject: *: correct and codify various file permissions The following files now have 0444 permissions: - loose objects - pack indexes - pack files - packs downloaded by fetch - packs downloaded by the HTTP transport And the following files now have 0666 permissions: - config files - repository indexes - reflogs - refs This brings libgit2 more in line with Git. Note that git_filebuf_commit() and git_filebuf_commit_at() have both gained a new mode parameter. The latter change fixes an important issue where filebufs created with GIT_FILEBUF_TEMPORARY received 0600 permissions (due to mkstemp(3) usage). Now we chmod() the file before renaming it into place. Tests have been added to confirm that new commit, tag, and tree objects are created with the right permissions. I don't have access to Windows, so for now I've guarded the tests with "#ifndef GIT_WIN32". --- tests/test_helpers.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'tests/test_helpers.c') diff --git a/tests/test_helpers.c b/tests/test_helpers.c index 7074e4822..7bba2e1d2 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -110,6 +110,18 @@ void locate_loose_object(const char *repository_folder, git_object *object, char *out_folder = top_folder; } +int loose_object_mode(const char *repository_folder, git_object *object) +{ + char *object_path; + struct stat st; + + locate_loose_object(repository_folder, object, &object_path, NULL); + assert(p_stat(object_path, &st) == 0); + free(object_path); + + return st.st_mode; +} + int loose_object_dir_mode(const char *repository_folder, git_object *object) { char *object_path; @@ -175,7 +187,7 @@ int copy_file(const char *src, const char *dst) if (git_futils_readbuffer(&source_buf, src) < GIT_SUCCESS) return GIT_ENOTFOUND; - dst_fd = git_futils_creat_withpath(dst, 0777, 0644); + dst_fd = git_futils_creat_withpath(dst, 0777, 0666); if (dst_fd < 0) goto cleanup; -- cgit v1.2.1 From 3286c408eccb18c525ca123383f3ebf5097441bc Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Fri, 28 Oct 2011 14:51:13 -0700 Subject: global: Properly use `git__` memory wrappers Ensure that all memory related functions (malloc, calloc, strdup, free, etc) are using their respective `git__` wrappers. --- tests/test_helpers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_helpers.c') diff --git a/tests/test_helpers.c b/tests/test_helpers.c index cb95607e1..d1d7c9ebd 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -116,7 +116,7 @@ int remove_loose_object(const char *repository_folder, git_object *object) return -1; } - free(full_path); + git__free(full_path); return GIT_SUCCESS; } -- cgit v1.2.1 From ec9079443c874fe1711acefb22b3fc606484c786 Mon Sep 17 00:00:00 2001 From: schu Date: Sun, 30 Oct 2011 13:48:00 +0100 Subject: test_helpers: do not rely on assert The functions loose_object_mode and loose_object_dir_mode call stat inside an assert statement which isn't evaluated when compiling in Release mode (NDEBUG) and leads to failing tests. Replace it. Signed-off-by: schu --- tests/test_helpers.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'tests/test_helpers.c') diff --git a/tests/test_helpers.c b/tests/test_helpers.c index 47a0b1b11..31e38bf6a 100644 --- a/tests/test_helpers.c +++ b/tests/test_helpers.c @@ -116,7 +116,8 @@ int loose_object_mode(const char *repository_folder, git_object *object) struct stat st; locate_loose_object(repository_folder, object, &object_path, NULL); - assert(p_stat(object_path, &st) == 0); + if (p_stat(object_path, &st) < 0) + return 0; free(object_path); return st.st_mode; @@ -138,7 +139,8 @@ int loose_object_dir_mode(const char *repository_folder, git_object *object) } } - assert(p_stat(object_path, &st) == 0); + if (p_stat(object_path, &st) < 0) + return 0; free(object_path); return st.st_mode; -- cgit v1.2.1