-
Notifications
You must be signed in to change notification settings - Fork 3
/
readimage.c
129 lines (113 loc) · 4.56 KB
/
readimage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "ext2.h"
#include "utilities.h"
unsigned char *disk;
int main(int argc, char **argv) {
if(argc != 2) {
fprintf(stderr, "Usage: readimg <image file name>\n");
exit(1);
}
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");
exit(1);
}
struct ext2_super_block *sb = getSuperblock();
printf("Inodes: %d\n", sb->s_inodes_count);
printf("Blocks: %d\n", sb->s_blocks_count);
struct ext2_group_desc *gd = getGroupDesc();
printf("Block group:\n");
printf("\tblock bitmap: %d\n", gd->bg_block_bitmap);
printf("\tinode bitmap: %d\n", gd->bg_inode_bitmap);
printf("\tinode table: %d\n", gd->bg_inode_table);
printf("\tfree blocks: %d\n", gd->bg_free_blocks_count);
printf("\tfree inodes: %d\n", gd->bg_free_inodes_count);
printf("\tused_dirs: %d\n", gd->bg_used_dirs_count);
// task1, print bitmap
int i,j;
char unsigned *block_bitmap = getBitmap(BLOCK_BITMAP);
char unsigned *inode_bitmap = getBitmap(INODE_BITMAP);
printf("Block bitmap:");
for (i=0; i<sb->s_blocks_count/8; i++) {
printf(" ");
for (j=0; j<8; j++) {
printf("%d", ((block_bitmap[i]>>j)&1));
}
}
printf("\nInode bitmap:");
for (i=0; i<sb->s_inodes_count/8; i++) {
printf(" ");
for (j=0; j<8; j++) {
printf("%d", ((inode_bitmap[i]>>j)&1));
}
}
printf("\n\n");
// task2, print inode
struct ext2_inode *inodeTable = getInodeTable();
char mode='\0';
printf("Inodes:\n");
for(i = 0; i < sb->s_inodes_count; i++)
{
// print root inode and used unreserved inodes
if ((getBit(inode_bitmap, i)==1 && i>=EXT2_GOOD_OLD_FIRST_INO) || (i==EXT2_ROOT_INO-1))
{
// skip the inodes that don't reserve blocks
if (inodeTable[i].i_block[0] == 0)
continue;
// print out the following info about the inode
if (inodeTable[i].i_mode & EXT2_S_IFDIR)
mode = 'd';
else if (inodeTable[i].i_mode & EXT2_S_IFREG)
mode = 'f';
else if (inodeTable[i].i_mode & EXT2_S_IFLNK)
mode = 'l';
printf("[%d] type: %c size: %d links: %d blocks: %d\n", i+1, mode, inodeTable[i].i_size, inodeTable[i].i_links_count, inodeTable[i].i_blocks);
printf("[%d] Blocks: %d\n", i+1, inodeTable[i].i_block[0]);
}
}
printf("\n");
// task3, print directory enties
char type='\0';
struct ext2_dir_entry_2 *dir_entry;
int total_rec_len;
printf("Directory Blocks:\n");
for(i = 0; i < sb->s_inodes_count; i++)
{
// print root inode and used unreserved inodes
if ((getBit(inode_bitmap, i)==1 && i>=EXT2_GOOD_OLD_FIRST_INO) || (i==EXT2_ROOT_INO-1))
{
// skip the inodes that not belong to a directory
if (!(inodeTable[i].i_mode & EXT2_S_IFDIR))
continue;
for (int j=0; j<12; j++) {
if (inodeTable[i].i_block[j] == 0) continue;
printf("\tDIR BLOCK NUM: %d (for inode %d)\n", inodeTable[i].i_block[j], i+1);
dir_entry = (struct ext2_dir_entry_2 *)getBlock(inodeTable[i].i_block[j]);
// while not hit the end of the block
total_rec_len = 0;
while (total_rec_len < EXT2_BLOCK_SIZE) {
if (dir_entry->file_type == EXT2_FT_UNKNOWN)
type = 'u';
else if (dir_entry->file_type == EXT2_FT_REG_FILE)
type = 'f';
else if (dir_entry->file_type == EXT2_FT_DIR)
type = 'd';
else if (dir_entry->file_type == EXT2_FT_SYMLINK)
type = 'l';
if (dir_entry->name_len != 0)
printf("Inode: %d rec_len: %d name_len: %d type= %c name=%s\n", dir_entry->inode, dir_entry->rec_len, dir_entry->name_len, type, dir_entry->name);
total_rec_len = total_rec_len + dir_entry->rec_len;
dir_entry = (void *) dir_entry + dir_entry->rec_len;
}
}
}
}
return 0;
}