简体   繁体   English

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

[英]What is the correct way to use the stat() function to test if a DIRENT is a directory or a file?

I'm having some trouble with the 'if(S_IFDIR(stbuf.st_mode))' line. 我在使用'if(S_IFDIR(stbuf.st_mode))'行时遇到了一些麻烦。 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. 此时的函数似乎正确地执行1或2个循环,然后失败并且分段错误。

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);
}

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". 您的代码将扫描其当前目录,并找到“a”。 It will determine that it is a directory, and call itself recursively, and open "a" for reading. 它将确定它是一个目录,并以递归方式调用自身,并打开“a”进行读取。 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". 该扫描将找到一个名为“b”的目录,但尝试仅使用条目名称打开它将失败,因为该路径现在是“a / b”。

I recommend changing into the directory (with chdir() ) before opening it. 我建议在打开之前更改到目录(使用chdir() )。 That means you can just opendir(".") . 这意味着你可以只是opendir(".") Store the old path, and chdir() out again when recursing that level is done (not before doing a recursive call to go deeper). 存储旧路径,并在递归该级别时再次将chdir()输出(不是在执行递归调用之前更深入)。

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. printf会打印错误的名称,所以你应该在这里添加一个else。

The same is true with dirpnt. 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. 当你走出while循环中的getFolderContents时,你最终会在一个封闭的dirpoint上调用readdir,这会让你退出循环。

But as stated by bahbar : You can't recurse and store temporary variable in global variable 但正如bahbar所述:你无法在全局变量中递归和存储临时变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM