Commit 85bd1746 by Russell Belfer

Some cleanup suggested during review

This cleans up a number of items suggested during code review
with @vmg, including:

* renaming "outside repo" config API to `git_config_open_default`
* killing the `git_config_open_global` API
* removing the `git_` prefix from the static functions in fileops
* removing some unnecessary functionality from the "cp" command
parent b769e936
...@@ -80,27 +80,16 @@ GIT_EXTERN(int) git_config_find_global(char *global_config_path, size_t length); ...@@ -80,27 +80,16 @@ GIT_EXTERN(int) git_config_find_global(char *global_config_path, size_t length);
GIT_EXTERN(int) git_config_find_system(char *system_config_path, size_t length); GIT_EXTERN(int) git_config_find_system(char *system_config_path, size_t length);
/** /**
* Open the global configuration file
*
* Utility wrapper that calls `git_config_find_global`
* and opens the located file, if it exists.
*
* @param out Pointer to store the config instance
* @return 0 or an error code
*/
GIT_EXTERN(int) git_config_open_global(git_config **out);
/**
* Open the global and system configuration files * Open the global and system configuration files
* *
* Utility wrapper that finds the global and system configuration files * Utility wrapper that finds the global and system configuration files
* and opens them into a single prioritized config object that can be * and opens them into a single prioritized config object that can be
* used when accessing config data outside a repository. * used when accessing default config data outside a repository.
* *
* @param out Pointer to store the config instance * @param out Pointer to store the config instance
* @return 0 or an error code * @return 0 or an error code
*/ */
GIT_EXTERN(int) git_config_open_outside_repo(git_config **out); GIT_EXTERN(int) git_config_open_default(git_config **out);
/** /**
* Create a configuration file backend for ondisk files * Create a configuration file backend for ondisk files
......
...@@ -501,21 +501,7 @@ int git_config_find_system(char *system_config_path, size_t length) ...@@ -501,21 +501,7 @@ int git_config_find_system(char *system_config_path, size_t length)
return 0; return 0;
} }
int git_config_open_global(git_config **out) int git_config_open_default(git_config **out)
{
int error;
git_buf path = GIT_BUF_INIT;
if ((error = git_config_find_global_r(&path)) < 0)
return error;
error = git_config_open_ondisk(out, git_buf_cstr(&path));
git_buf_free(&path);
return error;
}
int git_config_open_outside_repo(git_config **out)
{ {
int error; int error;
git_config *cfg = NULL; git_config *cfg = NULL;
......
...@@ -511,7 +511,7 @@ int git_futils_fake_symlink(const char *old, const char *new) ...@@ -511,7 +511,7 @@ int git_futils_fake_symlink(const char *old, const char *new)
return retcode; return retcode;
} }
static int git_futils_cp_fd(int ifd, int ofd, bool close_fd) static int cp_by_fd(int ifd, int ofd, bool close_fd_when_done)
{ {
int error = 0; int error = 0;
char buffer[4096]; char buffer[4096];
...@@ -528,7 +528,7 @@ static int git_futils_cp_fd(int ifd, int ofd, bool close_fd) ...@@ -528,7 +528,7 @@ static int git_futils_cp_fd(int ifd, int ofd, bool close_fd)
error = (int)len; error = (int)len;
} }
if (close_fd) { if (close_fd_when_done) {
p_close(ifd); p_close(ifd);
p_close(ofd); p_close(ofd);
} }
...@@ -536,14 +536,10 @@ static int git_futils_cp_fd(int ifd, int ofd, bool close_fd) ...@@ -536,14 +536,10 @@ static int git_futils_cp_fd(int ifd, int ofd, bool close_fd)
return error; return error;
} }
int git_futils_cp_withpath( int git_futils_cp(const char *from, const char *to, mode_t filemode)
const char *from, const char *to, mode_t filemode, mode_t dirmode)
{ {
int ifd, ofd; int ifd, ofd;
if (git_futils_mkpath2file(to, dirmode) < 0)
return -1;
if ((ifd = git_futils_open_ro(from)) < 0) if ((ifd = git_futils_open_ro(from)) < 0)
return ifd; return ifd;
...@@ -555,19 +551,18 @@ int git_futils_cp_withpath( ...@@ -555,19 +551,18 @@ int git_futils_cp_withpath(
return ofd; return ofd;
} }
return git_futils_cp_fd(ifd, ofd, true); return cp_by_fd(ifd, ofd, true);
} }
static int git_futils_cplink( static int cp_link(const char *from, const char *to, size_t link_size)
const char *from, size_t from_filesize, const char *to)
{ {
int error = 0; int error = 0;
ssize_t read_len; ssize_t read_len;
char *link_data = git__malloc(from_filesize + 1); char *link_data = git__malloc(link_size + 1);
GITERR_CHECK_ALLOC(link_data); GITERR_CHECK_ALLOC(link_data);
read_len = p_readlink(from, link_data, from_filesize); read_len = p_readlink(from, link_data, link_size);
if (read_len != (ssize_t)from_filesize) { if (read_len != (ssize_t)link_size) {
giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from); giterr_set(GITERR_OS, "Failed to read symlink data for '%s'", from);
error = -1; error = -1;
} }
...@@ -668,11 +663,9 @@ static int _cp_r_callback(void *ref, git_buf *from) ...@@ -668,11 +663,9 @@ static int _cp_r_callback(void *ref, git_buf *from)
/* make symlink or regular file */ /* make symlink or regular file */
if (S_ISLNK(from_st.st_mode)) if (S_ISLNK(from_st.st_mode))
return git_futils_cplink( return cp_link(from->ptr, info->to.ptr, (size_t)from_st.st_size);
from->ptr, (size_t)from_st.st_size, info->to.ptr);
else else
return git_futils_cp_withpath( return git_futils_cp(from->ptr, info->to.ptr, from_st.st_mode);
from->ptr, info->to.ptr, from_st.st_mode, info->dirmode);
} }
int git_futils_cp_r( int git_futils_cp_r(
......
...@@ -130,16 +130,14 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename); ...@@ -130,16 +130,14 @@ extern int git_futils_mktmp(git_buf *path_out, const char *filename);
extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode); extern int git_futils_mv_withpath(const char *from, const char *to, const mode_t dirmode);
/** /**
* Copy a file, creating the destination path if needed. * Copy a file
* *
* The filemode will be used for the file and the dirmode will be used for * The filemode will be used for the newly created file.
* any intervening directories if necessary.
*/ */
extern int git_futils_cp_withpath( extern int git_futils_cp(
const char *from, const char *from,
const char *to, const char *to,
mode_t filemode, mode_t filemode);
mode_t dirmode);
/** /**
* Flags that can be passed to `git_futils_cp_r`. * Flags that can be passed to `git_futils_cp_r`.
......
...@@ -936,7 +936,7 @@ static int repo_init_structure( ...@@ -936,7 +936,7 @@ static int repo_init_structure(
if (opts->template_path) if (opts->template_path)
tdir = opts->template_path; tdir = opts->template_path;
else if ((error = git_config_open_outside_repo(&cfg)) < 0) else if ((error = git_config_open_default(&cfg)) < 0)
return error; return error;
else { else {
error = git_config_get_string(&tdir, cfg, "init.templatedir"); error = git_config_get_string(&tdir, cfg, "init.templatedir");
......
...@@ -20,7 +20,6 @@ ...@@ -20,7 +20,6 @@
#define p_readlink(a, b, c) readlink(a, b, c) #define p_readlink(a, b, c) readlink(a, b, c)
#define p_symlink(o,n) symlink(o, n) #define p_symlink(o,n) symlink(o, n)
#define p_link(o,n) link(o, n) #define p_link(o,n) link(o, n)
#define p_symlink(o,n) symlink(o,n)
#define p_unlink(p) unlink(p) #define p_unlink(p) unlink(p)
#define p_mkdir(p,m) mkdir(p, m) #define p_mkdir(p,m) mkdir(p, m)
#define p_fsync(fd) fsync(fd) #define p_fsync(fd) fsync(fd)
......
...@@ -10,7 +10,7 @@ void test_core_copy__file(void) ...@@ -10,7 +10,7 @@ void test_core_copy__file(void)
cl_git_mkfile("copy_me", content); cl_git_mkfile("copy_me", content);
cl_git_pass(git_futils_cp_withpath("copy_me", "copy_me_two", 0664, 0775)); cl_git_pass(git_futils_cp("copy_me", "copy_me_two", 0664));
cl_git_pass(git_path_lstat("copy_me_two", &st)); cl_git_pass(git_path_lstat("copy_me_two", &st));
cl_assert(S_ISREG(st.st_mode)); cl_assert(S_ISREG(st.st_mode));
...@@ -29,10 +29,13 @@ void test_core_copy__file_in_dir(void) ...@@ -29,10 +29,13 @@ void test_core_copy__file_in_dir(void)
cl_git_mkfile("an_dir/in_a_dir/copy_me", content); cl_git_mkfile("an_dir/in_a_dir/copy_me", content);
cl_assert(git_path_isdir("an_dir")); cl_assert(git_path_isdir("an_dir"));
cl_git_pass(git_futils_cp_withpath cl_git_pass(git_futils_mkpath2file
("an_dir/second_dir/and_more/copy_me_two", 0775));
cl_git_pass(git_futils_cp
("an_dir/in_a_dir/copy_me", ("an_dir/in_a_dir/copy_me",
"an_dir/second_dir/and_more/copy_me_two", "an_dir/second_dir/and_more/copy_me_two",
0664, 0775)); 0664));
cl_git_pass(git_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st)); cl_git_pass(git_path_lstat("an_dir/second_dir/and_more/copy_me_two", &st));
cl_assert(S_ISREG(st.st_mode)); cl_assert(S_ISREG(st.st_mode));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment