对于 C++17 或更高版本,标准头 <filesystem> 带有
功能
std::filesystem::create_directories
应该在现代 C++ 程序中使用。
C++ 标准函数没有特定于 POSIX 的显式
不过,权限(模式)参数。
但是,这里有一个可以用 C++ 编译器编译的 C 函数。
/*
@(#)File: mkpath.c
@(#)Purpose: Create all directories in path
@(#)Author: J Leffler
@(#)Copyright: (C) JLSS 1990-2020
@(#)Derivation: mkpath.c 1.16 2020/06/19 15:08:10
*/
/*TABSTOP=4*/
#include "posixver.h"
#include "mkpath.h"
#include "emalloc.h"
#include <errno.h>
#include <string.h>
/* "sysstat.h" == <sys/stat.h> with fixup for (old) Windows - inc mode_t */
#include "sysstat.h"
typedef struct stat Stat;
static int do_mkdir(const char *path, mode_t mode)
{
Stat st;
int status = 0;
if (stat(path, &st) != 0)
{
/* Directory does not exist. EEXIST for race condition */
if (mkdir(path, mode) != 0 && errno != EEXIST)
status = -1;
}
else if (!S_ISDIR(st.st_mode))
{
errno = ENOTDIR;
status = -1;
}
return(status);
}
/**
** mkpath - ensure all directories in path exist
** Algorithm takes the pessimistic view and works top-down to ensure
** each directory in path exists, rather than optimistically creating
** the last element and working backwards.
*/
int mkpath(const char *path, mode_t mode)
{
char *pp;
char *sp;
int status;
char *copypath = STRDUP(path);
status = 0;
pp = copypath;
while (status == 0 && (sp = strchr(pp, '/')) != 0)
{
if (sp != pp)
{
/* Neither root nor double slash in path */
*sp = '\0';
status = do_mkdir(copypath, mode);
*sp = '/';
}
pp = sp + 1;
}
if (status == 0)
status = do_mkdir(path, mode);
FREE(copypath);
return (status);
}
#ifdef TEST
#include <stdio.h>
#include <unistd.h>
/*
** Stress test with parallel running of mkpath() function.
** Before the EEXIST test, code would fail.
** With the EEXIST test, code does not fail.
**
** Test shell script
** PREFIX=mkpath.$$
** NAME=./$PREFIX/sa/32/ad/13/23/13/12/13/sd/ds/ww/qq/ss/dd/zz/xx/dd/rr/ff/ff/ss/ss/ss/ss/ss/ss/ss/ss
** : ${MKPATH:=mkpath}
** ./$MKPATH $NAME &
** [...repeat a dozen times or so...]
** ./$MKPATH $NAME &
** wait
** rm -fr ./$PREFIX/
*/
int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++)
{
for (int j = 0; j < 20; j++)
{
if (fork() == 0)
{
int rc = mkpath(argv[i], 0777);
if (rc != 0)
fprintf(stderr, "%d: failed to create (%d: %s): %s\n",
(int)getpid(), errno, strerror(errno), argv[i]);
exit(rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
}
int status;
int fail = 0;
while (wait(&status) != -1)
{
if (WEXITSTATUS(status) != 0)
fail = 1;
}
if (fail == 0)
printf("created: %s\n", argv[i]);
}
return(0);
}
#endif /* TEST */
宏 STRDUP() 和 FREE() 是
strdup() 和 free(),在 emalloc.h 中声明(并在
emalloc.c 和 estrdup.c)。
"sysstat.h" 标头处理 <sys/stat.h> 的损坏版本
并且可以在现代 Unix 系统上被 <sys/stat.h> 替换(但有
早在 1990 年就有很多问题)。
"mkpath.h" 声明 mkpath()。
v1.12(答案的原始版本)和v1.13之间的变化
(答案的修改版本)是对EEXIST 的测试
do_mkdir()。
有人指出这是必要的
Switch — 谢谢
你,开关。
测试代码已升级并在MacBook上重现问题
Pro(2.3GHz Intel Core i7,运行 Mac OS X 10.7.4),并建议
该问题已在修订版中修复(但测试只能显示
错误的存在,从来没有他们的缺席)。
显示的代码现在是 v1.16;有化妆品或行政
自 v1.13 以来所做的更改(例如使用 mkpath.h 而不是 jlss.h 和
仅在测试代码中无条件地包含<unistd.h>)。
有理由认为 "sysstat.h" 应该替换为
<sys/stat.h> 除非你有一个异常顽固的系统。
(特此允许您出于任何目的使用此代码并注明出处。)
此代码可在我的SOQ 中找到
(堆栈溢出问题)GitHub 上的存储库作为文件mkpath.c 和
mkpath.h(等)在
src/so-0067-5039
子目录。