-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.c
80 lines (71 loc) · 2.69 KB
/
read.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
#include "api.h"
/* Function to read a data from a file. If file_size not exceeds 4096 bytes then read only direct block */
int read_file(char *filePath)
{
int i;
int file_inode_index = get_inode(filePath, 0, strlen(filePath));
//int file_inode_index = 2;
inode * file_inode = &(inode_table_object->inode_index[file_inode_index]);
int file_size = file_inode->size;
char buff[4096];
si_block * si_block_object ;
si_block_object = (si_block *) buff;
my_read(si_block_object, BS, file_inode->single_indirect_block_offset);
#ifdef DEBUG_BUILD
printf("in read.c read_file: *** File_inode: %d di_db_offset: %d si_block_offset:%d ***\n", file_inode_index, file_inode->direct_block_offset,file_inode->single_indirect_block_offset);
printf("\n--------------------------------------\n");
for(i = 0;i < (BS / sizeof(int *));i++){
printf("in read.c read_file(): si_block(%d) -> db_offset[%d]:%d\n",file_inode->single_indirect_block_offset,i,si_block_object->db_index[i]);
}
printf("\n--------------------------------------\n");
#endif
if(file_size < sizeof(data_block))
print_data_block(file_size, file_inode->direct_block_offset);
else
print_data_block(sizeof(data_block), file_inode->direct_block_offset);
if(file_size > BS){
//file_size -= BS;
read_file_from_indirect_block(file_size-BS, file_inode);
}
//putc('\n',stdout);
return 0;
}
/* Function to read from a file from single indirect block */
int read_file_from_indirect_block(int file_size, inode * file_inode)
{
int i,print_len;
char buff[BS];
si_block * si_block_object ;
si_block_object = (si_block *) buff;
my_read(si_block_object, BS, file_inode->single_indirect_block_offset);
int remain_block = ceil((float) (file_size * 1.0)/ BS);
print_len = BS;
while(remain_block > 0){
if(remain_block > BS / sizeof(int *)){
for(i = 0;i < (BS / sizeof(int *)) - 1;i++ ){
//printf("\n------------------- si_block[%d] at (%d)----------------------------\n",i,si_block_object->db_index[i]);
print_data_block(print_len, si_block_object->db_index[i]);
file_size -= BS;
}
}else{
for(i = 0;i < remain_block ;i++ ){
if(file_size < BS)
print_len = file_size;
//printf("\n------------------- si_block[%d] at (%d)----------------------------\n",i,si_block_object->db_index[i]);
print_data_block(print_len, si_block_object->db_index[i]);
file_size -= BS;
}
}
my_read(si_block_object, BS, si_block_object->db_index[(BS / sizeof(int *)) - 1]);
remain_block -= (BS / sizeof(int *)) - 1;
}
return 0;
}
int print_data_block(int count, unsigned int offset){
int i;
data_block data_block_object;
my_read(&data_block_object, count, offset);
for(i=0; i < count ; i++)
putc(data_block_object.data[i],stdout);
return 0;
}