-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.c
173 lines (139 loc) · 4.95 KB
/
write.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
#include "api.h"
int filewrite()
{
char c;
FILE * fp;
unsigned int needed_block,file_size,buffcount=0;
unsigned int tmp_offset,pos;
int file_inode_index;
inode *file_inode;
data_block *buff;
buff = (data_block *) malloc(sizeof(data_block));
fp = fopen ("bigfile.txt", "rb");
fseek(fp, 0, SEEK_END);
file_size = ftell(fp);
needed_block = ceil(file_size*1.0/(BS));
if(sb_object->no_free_inodes < needed_block){
printf("E: not enough memory");
return 0;
}
fseek(fp, 0, SEEK_SET);
// allocate inode
file_inode_index = sb_object->next_free_inode_index;
set_next_free_inode_index();
// fill data inode
file_inode = (inode *) &inode_table_object->inode_index[file_inode_index];
file_inode->file_type = 0;
file_inode->size = 0;
#ifdef DEBUG_BUILD
printf("in write.c: ***file_size %d***\n",file_size);
#endif
while((pos=ftell(fp))<=file_size){
fread(&c,sizeof(char),1,fp);
if(buffcount == BS || pos==file_size){
tmp_offset = get_next_offset_to_write(file_inode);
#ifdef DEBUG_BUILD
printf("*** db_block_write_offset(%d): %d size: %d ***\n", buffcount, tmp_offset,file_inode->size);
//printf("in write.c filewrite(): *** db_block_write_offset(%d): %d size: %d ***\n", buffcount, tmp_offset,file_inode->size);
#endif
my_write(buff,buffcount,tmp_offset);
set_next_free_block();
file_inode->size += buffcount;
buffcount=0;
write_fs();
}
if(pos!=file_size){
buff->data[buffcount]=c;
//printf("%d\n",buffcount);
buffcount++;
}else{
break;
}
}
free(buff);
return file_inode_index;
}
unsigned int get_next_offset_to_write(inode *file_inode)
{
int file_size = file_inode->size;
int remain_block;
int si_index,si_remblock,tmp_si_offset=0;
unsigned int new_si_off;
si_block * si_block_obj;
si_block_obj = (si_block *) malloc(sizeof(si_block));
if(file_size < sizeof(data_block)){
file_inode->direct_block_offset = sb_object->next_free_data_block_offset;
return file_inode->direct_block_offset;
}else if(file_size == sizeof(data_block)){
file_inode->single_indirect_block_offset = sb_object->next_free_data_block_offset;
set_next_free_block();
si_block_obj->db_index[0] = sb_object->next_free_data_block_offset;
my_write(si_block_obj,sizeof(si_block),file_inode->single_indirect_block_offset);
return sb_object->next_free_data_block_offset;
}else{
file_size -= sizeof(data_block);
remain_block = ceil((file_size*1.0)/sizeof(data_block));
si_index = remain_block /((sizeof(data_block)/sizeof(int *))-1);
si_remblock = remain_block %((sizeof(data_block)/sizeof(int *))-1);
if(!si_remblock)
si_index--;
tmp_si_offset = file_inode->single_indirect_block_offset;
my_read(si_block_obj,sizeof(si_block),file_inode->single_indirect_block_offset);
while(si_index > 0){
tmp_si_offset = si_block_obj->db_index[BS/sizeof(int *)-1] ;
my_read(si_block_obj,sizeof(si_block),tmp_si_offset);
si_index--;
}
if(!si_remblock){
si_block_obj->db_index[BS/sizeof(int *)-1] = sb_object->next_free_data_block_offset;
my_write(si_block_obj,sizeof(si_block),tmp_si_offset);
tmp_si_offset = sb_object->next_free_data_block_offset;
set_next_free_block();
si_block_obj->db_index[0] = sb_object->next_free_data_block_offset;
my_write(si_block_obj,sizeof(si_block),tmp_si_offset);
return sb_object->next_free_data_block_offset;
}else{
#ifdef DEBUG_BUILD
printf("in write.c filewrite(): *** block_no:%d si_blobk_no %d si_db[%d] --- ",remain_block,si_index ,si_remblock);
//printf("in write.c filewrite(): *** db_block_write_offset(%d): %d size: %d ***\n", buffcount, tmp_offset,file_inode->size);
#endif
my_read(si_block_obj,sizeof(si_block),tmp_si_offset);
si_block_obj->db_index[si_remblock]=sb_object->next_free_data_block_offset;
my_write(si_block_obj,sizeof(si_block),tmp_si_offset);
return sb_object->next_free_data_block_offset;
}
}
free(si_block_obj);
}
int write_fs(){
my_write(sb_object,sizeof(sb),0);
my_write(db_bmap_object,sizeof(db_bmap),sb_object->data_block_bmap_offset);
my_write(inode_bmap_object,sizeof(*inode_bmap_object),sb_object->inode_bmap_offset);
my_write(inode_table_object,sizeof(*inode_table_object),sb_object->inode_table_offset);
return 0;
}
int set_next_free_block(){
int i,tmp_offset;
sb_object->no_free_blocks--;
tmp_offset = (sb_object->next_free_data_block_offset - sb_object->data_block_offset)/sizeof(data_block);
db_bmap_object->db_bmap_index[tmp_offset]=1;
for(i=0;i<sizeof(db_bmap);i++){
if(!db_bmap_object->db_bmap_index[i]){
sb_object->next_free_data_block_offset=sb_object->data_block_offset+i*sizeof(data_block);
return 1;
}
}
return 0;
}
int set_next_free_inode_index(){
int i;
inode_bmap_object->inode_bmap_index[ sb_object->next_free_inode_index ] = 1;
sb_object->next_free_inode_index += 1;
sb_object->no_free_inodes -= 1;
for(i=0;i<(sizeof(inode_bmap));i++)
if(!inode_bmap_object->inode_bmap_index[i]){
sb_object->next_free_inode_index=i;
return 1;
}
return 0;
}