diff options
| author | unknown <Romain@.(none)> | 2011-06-03 20:47:56 +0200 |
|---|---|---|
| committer | Romain Geissler <romain.geissler@gmail.com> | 2011-06-03 21:04:02 +0200 |
| commit | 26a98ec8a2ebd6d54ad12466552442fde1c6a9d0 (patch) | |
| tree | fa75d50a47f5e135b34b95ba7d5742ecbca62942 /src | |
| parent | bb88da7f90f23a89ebae58bd90d9859feec84530 (diff) | |
| download | libgit2-26a98ec8a2ebd6d54ad12466552442fde1c6a9d0.tar.gz | |
Fileops: Added a fourth argument to the path prettifying functions to use an alternate basepath.
Fixed a Windows TO-DO in the prettifying functions.
Diffstat (limited to 'src')
| -rw-r--r-- | src/fileops.c | 26 | ||||
| -rw-r--r-- | src/fileops.h | 4 | ||||
| -rw-r--r-- | src/repository.c | 10 |
3 files changed, 25 insertions, 15 deletions
diff --git a/src/fileops.c b/src/fileops.c index da4e97efb..409d1cb5a 100644 --- a/src/fileops.c +++ b/src/fileops.c @@ -402,7 +402,7 @@ static int retrieve_previous_path_component_start(const char *path) return offset; } -int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path) +int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path) { int len = 0, segment_len, only_dots, root_path_offset, error = GIT_SUCCESS; char *current; @@ -414,9 +414,18 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path) root_path_offset = gitfo_retrieve_path_root_offset(path); if (root_path_offset < 0) { - error = gitfo_getcwd(buffer_out, size); - if (error < GIT_SUCCESS) - return error; /* The callee already takes care of setting the correct error message. */ + if (base_path == NULL) { + error = gitfo_getcwd(buffer_out, size); + if (error < GIT_SUCCESS) + return error; /* The callee already takes care of setting the correct error message. */ + } else { + if (size < (strlen(base_path) + 1) * sizeof(char)) + return git__throw(GIT_EOVERFLOW, "Failed to prettify dir path: the base path is too long for the buffer."); + + strcpy(buffer_out, base_path); + posixify_path(buffer_out); + git__joinpath(buffer_out, buffer_out, ""); + } len = strlen(buffer_out); buffer_out += len; @@ -480,9 +489,9 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path) return GIT_SUCCESS; } -int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path) +int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path) { - int error, path_len, i; + int error, path_len, i, root_offset; const char* pattern = "/.."; path_len = strlen(path); @@ -497,12 +506,13 @@ int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path) return git__throw(GIT_EINVALIDPATH, "Failed to normalize file path `%s`. The path points to a folder", path); } - error = gitfo_prettify_dir_path(buffer_out, size, path); + error = gitfo_prettify_dir_path(buffer_out, size, path, base_path); if (error < GIT_SUCCESS) return error; /* The callee already takes care of setting the correct error message. */ path_len = strlen(buffer_out); - if (path_len < 2) /* TODO: Fixme. We should also take of detecting Windows rooted path (probably through usage of retrieve_path_root_offset) */ + root_offset = gitfo_retrieve_path_root_offset(buffer_out) + 1; + if (path_len == root_offset) return git__throw(GIT_EINVALIDPATH, "Failed to normalize file path `%s`. The path points to a folder", path); /* Remove the trailing slash */ diff --git a/src/fileops.h b/src/fileops.h index d271f312b..1b70fc836 100644 --- a/src/fileops.h +++ b/src/fileops.h @@ -170,7 +170,7 @@ extern int gitfo_getcwd(char *buffer_out, size_t size); * - GIT_SUCCESS on success; * - GIT_ERROR when the input path is invalid or escapes the current directory. */ -int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path); +int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path); /** * Clean up a provided absolute or relative file path. @@ -193,7 +193,7 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path); * - GIT_SUCCESS on success; * - GIT_ERROR when the input path is invalid or escapes the current directory. */ -int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path); +int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path); int gitfo_retrieve_path_root_offset(const char *path); diff --git a/src/repository.c b/src/repository.c index 32ca8dd79..76042c214 100644 --- a/src/repository.c +++ b/src/repository.c @@ -65,7 +65,7 @@ static int assign_repository_dirs( if (git_dir == NULL) return git__throw(GIT_ENOTFOUND, "Failed to open repository. Git dir not found"); - error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir); + error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir, NULL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to open repository"); @@ -78,7 +78,7 @@ static int assign_repository_dirs( if (git_object_directory == NULL) git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR); else { - error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory); + error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory, NULL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to open repository"); } @@ -92,7 +92,7 @@ static int assign_repository_dirs( if (git_work_tree == NULL) repo->is_bare = 1; else { - error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree); + error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree, NULL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to open repository"); @@ -105,7 +105,7 @@ static int assign_repository_dirs( if (git_index_file == NULL) git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE); else { - error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file); + error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file, NULL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to open repository"); } @@ -390,7 +390,7 @@ static int repo_init_find_dir(repo_init *results, const char* path) char temp_path[GIT_PATH_MAX]; int error = GIT_SUCCESS; - error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path); + error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path, NULL); if (error < GIT_SUCCESS) return git__rethrow(error, "Failed to find directory to initialize repository"); |
