reading linux inode bitmap

3k Views Asked by At

I'm going to fetch linux inode bitmaps with c++. I've use this code to fetch super block first:

    #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;
}

but every time i run it , i get a error :

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

I couldn't find any good example or tutorial for this.is there anybody to help me?

EDIT :
I've include <linux/stat.h> but still get same error.

2

There are 2 best solutions below

4
On
#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)

So you should find stat.h in yours kernel source tree and include it.

1
On

The Linux source code "stat.h" is not the same file as that comes with the C-library. They just happen to have the same name. You will need to set your include path to find the correct stat.h (you may need BOTH, depending on what you are trying to do).