将inode位图打印为unsigned char

时间:2012-12-21 09:59:24

标签: c++ c inode unsigned-char

如何打印作为unsigned char获取的inode位图的位?这是我的代码,用于获取位图。我想做的是以二进制格式打印位图。

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

using namespace std;

int main() {
    int fd, block_size;
    char boot[1024];
    struct ext2_super_block super_block;
    fd = open("/dev/sda6", O_RDONLY);
    printf("Error: %s\n", strerror(errno));

    /* Reads the boot section and the superblock */
    read(fd, boot, 1024);
    read(fd, &super_block, sizeof (struct ext2_super_block));

    if (super_block.s_magic != EXT2_SUPER_MAGIC) {
        fprintf(stderr, "Not an Ext2 filesystem!\n");
        getchar();
        exit(1);
    }

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

    printf("%x\n", super_block.s_log_block_size);
    block_size = 4096 << super_block.s_log_block_size;

    /* calculate number of block groups on the disk */
    unsigned int group_count =
            1 + (super_block.s_blocks_count - 1) / super_block.s_blocks_per_group;
    /* calculate size of the group descriptor list in bytes */
    unsigned int descr_list_size =
            group_count * sizeof (struct ext2_group_desc);
    struct ext2_group_desc group_descr;

    /* position head above the group descriptor block */
    lseek(fd, 1024 + block_size, SEEK_SET);
    read(fd, &group_descr, sizeof (group_descr));
    /* location of the super-block in the first group */
#define BASE_OFFSET 1024
#define BLOCK_OFFSET(block) (BASE_OFFSET + (block-1)*block_size)

    struct ext2_super_block super;
    struct ext2_group_desc group;
    unsigned char *bitmap, bits[8];
    /* the super block */
    /* the group descritopr */
    /* ... [read superblock and group descriptor] ... */
    bitmap = (unsigned char*) malloc(block_size);
    /* allocate memory for the bitmap */
    lseek(fd, BLOCK_OFFSET(group.bg_block_bitmap), SEEK_SET);
    read(fd, bitmap, block_size);
    /* read bitmap from disk */
    unsigned char* bla = (unsigned char*) bitmap;
    printf("bitmap: ");
    for (int i = 0; i < block_size; i++) {
        printf("%x", &bitmap[i]);
    }
    free(bitmap);

    printf("%x\n", block_size);

    close(fd);

    return 0;
}

我对这个代码有点怀疑。我可以确定结果。我在ubuntu 12.4 ex4文件系统上运行它。

编辑:我已经创建了一个原始磁盘映像并试图读取它,但是得到的错误是它不是一个EXtended2文件系统? 这是我使用的地址:

fd = open("/dev/zero", O_RDONLY);

0 个答案:

没有答案