在Unix递归的mkdir()系统调用 [英] Recursive mkdir() system call on Unix

查看:237
本文介绍了在Unix递归的mkdir()系统调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读的mkdir(2)手册页Unix系统调用使用该名称后,似乎电话不会在路径中的路径创建中级目录,只有最后一个目录。有什么办法(或其他函数)来创建路径中的所有目录,而不诉诸手动解析我的目录串并单独创建每个目录?

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 ?

推荐答案

有没有一个系统调用来为你做它,很遗憾。我猜那是因为没有办法有在错误的情况下会发生什么真正明确的语义。如果它离开那个已经创建的目录?删除呢?如果删除失败了怎么办?等等...

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...

这是pretty容易推出自己的,但是,一个快速的<一个谷歌href=\"http://www.google.com/search?hl=en&safe=off&client=safari&rls=en&q=recursive+mkdir&aq=f&aqi=&aql=&oq=\">recursive MKDIR '变成了一些解决方案。这里有一个接近顶部:

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:

<一个href=\"http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html\">http://nion.modprobe.de/blog/archives/357-Recursive-directory-creation.html

static void _mkdir(const char *dir) {
        char tmp[256];
        char *p = NULL;
        size_t len;

        snprintf(tmp, sizeof(tmp),"%s",dir);
        len = strlen(tmp);
        if(tmp[len - 1] == '/')
                tmp[len - 1] = 0;
        for(p = tmp + 1; *p; p++)
                if(*p == '/') {
                        *p = 0;
                        mkdir(tmp, S_IRWXU);
                        *p = '/';
                }
        mkdir(tmp, S_IRWXU);
}

这篇关于在Unix递归的mkdir()系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆