【问题标题】:reading linux inode bitmap读取linux inode位图
【发布时间】:2012-12-08 06:21:34
【问题描述】:

我将使用 c++ 获取 linux inode 位图。我已经使用此代码首先获取超级块:

    #include <cstdlib>
    #include <linux/ext2_fs.h>
    #include <linux/fs.h>
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <fcntl.h>
    #include <linux/fs.h>

    using namespace std;

    /*
     * 
     */

    int main() {
        int fd;
        char boot[1024];
        struct ext2_super_block super_block;

        fd = open("/dev/sda1", O_RDONLY);
    /* Reads the boot section and the superblock */
    read(fd, boot, 1024);
    read(fd, &super_block, sizeof (struct ext2_super_block));

    /* Prints the Magic Number */
    printf("%x\n", super_block.s_magic);

    close(fd);

    return 0;
}

但是每次我运行它,我都会得到一个错误:

In file included from main.cpp:2:0:
/usr/include/linux/ext2_fs.h:181:18: error: ‘S_ISDIR’ was not declared in this scope
/usr/include/linux/ext2_fs.h:183:23: error: ‘S_ISREG’ was not declared in this scope

我找不到任何好的示例或教程。有人可以帮助我吗?

编辑:
我已经包含了&lt;linux/stat.h&gt;,但仍然出现同样的错误。

【问题讨论】:

  • 你无法读取 linux inode table:没有这样的东西,因为每个文件系统都有自己的格式(ext2,3,4 / reiserfs / xfs / btrfs / etc. )
  • 顺便说一句,要读取 ext2、ext3 或 ext4,您可能需要使用 e2fsprogs 包中的 libext2fs 提供的函数。
  • 你能帮我用这个包吗?

标签: c++ c linux inode superblock


【解决方案1】:
#grep -rw S_ISREG /usr/src/linux/include
/usr/src/linux/include/linux/fs.h:  if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/fs.h.~1~:  if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
/usr/src/linux/include/linux/stat.h:#define S_ISREG(m)  (((m) & S_IFMT) == S_IFREG)

所以你应该在你的内核源代码树中找到 stat.h 并包含它。

【讨论】:

  • 我该如何使用这个...你能帮帮我吗?我已经在我的项目中包含了 但仍然出现错误
  • 仅在将 -I/path/to/your/linux/kernel/sources 传递给编译器时才有效
  • 我应该将它包含在我的项目中还是包含在 ext2_fs.h 文件中?
  • 我将它包含在主项目和 ext2_fs.h 文件中,但仍然得到相同的错误
【解决方案2】:

Linux 源代码“stat.h”与 C 库附带的文件不同。他们只是碰巧有相同的名字。您需要设置包含路径才能找到正确的 stat.h(您可能需要两者,具体取决于您要执行的操作)。

【讨论】:

  • 你能给我一些源样本来读取超级块和索引节点位图吗?
猜你喜欢
  • 2020-11-01
  • 2014-04-30
  • 2011-09-11
  • 2019-06-08
  • 2011-05-20
  • 2010-10-10
  • 2014-04-08
  • 2015-05-04
  • 2011-11-27
相关资源
最近更新 更多