1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include "sysdeps.h"
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include "path.h"
/** Create a directory, optionally creating any parent directories.
This function creates the named directory using <tt>mkdir</tt>. If any
of the parent directories of the specified path do not exist, they are
created as well (with the same mode).
\note Unlike the "<tt>mkdir -p</tt>" command, this function will result
in an error if the path already exists.
*/
int path_mkdirs(const char* path, unsigned mode)
{
int i;
i = strlen(path);
while (i > 0 && path[i-1] == '/')
--i;
while (i > 0 && path[i-1] != '/')
--i;
while (i > 0 && path[i-1] == '/')
--i;
if (i > 0) {
struct stat st;
char prefix[i+1];
memcpy(prefix, path, i);
prefix[i] = 0;
if (stat(prefix, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
errno = ENOTDIR;
return -1;
}
}
else if (errno != ENOENT)
return -1;
else if (path_mkdirs(prefix, mode) != 0)
return -1;
}
return mkdir(path, mode);
}
|