diff options
| author | Vicent Marti <tanoku@gmail.com> | 2011-02-09 12:43:19 +0200 |
|---|---|---|
| committer | Vicent Marti <tanoku@gmail.com> | 2011-02-09 12:43:19 +0200 |
| commit | 995f9c34a580bc17be577d807f9a3bcfd417a748 (patch) | |
| tree | d8039db19889dce3ce99eb525367083030335bbe /src | |
| parent | 772c7e762363d6d5b5627941057775ba06886bb6 (diff) | |
| download | libgit2-995f9c34a580bc17be577d807f9a3bcfd417a748.tar.gz | |
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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/index.c | 3 | ||||
| -rw-r--r-- | src/refs.c | 9 | ||||
| -rw-r--r-- | src/util.c | 36 | ||||
| -rw-r--r-- | src/util.h | 22 |
4 files changed, 30 insertions, 40 deletions
diff --git a/src/index.c b/src/index.c index 6fdb46e18..0e77105b8 100644 --- a/src/index.c +++ b/src/index.c @@ -289,8 +289,7 @@ int git_index_add(git_index *index, const char *rel_path, int stage) if (index->repository == NULL) return GIT_EBAREINDEX; - strcpy(full_path, index->repository->path_workdir); - strcat(full_path, rel_path); + git__joinpath(full_path, index->repository->path_workdir, rel_path); if (gitfo_exists(full_path) < 0) return GIT_ENOTFOUND; diff --git a/src/refs.c b/src/refs.c index eca50ddaa..a3055650f 100644 --- a/src/refs.c +++ b/src/refs.c @@ -159,8 +159,7 @@ static int read_loose_ref(gitfo_buf *file_content, const char *name, const char char ref_path[GIT_PATH_MAX]; /* Determine the full path of the ref */ - strcpy(ref_path, repo_path); - strcat(ref_path, name); + git__joinpath(ref_path, repo_path, name); /* Does it even exist ? */ if (gitfo_exists(ref_path) < GIT_SUCCESS) @@ -228,8 +227,7 @@ static int read_packed_refs(gitfo_buf *packfile, const char *repo_path) char ref_path[GIT_PATH_MAX]; /* Determine the full path of the file */ - strcpy(ref_path, repo_path); - strcat(ref_path, GIT_PACKEDREFS_FILE); + git__joinpath(ref_path, repo_path, GIT_PACKEDREFS_FILE); /* Does it even exist ? */ if (gitfo_exists(ref_path) < GIT_SUCCESS) @@ -528,8 +526,7 @@ int git_reference_write(git_reference *ref) if ((error = check_refname(ref->name)) < GIT_SUCCESS) return error; - strcpy(ref_path, ref->owner->path_repository); - strcat(ref_path, ref->name); + git__joinpath(ref_path, ref->owner->path_repository, ref->name); if ((error = git_filelock_init(&lock, ref_path)) < GIT_SUCCESS) goto error_cleanup; diff --git a/src/util.c b/src/util.c index 67b74eeba..fc1682f90 100644 --- a/src/util.c +++ b/src/util.c @@ -202,30 +202,30 @@ const char *git__topdir(const char *path) 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; - char *path_new; - - assert(path_a && path_b); + va_list ap; + int i; - len_a = strlen(path_a); - len_b = strlen(path_b); + va_start(ap, count); + for (i = 0; i < count; ++i) { + const char *path; + int len; - path_new = git__malloc(len_a + len_b + 2); - if (path_new == NULL) - return NULL; + path = va_arg(ap, const char *); + if (i > 0 && *path == '/') + 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] != '/') - path_new[len_a++] = '/'; - - if (path_b[0] == '/') - path_b++; + if (i < count - 1 && buffer_out[-1] != '/') + *buffer_out++ = '/'; + } + va_end(ap); - strcpy(path_new + len_a, path_b); - return path_new; + *buffer_out = '\0'; } static char *strtok_raw(char *output, char *src, char *delimit, int keep) diff --git a/src/util.h b/src/util.h index 0f010929f..d5320e15b 100644 --- a/src/util.h +++ b/src/util.h @@ -62,9 +62,15 @@ extern const char *git__topdir(const char *path); * Join two paths together. Takes care of properly fixing the * 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 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) # define git__rotl(v, s) (uint32_t)(((uint32_t)(v) << (s)) | ((uint32_t)(v) >> (32 - (s)))) #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_keep(char *output, char *src, char *delimit); |
