列出文件的硬链接 C
List hard links of a file C
我需要在 "pure" C 中列出给定文件的所有硬链接,因此没有 bash 命令的帮助。
我用谷歌搜索了几个小时,但找不到任何有用的信息。
我目前的想法是获取 inode 编号然后循环遍历目录以查找具有相同 inode 的文件。
在bash中类似于
sudo find / -inum 199053
有更好的建议吗?
谢谢
要获取单个文件的 inode 编号,请调用 stat 函数并引用返回结构的 st_ino 值。
int result;
struct stat s;
result = stat("filename.txt", &s);
if ((result == 0) && (s.st_ino == 199053))
{
// match
}
您可以使用 stat 函数构建解决方案,使用 opendir, readdir, and closedir 递归扫描目录层次结构以查找匹配的 inode 值。
您也可以使用 scandir 来扫描整个目录:
int filter(const struct dirent* entry)
{
return (entry->d_ino == 199053) ? 1 : 0;
}
int main()
{
struct dirent **namelist = NULL;
scandir("/home/selbie", &namelist, filter, NULL);
free(namelist);
return 0;
}
我需要在 "pure" C 中列出给定文件的所有硬链接,因此没有 bash 命令的帮助。
我用谷歌搜索了几个小时,但找不到任何有用的信息。 我目前的想法是获取 inode 编号然后循环遍历目录以查找具有相同 inode 的文件。
在bash中类似于
sudo find / -inum 199053
有更好的建议吗?
谢谢
要获取单个文件的 inode 编号,请调用 stat 函数并引用返回结构的 st_ino 值。
int result;
struct stat s;
result = stat("filename.txt", &s);
if ((result == 0) && (s.st_ino == 199053))
{
// match
}
您可以使用 stat 函数构建解决方案,使用 opendir, readdir, and closedir 递归扫描目录层次结构以查找匹配的 inode 值。
您也可以使用 scandir 来扫描整个目录:
int filter(const struct dirent* entry)
{
return (entry->d_ino == 199053) ? 1 : 0;
}
int main()
{
struct dirent **namelist = NULL;
scandir("/home/selbie", &namelist, filter, NULL);
free(namelist);
return 0;
}