我将使用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("%xn", 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>
,但仍然得到相同的错误。
#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并包含它
Linux源代码"stat.h"与c库附带的文件不同。他们只是碰巧有相同的名字。您需要设置包含路径来找到正确的stat.h(您可能需要两者,这取决于您要做的事情)。