-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathext2group.c
71 lines (57 loc) · 1.76 KB
/
ext2group.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
/*
* ext2group.c
*
* Reads the first (and only) group-descriptor from a Ext2 floppy disk.
*
* Questions?
* Emanuele Altieri
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/ext2_fs.h>
#define BASE_OFFSET 1024 /* locates beginning of the super block (first group) */
#define FD_DEVICE "/dev/fd0" /* the floppy disk device */
static unsigned int block_size = 0; /* block size (to be calculated) */
int main(void)
{
struct ext2_super_block super;
struct ext2_group_desc group;
int fd;
/* open floppy device */
if ((fd = open(FD_DEVICE, O_RDONLY)) < 0) {
perror(FD_DEVICE);
exit(1); /* error while opening the floppy device */
}
/* read super-block */
lseek(fd, BASE_OFFSET, SEEK_SET);
read(fd, &super, sizeof(super));
if (super.s_magic != EXT2_SUPER_MAGIC) {
fprintf(stderr, "Not a Ext2 filesystem\n");
exit(1);
}
block_size = 1024 << super.s_log_block_size;
/* read group descriptor */
lseek(fd, BASE_OFFSET + block_size, SEEK_SET);
read(fd, &group, sizeof(group));
close(fd);
printf("Reading first group-descriptor from device " FD_DEVICE ":\n"
"Blocks bitmap block: %u\n"
"Inodes bitmap block: %u\n"
"Inodes table block : %u\n"
"Free blocks count : %u\n"
"Free inodes count : %u\n"
"Directories count : %u\n"
,
group.bg_block_bitmap,
group.bg_inode_bitmap,
group.bg_inode_table,
group.bg_free_blocks_count,
group.bg_free_inodes_count,
group.bg_used_dirs_count); /* directories count */
exit(0);
} /* main() */