Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "dberror.h"
- #include "storage_mgr.h"
- #include <stdio.h>
- #include <errno.h> //standard error nums for built in library functions, such as fwrite or fread.
- #include <stdlib.h>
- #include <string.h>
- extern int errno;
- RC updateCurrentInformation(int totalNumPages, int curPagePos, SM_FileHandle *fHandle);
- typedef struct SM_FileHeader {
- int totalNumPages;
- int curPagePos;
- } SM_FileHeader;
- void initStorageManager(){
- };
- /**************************************************************************************************
- * Function Name: createPageFile
- * Description:
- * Create a page file and write empty content. ('/0')
- *
- * Parameters:
- * char *fileName: file name
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
- * 2016-09-24 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- * 2016-09-27 Jose Carmona <jcarmonalopez@hawk.iit.edu> Fixed error handle.
- **************************************************************************************************/
- RC createPageFile (char *fileName){
- FILE *file = fopen(fileName, "w"); //creates pointer called file
- if(file){ //if the file xists, then
- char eof = '\0'; //eof is marked by this character, from specifications
- struct SM_FileHeader fileInfo; //create a FileHeader called fileinfo
- fileInfo.totalNumPages = 1; //
- fileInfo.curPagePos = 0;
- // SM_FileHeader struct will be stored at the start of the document
- // It will only contains totalNumPages and curPagePos
- errno = 0; //no error, so err num 0
- //writes data from the array pointed to, by ptr to the given stream.
- //size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
- //ptr − This is the pointer to the array of elements to be written.
- //size − This is the size in bytes of each element to be written.
- //nmemb − This is the number of elements, each one with a size of size bytes.
- //stream − This is the pointer to a FILE object that specifies an output stream.
- fwrite(&fileInfo, sizeof(SM_FileHeader), 1, file); //use fwrite,
- if(errno != 0){
- fclose(file);
- return RC_WRITE_FAILED;
- }
- int i;
- // We writes the first page full of '\0' (null value in C is \0)
- for (i = 0; i < PAGE_SIZE; ++i){
- errno = 0;
- fwrite(&eof, sizeof(eof), 1, file);
- if(errno != 0){
- fclose(file);
- return RC_WRITE_FAILED;
- }
- }
- // to close the file after we write to it successfully
- errno = 0;
- fclose(file);
- if(errno == 0)
- return RC_OK;
- else return RC_WRITE_FAILED;
- }
- else{//the file doesnt exist, so file handle is not initialized
- return RC_FILE_HANDLE_NOT_INIT;
- }
- };
- /**************************************************************************************************
- * Function Name: openPageFile
- * Description:
- * Opens the page file.
- *
- * Parameters:
- * char *fileName : file name
- * SM_FileHandle *fHandle: output file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main Logic.
- * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC openPageFile (char *fileName, SM_FileHandle *fHandle){
- // file is opened
- //FILE *fopen(const char *filename, const char *mode)
- /*"r" Opens a file for reading. The file must exist.
- "w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
- "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
- "r+" Opens a file to update both reading and writing. The file must exist.
- "w+" Creates an empty file for both reading and writing.
- "a+" Opens a file for reading and appending.
- */
- FILE *file = fopen(fileName, "r+"); //fopen is another built in function
- struct SM_FileHeader fileInfo;
- if (file){
- // we read first the metadata at the start of the file
- errno = 0;
- fread(&fileInfo, sizeof(SM_FileHeader), 1, file);
- if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
- // now we initialize the SM_FileHandle struct
- fHandle->fileName = fileName;
- fHandle->totalNumPages = fileInfo.totalNumPages;
- fHandle->curPagePos = fileInfo.curPagePos;
- fHandle->mgmtInfo = file; //the pointer to file is stored in fHandle.mgmtInfo, so if we want to close the file, we do fclose(fHandle.mgmtInfo)
- // we are using mgmtInfo to store the file handle needed
- // to write, read, close and other I/O operations
- return RC_OK;
- }else{
- return RC_FILE_NOT_FOUND;
- }
- };
- /**************************************************************************************************
- * Function Name: closePageFile
- * Description:
- * Closes the page file.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main Logic.
- * 2016-09-25 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Added header comment,
- * add comments.
- **************************************************************************************************/
- RC closePageFile (SM_FileHandle *fHandle){
- if(fHandle){
- //fclose (FILE *stream)
- fclose(fHandle->mgmtInfo);//when we used openPageFile, we set fhandle.mgmtinfo= file, which is the pointer to the file. we are closing that now
- return RC_OK;
- }else{
- return RC_FILE_HANDLE_NOT_INIT;
- }
- };
- /**************************************************************************************************
- * Function Name: destroyPageFile
- * Description:
- * Removes the file.
- *
- * Parameters:
- * char *fileName: file name
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
- * 2016-09-22 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC destroyPageFile (char *fileName){
- errno = 0;
- remove(fileName);//built in function to remove file
- if(errno != 0) return RC_FILE_NOT_FOUND;
- return RC_OK;
- };
- /**************************************************************************************************
- * Function Name: readBlock
- * Description:
- * Reads specified page number.
- *
- * Parameters:
- * int pageNum : page number to be read
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic
- * 2016-09-25 Victor Portals <vportalslorenzo@hawk.iit.edu> Error Handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Checks if pageNum is inside the bounds
- if(pageNum < 0 || pageNum >= fHandle->totalNumPages)
- return RC_READ_NON_EXISTING_PAGE;
- // we use fseek to move the read pointer of our file
- // where we want to start reading
- errno = 0;
- fseek(fHandle->mgmtInfo, sizeof(SM_FileHeader)+pageNum*PAGE_SIZE, SEEK_SET); //TRYING TO POINT TO THE START OF OUR ACTUAL DATA, not the header
- if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
- errno = 0;
- /*
- The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) reads data from the given stream into the array pointed to, by ptr.
- size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
- ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
- size − This is the size in bytes of each element to be read.
- nmemb − This is the number of elements, each one with a size of size bytes.
- stream − This is the pointer to a FILE object that specifies an input stream.
- */
- //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
- fread(memPage, PAGE_SIZE, 1, fHandle->mgmtInfo);//
- if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
- // this function will update metadata in fHandle struct and
- // it will write the metadata to the file
- updateCurrentInformation(fHandle->totalNumPages, pageNum, fHandle);
- return RC_OK;
- };
- /**************************************************************************************************
- * Function Name: getBlockPos
- * Description:
- * Gets current page position.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- int getBlockPos (SM_FileHandle *fHandle){
- if(fHandle)
- return fHandle->curPagePos;
- else return RC_FILE_HANDLE_NOT_INIT;
- };
- /**************************************************************************************************
- * Function Name: readFirstBlock
- * Description:
- * Reads first page.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readFirstBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // first block has pageNum = 0
- return readBlock(0, fHandle, memPage);//just call our read function and read the first page
- };
- /**************************************************************************************************
- * Function Name: readLastBlock
- * Description:
- * Reads last page in memory.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readLastBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Last block has pageNum = totalNumPages
- // readBlock function will check if pageNum is inside the bounds
- return readBlock(fHandle->totalNumPages-1, fHandle, memPage); //read the last block
- };
- /**************************************************************************************************
- * Function Name: readPreviousBlock
- * Description:
- * Reads previous page to current.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readPreviousBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Previous block has pageNum = curPagePos-1
- // readBlock function will check if pageNum is inside the bounds
- return readBlock(fHandle->curPagePos-1, fHandle, memPage);
- };
- /**************************************************************************************************
- * Function Name: readCurrentBlock
- * Description:
- * Reads current page in memory.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Current block has pageNum = curPagePos
- // readBlock function will check if pageNum is inside the bounds
- return readBlock(fHandle->curPagePos, fHandle, memPage);
- };
- /**************************************************************************************************
- * Function Name: readNextBlock
- * Description:
- * Reads next page to current.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC readNextBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Next block has pageNum = curPagePos+1
- // readBlock function will check if pageNum is inside the bounds
- return readBlock(fHandle->curPagePos+1, fHandle, memPage);
- };
- /**************************************************************************************************
- * Function Name: writeBlock
- * Description:
- * Writes page read from memory to page file.
- *
- * Parameters:
- * int pageNum : number of page to be writed
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
- * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- * 2016-09-27 Jose Carmona <jcarmonalopez@hawk.iit.edu> Fixed error handle.
- **************************************************************************************************/
- RC writeBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Checks if pageNum is inside the bounds
- if(pageNum < 0 || pageNum >= fHandle->totalNumPages)
- return RC_READ_NON_EXISTING_PAGE;
- // we use fseek to move the read pointer of our file
- // where we want to start writing
- errno = 0;
- fseek(fHandle->mgmtInfo, sizeof(SM_FileHeader)+pageNum*PAGE_SIZE, SEEK_SET); //moving to the start of the data
- if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
- errno = 0;
- fwrite(memPage, PAGE_SIZE, 1, fHandle->mgmtInfo); //just use fwrite to write to that page
- if(errno != 0) return RC_WRITE_FAILED;
- return RC_OK;
- };
- /**************************************************************************************************
- * Function Name: writeCurrentBlock
- * Description:
- * Writes page read from memory to current page.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- * SM_PageHandle memPage : where the block will be stored after read
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC writeCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
- // Current block has pageNum = curPagePos
- // readBlock function will check if pageNum is inside the bounds
- return writeBlock(fHandle->curPagePos, fHandle, memPage);
- };
- /**************************************************************************************************
- * Function Name: appendEmptyBlock
- * Description:
- * Writes empty page to the end of page file.
- *
- * Parameters:
- * SM_FileHandle *fHandle: file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization..
- * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
- * 2016-09-25 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC appendEmptyBlock (SM_FileHandle *fHandle){
- // the file is opened as 'r+'
- // we need to close and reopen as 'a'
- fclose(fHandle->mgmtInfo);
- FILE *file = fopen(fHandle->fileName, "a"); //a option appends
- /*
- "r" Opens a file for reading. The file must exist.
- "w" Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file.
- "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
- "r+" Opens a file to update both reading and writing. The file must exist.
- "w+" Creates an empty file for both reading and writing.
- "a+" Opens a file for reading and appending.
- */
- if(file){
- char eof = '\0';
- int i;
- // we append a new page full of '\0' character
- for(i = 0; i < PAGE_SIZE; i++){
- errno = 0;
- fwrite(&eof, sizeof(eof), 1, file);
- if(errno != 0) return RC_WRITE_FAILED;
- }
- // close file
- errno = 0;
- fclose(file);
- if(errno != 0) return RC_WRITE_FAILED;
- // we reopen again as 'r+'
- openPageFile(fHandle->fileName, fHandle);
- // we need to update metadata information, totalNumPages has been increased
- updateCurrentInformation(fHandle->totalNumPages+1, fHandle->curPagePos, fHandle);
- return RC_OK;
- }
- else
- {
- return RC_FILE_NOT_FOUND;
- }
- };
- /**************************************************************************************************
- * Function Name: ensureCapacity
- * Description:
- * Writes empty pages while page file has not a given number of them.
- *
- * Parameters:
- * int numberOfPages : number of pages that page file should have
- * SM_FileHandle *fHandle: file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main Logic,
- * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC ensureCapacity (int numberOfPages, SM_FileHandle *fHandle){
- // how many pages we need to append
- int numberToEnsure = numberOfPages - fHandle->totalNumPages;
- // if the number is negative or 0, we dont need to append any page
- if(numberToEnsure > 0){
- int i;
- for(i = 0; i < numberToEnsure; i++){
- RC res = appendEmptyBlock(fHandle);
- if(res != RC_OK) return res;
- }
- }
- return RC_OK;
- };
- /**************************************************************************************************
- * Function Name: updateCurrentInformation
- * Description:
- * Auxiliar functions that updates the file's metadata
- *
- * Parameters:
- * int totalNumPages : number of pages of current document
- * int curPagePos : current page position
- * SM_FILEHANDLE fHandle: file handle
- *
- * Return:
- * RC: returned code
- *
- * Author:
- * Jose Carmona <jcarmonalopez@hawk.iit.edu>
- *
- * History:
- * Date Name Content
- * ---------- ------------------------------------------------ ------------------------------
- * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
- * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
- * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
- * add comments.
- **************************************************************************************************/
- RC updateCurrentInformation(int totalNumPages, int curPagePos, SM_FileHandle *fHandle){
- // fHandle is updated
- fHandle->totalNumPages = totalNumPages;
- fHandle->curPagePos = curPagePos;
- // a SM_FileHeader struct is created to be written to the file
- struct SM_FileHeader fileInfo;
- fileInfo.totalNumPages = fHandle->totalNumPages;
- fileInfo.curPagePos = fHandle->curPagePos;
- // we want to write at the start of the file
- errno = 0;
- //int fseek(FILE *stream, long int offset, int whence)
- //stream − This is the pointer to a FILE object that identifies the stream.
- //offset − This is the number of bytes to offset from whence.
- //whence − This is the position from where offset is added. It is specified by one of the following constants −
- fseek(fHandle->mgmtInfo, 0, SEEK_SET); //SEEK_SET – It moves file pointer position to the beginning of the file.
- if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
- errno = 0;
- fwrite(&fileInfo, sizeof(fileInfo), 1, fHandle->mgmtInfo);
- if(errno != 0) return RC_WRITE_FAILED;
- return RC_OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement