-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_mgr.c
294 lines (249 loc) · 9.07 KB
/
storage_mgr.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
#include<stdio.h>
#include<stdlib.h>
#include "storage_mgr.h"
#include <math.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
FILE *fp;
extern void initStorageManager (void){
fp=NULL;
}
extern RC createPageFile (char *fileName){
fp = fopen(fileName, "w+");
if(fp == NULL)
return RC_FILE_NOT_FOUND;
else{
// fseek(fp, PAGE_SIZE, SEEK_SET); -> this does not wwork
// instead create empty page in memory and write it to file
SM_PageHandle EmptyPage = (SM_PageHandle)calloc(PAGE_SIZE,sizeof(char)); //create Empty Page
if(fwrite(EmptyPage,sizeof(char),PAGE_SIZE,fp) < PAGE_SIZE){ //write Empty page to file
printf("fwrite wrong \n");
}else{
printf("fwrite correct \n");
}
//fseek(fp, PAGE_SIZE, SEEK_SET);
fclose(fp); //always close file
free(EmptyPage);
return RC_OK;
}
}
extern RC openPageFile (char *fileName, SM_FileHandle *fHandle){
fp = fopen(fileName, "r+"); //open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND;
else{
//update file handle
fHandle->fileName = fileName;
fHandle->curPagePos = 0;
/* fseek(fp, 0L, SEEK_END);
fPos = ftell(fp);
fseek(fp, 0L, SEEK_SET);
fHandle.totalNumPages = int(fPos)/ PAGE_SIZE;
*/
//use file stats to get file size and derive number of pages
struct stat fileStat;
if(fstat(fileno(fp),&fileStat) < 0)
return RC_ERROR;
fHandle->totalNumPages = fileStat.st_size/ PAGE_SIZE;
fclose(fp);
return RC_OK;
}
}
extern RC closePageFile (SM_FileHandle *fHandle){
if(fp!=NULL)
fp=NULL;
return RC_OK;
}
extern RC destroyPageFile (char *fileName){
fp = fopen(fileName, "r"); //open file with the paramter file name
if(fp == NULL)
return RC_FILE_NOT_FOUND;
remove(fileName); //begone File
return RC_OK;
}
extern RC readBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName, "r"); //open file in r mode
if(fp == NULL){
return RC_FILE_NOT_FOUND; // error check
}
if (pageNum > fHandle->totalNumPages || pageNum < 0) {
fclose(fp);
return RC_READ_NON_EXISTING_PAGE;
}
fseek(fp, (pageNum * PAGE_SIZE), SEEK_SET);
if(fread(memPage, 1, PAGE_SIZE, fp) < PAGE_SIZE)
return RC_ERROR;
fHandle->curPagePos = ftell(fp); //update fhandle
fclose(fp); //always close fp
return RC_OK;
}
extern int getBlockPos (SM_FileHandle *fHandle){
return fHandle->curPagePos;
}
///////////////////////////////////////
extern RC readFirstBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName, "r+"); //Open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND; //Throw error if filepointer is null
int i;
for(i=0;i<PAGE_SIZE; i++){
char c = fgetc(fp); // Reading one char from the file.
if(feof(fp)){ //if filepointer pointing to end of file then break.
break;
}
else
memPage[i] = c;
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
return RC_OK;
}
extern RC readPreviousBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
if(fHandle->curPagePos <= PAGE_SIZE){ //condition to check if current position of file is on first page.
printf("File pointer on the first block. no previous block to read.");
return RC_READ_NON_EXISTING_PAGE;
}
else{
int curpagenum = ceil((float)fHandle->curPagePos/(float)PAGE_SIZE); //Calculating current Page number.
int startpos= (PAGE_SIZE*(curpagenum - 2)); // storing the previous page start position
fp=fopen(fHandle->fileName,"r+"); //Open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND; //Throw error if filepointer is null
fseek(fp,startpos,SEEK_SET);
int i;
for(i=0;i<PAGE_SIZE;i++){ //reading previous block character by character and storing in memPage
memPage[i] = fgetc(fp);
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
return RC_OK;
}
}
extern RC readCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
int curpagenum = ceil((float)fHandle->curPagePos/(float)PAGE_SIZE); //Calculating current Page number.
int startpos= (PAGE_SIZE*(curpagenum - 1)); // storing the current page start position
fp=fopen(fHandle->fileName,"r+"); //Open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND; //Throw error if filepointer is null
fseek(fp,startpos,SEEK_SET);
int i;
for(i=0;i<PAGE_SIZE;i++){ //reading current block character by character and storing in memPage
char c = fgetc(fp);
if(feof(fp))
break;
memPage[i] = c;
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
return RC_OK;
}
extern RC readNextBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
int curpagenum = ceil((float)fHandle->curPagePos/(float)PAGE_SIZE); //Calculating current Page number.
if(fHandle->totalNumPages == curpagenum){ //condition to check if current position of file is on last page.
printf("File pointer on the last block. No next block to read.");
return RC_READ_NON_EXISTING_PAGE;
}
else{
int startpos= (PAGE_SIZE*curpagenum); // storing the next page start position
fp=fopen(fHandle->fileName,"r+"); //Open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND; //Throw error if filepointer is null
fseek(fp,startpos,SEEK_SET);
int i;
for(i=0;i<PAGE_SIZE;i++){ //reading next block character by character and storing in memPage
char c = fgetc(fp);
if(feof(fp))
break;
memPage[i] = c;
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
return RC_OK;
}
}
extern RC readLastBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName, "r+"); //Open file in read mode
if(fp == NULL)
return RC_FILE_NOT_FOUND; //Throw error if filepointer is null
int startpos = (fHandle->totalNumPages - 1) * PAGE_SIZE; // storing the last page start position
fseek(fp,startpos,SEEK_SET);
int i;
for(i=0;i<PAGE_SIZE; i++){ //reading last block character by character and storing in memPage
char c = fgetc(fp);
if(feof(fp))
break;
memPage[i] = c;
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
return RC_OK;
}
/////////////////////////////////////////
//Write block to Absolute Position (pageNum)
extern RC writeBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName, "r+"); // open file in read+write mode.
if(fp == NULL)
return RC_FILE_NOT_FOUND; // Throw an error file not found
int spec_pos = (pageNum)*PAGE_SIZE; //storing starting position of pageNum
if(pageNum!=0){ // Write content to a non first page.
fHandle->curPagePos = spec_pos;
writeCurrentBlock(fHandle,memPage);
}
else{ //write content to the first page
fseek(fp,spec_pos,SEEK_SET);
int i;
for(i=0;i<PAGE_SIZE;i++)
{
if(feof(fp)) // check file is ending in between writing
{
appendEmptyBlock(fHandle); // append empty block at the end of file
}
fputc(memPage[i],fp);// write content to file
}
fHandle->curPagePos = ftell(fp);// set current file position to curPagePos
fclose(fp); //closing filepointer
}
return RC_OK;
}
// write block to current Page
extern RC writeCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName, "r+");// open file in read+write mode.
if(fp == NULL)
return RC_FILE_NOT_FOUND; // Throw an error file not found.
long int curPosition = fHandle->curPagePos; //Storing current file position.
appendEmptyBlock(fHandle); //Appending an empty block to make space for the new content.
fseek(fp,curPosition,SEEK_SET); //Seek to the current position.
int ctr=0;
while(fgetc(fp)!= EOF) //Calculating the total number of character after the point where we need to insert new data.
ctr++;
fseek(fp,curPosition,SEEK_SET);
char *string1 = malloc(PAGE_SIZE+1);
fread(string1,1,ctr-PAGE_SIZE,fp); //Storing in string1 the content after the current position.
fseek(fp,curPosition,SEEK_SET);
fwrite(memPage,1,strlen(memPage),fp);//Writing the memPage to our file.
fwrite(string1,1,strlen(string1),fp);//Writing the string1 to our file.
fHandle->curPagePos = ftell(fp); // set current file position to curPagePos
free(string1); //free string memory
fclose(fp); //closing filepointer
return RC_OK;
}
////////////////////////////////////////////////////////
extern RC appendEmptyBlock (SM_FileHandle *fHandle){
SM_PageHandle EmptyPage = (SM_PageHandle)calloc(PAGE_SIZE,sizeof(char)); //creating empty page of PAGE_SIZE bytes
fseek(fp, 0, SEEK_END);
fwrite(EmptyPage,sizeof(char),PAGE_SIZE,fp); //Writing Empty page to the file.
free(EmptyPage); //free memory from EmptyPage.
fHandle->totalNumPages++; //Increasing total number of pages.
return RC_OK;
}
extern RC ensureCapacity (int numberOfPages, SM_FileHandle *fHandle){
fp=fopen(fHandle->fileName,"a");
if(fp==NULL)
return RC_FILE_NOT_FOUND;
while(numberOfPages > fHandle->totalNumPages) //If numberOfPages is greater than totalNumPages then add emptly pages.
appendEmptyBlock(fHandle);
fclose(fp);
return RC_OK;
}