程序代写 #include

#include
#include
#include
#include

Copyright By PowCoder代写 加微信 powcoder

#include
#include
#include
#include “ext2.h”

unsigned char *disk;

int main(int argc, char **argv) {

if(argc != 2) {
fprintf(stderr, “Usage: %s \n”, argv[0]);
int fd = open(argv[1], O_RDWR);

disk = mmap(NULL, 128 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(disk == MAP_FAILED) {
perror(“mmap”);

struct ext2_super_block *sb = (struct ext2_super_block *)(disk + EXT2_BLOCK_SIZE);
printf(“Inodes: %d\n”, sb->s_inodes_count);
printf(“Blocks: %d\n”, sb->s_blocks_count);
printf(“Block group:\n”);
struct ext2_group_desc *gd = (struct ext2_group_desc *)(disk + EXT2_BLOCK_SIZE * 2);
printf(” block bitmap: %d\n”, gd->bg_block_bitmap);
printf(” inode bitmap: %d\n”, gd->bg_inode_bitmap);
printf(” inode table: %d\n”, gd->bg_inode_table);
printf(” free blocks: %d\n”, gd->bg_free_blocks_count);
printf(” free inodes: %d\n”, gd->bg_free_inodes_count);
printf(” used_dirs: %d\n”, gd->bg_used_dirs_count);
printf(“Block bitmap: “);
unsigned char *block_bits = (unsigned char *)(disk + EXT2_BLOCK_SIZE * gd->bg_block_bitmap);
for (int byte = 0; byte < sb->s_blocks_count / 8; byte++) {
for (int bit = 0; bit < 8; bit++) printf("%u", (block_bits[byte] >> bit) & 1);
printf(” “);
printf(“\nInode bitmap: “);
unsigned char *inode_bits = (unsigned char *)(disk + EXT2_BLOCK_SIZE * gd->bg_inode_bitmap);
for (int byte = 0; byte < sb->s_inodes_count / 8; byte++) {
for (int bit = 0; bit < 8; bit++) printf("%u", (inode_bits[byte] >> bit) & 1);
printf(” “);

printf(“\n\nInodes:\n”);
struct ext2_inode *inodes = (struct ext2_inode *)(disk + EXT2_BLOCK_SIZE * gd->bg_inode_table);
if (inodes[1].i_mode – inodes[1].i_mode % 4096 == EXT2_S_IFDIR) {
printf(“[2] type: %c size: %d links: %d blocks: %d\n[2] Blocks: “, ‘d’, inodes[1].i_size, inodes[1].i_links_count, inodes[1].i_blocks);
} else if (inodes[1].i_mode – inodes[1].i_mode % 4096 == EXT2_S_IFLNK) {
printf(“[2] type: %c size: %d links: %d blocks: %d\n[2] Blocks: “, ‘l’, inodes[1].i_size, inodes[1].i_links_count, inodes[1].i_blocks);
} else if (inodes[1].i_mode – inodes[1].i_mode % 4096 == EXT2_S_IFREG) {
printf(“[2] type: %c size: %d links: %d blocks: %d\n[2] Blocks: “, ‘f’, inodes[1].i_size, inodes[1].i_links_count, inodes[1].i_blocks);
for (int j = 0; j < (inodes[1].i_blocks / 2); j++) { printf(" %d", inodes[1].i_block[j]); printf("\n"); for (int i = EXT2_GOOD_OLD_FIRST_INO; i < sb->s_inodes_count; i++) {
if (inodes[i].i_size == 0) continue;
if (inodes[i].i_mode – inodes[i].i_mode % 4096 == EXT2_S_IFDIR) {
printf(“[%d] type: %c size: %d links: %d blocks: %d\n”, i + 1, ‘d’, inodes[i].i_size, inodes[i].i_links_count, inodes[i].i_blocks);
} else if (inodes[i].i_mode – inodes[i].i_mode % 4096 == EXT2_S_IFLNK) {
printf(“[%d] type: %c size: %d links: %d blocks: %d\n”, i + 1, ‘l’, inodes[i].i_size, inodes[i].i_links_count, inodes[i].i_blocks);
} else if (inodes[i].i_mode – inodes[i].i_mode % 4096 == EXT2_S_IFREG) {
printf(“[%d] type: %c size: %d links: %d blocks: %d\n”, i + 1, ‘f’, inodes[i].i_size, inodes[i].i_links_count, inodes[i].i_blocks);
printf(“[%d] Blocks: “, i + 1);
for (int j = 0; j < (inodes[i].i_blocks / 2); j++) { printf(" %d", inodes[i].i_block[j]); printf("\n"); printf("\nDirectory Blocks:\n"); printf(" DIR BLOCK NUM: %d (for inode %d)\n", inodes[1].i_block[0], 2); struct ext2_dir_entry *entry = (struct ext2_dir_entry *)(disk + EXT2_BLOCK_SIZE * inodes[1].i_block[0]); int sum = 0; while (sum < EXT2_BLOCK_SIZE) { if (entry->inode != 0) {
printf(“Inode: %d rec_len: %d name_len: %d”, entry->inode, entry->rec_len, entry->name_len);
if (entry->file_type == EXT2_FT_UNKNOWN) printf(” type= u name=%.*s\n”, entry->name_len, entry->name);
else if (entry->file_type == EXT2_FT_REG_FILE) printf(” type= f name=%.*s\n”, entry->name_len, entry->name);
else if (entry->file_type == EXT2_FT_DIR) printf(” type= d name=%.*s\n”, entry->name_len, entry->name);
else printf(” type= l name=%.*s\n”, entry->name_len, entry->name);
sum += entry->rec_len;
entry = (struct ext2_dir_entry *)(((char*) entry) + entry->rec_len);

for (int i = EXT2_GOOD_OLD_FIRST_INO; i < sb->s_inodes_count; i++) {
if (inodes[i].i_size == 0) continue;
if (inodes[i].i_mode – inodes[i].i_mode % 4096 == EXT2_S_IFDIR) {
for (int j = 0; j < (inodes[i].i_blocks / 2); j++) { printf(" DIR BLOCK NUM: %d (for inode %d)\n", inodes[i].i_block[j], i); struct ext2_dir_entry *entry = (struct ext2_dir_entry *)(disk + EXT2_BLOCK_SIZE * inodes[i].i_block[j]); while (sum < EXT2_BLOCK_SIZE) { if (entry->inode != 0) {
printf(“Inode: %d rec_len: %d name_len: %d”, entry->inode, entry->rec_len, entry->name_len);
if (entry->file_type == EXT2_FT_UNKNOWN) printf(” type= u name=%.*s\n”, entry->name_len, entry->name);
else if (entry->file_type == EXT2_FT_REG_FILE) printf(” type= f name=%.*s\n”, entry->name_len, entry->name);
else if (entry->file_type == EXT2_FT_DIR) printf(” type= d name=%.*s\n”, entry->name_len, entry->name);
else printf(” type= l name=%.*s\n”, entry->name_len, entry->name);
sum += entry->rec_len;
entry = (struct ext2_dir_entry *)(((char*) entry) + entry->rec_len);

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com