-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handling_functions.c
194 lines (149 loc) · 5.8 KB
/
file_handling_functions.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
#include "common.h"
#include "util.h"
#include "file_handling_functions.h"
char* readNbytesFromOffset(int n,int offset, char *fileSystemName)
{
FILE* fp = fopen(fileSystemName,"r");
fseek(fp, offset, SEEK_SET);
// n+1 is given as fgets always returns null terminated string so one extra
char* data = (char*) malloc((n+1)*sizeof(char));
if(data==NULL)
assert(0);
//fgets(data,n+1,fp);
fread(data,sizeof(char), DATA_BLOCK_SIZE, fp);
//printf("Data in syscall :%s\n",data);
fclose(fp);
return data;
}
void writeSuperBlock(WholeFS *fs)
{
FILE *fp = fopen(fs->fileSystemName, "r+");
int i;
// write super block
fseek(fp, sizeof(int)+1, SEEK_SET);
fprintf(fp, "%d ", fs->sb.inodeCount);
fprintf(fp, "%d ", fs->sb.freeInodeCount);
fprintf(fp, "%d ", fs->sb.iNodeOffset);
fprintf(fp, "%d ", fs->sb.dataBlockOffset);
fprintf(fp, "%d ", fs->sb.dataBlockCount);
fprintf(fp, "%d ", fs->sb.freeDataBlockCount);
fprintf(fp, "%d ", fs->sb.iNodeSize);
fprintf(fp, "%d ", fs->sb.dataBlockSize);
for(i = 0; i < NO_OF_INODES; i++)
fprintf(fp, "%d ", fs->sb.inodeList[i]);
for(i = 0; i < NO_OF_DATA_BLOCKS; i++)
fprintf(fp, "%d ", fs->sb.dataBlockList[i]);
fclose(fp);
}
// reads super block into memory
void readSuperBlock(WholeFS* fs)
{
FILE *fp = fopen(fs->fileSystemName, "r");
int i;
// write super block
fseek(fp, sizeof(int), SEEK_SET);
fscanf(fp, "%d ", &(fs->sb.inodeCount));
fscanf(fp, "%d ", &(fs->sb.freeInodeCount));
fscanf(fp, "%d ", &(fs->sb.iNodeOffset));
fscanf(fp, "%d ", &(fs->sb.dataBlockOffset));
fscanf(fp, "%d ", &(fs->sb.dataBlockCount));
fscanf(fp, "%d ", &(fs->sb.freeDataBlockCount));
fscanf(fp, "%d ", &(fs->sb.iNodeSize));
fscanf(fp, "%d ", &(fs->sb.dataBlockSize));
for(i = 0; i < NO_OF_INODES; i++)
fscanf(fp, "%d ", &(fs->sb.inodeList[i]));
for(i = 0; i < NO_OF_DATA_BLOCKS; i++)
fscanf(fp, "%d ", &(fs->sb.dataBlockList[i]));
/*print */
//printf("inodecount: %d \n", (fs->sb.inodeCount));
//printf("freeInode: %d \n", (fs->sb.freeInodeCount));
//printf("inodeOfsset: %d \n", (fs->sb.iNodeOffset));
//printf("datablockoffset %d \n", (fs->sb.dataBlockOffset));
//printf("datablockCount %d \n", (fs->sb.dataBlockCount));
//printf("freeDataBlockCount %d\n", (fs->sb.freeDataBlockCount));
//printf("inodesize: %d ", (fs->sb.iNodeSize));
//printf("datablocksize: %d ", (fs->sb.dataBlockSize));
fs->pwdInodeNumber = 1;
strcpy(fs->pwdPath,"/");
fclose(fp);
}
int writeNBytesToOffset(char* buffer,int n,int offset, char *fileSystemName)
{
FILE* fp = fopen(fileSystemName,"r+");
fseek(fp, offset, SEEK_SET);
//size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
int byteWritten = fwrite( (const void *) buffer, sizeof(char), n,fp );
//if(n!=byteWritten)
// assert(0);
fclose(fp);
return byteWritten;
}
// write in a data block at some offset
int writeDataBlockToFile(WholeFS*fs,char* blockBuffer,int existingFileSize, int index)
{
int offset = fs->sb.dataBlockOffset + (index * DATA_BLOCK_SIZE) + (existingFileSize % DATA_BLOCK_SIZE);
int byteWritten = writeNBytesToOffset(blockBuffer,DATA_BLOCK_SIZE,offset, fs->fileSystemName);
return byteWritten;
}
// write in a data block at some offset
int appendNbytesInDataBlockToFile(WholeFS*fs,char* blockBuffer,int off, int index,int n)
{
// actual offset to write
int offset = fs->sb.dataBlockOffset + (index * DATA_BLOCK_SIZE) + off;
int byteWritten = writeNBytesToOffset(blockBuffer,n,offset, fs->fileSystemName);
return byteWritten;
}
// write entire data block to file
int writeEntireDataBlockToFile(WholeFS*fs,char* blockBuffer, int index)
{
int offset = fs->sb.dataBlockOffset + (index * DATA_BLOCK_SIZE) ;
int byteWritten = writeNBytesToOffset(blockBuffer,DATA_BLOCK_SIZE,offset, fs->fileSystemName);
return byteWritten;
}
void writeInodeBlockToFile(WholeFS*fs,char* blockBuffer, int index)
{
int offset = fs->sb.iNodeOffset + (index * sizeof(Inode));
writeNBytesToOffset(blockBuffer,sizeof(Inode),offset, fs->fileSystemName);
}
void writeSuperNodeBlockToFile(WholeFS*fs,char* blockBuffer)
{
int offset = sizeof(int);
writeNBytesToOffset(blockBuffer,sizeof(SuperBlock),offset, fs->fileSystemName);
}
char* readDataBlockFromFile(WholeFS*fs, int index)
{
int offset = fs->sb.dataBlockOffset + (index * DATA_BLOCK_SIZE);
/* printf("offset %d \n", offset);
printf("inode list block offset %d \n", fs->sb.iNodeOffset);
printf("data block offset %d \n", fs->sb.dataBlockOffset);
printf("sizeof SUPER block offset %d \n", sizeof(SuperBlock)); */
char* data = readNbytesFromOffset(DATA_BLOCK_SIZE,offset, fs->fileSystemName);
//printf("Data %s \n", data);
return data;
}
char* readInodeBlockFromFile(WholeFS*fs, int index)
{
int offset = fs->sb.iNodeOffset + (index * sizeof(Inode));
char* data = readNbytesFromOffset(sizeof(Inode),offset, fs->fileSystemName);
return data;
}
char* readSuperBlockFromFile(WholeFS*fs)
{
int offset = sizeof(int);
char* data = readNbytesFromOffset(sizeof(SuperBlock),offset, fs->fileSystemName);
return data;
}
int writeInodeToFile(WholeFS* fs,Inode* inode,int inodeIndex)
{
FILE* fp = fopen(fs->fileSystemName,"r+");
fseek(fp, fs->sb.iNodeOffset, SEEK_SET);
fseek(fp, inodeIndex * fs->sb.iNodeSize, SEEK_CUR);
fprintf(fp, "%d ", inode->fileMode);
fprintf(fp, "%d ", inode->linkCount);
fprintf(fp, "%d ", inode->fileSize);
//printf("Inode WriteFS : %d, DB[0]: %d\n",inodeIndex,inode->directDBIndex[0]);
int i;
for(i = 0; i<DIRECT_DATA_BLOCK_NUMBER; i++)
fprintf(fp, "%d ", inode->directDBIndex[i]);
fclose(fp);
}