Skip to content

Commit

Permalink
Merge pull request libgit2#456 from brodie/perm-fixes
Browse files Browse the repository at this point in the history
Create objects, indexes, and directories with the right file permissions
  • Loading branch information
vmg committed Oct 29, 2011
2 parents 3286c40 + 01ad7b3 commit 89fb8f0
Show file tree
Hide file tree
Showing 41 changed files with 187 additions and 97 deletions.
1 change: 1 addition & 0 deletions src/config.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/config_file.c
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/fetch.c
Expand Up @@ -14,6 +14,7 @@
#include "transport.h"
#include "remote.h"
#include "refspec.h"
#include "pack.h"
#include "fetch.h"
#include "netops.h"

Expand Down Expand Up @@ -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);
Expand Down
18 changes: 13 additions & 5 deletions src/filebuf.c
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
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;

Expand All @@ -270,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:
Expand Down
4 changes: 2 additions & 2 deletions src/filebuf.h
Expand Up @@ -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);

Expand Down
19 changes: 9 additions & 10 deletions src/fileops.c
Expand Up @@ -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 int mode = 0755; /* or 0777 ? */
int error = GIT_SUCCESS;
char target_folder_path[GIT_PATH_MAX];

Expand Down Expand Up @@ -67,23 +66,23 @@ 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 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);
}

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 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);
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -289,7 +288,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;
Expand Down
12 changes: 6 additions & 6 deletions src/fileops.h
Expand Up @@ -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 dirmode, 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 dirmode, const mode_t mode);

/**
* Check if the given path points to a directory
Expand All @@ -74,13 +74,13 @@ 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
* 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);

Expand All @@ -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);


/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.c
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions src/index.h
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/indexer.c
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/odb.h
Expand Up @@ -14,6 +14,10 @@
#include "vector.h"
#include "cache.h"

#define GIT_OBJECTS_DIR "objects/"
#define GIT_OBJECT_DIR_MODE 0777
#define GIT_OBJECT_FILE_MODE 0444

/* DO NOT EXPORT */
typedef struct {
void *data; /**< Raw, decompressed object data. */
Expand Down
8 changes: 4 additions & 4 deletions src/odb_loose.c
Expand Up @@ -666,11 +666,11 @@ 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;
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)
Expand Down Expand Up @@ -787,10 +787,10 @@ 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);
return git_filebuf_commit_at(&fbuf, final_path, GIT_OBJECT_FILE_MODE);

cleanup:
git_filebuf_cleanup(&fbuf);
Expand Down
2 changes: 2 additions & 0 deletions src/pack.h
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/posix.c
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/posix.h
Expand Up @@ -40,9 +40,10 @@ 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, 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
Expand Down
4 changes: 2 additions & 2 deletions src/reflog.c
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/reflog.h
Expand Up @@ -12,6 +12,8 @@
#include "vector.h"

#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)

Expand Down
5 changes: 3 additions & 2 deletions src/refs.c
Expand Up @@ -9,6 +9,7 @@
#include "hash.h"
#include "repository.h"
#include "fileops.h"
#include "pack.h"

#include <git2/tag.h>
#include <git2/object.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -870,7 +871,7 @@ static int packed_write(git_repository *repo)
/* 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 */
Expand Down
2 changes: 2 additions & 0 deletions src/refs.h
Expand Up @@ -16,6 +16,8 @@
#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_REFS_FILE_MODE 0666

#define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF"

Expand Down

0 comments on commit 89fb8f0

Please sign in to comment.