-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.c
175 lines (141 loc) · 5.31 KB
/
convert.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "metadata.h"
#include "metadata_disk.h"
#include "list.h"
off_t list_array_size(list_t *dinodes)
{
list_iter_t *iter;
list_iter_create(&iter);
list_iter_init(iter,dinodes,FORWARD);
off_t total_size = 0;
dinode *di_n;
while((di_n=(dinode*)list_iter_next(iter))!=NULL)
total_size += sizeof(dinode_disk) +
list_get_len(di_n->dentry_list) * sizeof(dentry_disk);
list_iter_destroy(&iter);
return total_size;
}
/*
void list_to_array(list_t *dinodes,char *array)
{
list_iter_t *hier_iter;
list_iter_create(&hier_iter);
list_iter_init(hier_iter,dinodes,FORWARD);
dinode* current;
list_iter_t* dentries_iter;
list_iter_create(&dentries_iter);
dentry* current_dentry;
int i;
while((current = list_iter_next(hier_iter)) != NULL){
list_iter_init(dentries_iter,current->dentry_list,FORWARD);
while((current_dentry = list_iter_next(dentries_iter))!= NULL){
printf("The length is %d\n",current_dentry->length);
for(i=0;i<current_dentry->length;i++){
printf("File %d: %s\n",i,(current_dentry->tuple_entry[i]).filename);
}
}
}
list_iter_destroy(&hier_iter);
list_iter_t *di_iter;
list_iter_create(&di_iter);
list_iter_init(di_iter,dinodes,FORWARD);
dinode *di_n = (dinode*)list_iter_next(di_iter);
list_iter_t *de_iter;
list_iter_create(&de_iter);
list_t *queue;
list_create(&queue,sizeof(dinode),NULL);
list_enqueue(queue,di_n);
off_t dentry_off=0,dinode_off = 0;
while(list_get_len(queue)) {
dinode dn;
dinode_disk dn_disk;
list_pop(queue,&dn);
dn_disk.dinode_number = dn.dinode_number;
dn_disk.permissions = dn.permissions;
dn_disk.user_id = dn.user_id;
dn_disk.group_id = dn.group_id;
dn_disk.time_of_access = dn.time_of_access;
dn_disk.total_size = dn.total_size;
dn_disk.file_off = dn.file_off;
dn_disk.n_dentries = list_get_len(dn.dentry_list);
memcpy(array+dinode_off,&dn_disk,sizeof(dinode_disk));
dentry_off = dinode_off + sizeof(dinode_disk);
dinode_off += sizeof(dinode_disk) +
list_get_len(dn.dentry_list)*sizeof(dentry_disk);
dentry *de;
list_iter_init(de_iter,dn.dentry_list,FORWARD);
while((de=(dentry*)list_iter_next(de_iter)) != NULL) {
dentry_disk de_d;
de_d.length = de->length;
entry_disk entries[DENTRIES_NUM];
unsigned int i;
entry *en;
off_t entry_off = dinode_off;
for (i=0;i<de->length;++i) {
en = &(de->tuple_entry[i]);
strcpy(entries[i].filename,en->filename);
entries[i].dinode_num = en->dinode_num;
if (!strcmp(en->filename,".") || !strcmp(en->filename,"..")) continue;
//off_t entry_off = dinode_off;
entries[i].dinode_off = entry_off;
printf("Current %s",entries[i].filename);
entry_off += sizeof(dinode_disk) + list_get_len(en->dinode_idx->dentry_list);
list_enqueue(queue,en->dinode_idx);
}
memcpy(&(de_d.tuple_entry),entries,DENTRIES_NUM*sizeof(entry_disk));
memcpy(array+dentry_off,&de_d,sizeof(dentry_disk));
dentry_off += sizeof(dentry_disk);
}
}
list_iter_destroy(&di_iter);
list_iter_destroy(&de_iter);
list_destroy(&queue);
}
void array_to_list(char *array,list_t **dinodes)
{
list_create(dinodes,sizeof(dinode),free);
list_t *stack;
list_create(&stack,sizeof(convert_node),free);
convert_node *cv = malloc(sizeof(convert_node));
cv->_entry = NULL;
cv->off=0;
list_push(stack,cv);
while(list_get_len(stack)) {
convert_node cnode;
list_pop(stack,&cnode);
dinode_disk *di_d = (dinode_disk*)array + cnode.off;
printf("Cnodeoff %jd\n",cnode.off);
dinode *di = malloc(sizeof(dinode));
if (cnode._entry != NULL) (cnode._entry)->dinode_idx = di;
list_enqueue(*dinodes,di);
di->dinode_number = di_d->dinode_number;
di->permissions = di_d->permissions;
di->user_id = di_d->user_id;
di->group_id = di_d->group_id;
di->time_of_access = di_d->time_of_access;
di->total_size = di_d->total_size;
di->file_off = di_d->file_off;
list_create(&(di->dentry_list),sizeof(dentry),free);
size_t n_dentries = di_d->n_dentries;
dentry_disk *de_d = (dentry_disk*)(di_d + sizeof(dinode_disk));
unsigned int i;
for (i=0;i<n_dentries;++i){
dentry *de = malloc(sizeof(dentry));
de->length = de_d->length;
unsigned int j;
for (j=0;j<de_d->length;++j) {
entry_disk *en_d = &(de_d->tuple_entry[j]);
entry *en = &(de->tuple_entry[j]);
strcpy(en->filename,en_d->filename);
en->dinode_num = en_d->dinode_num;
convert_node *cv = malloc(sizeof(convert_node));
cv->off = en_d->dinode_off;
cv->_entry = en;
list_push(stack,cv);
}
list_enqueue(di->dentry_list,de);
de_d += (sizeof(dentry_disk));
}
}
list_destroy(&stack);
}
*/