使用 stat() 函数测试 DIRENT 是目录还是文件的正确方法是什么?

发布于 2024-08-07 00:40:16 字数 1045 浏览 9 评论 0 原文

我在使用 'if(S_IFDIR(stbuf.st_mode))' 行时遇到一些问题。这是测试要递归到的目录的正确方法吗?目前该函数似乎可以正确执行 1 或 2 个循环,然后失败并出现分段错误。

我已经尝试过以下方法,并且可能更多作为条件。

S_ISDIR(st_mode)
((st_mode & ST_IFMT) == S_IFDIR)
S_IFDIR(stbuf.st_mode)

我已经包含了整个函数,因为我担心问题可能出在其他地方。

void getFolderContents(char *source, int temp){
    struct stat stbuf;
    int isDir;
    dirPnt = opendir(source);
    if(dirPnt != NULL){
        while(entry = readdir(dirPnt)){
            char *c = entry->d_name;
            if(strcmp(entry->d_name, cwd) == 0 || strcmp(entry->d_name, parent) == 0){
            }
            else{
                stat(entry->d_name, &stbuf);
                printf("%i %i ", S_IFMT, stbuf.st_mode);
                if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                printf("DIR: %s\n", entry->d_name);
                getFolderContents(entry->d_name, 0);
            }
            printf("FILE: %s\n", entry->d_name);
        }
    }
    closedir(dirPnt);
}

I'm having some trouble with the 'if(S_IFDIR(stbuf.st_mode))' line. Is this the correct way to test for a directory to recurse into? The function at the moment seems to do it right for 1 or 2 loops and then fails and segmentation faults.

I've tried the following and probably more as the condition.

S_ISDIR(st_mode)
((st_mode & ST_IFMT) == S_IFDIR)
S_IFDIR(stbuf.st_mode)

I've included the whole function because I'm concerned the problem might be elsewhere.

void getFolderContents(char *source, int temp){
    struct stat stbuf;
    int isDir;
    dirPnt = opendir(source);
    if(dirPnt != NULL){
        while(entry = readdir(dirPnt)){
            char *c = entry->d_name;
            if(strcmp(entry->d_name, cwd) == 0 || strcmp(entry->d_name, parent) == 0){
            }
            else{
                stat(entry->d_name, &stbuf);
                printf("%i %i ", S_IFMT, stbuf.st_mode);
                if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                printf("DIR: %s\n", entry->d_name);
                getFolderContents(entry->d_name, 0);
            }
            printf("FILE: %s\n", entry->d_name);
        }
    }
    closedir(dirPnt);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下慵懒的猫 2024-08-14 00:40:16

是的,这是正确的。但由于您从未更改过该目录,因此您将找不到它。

考虑以下目录层次结构:

 a
 |
 +- b
 |  |
 |  +- c
 ...

您的代码将扫描其当前目录,并找到“a”。它会判断这是一个目录,并递归调用自身,并打开“a”进行读取。这有效。该扫描将找到一个名为“b”的目录,但尝试仅使用条目名称打开它将会失败,因为路径现在是“a/b”。

我建议更改为目录(使用 chdir() )在打开之前。这意味着您只需opendir(".")即可。存储旧路径,并在递归该级别完成时再次使用 chdir() 输出(而不是在进行更深层次的递归调用之前)。

Yes, that's correct. But since you never change into the directory, you will not find it.

Consider the following directory hierarchy:

 a
 |
 +- b
 |  |
 |  +- c
 ...

Your code will scan its current directory, and find "a". It will determine that it is a directory, and call itself recursively, and open "a" for reading. This works. That scan will find a directory called "b", but trying to open it using the entry name only will fail, since the path is now "a/b".

I recommend changing into the directory (with chdir()) before opening it. That means you can just opendir("."). Store the old path, and chdir() out again when recursing that level is done (not before doing a recursive call to go deeper).

情深缘浅 2024-08-14 00:40:16

条目在哪里定义?它是局部变量吗?
我不明白为什么它会出现段错误,但也许你应该将其设为局部变量。
这里有一个它会咬你的例子:

                    if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                            printf("DIR: %s\n", entry->d_name);
                            getFolderContents(entry->d_name, 0);
                    }
                    printf("FILE: %s\n", entry->d_name);

printf 将打印错误的名称,所以你应该在这里添加一个 else 。

dirpnt 也是如此。当您在 while 循环内退出 getFolderContents 时,
你最终会在一个关闭的dirpoint上调用readdir,这应该会让你脱离循环。

但正如 bahbar 所说:
您无法递归并将临时变量存储在全局变量中

Where is entry defined ? is it a local variable ?
I can't see why it would segfault, but may be you should make it a local variable.
One example where it will bite you is here :

                    if(S_IFDIR(stbuf.st_mode)){            //Test DIR or file
                            printf("DIR: %s\n", entry->d_name);
                            getFolderContents(entry->d_name, 0);
                    }
                    printf("FILE: %s\n", entry->d_name);

The printf is gonna print the wrong name, so you should probably add an else here.

The same is true with dirpnt. When you go out of getFolderContents inside the while loop,
you end up calling readdir on a closed dirpoint, which should get you out of the loop.

But as stated by bahbar :
You can't recurse and store temporary variable in global variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文