Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Take umask into account in filebuf_commit #1940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/git2/odb_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ GIT_EXTERN(int) git_odb_backend_loose(
const char *objects_dir,
int compression_level,
int do_fsync,
mode_t dir_mode,
mode_t file_mode);
unsigned int dir_mode,
unsigned int file_mode);

/**
* Create a backend out of a single packfile
Expand Down
2 changes: 1 addition & 1 deletion src/blob.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ int git_blob_create_fromchunks(
content = git__malloc(BUFFER_SIZE);
GITERR_CHECK_ALLOC(content);

if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY) < 0)
if (git_filebuf_open(&file, git_buf_cstr(&path), GIT_FILEBUF_TEMPORARY, 0666) < 0)
goto cleanup;

while (1) {
Expand Down
4 changes: 2 additions & 2 deletions src/checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,9 @@ static int checkout_write_merge(
goto done;

if ((error = git_futils_mkpath2file(path_workdir.ptr, 0755)) < 0 ||
(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER)) < 0 ||
(error = git_filebuf_open(&output, path_workdir.ptr, GIT_FILEBUF_DO_NOT_BUFFER, result.mode)) < 0 ||
(error = git_filebuf_write(&output, result.data, result.len)) < 0 ||
(error = git_filebuf_commit(&output, result.mode)) < 0)
(error = git_filebuf_commit(&output)) < 0)
goto done;

done:
Expand Down
4 changes: 2 additions & 2 deletions src/config_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
write_start = data_start;

/* Lock the file */
if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
if (git_filebuf_open(&file, cfg->file_path, 0, GIT_CONFIG_FILE_MODE) < 0)
return -1;

skip_bom(reader);
Expand Down Expand Up @@ -1369,7 +1369,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
/* refresh stats - if this errors, then commit will error too */
(void)git_filebuf_stats(&reader->file_mtime, &reader->file_size, &file);

result = git_filebuf_commit(&file, GIT_CONFIG_FILE_MODE);
result = git_filebuf_commit(&file);
git_buf_free(&reader->buffer);

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/fetchhead.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
if (git_buf_joinpath(&path, repo->path_repository, GIT_FETCH_HEAD_FILE) < 0)
return -1;

if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE) < 0) {
if (git_filebuf_open(&file, path.ptr, GIT_FILEBUF_FORCE, GIT_REFS_FILE_MODE) < 0) {
git_buf_free(&path);
return -1;
}
Expand All @@ -124,7 +124,7 @@ int git_fetchhead_write(git_repository *repo, git_vector *fetchhead_refs)
git_vector_foreach(fetchhead_refs, i, fetchhead_ref)
fetchhead_ref_write(&file, fetchhead_ref);

return git_filebuf_commit(&file, GIT_REFS_FILE_MODE);
return git_filebuf_commit(&file);
}

static int fetchhead_ref_parse(
Expand Down
25 changes: 9 additions & 16 deletions src/filebuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "filebuf.h"
#include "fileops.h"

#define GIT_LOCK_FILE_MODE 0644

static const size_t WRITE_BUFFER_SIZE = (4096 * 2);

enum buferr_t {
Expand Down Expand Up @@ -44,7 +42,7 @@ static int verify_last_error(git_filebuf *file)
}
}

static int lock_file(git_filebuf *file, int flags)
static int lock_file(git_filebuf *file, int flags, mode_t mode)
{
if (git_path_exists(file->path_lock) == true) {
if (flags & GIT_FILEBUF_FORCE)
Expand All @@ -60,9 +58,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, GIT_LOCK_FILE_MODE);
file->fd = git_futils_creat_locked_withpath(file->path_lock, 0777, mode);
} else {
file->fd = git_futils_creat_locked(file->path_lock, GIT_LOCK_FILE_MODE);
file->fd = git_futils_creat_locked(file->path_lock, mode);
}

if (file->fd < 0)
Expand Down Expand Up @@ -195,7 +193,7 @@ static int write_deflate(git_filebuf *file, void *source, size_t len)
return 0;
}

int git_filebuf_open(git_filebuf *file, const char *path, int flags)
int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode)
{
int compression, error = -1;
size_t path_len;
Expand Down Expand Up @@ -255,7 +253,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
git_buf tmp_path = GIT_BUF_INIT;

/* Open the file as temporary for locking */
file->fd = git_futils_mktmp(&tmp_path, path);
file->fd = git_futils_mktmp(&tmp_path, path, mode);

if (file->fd < 0) {
git_buf_free(&tmp_path);
Expand All @@ -282,7 +280,7 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags)
memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);

/* open the file for locking */
if ((error = lock_file(file, flags)) < 0)
if ((error = lock_file(file, flags, mode)) < 0)
goto cleanup;
}

Expand All @@ -309,16 +307,16 @@ int git_filebuf_hash(git_oid *oid, git_filebuf *file)
return 0;
}

int git_filebuf_commit_at(git_filebuf *file, const char *path, mode_t mode)
int git_filebuf_commit_at(git_filebuf *file, const char *path)
{
git__free(file->path_original);
file->path_original = git__strdup(path);
GITERR_CHECK_ALLOC(file->path_original);

return git_filebuf_commit(file, mode);
return git_filebuf_commit(file);
}

int git_filebuf_commit(git_filebuf *file, mode_t mode)
int git_filebuf_commit(git_filebuf *file)
{
/* temporary files cannot be committed */
assert(file && file->path_original);
Expand All @@ -338,11 +336,6 @@ int git_filebuf_commit(git_filebuf *file, mode_t mode)

file->fd = -1;

if (p_chmod(file->path_lock, mode)) {
giterr_set(GITERR_OS, "Failed to set attributes for file at '%s'", file->path_lock);
goto on_error;
}

p_unlink(file->path_original);

if (p_rename(file->path_lock, file->path_original) < 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/filebuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ int git_filebuf_write(git_filebuf *lock, const void *buff, size_t len);
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, mode_t mode);
int git_filebuf_commit_at(git_filebuf *lock, const char *path, mode_t mode);
int git_filebuf_open(git_filebuf *lock, const char *path, int flags, mode_t mode);
int git_filebuf_commit(git_filebuf *lock);
int git_filebuf_commit_at(git_filebuf *lock, const char *path);
void git_filebuf_cleanup(git_filebuf *lock);
int git_filebuf_hash(git_oid *oid, git_filebuf *file);
int git_filebuf_flush(git_filebuf *file);
Expand Down
11 changes: 10 additions & 1 deletion src/fileops.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ int git_futils_mkpath2file(const char *file_path, const mode_t mode)
GIT_MKDIR_PATH | GIT_MKDIR_SKIP_LAST | GIT_MKDIR_VERIFY_DIR);
}

int git_futils_mktmp(git_buf *path_out, const char *filename)
int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode)
{
int fd;
mode_t mask;

p_umask(mask = p_umask(0));

git_buf_sets(path_out, filename);
git_buf_puts(path_out, "_git2_XXXXXX");
Expand All @@ -35,6 +38,12 @@ int git_futils_mktmp(git_buf *path_out, const char *filename)
return -1;
}

if (p_chmod(path_out->ptr, (mode & ~mask))) {
giterr_set(GITERR_OS,
"Failed to set permissions on file '%s'", path_out->ptr);
return -1;
}

return fd;
}

Expand Down
2 changes: 1 addition & 1 deletion src/fileops.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ extern int git_futils_rmdir_r(const char *path, const char *base, uint32_t flags
* Writes the filename into path_out.
* @return On success, an open file descriptor, else an error code < 0.
*/
extern int git_futils_mktmp(git_buf *path_out, const char *filename);
extern int git_futils_mktmp(git_buf *path_out, const char *filename, mode_t mode);

/**
* Move a file on the filesystem, create the
Expand Down
4 changes: 2 additions & 2 deletions src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ int git_index_write(git_index *index)
git_vector_sort(&index->reuc);

if ((error = git_filebuf_open(
&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS)) < 0) {
&file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) {
if (error == GIT_ELOCKED)
giterr_set(GITERR_INDEX, "The index is locked. This might be due to a concurrrent or crashed process");

Expand All @@ -512,7 +512,7 @@ int git_index_write(git_index *index)
return error;
}

if ((error = git_filebuf_commit(&file, GIT_INDEX_FILE_MODE)) < 0)
if ((error = git_filebuf_commit(&file)) < 0)
return error;

error = git_futils_filestamp_check(&index->stamp, index->index_file_path);
Expand Down
10 changes: 6 additions & 4 deletions src/indexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ int git_indexer_new(
goto cleanup;

error = git_filebuf_open(&idx->pack_file, path.ptr,
GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER);
GIT_FILEBUF_TEMPORARY | GIT_FILEBUF_DO_NOT_BUFFER,
GIT_PACK_FILE_MODE);
git_buf_free(&path);
if (error < 0)
goto cleanup;
Expand Down Expand Up @@ -903,7 +904,8 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
if (git_buf_oom(&filename))
return -1;

if (git_filebuf_open(&index_file, filename.ptr, GIT_FILEBUF_HASH_CONTENTS) < 0)
if (git_filebuf_open(&index_file, filename.ptr,
GIT_FILEBUF_HASH_CONTENTS, GIT_PACK_FILE_MODE) < 0)
goto on_error;

/* Write out the header */
Expand Down Expand Up @@ -969,7 +971,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
goto on_error;

/* Commit file */
if (git_filebuf_commit_at(&index_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
if (git_filebuf_commit_at(&index_file, filename.ptr) < 0)
goto on_error;

git_mwindow_free_all(&idx->pack->mwf);
Expand All @@ -980,7 +982,7 @@ int git_indexer_commit(git_indexer *idx, git_transfer_progress *stats)
if (index_path(&filename, idx, ".pack") < 0)
goto on_error;
/* And don't forget to rename the packfile to its new place. */
if (git_filebuf_commit_at(&idx->pack_file, filename.ptr, GIT_PACK_FILE_MODE) < 0)
if (git_filebuf_commit_at(&idx->pack_file, filename.ptr) < 0)
return -1;

git_buf_free(&filename);
Expand Down
16 changes: 8 additions & 8 deletions src/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -1623,9 +1623,9 @@ static int write_orig_head(
git_oid_tostr(orig_oid_str, GIT_OID_HEXSZ+1, &our_head->oid);

if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_ORIG_HEAD_FILE)) == 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) == 0 &&
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) == 0 &&
(error = git_filebuf_printf(&file, "%s\n", orig_oid_str)) == 0)
error = git_filebuf_commit(&file, 0666);
error = git_filebuf_commit(&file);

if (error < 0)
git_filebuf_cleanup(&file);
Expand All @@ -1649,7 +1649,7 @@ static int write_merge_head(
assert(repo && heads);

if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_HEAD_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0)
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;

for (i = 0; i < heads_len; i++) {
Expand All @@ -1659,7 +1659,7 @@ static int write_merge_head(
goto cleanup;
}

error = git_filebuf_commit(&file, 0666);
error = git_filebuf_commit(&file);

cleanup:
if (error < 0)
Expand All @@ -1682,10 +1682,10 @@ static int write_merge_mode(git_repository *repo, unsigned int flags)
assert(repo);

if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MODE_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0)
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0)
goto cleanup;

error = git_filebuf_commit(&file, 0666);
error = git_filebuf_commit(&file);

cleanup:
if (error < 0)
Expand Down Expand Up @@ -1911,7 +1911,7 @@ static int write_merge_msg(
entries[i].merge_head = heads[i];

if ((error = git_buf_joinpath(&file_path, repo->path_repository, GIT_MERGE_MSG_FILE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE)) < 0 ||
(error = git_filebuf_open(&file, file_path.ptr, GIT_FILEBUF_FORCE, GIT_MERGE_FILE_MODE)) < 0 ||
(error = git_filebuf_write(&file, "Merge ", 6)) < 0)
goto cleanup;

Expand Down Expand Up @@ -1988,7 +1988,7 @@ static int write_merge_msg(
}

if ((error = git_filebuf_printf(&file, "\n")) < 0 ||
(error = git_filebuf_commit(&file, 0666)) < 0)
(error = git_filebuf_commit(&file)) < 0)
goto cleanup;

cleanup:
Expand Down
1 change: 1 addition & 0 deletions src/merge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#define GIT_MERGE_MSG_FILE "MERGE_MSG"
#define GIT_MERGE_MODE_FILE "MERGE_MODE"
#define GIT_MERGE_FILE_MODE 0666

#define GIT_MERGE_TREE_RENAME_THRESHOLD 50
#define GIT_MERGE_TREE_TARGET_LIMIT 1000
Expand Down
14 changes: 8 additions & 6 deletions src/odb_loose.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ static int loose_backend__stream_fwrite(git_odb_stream *_stream, const git_oid *
error = -1;
else
error = git_filebuf_commit_at(
&stream->fbuf, final_path.ptr, backend->object_file_mode);
&stream->fbuf, final_path.ptr);

git_buf_free(&final_path);

Expand Down Expand Up @@ -838,7 +838,8 @@ static int loose_backend__stream(git_odb_stream **stream_out, git_odb_backend *_
if (git_buf_joinpath(&tmp_path, backend->objects_dir, "tmp_object") < 0 ||
git_filebuf_open(&stream->fbuf, tmp_path.ptr,
GIT_FILEBUF_TEMPORARY |
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0 ||
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT),
backend->object_file_mode) < 0 ||
stream->stream.write((git_odb_stream *)stream, hdr, hdrlen) < 0)
{
git_filebuf_cleanup(&stream->fbuf);
Expand Down Expand Up @@ -867,7 +868,8 @@ static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, c
if (git_buf_joinpath(&final_path, backend->objects_dir, "tmp_object") < 0 ||
git_filebuf_open(&fbuf, final_path.ptr,
GIT_FILEBUF_TEMPORARY |
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT)) < 0)
(backend->object_zlib_level << GIT_FILEBUF_DEFLATE_SHIFT),
backend->object_file_mode) < 0)
{
error = -1;
goto cleanup;
Expand All @@ -878,7 +880,7 @@ static int loose_backend__write(git_odb_backend *_backend, const git_oid *oid, c

if (object_file_name(&final_path, backend, oid) < 0 ||
object_mkdir(&final_path, backend) < 0 ||
git_filebuf_commit_at(&fbuf, final_path.ptr, backend->object_file_mode) < 0)
git_filebuf_commit_at(&fbuf, final_path.ptr) < 0)
error = -1;

cleanup:
Expand All @@ -902,8 +904,8 @@ int git_odb_backend_loose(
const char *objects_dir,
int compression_level,
int do_fsync,
mode_t dir_mode,
mode_t file_mode)
unsigned int dir_mode,
unsigned int file_mode)
{
loose_backend *backend;
size_t objects_dirlen;
Expand Down
Loading