我想使用 C 语言在 ext2 映像中访问特定的 inode(使用其编号)。我打算通过使用 open() 系统调用打开 ext2 映像,然后使用具有正确偏移量的 lseek() 遍历它,直到我到达 inode。这个对吗?还是我做错了什么?我有点困惑使用 open() 是否正确,或者有更合适的功能来做到这一点。
int fd = open("ext2fs.img", O_RDONLY);
assert(fd != -1);
off_t startPosition = lseek(fd, 0, SEEK_CUR);
assert(startPosition != -1);
我应该只添加偏移量到 startPosition 直到我到达 inode 吗?但是如何搜索特定的 inode?
更新(更具体)
我已经有了 ext2 文件系统的布局(http://www.nongnu.org/ext2-doc/ext2.html),这给了我需要的一切(所有偏移量)。我需要创建一个 C 程序来操作元数据和数据。例如,删除和复制文件。
我知道该怎么做,但我在执行它时遇到了麻烦。
例如:为了测试我是否知道自己在做什么,我正在尝试读取教授提供的 ext2 磁盘映像中的空闲 inode 数量,这样做:
#define SUPER_BLOCK 1024
int main()
{
int freeInodes;
int fd = open("path.img", O_RDONLY);
off_t startPosition = lseek(fd, 0, SEEK_CUR);
lseek(fd, startPosition + SUPER_BLOCK + 16, SEEK_CUR);
read(fd, freeInodes, 4);
printf("Number of free inodes: %d", freeInodes);
}
我收到的输出是:“空闲 inode 数量:32767”
我是否正确解释了从 read() 获得的数据?我不知道收到的这个值是否正确。