-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump.c
More file actions
96 lines (84 loc) · 2.04 KB
/
dump.c
File metadata and controls
96 lines (84 loc) · 2.04 KB
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
#include "defs.h"
#include "main_helper.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int SHOW_NODE_DATA = 0;
void print_inode(inode_t *inode, int offset) {
if ((inode->flags & NODE_USED_FLAG) == 0) {
return;
}
char *type = "dir";
if ((inode->flags & DIR_FLAG) == 0) {
type = "file";
}
printf("\tinode(%s): %s\n", type, inode->name);
printf("id:%d\n", offset);
printf("parent:%d\n", inode->parrent);
printf("inodes:");
for (int i = 0; i < INODE_INODE_LIM; i++) {
int id = inode->inode_off[i];
if (id == 0)
continue;
printf(" %d ", id);
}
printf("\n");
printf("nodes:");
printf(" %d ", inode->node_off);
printf("\n");
printf("\n\n");
}
void print_node(node_t *node, int offset) {
if ((node->flags & NODE_USED_FLAG) == 0) {
return;
}
printf("\tnode:\n");
printf("id:%d\n", offset);
printf("next:%d\n", node->next);
printf("parent:%d\n", node->prev);
printf("data (%d bytes):\n", node->len);
if (SHOW_NODE_DATA) {
printf("===[START]===\n");
printf("%s\n", node->data);
printf("===[END]===\n");
}
printf("\n\n");
}
int main(int argc, char **argv) {
for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "-v") == 0) {
SHOW_NODE_DATA = 1;
}
}
char *rel_path = realpath("./", NULL);
char *_path = concat(rel_path, "/data");
if (_path == NULL) {
printf("cannot find file with name[%s]\n", rel_path);
exit(1);
}
int fd = open(_path, O_RDWR | O_CREAT, 0777);
if (fd == -1) {
printf("error\n");
free(_path);
free(rel_path);
exit(1);
}
inode_t *inode = malloc(INODE_SIZE);
node_t *node = malloc(NODE_SIZE);
int offset = 0;
for (int i = 0; i < META_INODE_QUANTITY; i++) {
pread(fd, inode, INODE_SIZE, offset);
print_inode(inode, offset);
offset += INODE_SIZE;
}
for (int i = 0; i < META_NODE_QUANTITY; i++) {
pread(fd, node, NODE_SIZE, offset);
print_node(node, offset);
offset += NODE_SIZE;
}
free(_path);
free(rel_path);
close(fd);
}