-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_mgr.c
306 lines (249 loc) · 7.23 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
295
296
297
298
299
300
301
302
303
304
305
#include "storage_mgr.h"
#include "dberror.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
FILE *fp;
void initStorageManager (){
printf("Storage manager initialised\n\n");
//No need to implement this as we don't have any global data structure to initialise
}
/*this method used to Create file if not exists
* fileName- is the parameter that points to file
*/
RC createPageFile(char *fileName){
//FILE *fp;
if(access(fileName, F_OK) == 0){
printf("File, %s already exists \n\n", fileName);
return RC_OK;
}
fp=fopen(fileName,"w+");
if(fp == NULL){
printf("File ceation failed\n\n");
return RC_FILE_CREATION_FAILED;
}
else{
printf("File, %s has been created\n\n", fileName);
char *input = (char*) malloc(PAGE_SIZE);
int i=0;
while(i<PAGE_SIZE){
input[i]='\0';
i++;
}
if(fwrite(input,sizeof(char),PAGE_SIZE,fp) != PAGE_SIZE){
return RC_WRITE_TO_OUTPUTSTREAM_FAILED;
}
free(input);
if(fclose(fp) != 0)
return RC_FILE_NOT_CLOSED;
else
return RC_OK;
}
}
/*this method used open file to read or write
filename- points to file
fHandle- points to file handler structure*/
RC openPageFile (char *fileName, SM_FileHandle *fHandle){
if(fileName != NULL){
fp =fopen(fileName,"r+");
}
else{
printf("File, %s do not exists \n\n", fileName);
return RC_FILE_NOT_FOUND;
}
if(fp!=NULL){
struct stat statPointer;
//printf("\n\ntotal pages %i\n",fHandle->totalNumPages);
//count the total number of page in a file
if(stat(fileName, &statPointer) != -1 ){
int numberOfPages;
numberOfPages = (statPointer.st_size)/PAGE_SIZE;
if((statPointer.st_size)%PAGE_SIZE > 0)
fHandle->totalNumPages = numberOfPages+1;
else
fHandle->totalNumPages = numberOfPages;
}
fHandle->totalNumPages =1;
fHandle->fileName=fileName;
fHandle->curPagePos=0;
fHandle->mgmtInfo=fp;
fclose(fp);
//printf("total pages after %i",fHandle->totalNumPages);
return RC_OK;
}
else{
printf("Error:File Not Found");
return RC_FILE_NOT_FOUND;
}
}
/* This method closes the file and frees the memory
fHandle- is the pointer to file handler structure that has the details of file to be closed*/
RC closePageFile(SM_FileHandle *fHandle){
//FILE *fp=NULL;
if(fHandle == NULL)
return RC_FILE_HANDLE_NOT_INIT;
else
fp=fHandle->mgmtInfo;
if(fclose(fp)==0){
printf("file, %s has been closed \n\n",fHandle->fileName);
return RC_OK;
}else{
return RC_FILE_NOT_CLOSED;
}
}
/*This method deletes the file from memory
fileName- is the pointer to file that need to be closed*/
RC destroyPageFile(char *fileName){
if(remove(fileName)==0)
return RC_OK;
else
return RC_FILE_NOT_FOUND;
}
/*This method reads the block from file and loads the data into memory
pageNum- this specify which block need to be read
memPage- this is the pointer to memory where data will be loaded from file*/
RC readBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
fp = fopen(fHandle->fileName,"r+");
if(fHandle == NULL)
return RC_FILE_HANDLE_NOT_INIT;
/*if(!fp) //check if the page is closed, if so, then open
{
fp= fopen(fHandle->fileName,"r");
//fHandle->mgmtInfo=fp;
}else
return RC_FILE_READ_FAILED;
*/
fseek(fp,(pageNum*PAGE_SIZE),SEEK_SET);
fread(memPage,sizeof(char),PAGE_SIZE,fp);
fHandle->curPagePos=ftell(fp);
fclose(fp);
return RC_OK;
}
/* returns the current page possition in a file*/
int getBlockPos(SM_FileHandle *fHandle){
return fHandle->curPagePos;
}
/*this method reads the first block/page in a file */
RC readFirstBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
return readBlock(0,fHandle,memPage); //call readBlock page by passing 0 as the page Number value
}
/*this method reads the previous block/page of a file with respect to curPagePos*/
RC readPreviousBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
FILE *fp;
int pageNum;
fp=fHandle->mgmtInfo;
pageNum=(fHandle->curPagePos)-1;
if(fp==NULL || pageNum<0)
return RC_READ_NON_EXISTING_PAGE;
else
return readBlock(pageNum, fHandle, memPage);
}
/*this method reads the current block/page of a file*/
RC readCurrentBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
FILE *fp;
int pageNum;
fp=fHandle->mgmtInfo;
pageNum=(fHandle->curPagePos);
if(fp==NULL || pageNum<0 || pageNum>fHandle->totalNumPages)
return RC_READ_NON_EXISTING_PAGE;
else
return readBlock(pageNum, fHandle, memPage);
}
/*this method reads the next block/page of a file with respect to curPagePos*/
RC readNextBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
FILE *fp;
int pageNum;
fp=fHandle->mgmtInfo;
pageNum=(fHandle->curPagePos)+1;
if(fp==NULL || pageNum<0 || pageNum>fHandle->totalNumPages)
return RC_READ_NON_EXISTING_PAGE;
else
return readBlock(pageNum, fHandle, memPage);
}
/*this method reads the last block/page in a file*/
RC readLastBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
int LastPage=(fHandle->totalNumPages)-1;
return readBlock(LastPage,fHandle,memPage);
}
/*This method writes a data from memory to page file that is present on disk and page number is passed in the parameter*/
RC writeBlock(int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
FILE *fp=fopen(fHandle->fileName,"r+");
fHandle->mgmtInfo=fp;
/*
if(pageNum<0 || pageNum>fHandle->totalNumPages || fp==NULL){
return RC_WRITE_FAILED;
}
*/
if(!fp) //check if the page is closed, if so, then open
{
fp= fopen(fHandle->fileName,"r+");
fHandle->mgmtInfo=fp;
}
if(fseek(fp,PAGE_SIZE*(pageNum),SEEK_SET)!=0)
{
return RC_FILE_OFFSET_FAILED;
}
else{
int rc = fwrite(memPage,sizeof(char),PAGE_SIZE,fp);
if(rc != PAGE_SIZE){
return RC_WRITE_FAILED;
}else{
fclose(fp);
fHandle->curPagePos=ftell(fp);
return RC_OK;
}
}
}
/*This method writes a data from memory to current page file*/
RC writeCurrentBlock(SM_FileHandle *fHandle, SM_PageHandle memPage){
int pageNum=fHandle->curPagePos;
return writeBlock(pageNum, fHandle, memPage);
}
/*This method appends a new page at the end of the file*/
RC appendEmptyBlock(SM_FileHandle *fHandle){
FILE *fp=fopen(fHandle->fileName,"a"); //open the file in append mode
char *input = (char*) malloc(PAGE_SIZE);
int i=0;
while(i<PAGE_SIZE){
input[i]='\0';
i++;
}
if(fwrite(input,sizeof(char),PAGE_SIZE,fp) != PAGE_SIZE){
return RC_WRITE_FAILED;
}
free(input);
fHandle->totalNumPages+1; //increase the total number of page count by 1
if(fclose(fp) != 0)
return RC_FILE_NOT_CLOSED;
else
return RC_OK;
}
/*This method ensures the number of pages in the file*/
RC ensureCapacity(int numberOfPages, SM_FileHandle *fHandle){
FILE *fp;
if(fHandle==NULL){
return RC_FILE_HANDLE_NOT_INIT;
}
fp=fHandle->mgmtInfo;
if(!fp){
fp = fopen(fHandle->fileName,"r+");
}
fseek(fp, 0L, SEEK_END);
int pagesInFile = ftell(fp)/PAGE_SIZE;
if(ftell(fp)%PAGE_SIZE > 0){
pagesInFile=pagesInFile+1;
}
if(pagesInFile < numberOfPages){
int pagesToAdd = numberOfPages - pagesInFile;
int totalSize = (pagesToAdd)*PAGE_SIZE;
int i=0;
while(i<totalSize){
fprintf(fp, "%c", '\0');
i++;
}
fHandle->totalNumPages = (fHandle->totalNumPages)+pagesToAdd;
return RC_OK;
}
}