在 Debian(以及一般的 Linux/Unix 系统)里,opendir() 本身是否“支持符号链接”要分情况看,关键在于你传给 opendir() 的路径是什么。
opendir() 对符号链接的默认行为opendir("/path/to/symlink_to_dir");
opendir() 会 自动跟随(dereference)符号链接这是最常见、也是“支持符号链接”的含义。
opendir("/path/to/symlink_to_file");
opendir() 会失败errno 被设为 ENOTDIRopendir("/path/to/broken_symlink");
errno = ENOENTopendir() vs opendir() + lstat()opendir() 不会告诉你路径是不是符号链接:
DIR *d = opendir(path);
如果你想判断是否符号链接,需要先用:
struct stat st;
lstat(path, &st);
if (S_ISLNK(st.st_mode)) {
// 是符号链接
}
| 函数 | 是否跟随符号链接 |
|---|---|
opendir() |
✅ 跟随 |
stat() |
✅ 跟随 |
lstat() |
❌ 不跟随 |
open() |
✅ 默认跟随 |
openat(O_NOFOLLOW) |
❌ 不跟随 |
Debian 的
opendir()支持“指向目录的符号链接”,并且会自动跟随;但它不把符号链接当作特殊对象来处理。
如果你是在写 递归目录遍历 / 防止符号链接死循环 / 安全遍历,我可以给你一个标准写法(比如 fts / nftw / openat + O_NOFOLLOW)。