Commit 995f9c34 by Vicent Marti

Use the new git__joinpath to build paths in methods

The `git__joinpath` function has been changed to use a statically
allocated buffer; we assume the buffer to be 4096 bytes, because fuck
you.

The new method also supports an arbritrary number of paths to join,
which may come in handy in the future.

Some methods which were manually joining paths with `strcpy` now use the
new function, namely those in `index.c` and `refs.c`.

Based on Emeric Fermas' original patch, which was using the old
`git__joinpath` because I'm stupid. Thanks!

Signed-off-by: Vicent Marti <tanoku@gmail.com>
parent 772c7e76
...@@ -289,8 +289,7 @@ int git_index_add(git_index *index, const char *rel_path, int stage) ...@@ -289,8 +289,7 @@ int git_index_add(git_index *index, const char *rel_path, int stage)
if (index->repository == NULL) if (index->repository == NULL)
return GIT_EBAREINDEX; return GIT_EBAREINDEX;
strcpy(full_path, index->repository->path_workdir); git__joinpath(full_path, index->repository->path_workdir, rel_path);
strcat(full_path, rel_path);
if (gitfo_exists(full_path) < 0) if (gitfo_exists(full_path) < 0)
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
......
...@@ -159,8 +159,7 @@ static int read_loose_ref(gitfo_buf *file_content, const char *name, const char ...@@ -159,8 +159,7 @@ static int read_loose_ref(gitfo_buf *file_content, const char *name, const char
char ref_path[GIT_PATH_MAX]; char ref_path[GIT_PATH_MAX];
/* Determine the full path of the ref */ /* Determine the full path of the ref */
strcpy(ref_path, repo_path); git__joinpath(ref_path, repo_path, name);
strcat(ref_path, name);
/* Does it even exist ? */ /* Does it even exist ? */
if (gitfo_exists(ref_path) < GIT_SUCCESS) if (gitfo_exists(ref_path) < GIT_SUCCESS)
...@@ -228,8 +227,7 @@ static int read_packed_refs(gitfo_buf *packfile, const char *repo_path) ...@@ -228,8 +227,7 @@ static int read_packed_refs(gitfo_buf *packfile, const char *repo_path)
char ref_path[GIT_PATH_MAX]; char ref_path[GIT_PATH_MAX];
/* Determine the full path of the file */ /* Determine the full path of the file */
strcpy(ref_path, repo_path); git__joinpath(ref_path, repo_path, GIT_PACKEDREFS_FILE);
strcat(ref_path, GIT_PACKEDREFS_FILE);
/* Does it even exist ? */ /* Does it even exist ? */
if (gitfo_exists(ref_path) < GIT_SUCCESS) if (gitfo_exists(ref_path) < GIT_SUCCESS)
...@@ -528,8 +526,7 @@ int git_reference_write(git_reference *ref) ...@@ -528,8 +526,7 @@ int git_reference_write(git_reference *ref)
if ((error = check_refname(ref->name)) < GIT_SUCCESS) if ((error = check_refname(ref->name)) < GIT_SUCCESS)
return error; return error;
strcpy(ref_path, ref->owner->path_repository); git__joinpath(ref_path, ref->owner->path_repository, ref->name);
strcat(ref_path, ref->name);
if ((error = git_filelock_init(&lock, ref_path)) < GIT_SUCCESS) if ((error = git_filelock_init(&lock, ref_path)) < GIT_SUCCESS)
goto error_cleanup; goto error_cleanup;
......
...@@ -202,30 +202,30 @@ const char *git__topdir(const char *path) ...@@ -202,30 +202,30 @@ const char *git__topdir(const char *path)
return &path[i + 1]; return &path[i + 1];
} }
char *git__joinpath(const char *path_a, const char *path_b) void git__joinpath_n(char *buffer_out, int count, ...)
{ {
int len_a, len_b; va_list ap;
char *path_new; int i;
assert(path_a && path_b);
len_a = strlen(path_a); va_start(ap, count);
len_b = strlen(path_b); for (i = 0; i < count; ++i) {
const char *path;
int len;
path_new = git__malloc(len_a + len_b + 2); path = va_arg(ap, const char *);
if (path_new == NULL) if (i > 0 && *path == '/')
return NULL; path++;
strcpy(path_new, path_a); len = strlen(path);
memcpy(buffer_out, path, len);
buffer_out = buffer_out + len;
if (len_a > 0 && len_b > 0 && path_new[len_a - 1] != '/') if (i < count - 1 && buffer_out[-1] != '/')
path_new[len_a++] = '/'; *buffer_out++ = '/';
}
if (path_b[0] == '/') va_end(ap);
path_b++;
strcpy(path_new + len_a, path_b); *buffer_out = '\0';
return path_new;
} }
static char *strtok_raw(char *output, char *src, char *delimit, int keep) static char *strtok_raw(char *output, char *src, char *delimit, int keep)
......
...@@ -62,9 +62,15 @@ extern const char *git__topdir(const char *path); ...@@ -62,9 +62,15 @@ extern const char *git__topdir(const char *path);
* Join two paths together. Takes care of properly fixing the * Join two paths together. Takes care of properly fixing the
* middle slashes and everything * middle slashes and everything
* *
* Returns a newly allocated string; must be free'd manually. * The paths are joined together into buffer_out; this is expected
* to be an user allocated buffer of `GIT_PATH_MAX` size
*/ */
extern char *git__joinpath(const char *path_a, const char *path_b); extern void git__joinpath_n(char *buffer_out, int npath, ...);
GIT_INLINE(void) git__joinpath(char *buffer_out, const char *path_a, const char *path_b)
{
git__joinpath_n(buffer_out, 2, path_a, path_b);
}
extern void git__hexdump(const char *buffer, size_t n); extern void git__hexdump(const char *buffer, size_t n);
extern uint32_t git__hash(const void *key, int len, uint32_t seed); extern uint32_t git__hash(const void *key, int len, uint32_t seed);
...@@ -84,18 +90,6 @@ GIT_INLINE(int) git__is_sizet(git_off_t p) ...@@ -84,18 +90,6 @@ GIT_INLINE(int) git__is_sizet(git_off_t p)
# define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s))))
#endif #endif
enum git_splitpath_flags
{
GIT_SPL_PATH = 1,
GIT_SPL_FILE = 2,
GIT_SPL_EXT = 4,
GIT_SPL_PATH_FILE = GIT_SPL_PATH + GIT_SPL_FILE,
GIT_SPL_FILE_EXT = GIT_SPL_FILE + GIT_SPL_EXT,
GIT_SPL_EXT_NO_PERIOD = 8,
};
extern char *git__splitpath(char *path, int flag);
extern char *git__strtok(char *output, char *src, char *delimit); extern char *git__strtok(char *output, char *src, char *delimit);
extern char *git__strtok_keep(char *output, char *src, char *delimit); extern char *git__strtok_keep(char *output, char *src, char *delimit);
......
...@@ -344,25 +344,15 @@ END_TEST ...@@ -344,25 +344,15 @@ END_TEST
static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path) static int ensure_joinpath(const char *path_a, const char *path_b, const char *expected_path)
{ {
int error = GIT_SUCCESS; char joined_path[GIT_PATH_MAX];
char* joined_path; git__joinpath(joined_path, path_a, path_b);
return strcmp(joined_path, expected_path) == 0 ? GIT_SUCCESS : GIT_ERROR;
joined_path = git__joinpath(path_a, path_b);
if (joined_path == NULL)
return GIT_ERROR;
if (strcmp(joined_path, expected_path))
error = GIT_ERROR;
free(joined_path);
return error;
} }
BEGIN_TEST("path", joinpath) BEGIN_TEST("path", joinpath)
must_pass(ensure_joinpath("", "", "")); must_pass(ensure_joinpath("", "", "/"));
must_pass(ensure_joinpath("", "a", "a")); must_pass(ensure_joinpath("", "a", "/a"));
must_pass(ensure_joinpath("a", "", "a")); must_pass(ensure_joinpath("a", "", "a/"));
must_pass(ensure_joinpath("a", "b", "a/b")); must_pass(ensure_joinpath("a", "b", "a/b"));
must_pass(ensure_joinpath("/", "a", "/a")); must_pass(ensure_joinpath("/", "a", "/a"));
must_pass(ensure_joinpath("/", "", "/")); must_pass(ensure_joinpath("/", "", "/"));
......
...@@ -192,7 +192,7 @@ BEGIN_TEST("createref", create_new_symbolic_ref) ...@@ -192,7 +192,7 @@ BEGIN_TEST("createref", create_new_symbolic_ref)
git_reference *new_reference, *looked_up_ref, *resolved_ref; git_reference *new_reference, *looked_up_ref, *resolved_ref;
git_repository *repo; git_repository *repo;
git_oid id; git_oid id;
char *ref_path; char ref_path[GIT_PATH_MAX];
const char *new_head_tracker = "another-head-tracker"; const char *new_head_tracker = "another-head-tracker";
...@@ -201,8 +201,7 @@ BEGIN_TEST("createref", create_new_symbolic_ref) ...@@ -201,8 +201,7 @@ BEGIN_TEST("createref", create_new_symbolic_ref)
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
/* Retrieve the physical path to the symbolic ref for further cleaning */ /* Retrieve the physical path to the symbolic ref for further cleaning */
ref_path = git__joinpath(repo->path_repository, new_head_tracker); git__joinpath(ref_path, repo->path_repository, new_head_tracker);
must_be_true(ref_path != NULL);
/* Create and write the new symbolic reference */ /* Create and write the new symbolic reference */
must_pass(git_reference_new(&new_reference, repo)); must_pass(git_reference_new(&new_reference, repo));
...@@ -211,16 +210,16 @@ BEGIN_TEST("createref", create_new_symbolic_ref) ...@@ -211,16 +210,16 @@ BEGIN_TEST("createref", create_new_symbolic_ref)
must_pass(git_reference_write(new_reference)); must_pass(git_reference_write(new_reference));
/* Ensure the reference can be looked-up... */ /* Ensure the reference can be looked-up... */
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head_tracker)); must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head_tracker));
must_be_true(looked_up_ref->type == GIT_REF_SYMBOLIC); must_be_true(looked_up_ref->type == GIT_REF_SYMBOLIC);
must_be_true(looked_up_ref->packed == 0); must_be_true(looked_up_ref->packed == 0);
must_be_true(strcmp(looked_up_ref->name, new_head_tracker) == 0); must_be_true(strcmp(looked_up_ref->name, new_head_tracker) == 0);
/* ...peeled.. */ /* ...peeled.. */
must_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
must_be_true(resolved_ref->type == GIT_REF_OID); must_be_true(resolved_ref->type == GIT_REF_OID);
/* ...and that it points to the current master tip */ /* ...and that it points to the current master tip */
must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0); must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
git_repository_free(repo); git_repository_free(repo);
...@@ -228,21 +227,20 @@ BEGIN_TEST("createref", create_new_symbolic_ref) ...@@ -228,21 +227,20 @@ BEGIN_TEST("createref", create_new_symbolic_ref)
/* Similar test with a fresh new repository */ /* Similar test with a fresh new repository */
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head_tracker)); must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head_tracker));
must_pass(git_reference_resolve(&resolved_ref, looked_up_ref)); must_pass(git_reference_resolve(&resolved_ref, looked_up_ref));
must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0); must_be_true(git_oid_cmp(&id, git_reference_oid(resolved_ref)) == 0);
git_repository_free(repo); git_repository_free(repo);
must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */ must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */
free(ref_path);
END_TEST END_TEST
BEGIN_TEST("createref", create_new_object_id_ref) BEGIN_TEST("createref", create_new_object_id_ref)
git_reference *new_reference, *looked_up_ref; git_reference *new_reference, *looked_up_ref;
git_repository *repo; git_repository *repo;
git_oid id; git_oid id;
char *ref_path; char ref_path[GIT_PATH_MAX];
const char *new_head = "refs/heads/new-head"; const char *new_head = "refs/heads/new-head";
...@@ -251,8 +249,7 @@ BEGIN_TEST("createref", create_new_object_id_ref) ...@@ -251,8 +249,7 @@ BEGIN_TEST("createref", create_new_object_id_ref)
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
/* Retrieve the physical path to the symbolic ref for further cleaning */ /* Retrieve the physical path to the symbolic ref for further cleaning */
ref_path = git__joinpath(repo->path_repository, new_head); git__joinpath(ref_path, repo->path_repository, new_head);
must_be_true(ref_path != NULL);
/* Create and write the new object id reference */ /* Create and write the new object id reference */
must_pass(git_reference_new(&new_reference, repo)); must_pass(git_reference_new(&new_reference, repo));
...@@ -261,12 +258,12 @@ BEGIN_TEST("createref", create_new_object_id_ref) ...@@ -261,12 +258,12 @@ BEGIN_TEST("createref", create_new_object_id_ref)
must_pass(git_reference_write(new_reference)); must_pass(git_reference_write(new_reference));
/* Ensure the reference can be looked-up... */ /* Ensure the reference can be looked-up... */
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head)); must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head));
must_be_true(looked_up_ref->type == GIT_REF_OID); must_be_true(looked_up_ref->type == GIT_REF_OID);
must_be_true(looked_up_ref->packed == 0); must_be_true(looked_up_ref->packed == 0);
must_be_true(strcmp(looked_up_ref->name, new_head) == 0); must_be_true(strcmp(looked_up_ref->name, new_head) == 0);
/* ...and that it points to the current master tip */ /* ...and that it points to the current master tip */
must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0); must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
git_repository_free(repo); git_repository_free(repo);
...@@ -274,13 +271,12 @@ BEGIN_TEST("createref", create_new_object_id_ref) ...@@ -274,13 +271,12 @@ BEGIN_TEST("createref", create_new_object_id_ref)
/* Similar test with a fresh new repository */ /* Similar test with a fresh new repository */
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head)); must_pass(git_repository_lookup_ref(&looked_up_ref, repo, new_head));
must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0); must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);
git_repository_free(repo); git_repository_free(repo);
must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */ must_pass(gitfo_unlink(ref_path)); /* TODO: replace with git_reference_delete() when available */
free(ref_path);
END_TEST END_TEST
git_testsuite *libgit2_suite_refs(void) git_testsuite *libgit2_suite_refs(void)
......
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