Commit 26a98ec8 by unknown Committed by Romain Geissler

Fileops: Added a fourth argument to the path prettifying functions to use an alternate basepath.

Fixed a Windows TO-DO in the prettifying functions.
parent bb88da7f
...@@ -402,7 +402,7 @@ static int retrieve_previous_path_component_start(const char *path) ...@@ -402,7 +402,7 @@ static int retrieve_previous_path_component_start(const char *path)
return offset; 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; int len = 0, segment_len, only_dots, root_path_offset, error = GIT_SUCCESS;
char *current; char *current;
...@@ -414,9 +414,18 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path) ...@@ -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); root_path_offset = gitfo_retrieve_path_root_offset(path);
if (root_path_offset < 0) { if (root_path_offset < 0) {
error = gitfo_getcwd(buffer_out, size); if (base_path == NULL) {
if (error < GIT_SUCCESS) error = gitfo_getcwd(buffer_out, size);
return error; /* The callee already takes care of setting the correct error message. */ 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); len = strlen(buffer_out);
buffer_out += len; buffer_out += len;
...@@ -480,9 +489,9 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path) ...@@ -480,9 +489,9 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path)
return GIT_SUCCESS; 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 = "/.."; const char* pattern = "/..";
path_len = strlen(path); path_len = strlen(path);
...@@ -497,12 +506,13 @@ int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *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); 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) if (error < GIT_SUCCESS)
return error; /* The callee already takes care of setting the correct error message. */ return error; /* The callee already takes care of setting the correct error message. */
path_len = strlen(buffer_out); 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); return git__throw(GIT_EINVALIDPATH, "Failed to normalize file path `%s`. The path points to a folder", path);
/* Remove the trailing slash */ /* Remove the trailing slash */
......
...@@ -170,7 +170,7 @@ extern int gitfo_getcwd(char *buffer_out, size_t size); ...@@ -170,7 +170,7 @@ extern int gitfo_getcwd(char *buffer_out, size_t size);
* - GIT_SUCCESS on success; * - GIT_SUCCESS on success;
* - GIT_ERROR when the input path is invalid or escapes the current directory. * - 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. * 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); ...@@ -193,7 +193,7 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path);
* - GIT_SUCCESS on success; * - GIT_SUCCESS on success;
* - GIT_ERROR when the input path is invalid or escapes the current directory. * - 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); int gitfo_retrieve_path_root_offset(const char *path);
......
...@@ -65,7 +65,7 @@ static int assign_repository_dirs( ...@@ -65,7 +65,7 @@ static int assign_repository_dirs(
if (git_dir == NULL) if (git_dir == NULL)
return git__throw(GIT_ENOTFOUND, "Failed to open repository. Git dir not found"); 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) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to open repository"); return git__rethrow(error, "Failed to open repository");
...@@ -78,7 +78,7 @@ static int assign_repository_dirs( ...@@ -78,7 +78,7 @@ static int assign_repository_dirs(
if (git_object_directory == NULL) if (git_object_directory == NULL)
git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR); git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
else { 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) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to open repository"); return git__rethrow(error, "Failed to open repository");
} }
...@@ -92,7 +92,7 @@ static int assign_repository_dirs( ...@@ -92,7 +92,7 @@ static int assign_repository_dirs(
if (git_work_tree == NULL) if (git_work_tree == NULL)
repo->is_bare = 1; repo->is_bare = 1;
else { 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) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to open repository"); return git__rethrow(error, "Failed to open repository");
...@@ -105,7 +105,7 @@ static int assign_repository_dirs( ...@@ -105,7 +105,7 @@ static int assign_repository_dirs(
if (git_index_file == NULL) if (git_index_file == NULL)
git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE); git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
else { 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) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to open repository"); return git__rethrow(error, "Failed to open repository");
} }
...@@ -390,7 +390,7 @@ static int repo_init_find_dir(repo_init *results, const char* path) ...@@ -390,7 +390,7 @@ static int repo_init_find_dir(repo_init *results, const char* path)
char temp_path[GIT_PATH_MAX]; char temp_path[GIT_PATH_MAX];
int error = GIT_SUCCESS; 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) if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to find directory to initialize repository"); return git__rethrow(error, "Failed to find directory to initialize repository");
......
...@@ -151,7 +151,7 @@ BEGIN_TEST(path2, "get the latest component in a path") ...@@ -151,7 +151,7 @@ BEGIN_TEST(path2, "get the latest component in a path")
#undef TOPDIR_TEST #undef TOPDIR_TEST
END_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 */ /* Assert flags */
#define CWD_AS_PREFIX 1 #define CWD_AS_PREFIX 1
...@@ -168,7 +168,7 @@ static int ensure_normalized(const char *input_path, const char *expected_path, ...@@ -168,7 +168,7 @@ static int ensure_normalized(const char *input_path, const char *expected_path,
if (error < GIT_SUCCESS) if (error < GIT_SUCCESS)
return error; 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) if (error < GIT_SUCCESS)
return error; return error;
...@@ -417,7 +417,7 @@ BEGIN_TEST(path7, "prevent a path which escapes the root directory from being pr ...@@ -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++) for (i = 0; i < number_to_escape + 1; i++)
git__joinpath(current_workdir, current_workdir, "../"); 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 END_TEST
typedef struct name_data { typedef struct name_data {
......
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