-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs.c
396 lines (322 loc) · 9.44 KB
/
fs.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "disk.h"
#include "fs.h"
void inicializa_descritor(struct i_node inode, int inumber,int i,int fd){
descritorArq->file_table[fd].node = inode;
descritorArq->file_table[fd].inumber = inumber;
descritorArq->file_table[fd].pointer_local_search = 0;
descritorArq->file_table[fd].offset = 0;
descritorArq->file_table[fd].bloco = i;
}
int getPointerLocal(){
return descritorArq->file_table->pointer_local_search;
}
void setPointerLocal(int p){
descritorArq->file_table->pointer_local_search = p;
}
struct i_node getInode(){
return descritorArq->file_table->node;
}
int retornaBlocoInumber(int inumber){
// retorna o bloco daquele inumber
int contador = retornaQuantBlocos();
return sBlock->size + (inumber - 1) / contador;
}
int retornaOffset(int inumber){
int contador = retornaQuantBlocos();
// retorna o deslocamento do inumber dentro do inodeblock
return (inumber - 1 ) % contador;
}
int getInumber(){
return descritorArq->file_table->inumber;
}
int retornaPonteiroSeek(int fd){
if(descritorArq->file_table[fd].pointer_local_search==0){
return -1;
}
return getPointerLocal();
}
int retornaInumber(int fd){
if(descritorArq->file_table[fd].inumber==0){
return 0;
}
return getInumber();
}
struct i_node retornaInode(int fd){
struct i_node nod;
nod.size=0;
if(descritorArq->file_table[fd].node.size==0){
return nod;
}
return getInode();
}
int adicionaDescritor(struct i_node inode,int fd,int inumber, int i){
if(descritorArq->file_table[fd].inumber != 0){
return -1;
}
inicializa_descritor(inode,inumber,i,fd);
return 0;
}
void liberaDescritor(int fd){
descritorArq->file_table[fd].node.size = 0;
descritorArq->file_table[fd].inumber = 0;
descritorArq->file_table[fd].bloco = 0;
descritorArq->file_table[fd].pointer_local_search = 0;
}
/* Retorna o proximo descritor de arquivo livre */
int retornaDescritorLivre(){
int i;
for(i = 0; i < MAX_FILES; ++i){
if(descritorArq->file_table[i].inumber==0){
return i;
}
}
return -1;
}
int setSeekPtr(int fd,int ptr){
if(descritorArq->file_table[fd].pointer_local_search==0){
return 0;
}else{
retornaPonteiroSeek(ptr);
return 1;
}
}
void setFileSize(int fd,int size){
descritorArq->file_table[fd].node.file_size = size;
}
void atualizaBlocoLivre(int quant,int i_size){
//vou escrever os blocos de dados
int i,comeco = i_size+1;
for(i=comeco;i<quant;i++){
blocosLivres = NULL;
fd_write_raw(i,blocosLivres);
}
}
int retornaPosInode(struct i_node_block *nodeB){
int i;
for (i = 0; i < retornaQuantBlocos(); ++i)
{
if(nodeB->node[i].size ==0){
return i;
}
}
return -1;
}
int verifica_arquivoAberto(int num){
for (int i = 0; i < 20; ++i){
if(descritorArq->file_table[i].inumber == num){
return 1;
}
}
return 0;
}
/* Inicializa o disco para o estado representando um sistema de arquivos vazio
Ele preenche o superbloco e vincula todos os blocos de dados na lista de blocos livres.
O parametro size representa o numero de blocos de disco no sistema de arquivos,
e i_size representa o numero de blocos de inode
*/
int ffs_format_disk(int size, int i_size){
struct super_block *sBlock = malloc(sizeof(struct super_block));
if( i_size >= size || i_size == 0 || size == 0){
return 0;
}
//inicializando super bloco
sBlock->size = size;
sBlock->isize = i_size;
sBlock->freeList = i_size+1; //
fd_write_super_block(0,sBlock);
struct i_node_block iBlock;
int i;
iBlock.node = NULL;
iBlock.nNodes =0;
for(i = 1; i <= sBlock->size; ++i){
inicializaInode(i);
fd_write_i_node_block(i, &iBlock);
}
return 1;
}
int ffs_create(){
struct super_block *sBlock = malloc(sizeof(struct super_block));
struct i_node_block *inodeB = malloc(sizeof(struct i_node_block));
struct i_node_block novo;
struct i_node node;
int i=0; //bloco de inode
int j=0;
fd_read_super_block(0,sBlock);
for(i=1;i<sBlock->size;i++){
fd_read_i_node_block(i, inodeB);
if(inodeB->nNodes < retornaQuantBlocos()){
break;
}
j++;
}
// no i eu tenho o valor do bloco
if(j!=0)
return -1;
node.size = 0;
node.flags = 0;
node.owner = 0;
node.file_size = 0;
node.pointer =NULL;
//fazer indiretos
inodeB->nNodes++;
int p = inodeB->nNodes -1;
inodeB->node = (struct i_node*)realloc(inodeB->node, inodeB->nNodes*sizeof(struct i_node));
inodeB->node[p] = node;
fd_write_i_node_block(i,inodeB);
fd_read_i_node_block(i,&novo);
return (sBlock->size*inodeB->nNodes +1);
}
int ffs_open(int i_number){
struct super_block *sBlock = malloc(sizeof(struct super_block));
//struct i_node_block *inodeB = malloc(sizeof(struct i_node_block));
struct i_node_block inodeB;
if(!ffs_check_range(i_number)){
return -1;
}
int fd = retornaDescritorLivre();
if(fd < 0){
return -1;
}
if(verifica_arquivoAberto(i_number)){
return -1;
}
fd_read_super_block(0,sBlock);
//bloco que ele está
int bloco = (((i_number - sBlock->isize)/ retornaQuantBlocos()))+1;
int pos_no_inode_block = (i_number - sBlock->isize -1);
fd_read_i_node_block(bloco,&inodeB);
descritorArq->file_table[fd].inumber = i_number;
//descritorArq->file_table[fd].node = inodeB.node[pos_no_inode_block];
descritorArq->file_table[fd].inumber = i_number;
descritorArq->file_table[fd].pointer_local_search = 0;
descritorArq->file_table[fd].offset = pos_no_inode_block;
descritorArq->file_table[fd].bloco = bloco;
descritorArq->file_table[fd].seek = 0;
return fd;
}
/* Libera o inode e todos os blocos do arquivo.
Uma tentativa de excluir o arquivo que está atualmente aberto resulta em erro.
*/
int ffs_delete(int i_number){
struct i_node_block inodeB;
if(verifica_arquivoAberto(i_number)){
return -1;
}
fd_read_i_node_block(i_number,&inodeB);
inodeB.nNodes--;
descritorArq->file_table[i_number].node.flags = 0;
liberaBloco(i_number);
free(descritorArq->file_table[i_number].node.pointer);
fd_write_i_node_block(i_number,&inodeB);
return 1;
}
/* Fecha todos os arquivos abertos e encerra o disco simulado */
int ffs_shutdown(){
int i;
for (i = 0; i < MAX_FILES; ++i){
descritorArq->abertos[i].seek_position = 0;
descritorArq->abertos[i].acesso = -1;
descritorArq->abertos[i].aponta = NULL;
free(blocosLivres);
fd_stop();
return 0;
}
return 0;
}
int ffs_write(int fd, char * buffer, int size){
int bytesEscritos = 0,numEscritas = 0;
int ptrSeek = getPointerLocal(fd);
struct super_block *sBlock = malloc(sizeof(struct super_block));
int num = descritorArq->file_table[fd].inumber;
if(!verifica_arquivoAberto(num)){
return -1;
}
while(bytesEscritos < size){
sBlock->freeList++;
descritorArq->file_table[fd].seek++;
numEscritas = (abs(size - bytesEscritos));
fd_write_super_block(0, sBlock);
fd_write_raw(num,buffer);
bytesEscritos += numEscritas;
ptrSeek += numEscritas;
setFileSize(fd,ptrSeek); // atualiza o tamamho do arquivo
setSeekPtr(fd,ptrSeek); // atualiza o ponteiro do seek
descritorArq->abertos->seek_position = bytesEscritos;
}
return bytesEscritos;
}
int ffs_read(int fd, char * buffer, int size){
//retorna 0 se o ponteiro de busca for maior ou igual ao tamanho do arquivo e o buffer não é modificado
if(descritorArq->file_table[fd].seek >= descritorArq->file_table[fd].node.file_size || descritorArq->file_table[fd].node.file_size == 0){
return 0;
}
if(descritorArq->abertos[fd].acesso==ESCRITA){
return -1;
}
char *dado;
int bytesLidos = 0;
dado = (char*)calloc(get_block_size(), sizeof(char));
int b = descritorArq->file_table[fd].inumber;
fd_read_raw(b, dado);
int i;
for(i = 0; i < size; i++){
buffer[i] = dado[i];
}
bytesLidos = i;
descritorArq->file_table[fd].seek += size;
descritorArq->abertos->seek_position = bytesLidos;
return bytesLidos;
}
int ffs_i_number(int fd){
if(fd < MAX_FILES)
return descritorArq->file_table[fd].inumber;
else
return -1;
}
/* Modifica o ponteiro de busca do seek */
int ffs_seek(int fd, int offset, int whence){
struct i_node nodo;
nodo = descritorArq->file_table[fd].node;
if(whence == 0){
descritorArq->file_table[fd].seek = offset;
}
/*SEEK_CUR*/
else if(whence == 1){
descritorArq->file_table[fd].seek += offset;
}
/*SEEK_END*/
else{
descritorArq->file_table[fd].seek = nodo.file_size + offset;
}
return 1;
}
/*
Escreve o inode no disco e libera a entrada da tabela de arquivo
*/
int ffs_close(int fd){
struct i_node_block inodeB;
fd_read_i_node_block(descritorArq->file_table[fd].bloco, &inodeB);
descritorArq->file_table[fd].node.flags = 0;
// inodeB.node[file_table[fd].offset] = file_table[fd].node;
descritorArq->file_table[fd].inumber = 0;
descritorArq->file_table[fd].pointer_local_search = 0;
descritorArq->file_table[fd].offset = 0;
descritorArq->file_table[fd].bloco = 0;
descritorArq->file_table[fd].seek =0;
//fd_write_i_node_block(file_table[fd].bloco, &inodeB);
return 1;
}
int ffs_check_range(int i_number){
int i;
for ( i = 0; i < MAX_FILES; ++i)
{
if(descritorArq->file_table[i].inumber==i_number){
return i;
}
}
return -1;
}