Commit 5ad739e8 by Vicent Marti

fileops: Drop `git_fileops_prettify_path`

The old `git_fileops_prettify_path` has been replaced with
`git_path_prettify`. This is a much simpler method that uses the OS's
`realpath` call to obtain the full path for directories and resolve
symlinks.

The `realpath` syscall is the original POSIX call in Unix system and
an emulated version under Windows using the Windows API.
parent f79026b4
......@@ -125,18 +125,16 @@ int git_futils_isfile(const char *path)
struct stat st;
int stat_error;
if (!path)
return git__throw(GIT_ENOTFOUND, "No path given to git_futils_isfile");
assert(path);
stat_error = p_stat(path, &st);
if (stat_error < GIT_SUCCESS)
return git__throw(GIT_ENOTFOUND, "%s does not exist", path);
return -1;
if (!S_ISREG(st.st_mode))
return git__throw(GIT_ENOTFOUND, "%s is not a file", path);
return -1;
return GIT_SUCCESS;
return 0;
}
int git_futils_exists(const char *path)
......@@ -149,7 +147,8 @@ git_off_t git_futils_filesize(git_file fd)
{
struct stat sb;
if (p_fstat(fd, &sb))
return git__throw(GIT_EOSERR, "Failed to get size of file. File missing or corrupted");
return GIT_ERROR;
return sb.st_size;
}
......@@ -273,22 +272,6 @@ int git_futils_direach(
return GIT_SUCCESS;
}
int git_futils_root_offset(const char *path)
{
int offset = 0;
#ifdef GIT_WIN32
/* Does the root of the path look like a windows drive ? */
if (isalpha(path[0]) && (path[1] == ':'))
offset += 2;
#endif
if (*(path + offset) == '/')
return offset;
return -1; /* Not a real error. Rather a signal than the path is not rooted */
}
int git_futils_mkdir_r(const char *path, int mode)
{
int error, root_path_offset;
......@@ -301,7 +284,7 @@ int git_futils_mkdir_r(const char *path, int mode)
error = GIT_SUCCESS;
pp = path_copy;
root_path_offset = git_futils_root_offset(pp);
root_path_offset = git_path_root(pp);
if (root_path_offset > 0)
pp += root_path_offset; /* On Windows, will skip the drive name (eg. C: or D:) */
......@@ -338,7 +321,7 @@ static int retrieve_previous_path_component_start(const char *path)
{
int offset, len, root_offset, start = 0;
root_offset = git_futils_root_offset(path);
root_offset = git_path_root(path);
if (root_offset > -1)
start += root_offset;
......@@ -373,7 +356,7 @@ int git_futils_prettify_dir(char *buffer_out, size_t size, const char *path, con
buffer_end = path + strlen(path);
buffer_out_start = buffer_out;
root_path_offset = git_futils_root_offset(path);
root_path_offset = git_path_root(path);
if (root_path_offset < 0) {
if (base_path == NULL) {
error = p_getcwd(buffer_out, size);
......@@ -446,7 +429,6 @@ int git_futils_prettify_dir(char *buffer_out, size_t size, const char *path, con
}
*buffer_out = '\0';
return GIT_SUCCESS;
}
......@@ -472,7 +454,7 @@ int git_futils_prettyify_file(char *buffer_out, size_t size, const char *path, c
return error; /* The callee already takes care of setting the correct error message. */
path_len = strlen(buffer_out);
root_offset = git_futils_root_offset(buffer_out) + 1;
root_offset = git_path_root(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);
......
......@@ -198,6 +198,5 @@ int git_futils_prettify_dir(char *buffer_out, size_t size, const char *path, con
*/
int git_futils_prettyify_file(char *buffer_out, size_t size, const char *path, const char *base_path);
int git_futils_root_offset(const char *path);
#endif /* INCLUDE_fileops_h__ */
#include "common.h"
#include "path.h"
#include "posix.h"
#include <stdarg.h>
#include <stdio.h>
#include <ctype.h>
......@@ -202,3 +205,50 @@ void git_path_join_n(char *buffer_out, int count, ...)
*buffer_out = '\0';
}
int git_path_root(const char *path)
{
int offset = 0;
#ifdef GIT_WIN32
/* Does the root of the path look like a windows drive ? */
if (isalpha(path[0]) && (path[1] == ':'))
offset += 2;
#endif
if (*(path + offset) == '/')
return offset;
return -1; /* Not a real error. Rather a signal than the path is not rooted */
}
int git_path_prettify(char *path_out, const char *path, const char *base)
{
char *result;
if (base == NULL || git_path_root(path) >= 0) {
result = p_realpath(path, path_out);
} else {
char aux_path[GIT_PATH_MAX];
git_path_join(aux_path, base, path);
result = p_realpath(aux_path, path_out);
}
return result ? GIT_SUCCESS : GIT_EOSERR;
}
int git_path_prettify_dir(char *path_out, const char *path, const char *base)
{
size_t end;
if (git_path_prettify(path_out, path, base) < GIT_SUCCESS)
return GIT_EOSERR;
end = strlen(path_out);
if (end && path_out[end - 1] != '/') {
path_out[end] = '/';
path_out[end + 1] = '\0';
}
return GIT_SUCCESS;
}
......@@ -59,6 +59,11 @@ GIT_INLINE(void) git_path_join(char *buffer_out, const char *path_a, const char
git_path_join_n(buffer_out, 2, path_a, path_b);
}
int git_path_root(const char *path);
int git_path_prettify(char *path_out, const char *path, const char *base);
int git_path_prettify_dir(char *path_out, const char *path, const char *base);
#ifdef GIT_WIN32
GIT_INLINE(void) git_path_mkposix(char *path)
{
......
......@@ -9,6 +9,6 @@
#define p_unlink(p) unlink(p)
#define p_mkdir(p,m) mkdir(p, m)
#define p_fsync(fd) fsync(fd)
#define p_realpath(p, r) realpath(p, r)
#define p_realpath(p, po) realpath(p, po)
#endif
#include "posix.h"
#include "path.h"
#include <errno.h>
int p_unlink(const char *path)
......@@ -189,3 +190,13 @@ int p_hide_directory__w32(const char *path)
return error;
}
int p_realpath(const char *orig_path, char *buffer)
{
int ret = GetFullPathName(orig_path, GIT_PATH_MAX, buffer, NULL);
if (!ret || ret > GIT_PATH_MAX)
return GIT_EOSERR;
git_path_mkposix(buffer);
return GIT_SUCCESS;
}
......@@ -21,5 +21,6 @@ extern int p_unlink(const char *path);
extern int p_lstat(const char *file_name, struct stat *buf);
extern int p_readlink(const char *link, char *target, size_t target_len);
extern int p_hide_directory__w32(const char *path);
extern int p_realpath(const char *orig_path, char *buffer);
#endif
......@@ -431,15 +431,15 @@ BEGIN_TEST(discover0, "test discover")
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER1, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER2, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_MALFORMED_FOLDER3, 0, ceiling_dirs));
must_be_true(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_fail(git_repository_discover(found_path, sizeof(found_path), ALTERNATE_NOT_FOUND_FOLDER, 0, ceiling_dirs));
must_pass(append_ceiling_dir(ceiling_dirs, SUB_REPOSITORY_FOLDER));
//this must pass as ceiling_directories cannot predent the current
//working directory to be checked
must_pass(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER, 0, ceiling_dirs));
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_be_true(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs) == GIT_ENOTAREPO);
must_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB, 0, ceiling_dirs));
must_fail(git_repository_discover(found_path, sizeof(found_path), SUB_REPOSITORY_FOLDER_SUB_SUB_SUB, 0, ceiling_dirs));
//.gitfile redirection should not be affected by ceiling directories
must_pass(ensure_repository_discover(REPOSITORY_ALTERNATE_FOLDER, ceiling_dirs, sub_repository_path));
......
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