path: Also accept / as directory separator on Windows

This adds helper functions to determine the first or last directory separator
in a string and to check if a given character is a separator.

Paths starting with a separator are now also considered absolute on
Windows as these are rooted at the current drive.

Note that it's fine to use DIRECTORY_SEPARATOR when combining strings as
Windows API calls accept both forward and backward slashes as separators.

Co-authored-by: Michał Skalski <[email protected]>

References #3684.
This commit is contained in:
Tobias Brunner
2021-02-03 17:27:57 +01:00
co-authored by Michał Skalski
parent 3de65f8d67
commit e6a6fc33b6
5 changed files with 225 additions and 42 deletions
@@ -337,7 +337,7 @@ METHOD(attest_db_t, set_directory, bool,
/* remove trailing '/' or '\' character if not root directory */ /* remove trailing '/' or '\' character if not root directory */
len = strlen(dir); len = strlen(dir);
if (len > 1 && dir[len-1] == DIRECTORY_SEPARATOR[0]) if (len > 1 && path_is_separator(dir[len-1]))
{ {
dir[len-1] = '\0'; dir[len-1] = '\0';
} }
+2 -2
View File
@@ -173,9 +173,9 @@ enumerator_t* enumerator_create_directory(const char *path)
return NULL; return NULL;
} }
/* append a '/' if not already done */ /* append a '/' if not already done */
if (this->full[len-1] != '/') if (!path_is_separator(this->full[len-1]))
{ {
this->full[len++] = '/'; this->full[len++] = DIRECTORY_SEPARATOR[0];
this->full[len] = '\0'; this->full[len] = '\0';
} }
this->full_end = &this->full[len]; this->full_end = &this->full[len];
+103 -3
View File
@@ -766,6 +766,97 @@ START_TEST(test_strreplace)
} }
END_TEST END_TEST
/*******************************************************************************
* path_first/last_separator
*/
static struct {
char *path;
int len;
int first;
int last;
} separator_data[] = {
{NULL, -1, -1, -1},
{"", -1, -1, -1},
{".", -1, -1, -1},
{"..", -1, -1, -1},
#ifdef WIN32
{"C:\\", -1, 2, 2},
{"C:/", -1, 2, 2},
{"X:\\\\", -1, 2, 3},
{"d:\\f", -1, 2, 2},
{"d:\\f", 2, -1, -1},
{"C:\\foo\\", -1, 2, 6},
{"foo\\bar", -1, 3, 3},
{"foo\\\\bar", -1, 3, 4},
{"\\foo\\bar", -1, 0, 4},
{"\\\\foo\\bar", -1, 0, 5},
{"\\\\foo\\bar", 4, 0, 1},
{"foo\\bar/baz", -1, 3, 7},
#endif /* WIN32 */
{"/", -1, 0, 0},
{"//", -1, 0, 1},
{"foo", -1, -1, -1},
{"f/", -1, 1, 1},
{"foo/", -1, 3, 3},
{"foo/", 2, -1, -1},
{"foo//", -1, 3, 4},
{"/foo", -1, 0, 0},
{"/foo/", -1, 0, 4},
{"/foo/", 3, 0, 0},
{"//foo/", -1, 0, 5},
{"foo/bar", -1, 3, 3},
{"foo/bar", 1, -1, -1},
{"foo/bar", 2, -1, -1},
{"foo/bar", 3, -1, -1},
{"foo/bar", 4, 3, 3},
{"foo/bar", 5, 3, 3},
{"foo//bar", -1, 3, 4},
{"/foo/bar", -1, 0, 4},
{"/foo/bar/", -1, 0, 8},
{"/foo/bar/", 0, -1, -1},
{"/foo/bar/", 1, 0, 0},
{"/foo/bar/", 2, 0, 0},
{"/foo/bar/", 3, 0, 0},
{"/foo/bar/", 4, 0, 0},
{"/foo/bar/", 5, 0, 4},
{"/foo/bar/", 7, 0, 4},
{"/foo/bar/", 8, 0, 4},
{"/foo/bar/", 9, 0, 8},
};
START_TEST(test_path_first_separator)
{
char *pos;
pos = path_first_separator(separator_data[_i].path, separator_data[_i].len);
if (separator_data[_i].first >= 0)
{
ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].first);
}
else
{
ck_assert(!pos);
}
}
END_TEST
START_TEST(test_path_last_separator)
{
char *pos;
pos = path_last_separator(separator_data[_i].path, separator_data[_i].len);
if (separator_data[_i].last >= 0)
{
ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].last);
}
else
{
ck_assert(!pos);
}
}
END_TEST
/******************************************************************************* /*******************************************************************************
* path_dirname/basename/absolute * path_dirname/basename/absolute
*/ */
@@ -782,6 +873,7 @@ static struct {
{"..", ".", "..", FALSE}, {"..", ".", "..", FALSE},
#ifdef WIN32 #ifdef WIN32
{"C:\\", "C:", "C:", TRUE}, {"C:\\", "C:", "C:", TRUE},
{"C:/", "C:", "C:", TRUE},
{"X:\\\\", "X:", "X:", TRUE}, {"X:\\\\", "X:", "X:", TRUE},
{"foo", ".", "foo", FALSE}, {"foo", ".", "foo", FALSE},
{"f\\", ".", "f", FALSE}, {"f\\", ".", "f", FALSE},
@@ -789,16 +881,20 @@ static struct {
{"foo\\\\", ".", "foo", FALSE}, {"foo\\\\", ".", "foo", FALSE},
{"d:\\f", "d:", "f", TRUE}, {"d:\\f", "d:", "f", TRUE},
{"C:\\f\\", "C:", "f", TRUE}, {"C:\\f\\", "C:", "f", TRUE},
{"C:\\f\\", "C:", "f", TRUE},
{"C:\\foo", "C:", "foo", TRUE}, {"C:\\foo", "C:", "foo", TRUE},
{"C:\\foo\\", "C:", "foo", TRUE}, {"C:\\foo\\", "C:", "foo", TRUE},
{"C:\\foo/", "C:", "foo", TRUE},
{"foo\\bar", "foo", "bar", FALSE}, {"foo\\bar", "foo", "bar", FALSE},
{"foo\\\\bar", "foo", "bar", FALSE}, {"foo\\\\bar", "foo", "bar", FALSE},
{"C:\\foo\\bar", "C:\\foo", "bar", TRUE}, {"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
{"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE}, {"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
{"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE}, {"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
{"\\foo\\bar", "\\foo", "bar", FALSE}, {"C:\\foo/bar\\baz", "C:\\foo/bar", "baz", TRUE},
{"C:/foo/bar/baz", "C:/foo/bar", "baz", TRUE},
{"\\foo\\bar", "\\foo", "bar", TRUE},
{"\\\\foo\\bar", "\\\\foo", "bar", TRUE}, {"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
#else /* !WIN32 */ #endif /* WIN32 */
{"/", "/", "/", TRUE}, {"/", "/", "/", TRUE},
{"//", "/", "/", TRUE}, {"//", "/", "/", TRUE},
{"foo", ".", "foo", FALSE}, {"foo", ".", "foo", FALSE},
@@ -815,7 +911,6 @@ static struct {
{"/foo/bar", "/foo", "bar", TRUE}, {"/foo/bar", "/foo", "bar", TRUE},
{"/foo/bar/", "/foo", "bar", TRUE}, {"/foo/bar/", "/foo", "bar", TRUE},
{"/foo/bar/baz", "/foo/bar", "baz", TRUE}, {"/foo/bar/baz", "/foo/bar", "baz", TRUE},
#endif
}; };
START_TEST(test_path_dirname) START_TEST(test_path_dirname)
@@ -1207,6 +1302,11 @@ Suite *utils_suite_create()
tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data)); tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
suite_add_tcase(s, tc); suite_add_tcase(s, tc);
tc = tcase_create("path_first/last_separator");
tcase_add_loop_test(tc, test_path_first_separator, 0, countof(separator_data));
tcase_add_loop_test(tc, test_path_last_separator, 0, countof(separator_data));
suite_add_tcase(s, tc);
tc = tcase_create("path_dirname"); tc = tcase_create("path_dirname");
tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data)); tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
suite_add_tcase(s, tc); suite_add_tcase(s, tc);
+82 -36
View File
@@ -24,22 +24,68 @@
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
/** /*
* Described in header. * Described in header
*/ */
char* path_dirname(const char *path) char *path_first_separator(const char *path, int len)
{
if (!path)
{
return NULL;
}
if (len < 0)
{
len = strlen(path);
}
for (; len; path++, len--)
{
if (path_is_separator(*path))
{
return (char*)path;
}
}
return NULL;
}
/*
* Described in header
*/
char *path_last_separator(const char *path, int len)
{
if (!path)
{
return NULL;
}
if (len < 0)
{
len = strlen(path);
}
while (len)
{
if (path_is_separator(path[--len]))
{
return (char*)&path[len];
}
}
return NULL;
}
/*
* Described in header
*/
char *path_dirname(const char *path)
{ {
char *pos; char *pos;
pos = path ? strrchr(path, DIRECTORY_SEPARATOR[0]) : NULL; pos = path_last_separator(path, -1);
if (pos && !pos[1]) if (pos && !pos[1])
{ /* if path ends with slashes we have to look beyond them */ { /* if path ends with separators, we have to look beyond them */
while (pos > path && *pos == DIRECTORY_SEPARATOR[0]) while (pos > path && path_is_separator(*pos))
{ /* skip trailing slashes */ { /* skip trailing separators */
pos--; pos--;
} }
pos = memrchr(path, DIRECTORY_SEPARATOR[0], pos - path + 1); pos = path_last_separator(path, pos - path + 1);
} }
if (!pos) if (!pos)
{ {
@@ -54,17 +100,17 @@ char* path_dirname(const char *path)
#endif #endif
return strdup("."); return strdup(".");
} }
while (pos > path && *pos == DIRECTORY_SEPARATOR[0]) while (pos > path && path_is_separator(*pos))
{ /* skip superfluous slashes */ { /* skip superfluous separators */
pos--; pos--;
} }
return strndup(path, pos - path + 1); return strndup(path, pos - path + 1);
} }
/** /*
* Described in header. * Described in header
*/ */
char* path_basename(const char *path) char *path_basename(const char *path)
{ {
char *pos, *trail = NULL; char *pos, *trail = NULL;
@@ -72,26 +118,26 @@ char* path_basename(const char *path)
{ {
return strdup("."); return strdup(".");
} }
pos = strrchr(path, DIRECTORY_SEPARATOR[0]); pos = path_last_separator(path, -1);
if (pos && !pos[1]) if (pos && !pos[1])
{ /* if path ends with slashes we have to look beyond them */ { /* if path ends with separators, we have to look beyond them */
while (pos > path && *pos == DIRECTORY_SEPARATOR[0]) while (pos > path && path_is_separator(*pos))
{ /* skip trailing slashes */ { /* skip trailing separators */
pos--; pos--;
} }
if (pos == path && *pos == DIRECTORY_SEPARATOR[0]) if (pos == path && path_is_separator(*pos))
{ /* contains only slashes */ { /* contains only separators */
return strdup(DIRECTORY_SEPARATOR); return strndup(pos, 1);
} }
trail = pos + 1; trail = pos + 1;
pos = memrchr(path, DIRECTORY_SEPARATOR[0], trail - path); pos = path_last_separator(path, trail - path);
} }
pos = pos ? pos + 1 : (char*)path; pos = pos ? pos + 1 : (char*)path;
return trail ? strndup(pos, trail - pos) : strdup(pos); return trail ? strndup(pos, trail - pos) : strdup(pos);
} }
/** /*
* Described in header. * Described in header
*/ */
bool path_absolute(const char *path) bool path_absolute(const char *path)
{ {
@@ -108,22 +154,21 @@ bool path_absolute(const char *path)
{ /* drive letter */ { /* drive letter */
return TRUE; return TRUE;
} }
#else /* !WIN32 */ #endif /* WIN32 */
if (path[0] == DIRECTORY_SEPARATOR[0]) if (path_is_separator(path[0]))
{ {
return TRUE; return TRUE;
} }
#endif
return FALSE; return FALSE;
} }
/** /*
* Described in header. * Described in header
*/ */
bool mkdir_p(const char *path, mode_t mode) bool mkdir_p(const char *path, mode_t mode)
{ {
int len; int len;
char *pos, full[PATH_MAX]; char *pos, sep, full[PATH_MAX];
pos = full; pos = full;
if (!path || *path == '\0') if (!path || *path == '\0')
{ {
@@ -135,19 +180,20 @@ bool mkdir_p(const char *path, mode_t mode)
DBG1(DBG_LIB, "path string %s too long", path); DBG1(DBG_LIB, "path string %s too long", path);
return FALSE; return FALSE;
} }
/* ensure that the path ends with a '/' */ /* ensure that the path ends with a separator */
if (full[len-1] != '/') if (!path_is_separator(full[len-1]))
{ {
full[len++] = '/'; full[len++] = DIRECTORY_SEPARATOR[0];
full[len] = '\0'; full[len] = '\0';
} }
/* skip '/' at the beginning */ /* skip separators at the beginning */
while (*pos == '/') while (path_is_separator(*pos))
{ {
pos++; pos++;
} }
while ((pos = strchr(pos, '/'))) while ((pos = path_first_separator(pos, -1)))
{ {
sep = *pos;
*pos = '\0'; *pos = '\0';
if (access(full, F_OK) < 0) if (access(full, F_OK) < 0)
{ {
@@ -161,7 +207,7 @@ bool mkdir_p(const char *path, mode_t mode)
return FALSE; return FALSE;
} }
} }
*pos = '/'; *pos = sep;
pos++; pos++;
} }
return TRUE; return TRUE;
+37
View File
@@ -27,10 +27,47 @@
*/ */
#ifdef WIN32 #ifdef WIN32
# define DIRECTORY_SEPARATOR "\\" # define DIRECTORY_SEPARATOR "\\"
/* the Windows API also accepts / as separator */
# define DIRECTORY_SEPARATOR_ALT "/"
#else #else
# define DIRECTORY_SEPARATOR "/" # define DIRECTORY_SEPARATOR "/"
#endif #endif
/**
* Check if the given character is a directory separator.
*
* @param c character
* @return TRUE if the character is a directory separator
*/
static inline bool path_is_separator(char c)
{
#ifdef WIN32
return c == DIRECTORY_SEPARATOR[0] || c == DIRECTORY_SEPARATOR_ALT[0];
#else
return c == DIRECTORY_SEPARATOR[0];
#endif
}
/**
* Returns a pointer to the first occurrence of a directory separator in the
* given string (optionally limited to a given length).
*
* @param path path to search
* @param len optional length to search, -1 for the full string
* @return pointer to separator, or NULL if not found
*/
char *path_first_separator(const char *path, int len);
/**
* Returns a pointer to the last occurrence of a directory separator in the
* given string (optionally limited to a given length).
*
* @param path path to search
* @param len optional length to search, -1 for the full string
* @return pointer to separator, or NULL if not found
*/
char *path_last_separator(const char *path, int len);
/** /**
* Like dirname(3) returns the directory part of the given null-terminated * Like dirname(3) returns the directory part of the given null-terminated
* pathname, up to but not including the final '/' (or '.' if no '/' is found). * pathname, up to but not including the final '/' (or '.' if no '/' is found).