c - 使用C删除非空目录时出现段错误

标签 c directory segmentation-fault dirent.h

我正在尝试在不使用系统调用且不使用大量库的情况下删除一个非空目录。到目前为止我的代码是...

int rmrf(char *path) {
    char* path_copy = (char *) malloc(1024 * sizeof(char));
    strcpy(path_copy, path);
    DIR *directory = opendir(path_copy);
    struct dirent *entry = readdir(directory);
    while (entry != NULL) {
        if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { //skip /. and /..
        } else if (entry->d_type == DT_DIR) { //directory recurse
            strcat(path_copy, "/");
            strcat(path_copy, entry->d_name);
            rmrf(path_copy);
            remove(path);
        } else { //file delete
            strcat(path_copy, "/");
            strcat(path_copy, entry->d_name);
            remove(path_copy);
        }
        entry = readdir(directory);
    }
    closedir(directory);
    return 0;
}

我当前的文件结构看起来像这样......

Who
|---Region 1
    |---County 1
        |---SubCounty 1
    |---County 2
|---Region 2
    |---County 1
|---Region 3

目前我遇到段错误,但随着时间的推移出现在不同的地方。今天早些时候,我将深入了解两层递归,然后将错误排除在外,但截至目前,我什至无法通过一个完整的层级。我不知道出了什么问题,当我使用 gdb 调查问题时,我得到...

malloc.c: No such file or directory.

如有任何帮助,我们将不胜感激!

更新:

我采纳了 paxdiablo 的建议并提出了结果函数...

int rmrf(char *path) {
    char* path_copy = malloc(1024);
    DIR *directory = opendir(path);
    struct dirent *entry = readdir(directory);
    while (entry != NULL) {
        if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { //skip /. and /..
        } else if (entry->d_type == DT_DIR) { //directory recurse
            strcpy(path_copy, path);
            strcat(path_copy, "/");
            strcat(path_copy, entry->d_name);
            rmrf(path_copy);
            remove(path);
        } else { //file delete
            strcpy(path_copy, path);
            strcat(path_copy, "/");
            strcat(path_copy, entry->d_name);
            remove(path_copy);
        }
        entry = readdir(directory);
    }
    closedir(directory);
    free(path_copy);
    return 0;
}

但是我仍然遇到段错误,尽管它在递归中越来越远。段错误的 gdb 输出如下...

Program received signal SIGSEGV, Segmentation fault.
_int_malloc (av=av@entry=0x7ffff7dd1b20 <main_arena>, bytes=bytes@entry=32816) at malloc.c:3802
3802    malloc.c: No such file or directory.
(gdb) where
#0  _int_malloc (av=av@entry=0x7ffff7dd1b20 <main_arena>, bytes=bytes@entry=32816) at malloc.c:3802
#1  0x00007ffff7a91184 in __GI___libc_malloc (bytes=32816) at malloc.c:2913
#2  0x00007ffff7ad51ba in __alloc_dir (statp=0x7fffffffe190, flags=0, close_fd=true, fd=6) at ../sysdeps/posix/opendir.c:247
#3  opendir_tail (fd=6) at ../sysdeps/posix/opendir.c:145
#4  __opendir (name=<optimized out>) at ../sysdeps/posix/opendir.c:200
#5  0x0000000000401bca in rmrf ()
#6  0x0000000000401c8d in rmrf ()
#7  0x0000000000401c8d in rmrf ()
#8  0x0000000000402380 in main ()

想法?

最佳答案

对于您的初始代码,您在进入函数时执行此操作一次:

strcpy(path_copy, path);

然后对当前目录中的每个 文件或目录执行此操作:

strcat(path_copy, "/");
strcat(path_copy, entry->d_name);

这意味着,如果您的当前目录 /xx 中有文件 abc , path_copy 变量将循环通过:

/xx/a   /xx/a/b   /xx/a/b/c

而不是正确的:

/xx/a   /xx/b     /xx/c

如果文件数量足够大,您将很容易耗尽为路径分配的 1024 字节。

如果你想解决这个问题,那么你应该每次都从头开始这个变量:

if ((strcmp(entry->d_name, ".") != 0) && (strcmp(entry->d_name, "..") != 0)) {
    if (entry->d_type == DT_DIR) {
        strcpy(path_copy, path);
        strcat(path_copy, "/");
        strcat(path_copy, entry->d_name);
        rmrf(path_copy);
        remove(path);
    } else {
        sprintf(path_copy, "%s/%s", path, entry->d_name);
        remove(path_copy);
    }
}

您会注意到我已经稍微修改了您的初始条件以使其更有意义(仅当文件既不是 . 也不是 ..).

我还在 else 子句中展示了使用 sprintf 而不是一组 strcpy/strcat 来构造要删除的字符串的更短方法 调用。如果您愿意,也可以在 if 子句中随意执行此操作,我使用旧方法保留它,因此您可以看到您需要做的就是添加初始路径。

还有一些额外的要点,适用于您的第一个和/或第二个代码片段:

  • 您还应该确保在 closedir() 之间,释放您在每个级别分配的内存,紧接在从函数返回之前返回

  • 从不需要转换 malloc 的返回值,因为 void * 可以隐式转换为任何其他类型的指针。事实上,这样做很危险,因为它可以隐藏某些细微的错误。

  • 同样,您永远不需要乘以 sizeof(char) - 也就是说,根据定义,总是乘以 1。

  • 您可以将 path_copy 的创建移动到文件/目录检查之前,因为它对这两个部分都是通用的。

  • 最后,如果您正在处理的目录实际上不存在,您将遇到麻烦,因为 opendir 将返回 NULL,您将立即尝试将其传递给readdir.


考虑到所有这些,我将从以下程序开始,该程序实际上遍历 树并打印出它找到的所有文件。对此感到满意后,您可以添加回删除的位:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>

int rmrf(char *path) {
    char *path_copy = malloc(1024);
    DIR *directory = opendir(path);
    if (directory != NULL) {
        struct dirent *entry = readdir(directory);
        while (entry != NULL) {
            if ((strcmp(entry->d_name, ".") != 0) && (strcmp(entry->d_name, "..") != 0)) {
                sprintf(path_copy, "%s/%s", path, entry->d_name);
                if (entry->d_type == DT_DIR) {
                    rmrf(path_copy);
                    puts(path);
                } else {
                    puts(path_copy);
                }
            }
            entry = readdir(directory);
        }
        closedir(directory);
    }
    free(path_copy);
    return 0;
}

主要代码只是一个驱动程序,以确保正确设置思想。只需确保在运行之前,您没有(在当前目录中)要保留的 paxtestpaxtest2 文件或目录。

int main(void) {
    system("rm -rf paxjunk");
    system("mkdir paxjunk");
    system("touch paxjunk/0.txt");
    system("mkdir paxjunk/1");
    system("touch paxjunk/1/1.txt");
    system("mkdir paxjunk/2");
    system("touch paxjunk/2/2.txt");

    rmrf("paxjunk");
    puts("===");

    system("rm -rf paxjunk2");

    rmrf("paxjunk2");
    puts("===");

    system("rm -rf paxjunk");

    return 0;
}

当你运行它时,你应该看到它工作正常:

paxjunk/0.txt
paxjunk/1/1.txt
paxjunk
paxjunk/2/2.txt
paxjunk
===
===

关于c - 使用C删除非空目录时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49746934/

相关文章:

linux - 目录具有 777 权限,但如果是 root 或用户拥有,则行为不同

c++ - 使用线程时出现段错误

c++ - 段错误,列出目录的内容

assembly - mov 0,%eax上具有x86组件的Segfault

sql-server - 从跨平台应用程序连接到 MS SQL Server

c - 变量声明完成后一次性初始化/定义结构变量

C: 删除非字母字符的程序

等待条件变量的线程能否被等待之前发出的信号唤醒

directory - 如何通过终端在 text mate 中打开目录

c# - 获取特定级别的目录