-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4Starter.c
69 lines (54 loc) · 1.65 KB
/
p4Starter.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include <stdbool.h>
#include "types.h"
#include "fs.h"
#define BLOCK_SIZE (BSIZE)
int
main(int argc, char *argv[])
{
int r,i,n,fsfd;
char *addr;
struct dinode *dip;
struct superblock *sb;
struct dirent *de;
if(argc < 2){
fprintf(stderr, "Usage: sample fs.img ...\n");
exit(1);
}
fsfd = open(argv[1], O_RDONLY);
if(fsfd < 0){
perror(argv[1]);
exit(1);
}
/* Dont hard code the size of file. Use fstat to get the size */
addr = mmap(NULL, 524248, PROT_READ, MAP_PRIVATE, fsfd, 0);
if (addr == MAP_FAILED){
perror("mmap failed");
exit(1);
}
/* read the super block */
sb = (struct superblock *) (addr + 1 * BLOCK_SIZE);
printf("fs size %d, no. of blocks %d, no. of inodes %d \n", sb->size, sb->nblocks, sb->ninodes);
/* read the inodes */
dip = (struct dinode *) (addr + IBLOCK((uint)0)*BLOCK_SIZE);
printf("begin addr %p, begin inode %p , offset %d \n", addr, dip, (char *)dip -addr);
// read root inode
printf("Root inode size %d links %d type %d \n", dip[ROOTINO].size, dip[ROOTINO].nlink, dip[ROOTINO].type);
// get the address of root dir
de = (struct dirent *) (addr + (dip[ROOTINO].addrs[0])*BLOCK_SIZE);
// print the entries in the first block of root dir
n = dip[ROOTINO].size/sizeof(struct dirent);
for (i = 0; i < n; i++,de++){
printf(" inum %d, name %s ", de->inum, de->name);
printf("inode size %d links %d type %d \n", dip[de->inum].size, dip[de->inum].nlink, dip[de->inum].type);
}
exit(0);
}