【发布时间】:2018-04-16 12:34:43
【问题描述】:
我是我大学系统编程课的助教。最近学生们一直在做一个涉及复制程序pwd的作业。
一些学生注意到似乎不一致的地方。当他们从 readdir 条目中读取 ino 时,它会提供与他们统计同一目录时不同的 inode。他们中的许多人都在问为什么。 目录条目的inode指向目标目录的inode不应该存在吗?如果是这样,为什么 stat 会给出不同的 inode?p>
这里有一些示例代码来演示:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#define DIR_NAME "~redacted~"
int getReaddirInode(char* entName)
{
DIR* directory;
struct dirent* entry;
ino_t result;
if ((directory = opendir(".")) == NULL)
{
perror("opendir");
exit(1);
}
while ((entry = readdir(directory)) != NULL)
{
if (strcmp(entName, entry->d_name) == 0)
{
result = entry->d_ino;
break;
}
}
if (entry == NULL)
{
fprintf(stderr, "No such directory: %s.\n", entName);
exit(1);
}
if (closedir(directory) == -1)
{
perror("closedir");
exit(1);
}
return result;
}
int getStatInode(char* entName)
{
struct stat buf;
if (stat(entName, &buf) == -1)
{
perror("stat");
exit(1);
}
return buf.st_ino;
}
int main()
{
if (chdir("/home") == -1)
{
perror("chdir");
return 1;
}
printf("readdir (3) gives an inode of:%9d.\n", getReaddirInode(DIR_NAME));
printf("stat (2) gives an inode of: %9d.\n", getStatInode(DIR_NAME));
return 0;
}
输出:
unix3:~ $ ./a.out
readdir (3) gives an inode of: 4053392.
stat (2) gives an inode of: 69205302.
编辑: 我可以确认 DIR_NAME 是一个挂载点:
unix3:~ $ mount | grep ~redacted~
csc-na01.csc.~redacted~.edu:/student01/student01/0_t/~redacted~ on /home/~redacted~ type nfs (rw,relatime,vers=3,rsize=65536,wsize=65536,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=129.65.158.8,mountvers=3,mountport=635,mountproto=udp,local_lock=none,addr=129.65.158.8)
编辑2: inode 似乎只在 nfs 文件系统转换时发生变化。我的问题是为什么。 readdir指向什么inode,stat指向什么inode?p>
自从我发布这篇文章以来,这两个 inode 在过去 4 小时内都发生了变化。
我没有卸载权限。
我检查了从同一个地址挂载的另一个目录,两个 inode 都与第一个目录不同,这表明每个目录确实有两个对该目录唯一的 inode,但我不明白为什么。
【问题讨论】:
-
DIR_NAME是挂载点吗?如果不是,它在什么文件系统上?无论哪种方式,我认为你在Unix and Linux Stack Exchange 上提问比在这里好。 -
是的,它是一个挂载点。我将其添加到我的原始帖子中。我可能会把它放在这里一会儿,如果我没有得到很好的回应,就移动它。谢谢你的想法。
-
小心...有些 IP 不是匿名的...您已经仔细修改了 DNS,但不是 IP...许多大学都有大量公共 IP - 加州理工州立大学机会? beep
-
您尝试过不是挂载点的东西吗?值是静态的吗?你有卸载它的能力吗?我猜
stat()是从本地文件系统提供inode,而readdir()是从服务器提供inode。