我正在学习 stat 和 readlink 之类的系统调用。我尝试以两种不同的方式统计根目录:
- 只需统计root的路径。简单的。
- 然后,一个迂回的方式,fopen“/”,从我得到的 fd 中创建一个文件描述符路径,并读取链接它以获得“/”。然后统计。
我不明白这是我期望的工作方式,并且 inode 编号是相同的,除了当我在之后引入更多代码时,即 getcwd,inode 奇怪地不一样并且测试 2 失败。如果您同时运行这两个版本,无论有没有它说要删除的部分,p2 的 printf 也会发生变化,您可以看到/
. 我在这里做错了什么或这里发生了什么?这些初始语句之后的代码是如何导致代码更早发生变化的?
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/* Stat root this way */
struct stat st1;
char *p1 = "/";
if (!stat(p1, &st1))
printf("PASS\n");
else
printf("FAIL - %m\n");
/* Stat root another way */
struct stat st2;
char fdpath[100], p2[100];
FILE *fp = fopen("/", "r");
sprintf(fdpath, "/proc/self/fd/%d", fileno(fp));
readlink(fdpath, p2, sizeof(p2));
if (!stat(p2, &st2))
printf("PASS\n");
else
printf("FAIL - %m\n");
printf("p2 = %s\n", p2);
/* Check inodes are the same */
printf(" st1.st_ino = %ld\n", st1.st_ino);
printf(" st2.st_ino = %ld\n", st2.st_ino);
/* TRY WITHOUT THIS - Adding this makes the inodes different! Take it out and they're the same */
char cwd_buf[100];
char *cwd_ret = getcwd(cwd_buf, sizeof(cwd_buf));
if (cwd_ret == NULL)
printf("getcwd failed - %m\n");
printf("cwd_ret = %s\n", cwd_ret);
return 0;
}