summaryrefslogtreecommitdiff
path: root/src/filebuf.c
diff options
context:
space:
mode:
authorBrodie Rao <brodie@bitheap.org>2011-09-06 15:48:45 -0700
committerBrodie Rao <brodie@bitheap.org>2011-10-14 16:07:47 -0700
commit01ad7b3a9ec8f5e465f94c2704e1e96b84f941c7 (patch)
treec44520dc0f23ceb24463ab6a2d0b754cd0b55cbb /src/filebuf.c
parentce8cd006ce3adcd012a38401b8a7555ba3307b3f (diff)
downloadlibgit2-01ad7b3a9ec8f5e465f94c2704e1e96b84f941c7.tar.gz
*: 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".
Diffstat (limited to 'src/filebuf.c')
-rw-r--r--src/filebuf.c17
1 files changed, 12 insertions, 5 deletions
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: