Commit 315a43b2 by Edward Thomson

path: introduce `git_path_str_is_valid`

Add a `git_str` based validity check; the existing `git_path_is_valid`
defers to it.
parent ebacd24c
...@@ -1647,7 +1647,7 @@ GIT_INLINE(bool) validate_length( ...@@ -1647,7 +1647,7 @@ GIT_INLINE(bool) validate_length(
} }
#endif #endif
bool git_fs_path_is_valid_str_ext( bool git_fs_path_str_is_valid_ext(
const git_str *path, const git_str *path,
unsigned int flags, unsigned int flags,
bool (*validate_char_cb)(char ch, void *payload), bool (*validate_char_cb)(char ch, void *payload),
......
...@@ -628,7 +628,7 @@ extern int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url ...@@ -628,7 +628,7 @@ extern int git_fs_path_from_url_or_path(git_str *local_path_out, const char *url
* Validate a filesystem path; with custom callbacks per-character and * Validate a filesystem path; with custom callbacks per-character and
* per-path component. * per-path component.
*/ */
extern bool git_fs_path_is_valid_str_ext( extern bool git_fs_path_str_is_valid_ext(
const git_str *path, const git_str *path,
unsigned int flags, unsigned int flags,
bool (*validate_char_cb)(char ch, void *payload), bool (*validate_char_cb)(char ch, void *payload),
...@@ -645,7 +645,7 @@ GIT_INLINE(bool) git_fs_path_is_valid_ext( ...@@ -645,7 +645,7 @@ GIT_INLINE(bool) git_fs_path_is_valid_ext(
void *payload) void *payload)
{ {
const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
return git_fs_path_is_valid_str_ext( return git_fs_path_str_is_valid_ext(
&str, &str,
flags, flags,
validate_char_cb, validate_char_cb,
...@@ -666,15 +666,15 @@ GIT_INLINE(bool) git_fs_path_is_valid( ...@@ -666,15 +666,15 @@ GIT_INLINE(bool) git_fs_path_is_valid(
unsigned int flags) unsigned int flags)
{ {
const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX); const git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
return git_fs_path_is_valid_str_ext(&str, flags, NULL, NULL, NULL, NULL); return git_fs_path_str_is_valid_ext(&str, flags, NULL, NULL, NULL, NULL);
} }
/** Validate a filesystem path in a `git_str`. */ /** Validate a filesystem path in a `git_str`. */
GIT_INLINE(bool) git_fs_path_is_valid_str( GIT_INLINE(bool) git_fs_path_str_is_valid(
const git_str *path, const git_str *path,
unsigned int flags) unsigned int flags)
{ {
return git_fs_path_is_valid_str_ext(path, flags, NULL, NULL, NULL, NULL); return git_fs_path_str_is_valid_ext(path, flags, NULL, NULL, NULL, NULL);
} }
/** /**
......
...@@ -285,9 +285,9 @@ GIT_INLINE(unsigned int) dotgit_flags( ...@@ -285,9 +285,9 @@ GIT_INLINE(unsigned int) dotgit_flags(
return flags; return flags;
} }
bool git_path_is_valid( bool git_path_str_is_valid(
git_repository *repo, git_repository *repo,
const char *path, const git_str *path,
uint16_t file_mode, uint16_t file_mode,
unsigned int flags) unsigned int flags)
{ {
...@@ -301,7 +301,7 @@ bool git_path_is_valid( ...@@ -301,7 +301,7 @@ bool git_path_is_valid(
data.file_mode = file_mode; data.file_mode = file_mode;
data.flags = flags; data.flags = flags;
return git_fs_path_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data); return git_fs_path_str_is_valid_ext(path, flags, NULL, validate_repo_component, NULL, &data);
} }
static const struct { static const struct {
......
...@@ -25,10 +25,20 @@ ...@@ -25,10 +25,20 @@
#define GIT_PATH_REJECT_INDEX_DEFAULTS \ #define GIT_PATH_REJECT_INDEX_DEFAULTS \
GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT
extern bool git_path_is_valid( extern bool git_path_str_is_valid(
git_repository *repo, git_repository *repo,
const char *path, const git_str *path,
uint16_t file_mode, uint16_t file_mode,
unsigned int flags); unsigned int flags);
GIT_INLINE(bool) git_path_is_valid(
git_repository *repo,
const char *path,
uint16_t file_mode,
unsigned int flags)
{
git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
return git_path_str_is_valid(repo, &str, file_mode, flags);
}
#endif #endif
...@@ -71,25 +71,25 @@ void test_path_core__isvalid_standard_str(void) ...@@ -71,25 +71,25 @@ void test_path_core__isvalid_standard_str(void)
unsigned int flags = GIT_FS_PATH_REJECT_EMPTY_COMPONENT; unsigned int flags = GIT_FS_PATH_REJECT_EMPTY_COMPONENT;
str.size = 0; str.size = 0;
cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
str.size = 3; str.size = 3;
cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
str.size = 4; str.size = 4;
cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
str.size = 5; str.size = 5;
cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
str.size = 7; str.size = 7;
cl_assert_equal_b(true, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(true, git_fs_path_str_is_valid(&str, flags));
str.size = 8; str.size = 8;
cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
str.size = strlen(str.ptr); str.size = strlen(str.ptr);
cl_assert_equal_b(false, git_fs_path_is_valid_str(&str, flags)); cl_assert_equal_b(false, git_fs_path_str_is_valid(&str, flags));
} }
void test_path_core__isvalid_empty_dir_component(void) void test_path_core__isvalid_empty_dir_component(void)
......
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