Fileops: Added a fourth argument to the path prettifying functions to use an alternate basepath. Fixed a Windows TO-DO in the prettifying functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
diff --git a/src/fileops.c b/src/fileops.c
index da4e97e..409d1cb 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 d271f31..1b70fc8 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 32ca8dd..76042c2 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");
diff --git a/tests/t00-core.c b/tests/t00-core.c
index ab9a683..9498178 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -151,7 +151,7 @@ BEGIN_TEST(path2, "get the latest component in a path")
#undef TOPDIR_TEST
END_TEST
-typedef int (normalize_path)(char *, size_t, const char *);
+typedef int (normalize_path)(char *, size_t, const char *, const char *);
/* Assert flags */
#define CWD_AS_PREFIX 1
@@ -168,7 +168,7 @@ static int ensure_normalized(const char *input_path, const char *expected_path,
if (error < GIT_SUCCESS)
return error;
- error = normalizer(buffer_out, sizeof(buffer_out), input_path);
+ error = normalizer(buffer_out, sizeof(buffer_out), input_path, NULL);
if (error < GIT_SUCCESS)
return error;
@@ -417,7 +417,7 @@ BEGIN_TEST(path7, "prevent a path which escapes the root directory from being pr
for (i = 0; i < number_to_escape + 1; i++)
git__joinpath(current_workdir, current_workdir, "../");
- must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir));
+ must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir, NULL));
END_TEST
typedef struct name_data {