读取linux inode位图

时间:2012-12-20 17:07:05

标签: c++ c linux inode superblock

我要用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

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

编辑:
我已经包含<linux/stat.h>但仍然会收到相同的错误。

2 个答案:

答案 0 :(得分:3)

#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并包含它。

答案 1 :(得分:2)

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