Unix 上的递归 mkdir() 系统调用
阅读具有该名称的 Unix 系统调用的 mkdir(2) 手册页后,该调用似乎不会在路径中创建中间目录,而只会在路径中创建最后一个目录。有没有什么方法(或其他函数)可以创建路径中的所有目录,而无需手动解析我的目录字符串并单独创建每个目录?
After reading the mkdir(2) man page for the Unix system call with that name, it appears that the call doesn't create intermediate directories in a path, only the last directory in the path. Is there any way (or other function) to create all the directories in the path without resorting to manually parsing my directory string and individually creating each directory ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我迟到了,但我仍然想分享这个非常简单的功能。我不明白为什么人们把事情搞得这么复杂。
请注意,
mkdir_p("this/is/a/path", 0755)
创建目录"this/is/a/"
,就好像它是一个文件路径一样。I'm late to the game, but I still wanted to share this very simple function. I don't get why people make things so complicated.
Note that
mkdir_p("this/is/a/path", 0755)
creates the directory"this/is/a/"
, as if it were a file path.给出的其他两个答案是针对
mkdir(1)
而不是您要求的mkdir(2)
,但您可以查看 该程序的源代码 并查看它如何实现-p
选项根据需要重复调用mkdir(2)
。The two other answers given are for
mkdir(1)
and notmkdir(2)
like you ask for, but you can look at the source code for that program and see how it implements the-p
options which callsmkdir(2)
repeatedly as needed.我的解决方案:
My solution:
这是我的一个更通用的解决方案:
Here's my shot at a more general solution:
如果您喜欢递归,因为它很有趣!
If you like recursion because it's fun!
一个非常简单的解决方案,只需传入输入:
mkdir dirname
A very simple solution, just pass in input:
mkdir dirname
很直。这可能是一个很好的起点,
您可以解析此函数的完整路径以及所需的权限,即S_IRUSR,有关模式的完整列表,请转到此处https://techoverflow.net/2013/04/05/how-to-use-mkdir -from-sysstat-h/
完整路径字符串将由“/”字符分割,各个目录将一次附加到 aggrpaz 字符串中。每次循环迭代都会调用 mkdir 函数,向其传递迄今为止的聚合路径以及权限。这个示例可以改进,我没有检查 mkdir 函数输出,并且该函数仅适用于绝对路径。
Quite straight. This can be a good starting point
You parse this function a full path plus the permissions you want, i.e S_IRUSR, for a full list of modes go here https://techoverflow.net/2013/04/05/how-to-use-mkdir-from-sysstat-h/
The fullpath string will be split by the "/" character and individual dirs will be appended to the aggrpaz string one at a time. Each loop iteration calls the mkdir function, passing it the aggregate path so far plus the permissions. This example can be improved, I am not checking the mkdir function output and this function only works with absolute paths.
这是我的解决方案
here is my solution
不幸的是,没有系统调用可以为您执行此操作。我猜这是因为没有一种方法可以对错误情况下应该发生的情况有真正明确的语义。它应该保留已经创建的目录吗?删除它们?如果删除失败怎么办?等等...
但是,自己推出非常容易,并且可以快速搜索 '递归 mkdir' 出现了许多解决方案。这是接近顶部的一个:
http://nion.modprobe .de/blog/archives/357-Recursive-directory-creation.html
There is not a system call to do it for you, unfortunately. I'm guessing that's because there isn't a way to have really well-defined semantics for what should happen in error cases. Should it leave the directories that have already been created? Delete them? What if the deletions fail? And so on...
It is pretty easy to roll your own, however, and a quick google for 'recursive mkdir' turned up a number of solutions. Here's one that was near the top:
http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html
嗯,我认为 mkdir -p 可以做到这一点吗?
hmm I thought that mkdir -p does that?
这是我的解决方案。通过调用下面的函数,您可以确保指向指定文件路径的所有目录都存在。请注意,这里的 file_path 参数不是目录名称,而是调用 mkpath() 后要创建的文件的路径。
例如,如果没有,
mkpath("/home/me/dir/subdir/file.dat", 0755)
将创建/home/me/dir/subdir
存在。mkpath("/home/me/dir/subdir/", 0755)
的作用相同。也适用于相对路径。
如果发生错误,则返回
-1
并设置errno
。请注意,
file_path
在操作过程中被修改,但之后会恢复。因此,file_path
并不是严格意义上的const
。Here is my solution. By calling the function below you ensure that all dirs leading to the file path specified exist. Note that
file_path
argument is not directory name here but rather a path to a file that you are going to create after callingmkpath()
.Eg.,
mkpath("/home/me/dir/subdir/file.dat", 0755)
shall create/home/me/dir/subdir
if it does not exist.mkpath("/home/me/dir/subdir/", 0755)
does the same.Works with relative paths as well.
Returns
-1
and setserrno
in case of an error.Note that
file_path
is modified during the action but gets restored afterwards. Thereforefile_path
is not strictlyconst
.这是使用递归的
mkpath()
的另一种形式,它既小又可读。它利用strdupa()
来避免直接更改给定的dir
字符串参数,并避免使用malloc()
& 。免费()
。确保使用-D_GNU_SOURCE
进行编译以激活strdupa()
...这意味着此代码仅适用于 GLIBC、EGLIBC、uClibc 和其他 GLIBC 兼容 C 库。在此处和 Valery Frolov 的输入之后,在 Inadyn 项目中,以下修订版本的
mkpath()
现已推送至 libite它又使用了一个系统调用,但代码现在更具可读性。
Here's another take on
mkpath()
, using recursion, which is both small and readable. It makes use ofstrdupa()
to avoid altering the givendir
string argument directly and to avoid usingmalloc()
&free()
. Make sure to compile with-D_GNU_SOURCE
to activatestrdupa()
... meaning this code only works on GLIBC, EGLIBC, uClibc, and other GLIBC compatible C libraries.After input both here and from Valery Frolov, in the Inadyn project, the following revised version of
mkpath()
has now been pushed to libiteIt uses one more syscall, but otoh the code is more readable now.
此处查看 bash 源代码,具体内容查看examples/loadables/mkdir.c,特别是第136-210行。如果您不想这样做,这里有一些处理此问题的源代码(直接取自我链接的 tar.gz):
您可能可以使用不太通用的实现。
Take a look at the bash source code here, and specifically look in examples/loadables/mkdir.c especially lines 136-210. If you don't want to do that, here's some of the source that deals with this (taken straight from the tar.gz that I've linked):
You can probably get away with a less general implementation.
显然不是,我的两个建议是:
或者,如果您不想使用
system()
,请尝试查看 coreutilsmkdir
源代码,看看他们是如何实现>-p
选项。Apparently not, my two suggestions are:
Or if you don't want to use
system()
try looking at the coreutilsmkdir
source code and see how they implemented the-p
option.我不允许对第一个(和已接受的)答案发表评论(没有足够的代表),因此我将在新答案中将我的评论作为代码发布。下面的代码基于第一个答案,但修复了许多问题:
opath[]
开头之前的字符(是的,“为什么要这样称呼它?”,但另一方面“为什么不修复该漏洞?”)opath
的大小现在是PATH_MAX
(这并不完美,但比常量更好)sizeof(opath)
那么它在复制时会正确终止(其中strncpy()
不这样做)mkdir()
一样(尽管如果您指定非用户可写或非用户-executable 那么递归将不起作用)#include
I'm not allowed to comment on the first (and accepted) answer (not enough rep), so I'll post my comments as code in a new answer. The code below is based on the first answer, but fixes a number of problems:
opath[]
(yes, "why would you call it that way?", but on the other hand "why would you not fix the vulnerability?")opath
is nowPATH_MAX
(which isn't perfect, but is better than a constant)sizeof(opath)
then it is properly terminated when copied (whichstrncpy()
doesn't do)mkdir()
(although if you specify non-user-writeable or non-user-executable then the recursion won't work)#include
s我这样做的递归方式:
编辑:修复了我的旧代码片段,错误报告由 Namchester
My recursive way of doing this:
EDIT: fixed my old code snippet, bug-report by Namchester
无需递归即可简单工作
Simply works without recursion