mirror of
https://github.com/AuxXxilium/kmod.git
synced 2024-12-25 03:35:03 +07:00
testsuite: Fix mkdir_p corner cases
- Fix infinite loop when path is relative - Fix not considering EEXIST as a success - General refactor to mkdir_p so it never calls mkdir for an existing dir (given no one creates it from outside)
This commit is contained in:
parent
83b855a6ed
commit
7980eaf0ec
@ -23,47 +23,60 @@
|
|||||||
#include "mkdir.h"
|
#include "mkdir.h"
|
||||||
#include "testsuite.h"
|
#include "testsuite.h"
|
||||||
|
|
||||||
|
static inline int is_dir(const char *path)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if (stat(path, &st) >= 0)
|
||||||
|
return S_ISDIR(st.st_mode);
|
||||||
|
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
TS_EXPORT int mkdir_p(const char *path, mode_t mode)
|
TS_EXPORT int mkdir_p(const char *path, mode_t mode)
|
||||||
{
|
{
|
||||||
char *start = strdupa(path);
|
char *start = strdupa(path);
|
||||||
int len = strlen(path);
|
int len = strlen(path);
|
||||||
char *end = start + len;
|
char *end = start + len;
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* scan backwards, replacing '/' with '\0' while the component doesn't
|
* scan backwards, replacing '/' with '\0' while the component doesn't
|
||||||
* exist
|
* exist
|
||||||
*/
|
*/
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (stat(start, &st) >= 0) {
|
int r = is_dir(start);
|
||||||
if (S_ISDIR(st.st_mode))
|
if (r > 0) {
|
||||||
break;
|
end += strlen(end);
|
||||||
|
|
||||||
|
if (end == start + len)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* end != start, since it would be caught on the first
|
||||||
|
* iteration */
|
||||||
|
*end = '/';
|
||||||
|
break;
|
||||||
|
} else if (r == 0)
|
||||||
return -ENOTDIR;
|
return -ENOTDIR;
|
||||||
}
|
|
||||||
|
|
||||||
/* Find the next component, backwards, discarding extra '/'*/
|
|
||||||
for (; end != start && *end != '/'; end--)
|
|
||||||
;
|
|
||||||
|
|
||||||
for (; end != start - 1 && *end == '/'; end--)
|
|
||||||
;
|
|
||||||
|
|
||||||
end++;
|
|
||||||
if (end == start)
|
if (end == start)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
*end = '\0';
|
*end = '\0';
|
||||||
|
|
||||||
|
/* Find the next component, backwards, discarding extra '/'*/
|
||||||
|
while (end > start && *end != '/')
|
||||||
|
end--;
|
||||||
|
|
||||||
|
while (end > start && *(end - 1) == '/')
|
||||||
|
end--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (end == start + len)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
for (; end < start + len;) {
|
for (; end < start + len;) {
|
||||||
*end = '/';
|
if (mkdir(start, mode) < 0 && errno != EEXIST)
|
||||||
end += strlen(end);
|
|
||||||
|
|
||||||
if (mkdir(start, mode) < 0)
|
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
||||||
|
end += strlen(end);
|
||||||
|
*end = '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user