c - Ubuntu递归列出文件,检测符号链接(symbolic link)

标签 c linux symlink stat dirent.h

我正在尝试使用 dirent.h 递归地列出我的本地文件系统。为了防止跟踪符号链接(symbolic link),我使用了 sys/stat.h header 。在下面你可以找到我的 SSCCE 程序。

/**
 * coding: utf-8
 *
 * Copyright (C) 2013, Niklas Rosenstein
 *
 * listdir.c - List up directories and file-content recursively.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>

void list_dir(const char* directory_name) {
    DIR* directory_handle = opendir(directory_name);
    if (directory_handle == NULL) {
        fprintf(stderr, "Could not open directory %s.\n", directory_name);
        return;
    }

    char buffer[1024];
    struct dirent* dentry;
    int directory_name_length = strlen(directory_name);

    memcpy(buffer, directory_name, directory_name_length);
    buffer[directory_name_length] = '/';

    while ((dentry = readdir(directory_handle)) != NULL) {
        char* name = dentry->d_name;
        int length = strlen(name);

        // Skip the dotted elements.
        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;

        // Concatenate the directory name with the element name.
        memcpy(buffer + directory_name_length + 1, name, length);
        buffer[directory_name_length + 1 + length] = 0;

        printf("%s\n", buffer);

        // Proceed recursively if the element is a directory.
        struct stat s;
        if (stat(buffer, &s) != 0) {
            fprintf(stderr, "WARNING: stat() failed on %s\n", buffer);
            continue;
        }

        mode_t mode = s.st_mode;
        if (mode & S_IFDIR && !(mode & S_IFLNK)) {
            list_dir(buffer);
        }

    }
    closedir(directory_handle);
}

int main(int argc, char** argv) {
    if (argc != 2) {
        fprintf(stderr, "Expected exactly 2 arguments.\n");
        return -1;
    }
    list_dir(argv[1]);
    return 0;
}

我只是无法让它正确检测符号链接(symbolic link)。当它遇到一个符号链接(symbolic link),链接到它的父目录时,它会一直持续下去。似乎我的系统上有几个这样的文件夹,例如。 /usr/bin/X11:

/usr/bin/X11/
    X11/ -> .

这一行不能完全正确:if (mode & S_IFDIR && !(mode & S_IFLNK)) {。这可能是 stat() 函数的问题,还是我在这里遗漏了一些明显的东西?

这是调用 ./listdir/usr/bin/X11 后我的终端的图片,大约一秒钟后按 ^C 停止程序。

enter image description here

最佳答案

尝试使用 lstat 而不是 stat:后者在符号链接(symbolic link)上完成时,返回有关其目标(最后一个,不是符号链接(symbolic link))的信息。

S_IFDIRS_IFLNK 不应该像它们是独占位标志一样使用。对于目录,使用S_ISDIR(mode);对于符号链接(symbolic link),您不需要测试:lstat 不会将符号链接(symbolic link)报告为目录,当您只想跳过 符号链接(symbolic link)时,这就足够了。

关于c - Ubuntu递归列出文件,检测符号链接(symbolic link),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14449370/

相关文章:

c - do{} while(0) 的意义

c# - 将 C 输出转换为 C# 输入

c - 在 4D 矩阵类型中使用匿名结构时的警告

linux - 限制核心文件大小

linux - 如何找到链接到重命名卷的所有符号链接(symbolic link)?

linux - 使用 "find"列出所有 c/h/cc 文件但排除符号链接(symbolic link)

c - 将控制台输入重定向到 c 中的管道

linux - 如何在启动 Linux 时写入 USB 闪存驱动器上的文本文件?

windows - 应用程序如何处理 Windows 符号链接(symbolic link)?

linux - 将.net core 3.1 Web应用部署到Azure Linux应用服务