diff options
Diffstat (limited to 'src/filebuf.c')
-rw-r--r-- | src/filebuf.c | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/src/filebuf.c b/src/filebuf.c index 1a98e3f43..199418032 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) @@ -23,9 +25,10 @@ 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, 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) @@ -63,13 +66,13 @@ void git_filebuf_cleanup(git_filebuf *file) if (file->digest) git_hash_free_ctx(file->digest); - free(file->buffer); - free(file->z_buf); + git__free(file->buffer); + git__free(file->z_buf); deflateEnd(&file->zs); - free(file->path_original); - free(file->path_lock); + git__free(file->path_original); + git__free(file->path_lock); } GIT_INLINE(int) flush_buffer(git_filebuf *file) @@ -246,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); + git__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; @@ -270,7 +273,12 @@ int git_filebuf_commit(git_filebuf *file) p_close(file->fd); file->fd = -1; - error = git_futils_mv_atomic(file->path_lock, file->path_original); + if (p_chmod(file->path_lock, mode)) { + error = git__throw(GIT_EOSERR, "Failed to chmod locked file before committing"); + goto cleanup; + } + + error = p_rename(file->path_lock, file->path_original); cleanup: git_filebuf_cleanup(file); @@ -368,12 +376,12 @@ int git_filebuf_printf(git_filebuf *file, const char *format, ...) va_end(arglist); if (len < 0) { - free(tmp_buffer); + git__free(tmp_buffer); return git__throw(GIT_EOSERR, "Failed to format string"); } error = git_filebuf_write(file, tmp_buffer, len); - free(tmp_buffer); + git__free(tmp_buffer); return error; } |