Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 28.05 KB | None | 0 0
  1. #include "dberror.h"
  2. #include "storage_mgr.h"
  3.  
  4. #include <stdio.h>
  5. #include <errno.h> //standard error nums for built in library functions, such as fwrite or fread.
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. extern int errno;
  10.  
  11. RC updateCurrentInformation(int totalNumPages, int curPagePos, SM_FileHandle *fHandle);
  12.  
  13. typedef struct SM_FileHeader {
  14. int totalNumPages;
  15. int curPagePos;
  16. } SM_FileHeader;
  17.  
  18. void initStorageManager(){
  19.  
  20. };
  21.  
  22. /**************************************************************************************************
  23. * Function Name: createPageFile
  24. * Description:
  25. * Create a page file and write empty content. ('/0')
  26. *
  27. * Parameters:
  28. * char *fileName: file name
  29. *
  30. * Return:
  31. * RC: returned code
  32. *
  33. * Author:
  34. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  35. *
  36. * History:
  37. * Date Name Content
  38. * ---------- ------------------------------------------------ ------------------------------
  39. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  40. * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
  41. * 2016-09-24 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
  42. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  43. * add comments.
  44. * 2016-09-27 Jose Carmona <jcarmonalopez@hawk.iit.edu> Fixed error handle.
  45. **************************************************************************************************/
  46. RC createPageFile (char *fileName){
  47. FILE *file = fopen(fileName, "w"); //creates pointer called file
  48. if(file){ //if the file xists, then
  49. char eof = '\0'; //eof is marked by this character, from specifications
  50.  
  51. struct SM_FileHeader fileInfo; //create a FileHeader called fileinfo
  52. fileInfo.totalNumPages = 1; //
  53. fileInfo.curPagePos = 0;
  54.  
  55. // SM_FileHeader struct will be stored at the start of the document
  56. // It will only contains totalNumPages and curPagePos
  57.  
  58. errno = 0; //no error, so err num 0
  59. //writes data from the array pointed to, by ptr to the given stream.
  60. //size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
  61. //ptr − This is the pointer to the array of elements to be written.
  62. //size − This is the size in bytes of each element to be written.
  63. //nmemb − This is the number of elements, each one with a size of size bytes.
  64. //stream − This is the pointer to a FILE object that specifies an output stream.
  65.  
  66. fwrite(&fileInfo, sizeof(SM_FileHeader), 1, file); //use fwrite,
  67.  
  68. if(errno != 0){
  69. fclose(file);
  70. return RC_WRITE_FAILED;
  71. }
  72. int i;
  73. // We writes the first page full of '\0' (null value in C is \0)
  74. for (i = 0; i < PAGE_SIZE; ++i){
  75. errno = 0;
  76. fwrite(&eof, sizeof(eof), 1, file);
  77. if(errno != 0){
  78. fclose(file);
  79. return RC_WRITE_FAILED;
  80. }
  81. }
  82.  
  83. // to close the file after we write to it successfully
  84. errno = 0;
  85. fclose(file);
  86. if(errno == 0)
  87. return RC_OK;
  88. else return RC_WRITE_FAILED;
  89.  
  90. }
  91.  
  92. else{//the file doesnt exist, so file handle is not initialized
  93. return RC_FILE_HANDLE_NOT_INIT;
  94. }
  95. };
  96.  
  97.  
  98. /**************************************************************************************************
  99. * Function Name: openPageFile
  100. * Description:
  101. * Opens the page file.
  102. *
  103. * Parameters:
  104. * char *fileName : file name
  105. * SM_FileHandle *fHandle: output file handle
  106. *
  107. * Return:
  108. * RC: returned code
  109. *
  110. * Author:
  111. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  112. *
  113. * History:
  114. * Date Name Content
  115. * ---------- ------------------------------------------------ ------------------------------
  116. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  117. * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main Logic.
  118. * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
  119. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  120. * add comments.
  121. **************************************************************************************************/
  122. RC openPageFile (char *fileName, SM_FileHandle *fHandle){
  123.  
  124. // file is opened
  125. //FILE *fopen(const char *filename, const char *mode)
  126. /*"r" Opens a file for reading. The file must exist.
  127. "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.
  128. "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
  129. "r+" Opens a file to update both reading and writing. The file must exist.
  130. "w+" Creates an empty file for both reading and writing.
  131. "a+" Opens a file for reading and appending.
  132. */
  133. FILE *file = fopen(fileName, "r+"); //fopen is another built in function
  134. struct SM_FileHeader fileInfo;
  135.  
  136. if (file){
  137. // we read first the metadata at the start of the file
  138. errno = 0;
  139. fread(&fileInfo, sizeof(SM_FileHeader), 1, file);
  140. if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
  141.  
  142. // now we initialize the SM_FileHandle struct
  143. fHandle->fileName = fileName;
  144. fHandle->totalNumPages = fileInfo.totalNumPages;
  145. fHandle->curPagePos = fileInfo.curPagePos;
  146. 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)
  147. // we are using mgmtInfo to store the file handle needed
  148. // to write, read, close and other I/O operations
  149. return RC_OK;
  150. }else{
  151. return RC_FILE_NOT_FOUND;
  152. }
  153. };
  154.  
  155. /**************************************************************************************************
  156. * Function Name: closePageFile
  157. * Description:
  158. * Closes the page file.
  159. *
  160. * Parameters:
  161. * SM_FileHandle *fHandle: file handle
  162. *
  163. * Return:
  164. * RC: returned code
  165. *
  166. * Author:
  167. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  168. *
  169. * History:
  170. * Date Name Content
  171. * ---------- ------------------------------------------------ ------------------------------
  172. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  173. * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main Logic.
  174. * 2016-09-25 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
  175. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Added header comment,
  176. * add comments.
  177. **************************************************************************************************/
  178. RC closePageFile (SM_FileHandle *fHandle){
  179. if(fHandle){
  180. //fclose (FILE *stream)
  181. fclose(fHandle->mgmtInfo);//when we used openPageFile, we set fhandle.mgmtinfo= file, which is the pointer to the file. we are closing that now
  182. return RC_OK;
  183. }else{
  184. return RC_FILE_HANDLE_NOT_INIT;
  185. }
  186. };
  187.  
  188. /**************************************************************************************************
  189. * Function Name: destroyPageFile
  190. * Description:
  191. * Removes the file.
  192. *
  193. * Parameters:
  194. * char *fileName: file name
  195. *
  196. * Return:
  197. * RC: returned code
  198. *
  199. * Author:
  200. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  201. *
  202. * History:
  203. * Date Name Content
  204. * ---------- ------------------------------------------------ ------------------------------
  205. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  206. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
  207. * 2016-09-22 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
  208. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  209. * add comments.
  210. **************************************************************************************************/
  211. RC destroyPageFile (char *fileName){
  212. errno = 0;
  213. remove(fileName);//built in function to remove file
  214. if(errno != 0) return RC_FILE_NOT_FOUND;
  215. return RC_OK;
  216. };
  217.  
  218. /**************************************************************************************************
  219. * Function Name: readBlock
  220. * Description:
  221. * Reads specified page number.
  222. *
  223. * Parameters:
  224. * int pageNum : page number to be read
  225. * SM_FileHandle *fHandle: file handle
  226. * SM_PageHandle memPage : where the block will be stored after read
  227. *
  228. * Return:
  229. * RC: returned code
  230. *
  231. * Author:
  232. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  233. *
  234. * History:
  235. * Date Name Content
  236. * ---------- ------------------------------------------------ ------------------------------
  237. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  238. * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic
  239. * 2016-09-25 Victor Portals <vportalslorenzo@hawk.iit.edu> Error Handle.
  240. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  241. * add comments.
  242. **************************************************************************************************/
  243. RC readBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
  244. // Checks if pageNum is inside the bounds
  245. if(pageNum < 0 || pageNum >= fHandle->totalNumPages)
  246. return RC_READ_NON_EXISTING_PAGE;
  247.  
  248. // we use fseek to move the read pointer of our file
  249. // where we want to start reading
  250. errno = 0;
  251. fseek(fHandle->mgmtInfo, sizeof(SM_FileHeader)+pageNum*PAGE_SIZE, SEEK_SET); //TRYING TO POINT TO THE START OF OUR ACTUAL DATA, not the header
  252. if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
  253. errno = 0;
  254. /*
  255. 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.
  256. size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  257. ptr − This is the pointer to a block of memory with a minimum size of size*nmemb bytes.
  258. size − This is the size in bytes of each element to be read.
  259. nmemb − This is the number of elements, each one with a size of size bytes.
  260. stream − This is the pointer to a FILE object that specifies an input stream.
  261. */
  262. //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
  263. fread(memPage, PAGE_SIZE, 1, fHandle->mgmtInfo);//
  264. if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
  265.  
  266. // this function will update metadata in fHandle struct and
  267. // it will write the metadata to the file
  268. updateCurrentInformation(fHandle->totalNumPages, pageNum, fHandle);
  269. return RC_OK;
  270. };
  271.  
  272. /**************************************************************************************************
  273. * Function Name: getBlockPos
  274. * Description:
  275. * Gets current page position.
  276. *
  277. * Parameters:
  278. * SM_FileHandle *fHandle: file handle
  279. *
  280. * Return:
  281. * RC: returned code
  282. *
  283. * Author:
  284. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  285. *
  286. * History:
  287. * Date Name Content
  288. * ---------- ------------------------------------------------ ------------------------------
  289. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  290. * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
  291. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
  292. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  293. * add comments.
  294. **************************************************************************************************/
  295. int getBlockPos (SM_FileHandle *fHandle){
  296. if(fHandle)
  297. return fHandle->curPagePos;
  298. else return RC_FILE_HANDLE_NOT_INIT;
  299. };
  300.  
  301. /**************************************************************************************************
  302. * Function Name: readFirstBlock
  303. * Description:
  304. * Reads first page.
  305. *
  306. * Parameters:
  307. * SM_FileHandle *fHandle: file handle
  308. * SM_PageHandle memPage : where the block will be stored after read
  309. *
  310. * Return:
  311. * RC: returned code
  312. *
  313. * Author:
  314. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  315. *
  316. * History:
  317. * Date Name Content
  318. * ---------- ------------------------------------------------ ------------------------------
  319. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  320. * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
  321. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  322. * add comments.
  323. **************************************************************************************************/
  324. RC readFirstBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  325. // first block has pageNum = 0
  326. return readBlock(0, fHandle, memPage);//just call our read function and read the first page
  327. };
  328.  
  329. /**************************************************************************************************
  330. * Function Name: readLastBlock
  331. * Description:
  332. * Reads last page in memory.
  333. *
  334. * Parameters:
  335. * SM_FileHandle *fHandle: file handle
  336. * SM_PageHandle memPage : where the block will be stored after read
  337. *
  338. * Return:
  339. * RC: returned code
  340. *
  341. * Author:
  342. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  343. *
  344. * History:
  345. * Date Name Content
  346. * ---------- ------------------------------------------------ ------------------------------
  347. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  348. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
  349. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  350. * add comments.
  351. **************************************************************************************************/
  352. RC readLastBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  353. // Last block has pageNum = totalNumPages
  354. // readBlock function will check if pageNum is inside the bounds
  355. return readBlock(fHandle->totalNumPages-1, fHandle, memPage); //read the last block
  356. };
  357.  
  358. /**************************************************************************************************
  359. * Function Name: readPreviousBlock
  360. * Description:
  361. * Reads previous page to current.
  362. *
  363. * Parameters:
  364. * SM_FileHandle *fHandle: file handle
  365. * SM_PageHandle memPage : where the block will be stored after read
  366. *
  367. * Return:
  368. * RC: returned code
  369. *
  370. * Author:
  371. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  372. *
  373. * History:
  374. * Date Name Content
  375. * ---------- ------------------------------------------------ ------------------------------
  376. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  377. * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic.
  378. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  379. * add comments.
  380. **************************************************************************************************/
  381. RC readPreviousBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  382. // Previous block has pageNum = curPagePos-1
  383. // readBlock function will check if pageNum is inside the bounds
  384. return readBlock(fHandle->curPagePos-1, fHandle, memPage);
  385. };
  386.  
  387. /**************************************************************************************************
  388. * Function Name: readCurrentBlock
  389. * Description:
  390. * Reads current page in memory.
  391. *
  392. * Parameters:
  393. * SM_FileHandle *fHandle: file handle
  394. * SM_PageHandle memPage : where the block will be stored after read
  395. *
  396. * Return:
  397. * RC: returned code
  398. *
  399. * Author:
  400. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  401. *
  402. * History:
  403. * Date Name Content
  404. * ---------- ------------------------------------------------ ------------------------------
  405. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  406. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
  407. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  408. * add comments.
  409. **************************************************************************************************/
  410. RC readCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  411. // Current block has pageNum = curPagePos
  412. // readBlock function will check if pageNum is inside the bounds
  413. return readBlock(fHandle->curPagePos, fHandle, memPage);
  414. };
  415.  
  416. /**************************************************************************************************
  417. * Function Name: readNextBlock
  418. * Description:
  419. * Reads next page to current.
  420. *
  421. * Parameters:
  422. * SM_FileHandle *fHandle: file handle
  423. * SM_PageHandle memPage : where the block will be stored after read
  424. *
  425. * Return:
  426. * RC: returned code
  427. *
  428. * Author:
  429. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  430. *
  431. * History:
  432. * Date Name Content
  433. * ---------- ------------------------------------------------ ------------------------------
  434. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  435. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
  436. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  437. * add comments.
  438. **************************************************************************************************/
  439. RC readNextBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  440. // Next block has pageNum = curPagePos+1
  441. // readBlock function will check if pageNum is inside the bounds
  442. return readBlock(fHandle->curPagePos+1, fHandle, memPage);
  443. };
  444.  
  445. /**************************************************************************************************
  446. * Function Name: writeBlock
  447. * Description:
  448. * Writes page read from memory to page file.
  449. *
  450. * Parameters:
  451. * int pageNum : number of page to be writed
  452. * SM_FileHandle *fHandle: file handle
  453. * SM_PageHandle memPage : where the block will be stored after read
  454. *
  455. * Return:
  456. * RC: returned code
  457. *
  458. * Author:
  459. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  460. *
  461. * History:
  462. * Date Name Content
  463. * ---------- ------------------------------------------------ ------------------------------
  464. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  465. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main logic.
  466. * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
  467. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  468. * add comments.
  469. * 2016-09-27 Jose Carmona <jcarmonalopez@hawk.iit.edu> Fixed error handle.
  470. **************************************************************************************************/
  471. RC writeBlock (int pageNum, SM_FileHandle *fHandle, SM_PageHandle memPage){
  472. // Checks if pageNum is inside the bounds
  473. if(pageNum < 0 || pageNum >= fHandle->totalNumPages)
  474. return RC_READ_NON_EXISTING_PAGE;
  475.  
  476. // we use fseek to move the read pointer of our file
  477. // where we want to start writing
  478. errno = 0;
  479. fseek(fHandle->mgmtInfo, sizeof(SM_FileHeader)+pageNum*PAGE_SIZE, SEEK_SET); //moving to the start of the data
  480. if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
  481.  
  482.  
  483. errno = 0;
  484. fwrite(memPage, PAGE_SIZE, 1, fHandle->mgmtInfo); //just use fwrite to write to that page
  485. if(errno != 0) return RC_WRITE_FAILED;
  486. return RC_OK;
  487. };
  488.  
  489. /**************************************************************************************************
  490. * Function Name: writeCurrentBlock
  491. * Description:
  492. * Writes page read from memory to current page.
  493. *
  494. * Parameters:
  495. * SM_FileHandle *fHandle: file handle
  496. * SM_PageHandle memPage : where the block will be stored after read
  497. *
  498. * Return:
  499. * RC: returned code
  500. *
  501. * Author:
  502. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  503. *
  504. * History:
  505. * Date Name Content
  506. * ---------- ------------------------------------------------ ------------------------------
  507. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  508. * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main logic
  509. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  510. * add comments.
  511. **************************************************************************************************/
  512. RC writeCurrentBlock (SM_FileHandle *fHandle, SM_PageHandle memPage){
  513. // Current block has pageNum = curPagePos
  514. // readBlock function will check if pageNum is inside the bounds
  515. return writeBlock(fHandle->curPagePos, fHandle, memPage);
  516. };
  517.  
  518. /**************************************************************************************************
  519. * Function Name: appendEmptyBlock
  520. * Description:
  521. * Writes empty page to the end of page file.
  522. *
  523. * Parameters:
  524. * SM_FileHandle *fHandle: file handle
  525. *
  526. * Return:
  527. * RC: returned code
  528. *
  529. * Author:
  530. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  531. *
  532. * History:
  533. * Date Name Content
  534. * ---------- ------------------------------------------------ ------------------------------
  535. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization..
  536. * 2016-09-21 Jose Carmona <jcarmonalopez@hawk.iit.edu> Main logic.
  537. * 2016-09-25 Victor Portals <vportalslorenzo@hawk.iit.edu> Error handle.
  538. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  539. * add comments.
  540. **************************************************************************************************/
  541. RC appendEmptyBlock (SM_FileHandle *fHandle){
  542. // the file is opened as 'r+'
  543. // we need to close and reopen as 'a'
  544. fclose(fHandle->mgmtInfo);
  545.  
  546. FILE *file = fopen(fHandle->fileName, "a"); //a option appends
  547. /*
  548. "r" Opens a file for reading. The file must exist.
  549. "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.
  550. "a" Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.
  551. "r+" Opens a file to update both reading and writing. The file must exist.
  552. "w+" Creates an empty file for both reading and writing.
  553. "a+" Opens a file for reading and appending.
  554. */
  555. if(file){
  556. char eof = '\0';
  557. int i;
  558. // we append a new page full of '\0' character
  559. for(i = 0; i < PAGE_SIZE; i++){
  560. errno = 0;
  561. fwrite(&eof, sizeof(eof), 1, file);
  562. if(errno != 0) return RC_WRITE_FAILED;
  563. }
  564.  
  565. // close file
  566. errno = 0;
  567. fclose(file);
  568. if(errno != 0) return RC_WRITE_FAILED;
  569. // we reopen again as 'r+'
  570. openPageFile(fHandle->fileName, fHandle);
  571. // we need to update metadata information, totalNumPages has been increased
  572. updateCurrentInformation(fHandle->totalNumPages+1, fHandle->curPagePos, fHandle);
  573. return RC_OK;
  574. }
  575. else
  576. {
  577. return RC_FILE_NOT_FOUND;
  578. }
  579. };
  580.  
  581. /**************************************************************************************************
  582. * Function Name: ensureCapacity
  583. * Description:
  584. * Writes empty pages while page file has not a given number of them.
  585. *
  586. * Parameters:
  587. * int numberOfPages : number of pages that page file should have
  588. * SM_FileHandle *fHandle: file handle
  589. *
  590. * Return:
  591. * RC: returned code
  592. *
  593. * Author:
  594. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  595. *
  596. * History:
  597. * Date Name Content
  598. * ---------- ------------------------------------------------ ------------------------------
  599. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  600. * 2016-09-21 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Main Logic,
  601. * 2016-09-25 Jose Carmona <jcarmonalopez@hawk.iit.edu> Error handle.
  602. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  603. * add comments.
  604. **************************************************************************************************/
  605. RC ensureCapacity (int numberOfPages, SM_FileHandle *fHandle){
  606. // how many pages we need to append
  607. int numberToEnsure = numberOfPages - fHandle->totalNumPages;
  608. // if the number is negative or 0, we dont need to append any page
  609. if(numberToEnsure > 0){
  610. int i;
  611. for(i = 0; i < numberToEnsure; i++){
  612. RC res = appendEmptyBlock(fHandle);
  613. if(res != RC_OK) return res;
  614. }
  615. }
  616. return RC_OK;
  617. };
  618.  
  619. /**************************************************************************************************
  620. * Function Name: updateCurrentInformation
  621. * Description:
  622. * Auxiliar functions that updates the file's metadata
  623. *
  624. * Parameters:
  625. * int totalNumPages : number of pages of current document
  626. * int curPagePos : current page position
  627. * SM_FILEHANDLE fHandle: file handle
  628. *
  629. * Return:
  630. * RC: returned code
  631. *
  632. * Author:
  633. * Jose Carmona <jcarmonalopez@hawk.iit.edu>
  634. *
  635. * History:
  636. * Date Name Content
  637. * ---------- ------------------------------------------------ ------------------------------
  638. * 2016-09-20 Jose Carmona <jcarmonalopez@hawk.iit.edu> Initialization.
  639. * 2016-09-21 Victor Portals <vportalslorenzo@hawk.iit.edu> Main Logic.
  640. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Error handle.
  641. * 2016-09-26 Sergio Penavades <spenavadessuarez@hawk.iit.edu> Add header comment,
  642. * add comments.
  643. **************************************************************************************************/
  644.  
  645. RC updateCurrentInformation(int totalNumPages, int curPagePos, SM_FileHandle *fHandle){
  646. // fHandle is updated
  647. fHandle->totalNumPages = totalNumPages;
  648. fHandle->curPagePos = curPagePos;
  649.  
  650. // a SM_FileHeader struct is created to be written to the file
  651. struct SM_FileHeader fileInfo;
  652. fileInfo.totalNumPages = fHandle->totalNumPages;
  653. fileInfo.curPagePos = fHandle->curPagePos;
  654.  
  655. // we want to write at the start of the file
  656. errno = 0;
  657. //int fseek(FILE *stream, long int offset, int whence)
  658. //stream − This is the pointer to a FILE object that identifies the stream.
  659. //offset − This is the number of bytes to offset from whence.
  660. //whence − This is the position from where offset is added. It is specified by one of the following constants −
  661.  
  662. fseek(fHandle->mgmtInfo, 0, SEEK_SET); //SEEK_SET – It moves file pointer position to the beginning of the file.
  663. if(errno != 0) return RC_FILE_HANDLE_NOT_INIT;
  664. errno = 0;
  665. fwrite(&fileInfo, sizeof(fileInfo), 1, fHandle->mgmtInfo);
  666. if(errno != 0) return RC_WRITE_FAILED;
  667. return RC_OK;
  668. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement