Commit 618818dc by nulltoken Committed by Vicent Marti

Added git_prettify_file_path().

parent 4581c22a
......@@ -456,3 +456,30 @@ int git_prettify_dir_path(char *buffer_out, const char *path)
return GIT_SUCCESS;
}
int git_prettify_file_path(char *buffer_out, const char *path)
{
int error, path_len, i;
const char* pattern = "/..";
path_len = strlen(path);
/* Let's make sure the filename doesn't end with "/", "/." or "/.." */
for (i = 1; path_len > i && i < 4; i++) {
if (!strncmp(path + path_len - i, pattern, i))
return GIT_ERROR;
}
error = git_prettify_dir_path(buffer_out, path);
if (error < GIT_SUCCESS)
return error;
path_len = strlen(buffer_out);
if (path_len < 2)
return GIT_ERROR;
/* Remove the trailing slash */
buffer_out[path_len - 1] = '\0';
return GIT_SUCCESS;
}
......@@ -155,4 +155,26 @@ extern int gitfo_close_cached(gitfo_cache *ioc);
*/
GIT_EXTERN(int) git_prettify_dir_path(char *buffer_out, const char *path);
/**
* Clean up a provided absolute or relative file path.
*
* This prettification relies on basic operations such as coalescing
* multiple forward slashes into a single slash, removing '.' and
* './' current directory segments, and removing parent directory
* whenever '..' is encountered.
*
* For instance, this will turn "d1/s1///s2/..//../s3" into "d1/s3".
*
* This only performs a string based analysis of the path.
* No checks are done to make sure the path actually makes sense from
* the file system perspective.
*
* @param buffer_out buffer to populate with the normalized path.
* @param path file path to clean up.
* @return
* - GIT_SUCCESS on success;
* - GIT_ERROR when the input path is invalid or escapes the current directory.
*/
GIT_EXTERN(int) git_prettify_file_path(char *buffer_out, const char *path);
#endif /* INCLUDE_fileops_h__ */
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