path: provide a helper to compute len with trailing slashes

We may need to strip multiple slashes at the end of a path; provide a
method to do so.
This commit is contained in:
Edward Thomson
2024-02-18 00:02:47 +00:00
parent 49487b5e39
commit bec1d7b7b0
3 changed files with 28 additions and 0 deletions

View File

@@ -419,6 +419,16 @@ int git_fs_path_to_dir(git_str *path)
return git_str_oom(path) ? -1 : 0;
}
size_t git_fs_path_dirlen(const char *path)
{
size_t len = strlen(path);
while (len > 1 && path[len - 1] == '/')
len--;
return len;
}
void git_fs_path_string_to_dir(char *path, size_t size)
{
size_t end = strlen(path);

View File

@@ -86,6 +86,12 @@ extern int git_fs_path_to_dir(git_str *path);
*/
extern void git_fs_path_string_to_dir(char *path, size_t size);
/**
* Provides the length of the given path string with no trailing
* slashes.
*/
size_t git_fs_path_dirlen(const char *path);
/**
* Taken from git.git; returns nonzero if the given path is "." or "..".
*/

View File

@@ -766,3 +766,15 @@ void test_path__validate_current_user_ownership(void)
cl_git_fail(git_fs_path_owner_is_current_user(&is_cur, "/path/does/not/exist"));
#endif
}
void test_path__dirlen(void)
{
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf"));
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf/"));
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf//"));
cl_assert_equal_sz(3, git_fs_path_dirlen("foo////"));
cl_assert_equal_sz(3, git_fs_path_dirlen("foo"));
cl_assert_equal_sz(1, git_fs_path_dirlen("/"));
cl_assert_equal_sz(1, git_fs_path_dirlen("////"));
cl_assert_equal_sz(0, git_fs_path_dirlen(""));
}