From 33127043b3ef8dd8dd695ba3613eaa104a80a56b Mon Sep 17 00:00:00 2001 From: Brodie Rao Date: Fri, 14 Oct 2011 14:18:02 -0700 Subject: [PATCH 1/3] fileops/posix: replace usage of "int mode" with "mode_t mode" Note: Functions exported from fileops take const mode_t, while the underlying POSIX wrappers take mode_t. --- src/fileops.c | 10 +++++----- src/fileops.h | 8 ++++---- src/posix.c | 2 +- src/posix.h | 2 +- src/repository.c | 2 +- src/win32/posix.h | 8 ++++---- src/win32/posix_w32.c | 8 ++++---- tests/t10-refs.c | 2 +- tests/t12-repo.c | 6 +++--- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/fileops.c b/src/fileops.c index 203cce0a4..da9c55f14 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -33,7 +33,7 @@ int git_futils_mv_atomic(const char *from, const char *to) int git_futils_mkpath2file(const char *file_path) { - const int mode = 0755; /* or 0777 ? */ + const mode_t mode = 0755; /* or 0777 ? */ int error = GIT_SUCCESS; char target_folder_path[GIT_PATH_MAX]; @@ -67,7 +67,7 @@ int git_futils_mktmp(char *path_out, const char *filename) return fd; } -int git_futils_creat_withpath(const char *path, int mode) +int git_futils_creat_withpath(const char *path, const mode_t mode) { if (git_futils_mkpath2file(path) < GIT_SUCCESS) return git__throw(GIT_EOSERR, "Failed to create file %s", path); @@ -75,13 +75,13 @@ int git_futils_creat_withpath(const char *path, int mode) return p_creat(path, mode); } -int git_futils_creat_locked(const char *path, int mode) +int git_futils_creat_locked(const char *path, const mode_t mode) { int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_EXCL, mode); return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create locked file. Could not open %s", path); } -int git_futils_creat_locked_withpath(const char *path, int mode) +int git_futils_creat_locked_withpath(const char *path, const mode_t mode) { if (git_futils_mkpath2file(path) < GIT_SUCCESS) return git__throw(GIT_EOSERR, "Failed to create locked file %s", path); @@ -289,7 +289,7 @@ int git_futils_direach( return GIT_SUCCESS; } -int git_futils_mkdir_r(const char *path, int mode) +int git_futils_mkdir_r(const char *path, const mode_t mode) { int error, root_path_offset; char *pp, *sp; diff --git a/src/fileops.h b/src/fileops.h index 5b69199d2..176888895 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -48,18 +48,18 @@ extern int git_futils_exists(const char *path); * Create and open a file, while also * creating all the folders in its path */ -extern int git_futils_creat_withpath(const char *path, int mode); +extern int git_futils_creat_withpath(const char *path, const mode_t mode); /** * Create an open a process-locked file */ -extern int git_futils_creat_locked(const char *path, int mode); +extern int git_futils_creat_locked(const char *path, const mode_t mode); /** * Create an open a process-locked file, while * also creating all the folders in its path */ -extern int git_futils_creat_locked_withpath(const char *path, int mode); +extern int git_futils_creat_locked_withpath(const char *path, const mode_t mode); /** * Check if the given path points to a directory @@ -74,7 +74,7 @@ extern int git_futils_isfile(const char *path); /** * Create a path recursively */ -extern int git_futils_mkdir_r(const char *path, int mode); +extern int git_futils_mkdir_r(const char *path, const mode_t mode); /** * Create all the folders required to contain diff --git a/src/posix.c b/src/posix.c index 1b85b053d..7cd0749b6 100644 --- a/src/posix.c +++ b/src/posix.c @@ -17,7 +17,7 @@ int p_open(const char *path, int flags) return open(path, flags | O_BINARY); } -int p_creat(const char *path, int mode) +int p_creat(const char *path, mode_t mode) { return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); } diff --git a/src/posix.h b/src/posix.h index 59bec2794..389578d5b 100644 --- a/src/posix.h +++ b/src/posix.h @@ -42,7 +42,7 @@ extern int p_write(git_file fd, const void *buf, size_t cnt); #define p_close(fd) close(fd) extern int p_open(const char *path, int flags); -extern int p_creat(const char *path, int mode); +extern int p_creat(const char *path, mode_t mode); extern int p_getcwd(char *buffer_out, size_t size); #ifndef GIT_WIN32 diff --git a/src/repository.c b/src/repository.c index 328bc0d57..36642e5ae 100644 --- a/src/repository.c +++ b/src/repository.c @@ -609,7 +609,7 @@ static int repo_init_createhead(git_repository *repo) static int repo_init_structure(const char *git_dir, int is_bare) { - const int mode = 0755; /* or 0777 ? */ + const mode_t mode = 0755; /* or 0777 ? */ int error; char temp_path[GIT_PATH_MAX]; diff --git a/src/win32/posix.h b/src/win32/posix.h index 442717e42..c0af62b1f 100644 --- a/src/win32/posix.h +++ b/src/win32/posix.h @@ -19,7 +19,7 @@ GIT_INLINE(int) p_link(const char *GIT_UNUSED(old), const char *GIT_UNUSED(new)) return -1; } -GIT_INLINE(int) p_mkdir(const char *path, int GIT_UNUSED(mode)) +GIT_INLINE(int) p_mkdir(const char *path, mode_t GIT_UNUSED(mode)) { wchar_t* buf = conv_utf8_to_utf16(path); int ret = _wmkdir(buf); @@ -41,12 +41,12 @@ extern int p_mkstemp(char *tmp_path); extern int p_setenv(const char* name, const char* value, int overwrite); extern int p_stat(const char* path, struct stat* buf); extern int p_chdir(const char* path); -extern int p_chmod(const char* path, int mode); +extern int p_chmod(const char* path, mode_t mode); extern int p_rmdir(const char* path); -extern int p_access(const char* path, int mode); +extern int p_access(const char* path, mode_t mode); extern int p_fsync(int fd); extern int p_open(const char *path, int flags); -extern int p_creat(const char *path, int mode); +extern int p_creat(const char *path, mode_t mode); extern int p_getcwd(char *buffer_out, size_t size); #endif diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c index cc17cc71f..bbc496f16 100644 --- a/src/win32/posix_w32.c +++ b/src/win32/posix_w32.c @@ -230,7 +230,7 @@ int p_open(const char *path, int flags) return fd; } -int p_creat(const char *path, int mode) +int p_creat(const char *path, mode_t mode) { int fd; wchar_t* buf = conv_utf8_to_utf16(path); @@ -268,7 +268,7 @@ int p_chdir(const char* path) return ret; } -int p_chmod(const char* path, int mode) +int p_chmod(const char* path, mode_t mode) { wchar_t* buf = conv_utf8_to_utf16(path); int ret = _wchmod(buf, mode); @@ -355,7 +355,7 @@ int p_snprintf(char *buffer, size_t count, const char *format, ...) return r; } -extern int p_creat(const char *path, int mode); +extern int p_creat(const char *path, mode_t mode); int p_mkstemp(char *tmp_path) { @@ -378,7 +378,7 @@ int p_setenv(const char* name, const char* value, int overwrite) return (SetEnvironmentVariableA(name, value) == 0 ? GIT_EOSERR : GIT_SUCCESS); } -int p_access(const char* path, int mode) +int p_access(const char* path, mode_t mode) { wchar_t *buf = conv_utf8_to_utf16(path); int ret; diff --git a/tests/t10-refs.c b/tests/t10-refs.c index c7bfe4eea..aa6735a30 100644 --- a/tests/t10-refs.c +++ b/tests/t10-refs.c @@ -431,7 +431,7 @@ END_TEST BEGIN_TEST(pack0, "create a packfile for an empty folder") git_repository *repo; char temp_path[GIT_PATH_MAX]; - const int mode = 0755; /* or 0777 ? */ + const mode_t mode = 0755; /* or 0777 ? */ must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); diff --git a/tests/t12-repo.c b/tests/t12-repo.c index de921f9ca..6884188a3 100644 --- a/tests/t12-repo.c +++ b/tests/t12-repo.c @@ -173,7 +173,7 @@ END_TEST BEGIN_TEST(init2, "Initialize and open a bare repo with a relative path escaping out of the current working directory") char path_repository[GIT_PATH_MAX]; char current_workdir[GIT_PATH_MAX]; - const int mode = 0755; /* or 0777 ? */ + const mode_t mode = 0755; /* or 0777 ? */ git_repository* repo; must_pass(p_getcwd(current_workdir, sizeof(current_workdir))); @@ -232,7 +232,7 @@ BEGIN_TEST(open2, "Open a bare repository with a relative path escaping out of t char current_workdir[GIT_PATH_MAX]; char path_repository[GIT_PATH_MAX]; - const int mode = 0755; /* or 0777 ? */ + const mode_t mode = 0755; /* or 0777 ? */ git_repository* repo; /* Setup the repository to open */ @@ -384,7 +384,7 @@ BEGIN_TEST(discover0, "test discover") char repository_path[GIT_PATH_MAX]; char sub_repository_path[GIT_PATH_MAX]; char found_path[GIT_PATH_MAX]; - int mode = 0755; + mode_t mode = 0755; git_futils_mkdir_r(DISCOVER_FOLDER, mode); must_pass(append_ceiling_dir(ceiling_dirs, TEMP_REPO_FOLDER)); From ce8cd006ce3adcd012a38401b8a7555ba3307b3f Mon Sep 17 00:00:00 2001 From: Brodie Rao Date: Wed, 7 Sep 2011 15:32:44 -0700 Subject: [PATCH 2/3] 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. --- src/filebuf.c | 3 ++- src/fileops.c | 15 +++++++------- src/fileops.h | 8 ++++---- src/odb.h | 3 +++ src/odb_loose.c | 4 ++-- src/posix.h | 1 + src/reflog.c | 2 +- src/reflog.h | 1 + src/refs.h | 1 + src/repository.c | 11 +++++------ src/repository.h | 4 +++- tests-clay/core/dirent.c | 4 ++-- tests-clay/core/rmdir.c | 12 ++++++------ tests/t00-core.c | 18 ++++++++--------- tests/t03-objwrite.c | 2 +- tests/t06-index.c | 2 +- tests/t09-tree.c | 3 +++ tests/t10-refs.c | 3 +-- tests/t12-repo.c | 8 ++++---- tests/test_helpers.c | 42 ++++++++++++++++++++++++++++++++++++---- tests/test_helpers.h | 4 ++++ tests/test_main.c | 4 ++++ 22 files changed, 103 insertions(+), 52 deletions(-) diff --git a/src/filebuf.c b/src/filebuf.c index 1a98e3f43..e6167d014 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -23,7 +23,8 @@ static int lock_file(git_filebuf *file, int flags) /* create path to the file buffer is required */ if (flags & GIT_FILEBUF_FORCE) { - file->fd = git_futils_creat_locked_withpath(file->path_lock, 0644); + /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ + file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, 0644); } else { file->fd = git_futils_creat_locked(file->path_lock, 0644); } diff --git a/src/fileops.c b/src/fileops.c index da9c55f14..ff36b007e 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -31,9 +31,8 @@ int git_futils_mv_atomic(const char *from, const char *to) #endif } -int git_futils_mkpath2file(const char *file_path) +int git_futils_mkpath2file(const char *file_path, const mode_t mode) { - const mode_t mode = 0755; /* or 0777 ? */ int error = GIT_SUCCESS; char target_folder_path[GIT_PATH_MAX]; @@ -67,9 +66,9 @@ int git_futils_mktmp(char *path_out, const char *filename) return fd; } -int git_futils_creat_withpath(const char *path, const mode_t mode) +int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode) { - if (git_futils_mkpath2file(path) < GIT_SUCCESS) + if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS) return git__throw(GIT_EOSERR, "Failed to create file %s", path); return p_creat(path, mode); @@ -81,9 +80,9 @@ int git_futils_creat_locked(const char *path, const mode_t mode) return fd >= 0 ? fd : git__throw(GIT_EOSERR, "Failed to create locked file. Could not open %s", path); } -int git_futils_creat_locked_withpath(const char *path, const mode_t mode) +int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode) { - if (git_futils_mkpath2file(path) < GIT_SUCCESS) + if (git_futils_mkpath2file(path, dirmode) < GIT_SUCCESS) return git__throw(GIT_EOSERR, "Failed to create locked file %s", path); return git_futils_creat_locked(path, mode); @@ -212,9 +211,9 @@ void git_futils_freebuffer(git_fbuffer *obj) } -int git_futils_mv_withpath(const char *from, const char *to) +int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode) { - if (git_futils_mkpath2file(to) < GIT_SUCCESS) + if (git_futils_mkpath2file(to, dirmode) < GIT_SUCCESS) return GIT_EOSERR; /* The callee already takes care of setting the correct error message. */ return git_futils_mv_atomic(from, to); /* The callee already takes care of setting the correct error message. */ diff --git a/src/fileops.h b/src/fileops.h index 176888895..56c4770cb 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -48,7 +48,7 @@ extern int git_futils_exists(const char *path); * Create and open a file, while also * creating all the folders in its path */ -extern int git_futils_creat_withpath(const char *path, const mode_t mode); +extern int git_futils_creat_withpath(const char *path, const mode_t dirmode, const mode_t mode); /** * Create an open a process-locked file @@ -59,7 +59,7 @@ extern int git_futils_creat_locked(const char *path, const mode_t mode); * Create an open a process-locked file, while * also creating all the folders in its path */ -extern int git_futils_creat_locked_withpath(const char *path, const mode_t mode); +extern int git_futils_creat_locked_withpath(const char *path, const mode_t dirmode, const mode_t mode); /** * Check if the given path points to a directory @@ -80,7 +80,7 @@ extern int git_futils_mkdir_r(const char *path, const mode_t mode); * Create all the folders required to contain * the full path of a file */ -extern int git_futils_mkpath2file(const char *path); +extern int git_futils_mkpath2file(const char *path, const mode_t mode); extern int git_futils_rmdir_r(const char *path, int force); @@ -98,7 +98,7 @@ extern int git_futils_mv_atomic(const char *from, const char *to); * Move a file on the filesystem, create the * destination path if it doesn't exist */ -extern int git_futils_mv_withpath(const char *from, const char *to); +extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); /** diff --git a/src/odb.h b/src/odb.h index 4e850916b..7c8c9f9e2 100644 --- a/src/odb.h +++ b/src/odb.h @@ -14,6 +14,9 @@ #include "vector.h" #include "cache.h" +#define GIT_OBJECTS_DIR "objects/" +#define GIT_OBJECT_DIR_MODE 0777 + /* DO NOT EXPORT */ typedef struct { void *data; /**< Raw, decompressed object data. */ diff --git a/src/odb_loose.c b/src/odb_loose.c index 80f0aa9e7..a3013d3dd 100644 --- a/src/odb_loose.c +++ b/src/odb_loose.c @@ -666,7 +666,7 @@ static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream) if (object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid)) return GIT_ENOMEM; - if ((error = git_futils_mkpath2file(final_path)) < GIT_SUCCESS) + if ((error = git_futils_mkpath2file(final_path, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS) return git__rethrow(error, "Failed to write loose backend"); stream->finished = 1; @@ -787,7 +787,7 @@ static int loose_backend__write(git_oid *oid, git_odb_backend *_backend, const v if ((error = object_file_name(final_path, sizeof(final_path), backend->objects_dir, oid)) < GIT_SUCCESS) goto cleanup; - if ((error = git_futils_mkpath2file(final_path)) < GIT_SUCCESS) + if ((error = git_futils_mkpath2file(final_path, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS) goto cleanup; return git_filebuf_commit_at(&fbuf, final_path); diff --git a/src/posix.h b/src/posix.h index 389578d5b..55cd35a38 100644 --- a/src/posix.h +++ b/src/posix.h @@ -40,6 +40,7 @@ extern int p_write(git_file fd, const void *buf, size_t cnt); #define p_fstat(f,b) fstat(f, b) #define p_lseek(f,n,w) lseek(f, n, w) #define p_close(fd) close(fd) +#define p_umask(m) umask(m) extern int p_open(const char *path, int flags); extern int p_creat(const char *path, mode_t mode); diff --git a/src/reflog.c b/src/reflog.c index 594963c03..6cdb35304 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -226,7 +226,7 @@ int git_reflog_write(git_reference *ref, const git_oid *oid_old, git_path_join_n(log_path, 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name); if (git_futils_exists(log_path)) { - if ((error = git_futils_mkpath2file(log_path)) < GIT_SUCCESS) + if ((error = git_futils_mkpath2file(log_path, GIT_REFLOG_DIR_MODE)) < GIT_SUCCESS) return git__rethrow(error, "Failed to write reflog. Cannot create reflog directory"); } else if (git_futils_isfile(log_path)) { return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path); diff --git a/src/reflog.h b/src/reflog.h index 093874e51..16e9a94ec 100644 --- a/src/reflog.h +++ b/src/reflog.h @@ -12,6 +12,7 @@ #include "vector.h" #define GIT_REFLOG_DIR "logs/" +#define GIT_REFLOG_DIR_MODE 0777 #define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17) diff --git a/src/refs.h b/src/refs.h index c4b0b0e39..f802cfe4a 100644 --- a/src/refs.h +++ b/src/refs.h @@ -16,6 +16,7 @@ #define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/" #define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/" #define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/" +#define GIT_REFS_DIR_MODE 0777 #define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF" diff --git a/src/repository.c b/src/repository.c index 36642e5ae..d583cad6a 100644 --- a/src/repository.c +++ b/src/repository.c @@ -609,12 +609,11 @@ static int repo_init_createhead(git_repository *repo) static int repo_init_structure(const char *git_dir, int is_bare) { - const mode_t mode = 0755; /* or 0777 ? */ int error; char temp_path[GIT_PATH_MAX]; - if (git_futils_mkdir_r(git_dir, mode)) + if (git_futils_mkdir_r(git_dir, is_bare ? GIT_BARE_DIR_MODE : GIT_DIR_MODE)) return git__throw(GIT_ERROR, "Failed to initialize repository structure. Could not mkdir"); /* Hides the ".git" directory */ @@ -628,25 +627,25 @@ static int repo_init_structure(const char *git_dir, int is_bare) /* Creates the '/objects/info/' directory */ git_path_join(temp_path, git_dir, GIT_OBJECTS_INFO_DIR); - error = git_futils_mkdir_r(temp_path, mode); + error = git_futils_mkdir_r(temp_path, GIT_OBJECT_DIR_MODE); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to initialize repository structure"); /* Creates the '/objects/pack/' directory */ git_path_join(temp_path, git_dir, GIT_OBJECTS_PACK_DIR); - error = p_mkdir(temp_path, mode); + error = p_mkdir(temp_path, GIT_OBJECT_DIR_MODE); if (error < GIT_SUCCESS) return git__throw(error, "Unable to create `%s` folder", temp_path); /* Creates the '/refs/heads/' directory */ git_path_join(temp_path, git_dir, GIT_REFS_HEADS_DIR); - error = git_futils_mkdir_r(temp_path, mode); + error = git_futils_mkdir_r(temp_path, GIT_REFS_DIR_MODE); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to initialize repository structure"); /* Creates the '/refs/tags/' directory */ git_path_join(temp_path, git_dir, GIT_REFS_TAGS_DIR); - error = p_mkdir(temp_path, mode); + error = p_mkdir(temp_path, GIT_REFS_DIR_MODE); if (error < GIT_SUCCESS) return git__throw(error, "Unable to create `%s` folder", temp_path); diff --git a/src/repository.h b/src/repository.h index 99217e5a4..a12dd9da0 100644 --- a/src/repository.h +++ b/src/repository.h @@ -18,10 +18,12 @@ #include "cache.h" #include "refs.h" #include "buffer.h" +#include "odb.h" #define DOT_GIT ".git" #define GIT_DIR DOT_GIT "/" -#define GIT_OBJECTS_DIR "objects/" +#define GIT_DIR_MODE 0755 +#define GIT_BARE_DIR_MODE 0777 #define GIT_INDEX_FILE "index" struct git_object { diff --git a/tests-clay/core/dirent.c b/tests-clay/core/dirent.c index 73f571595..898b1227b 100644 --- a/tests-clay/core/dirent.c +++ b/tests-clay/core/dirent.c @@ -20,12 +20,12 @@ static void setup(walk_data *d) { name_data *n; - cl_must_pass(p_mkdir(top_dir, 0755)); + 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, 0755)); + cl_must_pass(p_mkdir(d->sub, 0777)); strcpy(path_buffer, d->sub); state_loc = d; diff --git a/tests-clay/core/rmdir.c b/tests-clay/core/rmdir.c index aa21c6a3d..03baecf23 100644 --- a/tests-clay/core/rmdir.c +++ b/tests-clay/core/rmdir.c @@ -7,22 +7,22 @@ void test_core_rmdir__initialize(void) { char path[GIT_PATH_MAX]; - cl_must_pass(p_mkdir(empty_tmp_dir, 0755)); + cl_must_pass(p_mkdir(empty_tmp_dir, 0777)); git_path_join(path, empty_tmp_dir, "/one"); - cl_must_pass(p_mkdir(path, 0755)); + cl_must_pass(p_mkdir(path, 0777)); git_path_join(path, empty_tmp_dir, "/one/two_one"); - cl_must_pass(p_mkdir(path, 0755)); + cl_must_pass(p_mkdir(path, 0777)); git_path_join(path, empty_tmp_dir, "/one/two_two"); - cl_must_pass(p_mkdir(path, 0755)); + cl_must_pass(p_mkdir(path, 0777)); git_path_join(path, empty_tmp_dir, "/one/two_two/three"); - cl_must_pass(p_mkdir(path, 0755)); + cl_must_pass(p_mkdir(path, 0777)); git_path_join(path, empty_tmp_dir, "/two"); - cl_must_pass(p_mkdir(path, 0755)); + cl_must_pass(p_mkdir(path, 0777)); } /* make sure empty dir can be deleted recusively */ diff --git a/tests/t00-core.c b/tests/t00-core.c index 703504bf8..185681562 100644 --- a/tests/t00-core.c +++ b/tests/t00-core.c @@ -250,14 +250,14 @@ static int setup(walk_data *d) { name_data *n; - if (p_mkdir(top_dir, 0755) < 0) + if (p_mkdir(top_dir, 0777) < 0) return error("can't mkdir(\"%s\")", top_dir); if (p_chdir(top_dir) < 0) return error("can't chdir(\"%s\")", top_dir); if (strcmp(d->sub, ".") != 0) - if (p_mkdir(d->sub, 0755) < 0) + if (p_mkdir(d->sub, 0777) < 0) return error("can't mkdir(\"%s\")", d->sub); strcpy(path_buffer, d->sub); @@ -510,27 +510,27 @@ static int setup_empty_tmp_dir(void) { char path[GIT_PATH_MAX]; - if (p_mkdir(empty_tmp_dir, 0755)) + if (p_mkdir(empty_tmp_dir, 0777)) return -1; git_path_join(path, empty_tmp_dir, "/one"); - if (p_mkdir(path, 0755)) + if (p_mkdir(path, 0777)) return -1; git_path_join(path, empty_tmp_dir, "/one/two_one"); - if (p_mkdir(path, 0755)) + if (p_mkdir(path, 0777)) return -1; git_path_join(path, empty_tmp_dir, "/one/two_two"); - if (p_mkdir(path, 0755)) + if (p_mkdir(path, 0777)) return -1; git_path_join(path, empty_tmp_dir, "/one/two_two/three"); - if (p_mkdir(path, 0755)) + if (p_mkdir(path, 0777)) return -1; git_path_join(path, empty_tmp_dir, "/two"); - if (p_mkdir(path, 0755)) + if (p_mkdir(path, 0777)) return -1; return 0; @@ -547,7 +547,7 @@ BEGIN_TEST(rmdir1, "make sure non-empty dir cannot be deleted recusively") must_pass(setup_empty_tmp_dir()); git_path_join(file, empty_tmp_dir, "/two/file.txt"); - fd = p_creat(file, 0755); + fd = p_creat(file, 0777); must_pass(fd); must_pass(p_close(fd)); must_fail(git_futils_rmdir_r(empty_tmp_dir, 0)); diff --git a/tests/t03-objwrite.c b/tests/t03-objwrite.c index 31f611a5c..7563d0e3a 100644 --- a/tests/t03-objwrite.c +++ b/tests/t03-objwrite.c @@ -31,7 +31,7 @@ static char *odb_dir = "test-objects"; static int make_odb_dir(void) { - 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) diff --git a/tests/t06-index.c b/tests/t06-index.c index 621e742b3..dde98abaf 100644 --- a/tests/t06-index.c +++ b/tests/t06-index.c @@ -176,7 +176,7 @@ BEGIN_TEST(add0, "add a new file to the index") must_pass(git_index_entrycount(index) == 0); /* Create a new file in the working directory */ - must_pass(git_futils_mkpath2file(TEMP_REPO_FOLDER "myrepo/test.txt")); + must_pass(git_futils_mkpath2file(TEMP_REPO_FOLDER "myrepo/test.txt", 0777)); must_pass(git_filebuf_open(&file, TEMP_REPO_FOLDER "myrepo/test.txt", 0)); must_pass(git_filebuf_write(&file, "hey there\n", 10)); must_pass(git_filebuf_commit(&file)); diff --git a/tests/t09-tree.c b/tests/t09-tree.c index 3af06ea67..1f2fe3f02 100644 --- a/tests/t09-tree.c +++ b/tests/t09-tree.c @@ -200,6 +200,9 @@ BEGIN_TEST(write3, "write a hierarchical tree from a memory") // check data is correct must_pass(git_tree_lookup(&tree, repo, &id_hiearar)); must_be_true(2 == git_tree_entrycount(tree)); +#ifndef GIT_WIN32 + must_be_true((loose_object_dir_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_DIR_MODE); +#endif git_tree_close(tree); close_temp_repo(repo); diff --git a/tests/t10-refs.c b/tests/t10-refs.c index aa6735a30..4cb31d5e7 100644 --- a/tests/t10-refs.c +++ b/tests/t10-refs.c @@ -431,12 +431,11 @@ END_TEST BEGIN_TEST(pack0, "create a packfile for an empty folder") git_repository *repo; char temp_path[GIT_PATH_MAX]; - const mode_t mode = 0755; /* or 0777 ? */ must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); git_path_join_n(temp_path, 3, repo->path_repository, GIT_REFS_HEADS_DIR, "empty_dir"); - must_pass(git_futils_mkdir_r(temp_path, mode)); + must_pass(git_futils_mkdir_r(temp_path, GIT_REFS_DIR_MODE)); must_pass(git_reference_packall(repo)); diff --git a/tests/t12-repo.c b/tests/t12-repo.c index 6884188a3..c07150c9c 100644 --- a/tests/t12-repo.c +++ b/tests/t12-repo.c @@ -173,7 +173,7 @@ END_TEST BEGIN_TEST(init2, "Initialize and open a bare repo with a relative path escaping out of the current working directory") char path_repository[GIT_PATH_MAX]; char current_workdir[GIT_PATH_MAX]; - const mode_t mode = 0755; /* or 0777 ? */ + const mode_t mode = 0777; git_repository* repo; must_pass(p_getcwd(current_workdir, sizeof(current_workdir))); @@ -232,7 +232,7 @@ BEGIN_TEST(open2, "Open a bare repository with a relative path escaping out of t char current_workdir[GIT_PATH_MAX]; char path_repository[GIT_PATH_MAX]; - const mode_t mode = 0755; /* or 0777 ? */ + const mode_t mode = 0777; git_repository* repo; /* Setup the repository to open */ @@ -351,7 +351,7 @@ static int write_file(const char *path, const char *content) return error; } - file = git_futils_creat_withpath(path, 0644); + file = git_futils_creat_withpath(path, 0777, 0644); if (file < GIT_SUCCESS) return file; @@ -384,7 +384,7 @@ BEGIN_TEST(discover0, "test discover") char repository_path[GIT_PATH_MAX]; char sub_repository_path[GIT_PATH_MAX]; char found_path[GIT_PATH_MAX]; - mode_t mode = 0755; + const mode_t mode = 0777; git_futils_mkdir_r(DISCOVER_FOLDER, mode); must_pass(append_ceiling_dir(ceiling_dirs, TEMP_REPO_FOLDER)); 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; diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 53361b7b1..2e5194d7c 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -63,6 +63,10 @@ extern int remove_object_files(const char *odb_dir, object_data *d); extern int cmp_objects(git_rawobj *o, object_data *d); +extern void locate_loose_object(const char *odb_dir, git_object *object, char **out, char **out_folder); + +extern int loose_object_dir_mode(const char *odb_dir, git_object *object); + extern int remove_loose_object(const char *odb_dir, git_object *object); extern int cmp_files(const char *a, const char *b); diff --git a/tests/test_main.c b/tests/test_main.c index c9f8da3a4..9961ffd6b 100644 --- a/tests/test_main.c +++ b/tests/test_main.c @@ -26,6 +26,8 @@ #include #include +#include "posix.h" + #include "test_lib.h" #include "test_helpers.h" @@ -81,6 +83,8 @@ main(int GIT_UNUSED(argc), char *GIT_UNUSED(argv[])) GIT_UNUSED_ARG(argc); GIT_UNUSED_ARG(argv); + p_umask(0); + failures = 0; for (i = 0; i < GIT_SUITE_COUNT; ++i) From 01ad7b3a9ec8f5e465f94c2704e1e96b84f941c7 Mon Sep 17 00:00:00 2001 From: Brodie Rao Date: Tue, 6 Sep 2011 15:48:45 -0700 Subject: [PATCH 3/3] *: 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". --- src/config.h | 1 + src/config_file.c | 2 +- src/fetch.c | 3 ++- src/filebuf.c | 17 ++++++++++++----- src/filebuf.h | 4 ++-- src/index.c | 2 +- src/index.h | 3 +++ src/indexer.c | 2 +- src/odb.h | 1 + src/odb_loose.c | 4 ++-- src/pack.h | 2 ++ src/reflog.c | 2 +- src/reflog.h | 1 + src/refs.c | 5 +++-- src/refs.h | 1 + src/repository.h | 1 - src/transports/http.c | 3 ++- tests-clay/core/dirent.c | 2 +- tests-clay/core/filebuf.c | 6 +++--- tests-clay/core/rmdir.c | 2 +- tests-clay/status/single.c | 2 +- tests/t00-core.c | 8 ++++---- tests/t04-commit.c | 4 ++++ tests/t06-index.c | 2 +- tests/t08-tag.c | 3 +++ tests/t09-tree.c | 1 + tests/t12-repo.c | 2 +- tests/t15-config.c | 2 +- tests/t18-status.c | 4 ++-- tests/test_helpers.c | 14 +++++++++++++- tests/test_helpers.h | 1 + 31 files changed, 73 insertions(+), 34 deletions(-) diff --git a/src/config.h b/src/config.h index 7749a9c1a..43574a586 100644 --- a/src/config.h +++ b/src/config.h @@ -14,6 +14,7 @@ #define GIT_CONFIG_FILENAME ".gitconfig" #define GIT_CONFIG_FILENAME_INREPO "config" +#define GIT_CONFIG_FILE_MODE 0666 struct git_config { git_vector files; diff --git a/src/config_file.c b/src/config_file.c index a85ae1578..855574d7e 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -1034,7 +1034,7 @@ static int config_write(diskfile_backend *cfg, cvar_t *var) if (error < GIT_SUCCESS) git_filebuf_cleanup(&file); else - error = git_filebuf_commit(&file); + error = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE); git_futils_freebuffer(&cfg->reader.buffer); return error; diff --git a/src/fetch.c b/src/fetch.c index ac7282819..af7dbaffd 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -14,6 +14,7 @@ #include "transport.h" #include "remote.h" #include "refspec.h" +#include "pack.h" #include "fetch.h" #include "netops.h" @@ -181,7 +182,7 @@ int git_fetch__download_pack(char **out, const char *buffered, size_t buffered_s } /* A bit dodgy, but we need to keep the pack at the temporary path */ - error = git_filebuf_commit_at(&file, file.path_lock); + error = git_filebuf_commit_at(&file, file.path_lock, GIT_PACK_FILE_MODE); cleanup: if (error < GIT_SUCCESS) git_filebuf_cleanup(&file); diff --git a/src/filebuf.c b/src/filebuf.c index e6167d014..a86d25b5a 100644 --- a/src/filebuf.c +++ b/src/filebuf.c @@ -10,6 +10,8 @@ #include "filebuf.h" #include "fileops.h" +#define GIT_LOCK_FILE_MODE 0644 + static const size_t WRITE_BUFFER_SIZE = (4096 * 2); static int lock_file(git_filebuf *file, int flags) @@ -24,9 +26,9 @@ static int lock_file(git_filebuf *file, int flags) /* create path to the file buffer is required */ if (flags & GIT_FILEBUF_FORCE) { /* XXX: Should dirmode here be configurable? Or is 0777 always fine? */ - file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, 0644); + file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, GIT_LOCK_FILE_MODE); } else { - file->fd = git_futils_creat_locked(file->path_lock, 0644); + file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE); } if (file->fd < 0) @@ -247,17 +249,17 @@ int git_filebuf_hash(git_oid *oid, git_filebuf *file) return GIT_SUCCESS; } -int git_filebuf_commit_at(git_filebuf *file, const char *path) +int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode) { free(file->path_original); file->path_original = git__strdup(path); if (file->path_original == NULL) return GIT_ENOMEM; - return git_filebuf_commit(file); + return git_filebuf_commit(file, mode); } -int git_filebuf_commit(git_filebuf *file) +int git_filebuf_commit(git_filebuf *file, mode_t mode) { int error; @@ -271,6 +273,11 @@ int git_filebuf_commit(git_filebuf *file) p_close(file->fd); file->fd = -1; + if (p_chmod(file->path_lock, mode)) { + error = git__throw(GIT_EOSERR, "Failed to chmod locked file before committing"); + goto cleanup; + } + error = git_futils_mv_atomic(file->path_lock, file->path_original); cleanup: diff --git a/src/filebuf.h b/src/filebuf.h index 525ca3c81..d08505e8d 100644 --- a/src/filebuf.h +++ b/src/filebuf.h @@ -49,8 +49,8 @@ int git_filebuf_reserve(git_filebuf *file, void **buff, size_t len); int git_filebuf_printf(git_filebuf *file, const char *format, ...) GIT_FORMAT_PRINTF(2, 3); int git_filebuf_open(git_filebuf *lock, const char *path, int flags); -int git_filebuf_commit(git_filebuf *lock); -int git_filebuf_commit_at(git_filebuf *lock, const char *path); +int git_filebuf_commit(git_filebuf *lock, mode_t mode); +int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode); void git_filebuf_cleanup(git_filebuf *lock); int git_filebuf_hash(git_oid *oid, git_filebuf *file); diff --git a/src/index.c b/src/index.c index 7bf5daf2c..2655aefa9 100644 --- a/src/index.c +++ b/src/index.c @@ -262,7 +262,7 @@ int git_index_write(git_index *index) return git__rethrow(error, "Failed to write index"); } - if ((error = git_filebuf_commit(&file)) < GIT_SUCCESS) + if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < GIT_SUCCESS) return git__rethrow(error, "Failed to write index"); if (p_stat(index->index_file_path, &indexst) == 0) { diff --git a/src/index.h b/src/index.h index e912770b7..a1cd3403e 100644 --- a/src/index.h +++ b/src/index.h @@ -14,6 +14,9 @@ #include "git2/odb.h" #include "git2/index.h" +#define GIT_INDEX_FILE "index" +#define GIT_INDEX_FILE_MODE 0666 + struct git_index { git_repository *repository; char *index_file_path; diff --git a/src/indexer.c b/src/indexer.c index d5f605fdb..6be4f4a7e 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -272,7 +272,7 @@ int git_indexer_write(git_indexer *idx) /* Figure out what the final name should be */ index_path(filename, idx); /* Commit file */ - error = git_filebuf_commit_at(&idx->file, filename); + error = git_filebuf_commit_at(&idx->file, filename, GIT_PACK_FILE_MODE); cleanup: git_mwindow_free_all(&idx->pack->mwf); diff --git a/src/odb.h b/src/odb.h index 7c8c9f9e2..833739e99 100644 --- a/src/odb.h +++ b/src/odb.h @@ -16,6 +16,7 @@ #define GIT_OBJECTS_DIR "objects/" #define GIT_OBJECT_DIR_MODE 0777 +#define GIT_OBJECT_FILE_MODE 0444 /* DO NOT EXPORT */ typedef struct { diff --git a/src/odb_loose.c b/src/odb_loose.c index a3013d3dd..dc9897288 100644 --- a/src/odb_loose.c +++ b/src/odb_loose.c @@ -670,7 +670,7 @@ static int loose_backend__stream_fwrite(git_oid *oid, git_odb_stream *_stream) return git__rethrow(error, "Failed to write loose backend"); stream->finished = 1; - return git_filebuf_commit_at(&stream->fbuf, final_path); + return git_filebuf_commit_at(&stream->fbuf, final_path, GIT_OBJECT_FILE_MODE); } static int loose_backend__stream_write(git_odb_stream *_stream, const char *data, size_t len) @@ -790,7 +790,7 @@ static int loose_backend__write(git_oid *oid, git_odb_backend *_backend, const v if ((error = git_futils_mkpath2file(final_path, GIT_OBJECT_DIR_MODE)) < GIT_SUCCESS) goto cleanup; - return git_filebuf_commit_at(&fbuf, final_path); + return git_filebuf_commit_at(&fbuf, final_path, GIT_OBJECT_FILE_MODE); cleanup: git_filebuf_cleanup(&fbuf); diff --git a/src/pack.h b/src/pack.h index 0fddd9dc8..aecf580e9 100644 --- a/src/pack.h +++ b/src/pack.h @@ -15,6 +15,8 @@ #include "mwindow.h" #include "odb.h" +#define GIT_PACK_FILE_MODE 0444 + #define PACK_SIGNATURE 0x5041434b /* "PACK" */ #define PACK_VERSION 2 #define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3)) diff --git a/src/reflog.c b/src/reflog.c index 6cdb35304..303c2b494 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -71,7 +71,7 @@ static int reflog_write(const char *log_path, const char *oid_old, } git_filebuf_write(&fbuf, log.ptr, log.size); - error = git_filebuf_commit(&fbuf); + error = git_filebuf_commit(&fbuf, GIT_REFLOG_FILE_MODE); git_buf_free(&log); return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write reflog"); diff --git a/src/reflog.h b/src/reflog.h index 16e9a94ec..44b063700 100644 --- a/src/reflog.h +++ b/src/reflog.h @@ -13,6 +13,7 @@ #define GIT_REFLOG_DIR "logs/" #define GIT_REFLOG_DIR_MODE 0777 +#define GIT_REFLOG_FILE_MODE 0666 #define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17) diff --git a/src/refs.c b/src/refs.c index fcf771b5e..b34067f00 100644 --- a/src/refs.c +++ b/src/refs.c @@ -9,6 +9,7 @@ #include "hash.h" #include "repository.h" #include "fileops.h" +#include "pack.h" #include #include @@ -357,7 +358,7 @@ static int loose_write(git_reference *ref) goto unlock; } - error = git_filebuf_commit(&file); + error = git_filebuf_commit(&file, GIT_REFS_FILE_MODE); if (p_stat(ref_path, &st) == GIT_SUCCESS) ref->mtime = st.st_mtime; @@ -870,7 +871,7 @@ cleanup: /* if we've written all the references properly, we can commit * the packfile to make the changes effective */ if (error == GIT_SUCCESS) { - error = git_filebuf_commit(&pack_file); + error = git_filebuf_commit(&pack_file, GIT_PACK_FILE_MODE); /* when and only when the packfile has been properly written, * we can go ahead and remove the loose refs */ diff --git a/src/refs.h b/src/refs.h index f802cfe4a..33c1e6983 100644 --- a/src/refs.h +++ b/src/refs.h @@ -17,6 +17,7 @@ #define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/" #define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/" #define GIT_REFS_DIR_MODE 0777 +#define GIT_REFS_FILE_MODE 0666 #define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF" diff --git a/src/repository.h b/src/repository.h index a12dd9da0..0c17958fd 100644 --- a/src/repository.h +++ b/src/repository.h @@ -24,7 +24,6 @@ #define GIT_DIR DOT_GIT "/" #define GIT_DIR_MODE 0755 #define GIT_BARE_DIR_MODE 0777 -#define GIT_INDEX_FILE "index" struct git_object { git_cached_obj cached; diff --git a/src/transports/http.c b/src/transports/http.c index 680354bae..c324bb4ab 100644 --- a/src/transports/http.c +++ b/src/transports/http.c @@ -15,6 +15,7 @@ #include "buffer.h" #include "pkt.h" #include "refs.h" +#include "pack.h" #include "fetch.h" #include "filebuf.h" #include "repository.h" @@ -702,7 +703,7 @@ static int http_download_pack(char **out, git_transport *transport, git_reposito } /* A bit dodgy, but we need to keep the pack at the temporary path */ - error = git_filebuf_commit_at(&file, file.path_lock); + error = git_filebuf_commit_at(&file, file.path_lock, GIT_PACK_FILE_MODE); cleanup: if (error < GIT_SUCCESS) diff --git a/tests-clay/core/dirent.c b/tests-clay/core/dirent.c index 898b1227b..105e8d8f0 100644 --- a/tests-clay/core/dirent.c +++ b/tests-clay/core/dirent.c @@ -31,7 +31,7 @@ static void setup(walk_data *d) state_loc = d; for (n = d->names; n->name; n++) { - git_file fd = p_creat(n->name, 0600); + git_file fd = p_creat(n->name, 0666); cl_assert(fd >= 0); p_close(fd); n->count = 0; diff --git a/tests-clay/core/filebuf.c b/tests-clay/core/filebuf.c index e00e20497..e1ecb2798 100644 --- a/tests-clay/core/filebuf.c +++ b/tests-clay/core/filebuf.c @@ -27,14 +27,14 @@ void test_core_filebuf__1(void) int fd; char test[] = "test"; - fd = p_creat(test, 0644); + fd = p_creat(test, 0666); cl_must_pass(fd); cl_must_pass(p_write(fd, "libgit2 rocks\n", 14)); cl_must_pass(p_close(fd)); cl_git_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND)); cl_git_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); - cl_git_pass(git_filebuf_commit(&file)); + cl_git_pass(git_filebuf_commit(&file, 0666)); cl_must_pass(p_unlink(test)); } @@ -51,7 +51,7 @@ void test_core_filebuf__2(void) cl_git_pass(git_filebuf_open(&file, test, 0)); cl_git_pass(git_filebuf_write(&file, buf, sizeof(buf))); - cl_git_pass(git_filebuf_commit(&file)); + cl_git_pass(git_filebuf_commit(&file, 0666)); cl_must_pass(p_unlink(test)); } diff --git a/tests-clay/core/rmdir.c b/tests-clay/core/rmdir.c index 03baecf23..20cc8f5f0 100644 --- a/tests-clay/core/rmdir.c +++ b/tests-clay/core/rmdir.c @@ -39,7 +39,7 @@ void test_core_rmdir__fail_to_delete_non_empty_dir(void) git_path_join(file, empty_tmp_dir, "/two/file.txt"); - fd = p_creat(file, 0755); + fd = p_creat(file, 0666); cl_assert(fd >= 0); cl_must_pass(p_close(fd)); diff --git a/tests-clay/status/single.c b/tests-clay/status/single.c index ea1c77fa0..4fd6e6ff4 100644 --- a/tests-clay/status/single.c +++ b/tests-clay/status/single.c @@ -10,7 +10,7 @@ cleanup__remove_file(void *_file) static void file_create(const char *filename, const char *content) { - int fd = p_creat(filename, 0644); + int fd = p_creat(filename, 0666); cl_assert(fd >= 0); cl_must_pass(p_write(fd, content, strlen(content))); cl_must_pass(p_close(fd)); diff --git a/tests/t00-core.c b/tests/t00-core.c index 185681562..dbc80670f 100644 --- a/tests/t00-core.c +++ b/tests/t00-core.c @@ -264,7 +264,7 @@ static int setup(walk_data *d) state_loc = d; for (n = d->names; n->name; n++) { - git_file fd = p_creat(n->name, 0600); + git_file fd = p_creat(n->name, 0666); if (fd < 0) return GIT_ERROR; p_close(fd); @@ -479,14 +479,14 @@ BEGIN_TEST(filebuf1, "make sure GIT_FILEBUF_APPEND works as expected") int fd; char test[] = "test"; - fd = p_creat(test, 0644); + fd = p_creat(test, 0666); must_pass(fd); must_pass(p_write(fd, "libgit2 rocks\n", 14)); must_pass(p_close(fd)); must_pass(git_filebuf_open(&file, test, GIT_FILEBUF_APPEND)); must_pass(git_filebuf_printf(&file, "%s\n", "libgit2 rocks")); - must_pass(git_filebuf_commit(&file)); + must_pass(git_filebuf_commit(&file, 0666)); must_pass(p_unlink(test)); END_TEST @@ -499,7 +499,7 @@ BEGIN_TEST(filebuf2, "make sure git_filebuf_write writes large buffer correctly" memset(buf, 0xfe, sizeof(buf)); must_pass(git_filebuf_open(&file, test, 0)); must_pass(git_filebuf_write(&file, buf, sizeof(buf))); - must_pass(git_filebuf_commit(&file)); + must_pass(git_filebuf_commit(&file, 0666)); must_pass(p_unlink(test)); END_TEST diff --git a/tests/t04-commit.c b/tests/t04-commit.c index 3fb4d370c..4abbf01c2 100644 --- a/tests/t04-commit.c +++ b/tests/t04-commit.c @@ -690,6 +690,10 @@ BEGIN_TEST(write0, "write a new commit object from memory to disk") must_be_true(strcmp(git_commit_message(commit), COMMIT_MESSAGE) == 0); +#ifndef GIT_WIN32 + must_be_true((loose_object_mode(REPOSITORY_FOLDER, (git_object *)commit) & 0777) == GIT_OBJECT_FILE_MODE); +#endif + must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)commit)); git_commit_close(commit); diff --git a/tests/t06-index.c b/tests/t06-index.c index dde98abaf..44562d004 100644 --- a/tests/t06-index.c +++ b/tests/t06-index.c @@ -179,7 +179,7 @@ BEGIN_TEST(add0, "add a new file to the index") must_pass(git_futils_mkpath2file(TEMP_REPO_FOLDER "myrepo/test.txt", 0777)); must_pass(git_filebuf_open(&file, TEMP_REPO_FOLDER "myrepo/test.txt", 0)); must_pass(git_filebuf_write(&file, "hey there\n", 10)); - must_pass(git_filebuf_commit(&file)); + must_pass(git_filebuf_commit(&file, 0666)); /* Store the expected hash of the file/blob * This has been generated by executing the following diff --git a/tests/t08-tag.c b/tests/t08-tag.c index 216fb9dfc..85ef9225e 100644 --- a/tests/t08-tag.c +++ b/tests/t08-tag.c @@ -189,6 +189,9 @@ BEGIN_TEST(write0, "write a tag to the repository and read it again") must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/the-tag")); must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0); must_pass(git_reference_delete(ref_tag)); +#ifndef GIT_WIN32 + must_be_true((loose_object_mode(REPOSITORY_FOLDER, (git_object *)tag) & 0777) == GIT_OBJECT_FILE_MODE); +#endif must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag)); diff --git a/tests/t09-tree.c b/tests/t09-tree.c index 1f2fe3f02..2341a1ca4 100644 --- a/tests/t09-tree.c +++ b/tests/t09-tree.c @@ -202,6 +202,7 @@ BEGIN_TEST(write3, "write a hierarchical tree from a memory") must_be_true(2 == git_tree_entrycount(tree)); #ifndef GIT_WIN32 must_be_true((loose_object_dir_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_DIR_MODE); + must_be_true((loose_object_mode(TEMP_REPO_FOLDER, (git_object *)tree) & 0777) == GIT_OBJECT_FILE_MODE); #endif git_tree_close(tree); diff --git a/tests/t12-repo.c b/tests/t12-repo.c index c07150c9c..6197cdd00 100644 --- a/tests/t12-repo.c +++ b/tests/t12-repo.c @@ -351,7 +351,7 @@ static int write_file(const char *path, const char *content) return error; } - file = git_futils_creat_withpath(path, 0777, 0644); + file = git_futils_creat_withpath(path, 0777, 0666); if (file < GIT_SUCCESS) return file; diff --git a/tests/t15-config.c b/tests/t15-config.c index c4cb590f5..a97f82067 100644 --- a/tests/t15-config.c +++ b/tests/t15-config.c @@ -319,7 +319,7 @@ BEGIN_TEST(config16, "add a variable in a new section") /* As the section wasn't removed, owerwrite the file */ must_pass(git_filebuf_open(&buf, CONFIG_BASE "/config10", 0)); must_pass(git_filebuf_write(&buf, "[empty]\n", strlen("[empty]\n"))); - must_pass(git_filebuf_commit(&buf)); + must_pass(git_filebuf_commit(&buf, 0666)); END_TEST BEGIN_TEST(config17, "prefixes aren't broken") diff --git a/tests/t18-status.c b/tests/t18-status.c index bd205a438..d836fb9a9 100644 --- a/tests/t18-status.c +++ b/tests/t18-status.c @@ -37,7 +37,7 @@ static int file_create(const char *filename, const char *content) { int fd; - fd = p_creat(filename, 0644); + fd = p_creat(filename, 0666); if (fd == 0) return GIT_ERROR; if (p_write(fd, content, strlen(content)) != 0) @@ -400,7 +400,7 @@ BEGIN_TEST(singlestatus3, "test retrieving status for a new file in an empty rep must_pass(remove_placeholders(TEST_STD_REPO_FOLDER, "dummy-marker.txt")); git_path_join(file_path, TEMP_REPO_FOLDER, filename); - fd = p_creat(file_path, 0644); + fd = p_creat(file_path, 0666); must_pass(fd); must_pass(p_write(fd, "new_file\n", 9)); must_pass(p_close(fd)); 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; diff --git a/tests/test_helpers.h b/tests/test_helpers.h index 2e5194d7c..16a3a2ced 100644 --- a/tests/test_helpers.h +++ b/tests/test_helpers.h @@ -65,6 +65,7 @@ extern int cmp_objects(git_rawobj *o, object_data *d); extern void locate_loose_object(const char *odb_dir, git_object *object, char **out, char **out_folder); +extern int loose_object_mode(const char *odb_dir, git_object *object); extern int loose_object_dir_mode(const char *odb_dir, git_object *object); extern int remove_loose_object(const char *odb_dir, git_object *object);