int HDF_CreateFile(char* fileName) { FILE* fp; if((fopen(fileName, "rb"))!= NULL) { BF_errno = BFE_FILEEXISTS ; return BF_errno; } fp = fopen(fileName, "wb+"); if( fp == NULL) { BF_errno = BFE_CANNOTCREATEFILE; return BF_errno; } else { if(HDF_CloseFile(fp) !=BFE_OK) return BF_errno; return BFE_OK; } }
/* error code, else return BFE_OK. */ int FMM_Close(int fileDesc) { int RetVal; if ( FMM_IsValid(fileDesc) == FALSE ){ /* check validy of fileDesc */ BF_Errno=BFE_FD; return BFE_FD; } file_headers[file_openings[fileDesc].header].openings--; /* decrease counter of file openings reffering to current header */ if ( file_headers[file_openings[fileDesc].header].openings == 0 /* if no file opening reffers to current header and header has */ && file_headers[file_openings[fileDesc].header].dirty == 1 ) /* been changed, write it back to file. */ if ( (RetVal=HDF_WriteBlock(0, file_headers[file_openings[fileDesc].header].header, file_openings[fileDesc].fp)) != BFE_OK ) return RetVal; file_openings[fileDesc].empty=1; /* set current position as empty */ if ( (RetVal=HDF_CloseFile(file_openings[fileDesc].fp)) != BFE_OK ) /* closing, using file pointer */ return RetVal; return BFE_OK; }
//an yparxei to arxeio epistrefei true, alliws false BOOLEAN HDF_ExistsFile(char* fileName) { FILE* fp; fp = fopen(fileName, "rb"); if(fp != NULL) { if(HDF_CloseFile(fp)!= BFE_OK) return FALSE; return TRUE; } else if( fp == NULL) return FALSE; }
/* Create a file with name: fileName * using services provided from lower * Levels. Check all errors that may * occur and return respective error * code, else return BFE_OK. */ int BF_CreateFile(char *fileName) { int RetVal; char header[BF_BLOCK_SIZE]; FILE* fp; if ( HDF_ExistsFile(fileName) == TRUE ){ /* check if file exists */ BF_Errno=BFE_FILEEXISTS; return BFE_FILEEXISTS; } if ( (RetVal=HDF_CreateFile(fileName)) != BFE_OK ) /* if not, create it */ return RetVal; bzero(header,BF_BLOCK_SIZE); if ( (RetVal=HDF_OpenFile(fileName,&fp)) != BFE_OK ) /* open it */ return RetVal; if ( (RetVal=HDF_WriteBlock(0,header,fp)) != BFE_OK ) /* write empty header */ return RetVal; if ( (RetVal=HDF_CloseFile(fp)) != BFE_OK ) /* close it */ return RetVal; return BFE_OK; }
int main(void) { int rvalue; FILE *fp1, *fp2; char *buffer, *bufStart; int fd11, fd12, fd13; int fd21, fd22, fd23; int i,j, randNum, randFd; fileInfo_t finfo; // ---------------------- Dimiourgia twn fileName1 kai fileName2 ---------------------- // ---------------------- Dimiourgia tou fileName1 ---------------------- printf("\n--------------\n"); printf("CREATING FILES\n"); printf("--------------\n\n"); // Elegxos an yparxei to fileName1 printf("Checking if file %s exists...\n\n", fileName1); if (HDF_ExistsFile(fileName1) == TRUE) { printf("Checkpoint 1:\n"); printf("HDF_ExistsFile returned TRUE!\n\n"); } // Dimiourgia tou arxeiou fileName1 printf("Creating file %s...\n\n", fileName1); if ( (rvalue = HDF_CreateFile(fileName1)) != BFE_OK) { printf("Checkpoint 2:\n"); printf("HDF_CreateFile() returned %d!\n\n", rvalue); } // Anoigma tou arxeiou fileName1 printf("Opening file %s...\n\n", fileName1); if ((rvalue = HDF_OpenFile(fileName1, &fp1)) != BFE_OK) { printf("Checkpoint 3:\n"); printf("HDF_OpenFile() returned %d, should have returned 0!\n\n",rvalue); } // Dimiourgia epikefalidas tou arxeiou sti mnimi buffer = malloc(sizeof(char) * BF_BLOCK_SIZE); bufStart = buffer; memset(buffer, 0, BF_BLOCK_SIZE); // Antigrafi epikefalidas sto block 0 tou arxeiou printf("Writing file header to disk...\n\n"); if ((rvalue = HDF_WriteBlock(0, buffer, fp1)) != BFE_OK) { printf("Checkpoint 4:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Kleisimo tou arxeiou fileName1 printf("Closing file %s\n\n", fileName1); if ((rvalue = HDF_CloseFile(fp1)) != BFE_OK) { printf("Checkpoint 5:\n"); printf("HDF_CloseFile() returned %d!\n\n", rvalue); } // ---------------------- Dimiourgia tou fileName2 ---------------------- // Elegxos an yparxei to fileName2 printf("Checking if file %s exists...\n\n", fileName2); if (HDF_ExistsFile(fileName2) == TRUE) { printf("Checkpoint 6:\n"); printf("HDF_ExistsFile returned TRUE!\n\n"); } // Dimiourgia tou arxeiou fileName2 printf("Creating file %s...\n\n", fileName2); if ( (rvalue=HDF_CreateFile(fileName2)) != BFE_OK) { printf("Checkpoint 7:\n"); printf("HDF_CreateFile() returned %d!\n\n", rvalue); } // Anoigma tou arxeiou fileName2 printf("Opening file %s...\n\n", fileName2); if ((rvalue = HDF_OpenFile(fileName2, &fp2)) != BFE_OK) { printf("Checkpoint 8:\n"); printf("HDF_OpenFile() returned %d, should have returned 0!\n\n",rvalue); } // Antigrafi epikefalidas sto block 0 tou arxeiou printf("Writing file header to disk...\n\n"); if ((rvalue = HDF_WriteBlock(0, buffer, fp2)) != BFE_OK) { printf("Checkpoint 9:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Kleisimo tou arxeiou fileName2 printf("Closing file %s\n\n", fileName2); if ((rvalue = HDF_CloseFile(fp2)) != BFE_OK) { printf("Checkpoint 10:\n"); printf("HDF_CloseFile() returned %d!\n\n", rvalue); } // ---------------------- FMM epipedo ---------------------- printf("------------------------------------\n"); printf("OPENING AND PROCESSING FILES\n"); printf("------------------------------------\n\n"); FMM_Init(); BMM_Init(); // 1o anoigma tou fileName1 printf("Opening file %s (1)...\n\n", fileName1); if ((fd11 = FMM_Open(fileName1)) < 0) { printf("Checkpoint 11:\n"); printf("FMM_Open() returned %d!\n\n", fd11); } // 2o anoigma tou fileName1 printf("Opening file %s (2)...\n\n", fileName1); if ((fd12 = FMM_Open(fileName1)) < 0) { printf("Checkpoint 12:\n"); printf("FMM_Open() returned %d!\n\n", fd12); } // 3o anoigma tou fileName1 printf("Opening file %s (3)...\n\n", fileName1); if ((fd13 = FMM_Open(fileName1)) < 0) { printf("Checkpoint 13:\n"); printf("FMM_Open() returned %d!\n\n", fd12); } // 1o anoigma tou fileName2 printf("Opening file %s (1)...\n\n", fileName2); if ((fd21 = FMM_Open(fileName2)) < 0) { printf("Checkpoint 14:\n"); printf("FMM_Open() returned %d!\n\n", fd21); } // 2o anoigma tou fileName2 printf("Opening file %s (2)...\n\n", fileName2); if ((fd22 = FMM_Open(fileName2)) < 0) { printf("Checkpoint 15:\n"); printf("FMM_Open() returned %d!\n\n", fd21); } // 3o anoigma tou fileName2 printf("Opening file %s (3)...\n\n", fileName2); if ((fd23 = FMM_Open(fileName2)) < 0) { printf("Checkpoint 16:\n"); printf("FMM_Open() returned %d!\n\n", fd21); } // Ftiakse BLOCKS blocks gia kathe ena apo ta files for (i=0;i<BLOCKS;i++){ randNum = rand()%3; if (randNum==1) randFd = fd13; else if (randNum==2) randFd = fd11; else randFd = fd12; finfo.fd = randFd; if ((rvalue = FMM_GetFileInfoByFD(&finfo)) < 0) { printf("Checkpoint 17:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } if ((rvalue = BMM_AllocBlock(finfo,i,&buffer)) < 0) { printf("Checkpoint 18:\n"); printf("BMM_AllocBlock() returned %d!\n\n", rvalue); } if ((rvalue = FMM_HEAD_Set(fd11, i, TRUE)) < 0) { printf("Checkpoint 19:\n"); printf("FMM_HEAD_Set() returned %d!\n\n", rvalue); } // Tropopoiisi tou block pou "efere" i BMM_AllocBlock j=(300-i)*(300-i); memcpy(buffer,&j,sizeof(int)); if ((rvalue = BMM_UnpinBlock(finfo,i,TRUE)) < 0) { printf("Checkpoint 20:\n"); printf("BMM_UnpinBlock() returned %d!\n\n", rvalue); } randNum = rand()%3; if (randNum==1) randFd = fd22; else if (randNum==2) randFd = fd21; else randFd = fd23; finfo.fd = randFd; if ((rvalue = FMM_GetFileInfoByFD(&finfo)) < 0) { printf("Checkpoint 21:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } if ((rvalue = BMM_AllocBlock(finfo,i,&buffer)) < 0) { printf("Checkpoint 22:\n"); printf("BMM_AllocBlock() returned %d!\n\n", rvalue); } if ((rvalue = FMM_HEAD_Set(fd21, i, TRUE)) < 0) { printf("Checkpoint 23:\n"); printf("FMM_HEAD_Set() returned %d!\n\n", rvalue); } // Tropopoiisi tou block pou "efere" i BMM_AllocBlock memcpy(buffer,&i,sizeof(int)); if ((rvalue = BMM_UnpinBlock(finfo,i,TRUE)) < 0) { printf("Checkpoint 24:\n"); printf("BMM_UnpinBlock() returned %d!\n\n", rvalue); } } finfo.fd = fd11; if ((rvalue = FMM_GetFileInfoByFD(&finfo)) < 0) { printf("Checkpoint 25:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } if ((rvalue = BMM_Flush(finfo)) < 0) { printf("Checkpoint 26:\n"); printf("BMM_Flush() returned %d!\n\n", rvalue); } finfo.fd = fd21; if ((rvalue = FMM_GetFileInfoByFD(&finfo)) < 0) { printf("Checkpoint 27:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } if ((rvalue = BMM_Flush(finfo)) < 0) { printf("Checkpoint 28:\n"); printf("BMM_Flush() returned %d!\n\n", rvalue); } printf("-------------\n"); printf("CLOSING FILES\n"); printf("-------------\n\n"); // Kleisimo 1ou anoigmatos tou fileName1 printf("Closing file %s (1)...\n\n", fileName1); if ((rvalue = FMM_Close(fd11)) != BFE_OK) { printf("Checkpoint 29:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } // Kleisimo 2ou anoigmatos tou fileName1 printf("Closing file %s (2)...\n\n", fileName1); if ((rvalue = FMM_Close(fd12)) != BFE_OK) { printf("Checkpoint 31:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } // Kleisimo 3ou anoigmatos tou fileName1 printf("Closing file %s (3)...\n\n", fileName1); if ((rvalue = FMM_Close(fd13)) != BFE_OK) { printf("Checkpoint 31:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } // Kleisimo 1ou anoigmatos tou fileName2 printf("Closing file %s (1)...\n\n", fileName2); if ((rvalue = FMM_Close(fd21)) != BFE_OK) { printf("Checkpoint 32:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } // Kleisimo 2ou anoigmatos tou fileName2 printf("Closing file %s (2)...\n\n", fileName2); if ((rvalue = FMM_Close(fd22)) != BFE_OK) { printf("Checkpoint 33:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } // Kleisimo 3ou anoigmatos tou fileName2 printf("Closing file %s (3)...\n\n", fileName2); if ((rvalue = FMM_Close(fd23)) != BFE_OK) { printf("Checkpoint 34:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } free(bufStart); return 0; }
int main(void) { int rvalue; FILE *fp; char *buffer; int fd; fileInfo_t finfo; char athlete[ATHLETE_SIZE]; int offset; int blockNumInFile1; int blockNumInHeader1; int blockNumInFile2; int blockNumInHeader2; int firstValid; int secondValid; // ---------------------- HDF epipedo ---------------------- // Elegxos an yparxei to fileName printf("\nChecking if file %s exists...\n\n", fileName); if (HDF_ExistsFile(fileName) == TRUE) { printf("Checkpoint 1:\n"); printf("HDF_ExistsFile returned TRUE!\n\n"); } // Dimiourgia tou arxeiou fileName printf("Creating file %s...\n\n", fileName); if ( (rvalue=HDF_CreateFile(fileName)) != BFE_OK) { printf("Checkpoint 2:\n"); printf("HDF_CreateFile() returned %d!\n\n", rvalue); } // Anoigma tou arxeiou fileName printf("Opening file %s...\n\n", fileName); if ((rvalue = HDF_OpenFile(fileName,&fp)) != BFE_OK) { printf("Checkpoint 3:\n"); printf("HDF_OpenFile() returned %d, should have returned 0!\n\n",rvalue); } // Dimiourgia epikefalidas tou arxeiou sti mnimi buffer = malloc(sizeof(char) * BF_BLOCK_SIZE); memset(buffer, 0, BF_BLOCK_SIZE); // Antigrafi epikefalidas sto block 0 tou arxeiou printf("Writing file header to disk...\n\n"); if ((rvalue = HDF_WriteBlock(0, buffer, fp)) != BFE_OK) { printf("Checkpoint 4:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Kleisimo tou arxeiou fileName printf("Closing file %s\n\n", fileName); if ((rvalue = HDF_CloseFile(fp)) != BFE_OK) { printf("Checkpoint 5:\n"); printf("HDF_CloseFile() returned %d!\n\n", rvalue); } // ---------------------- FMM epipedo ---------------------- FMM_Init(); // Anoigma tou arxeiou printf("Opening file %s...\n\n", fileName); if ((fd = FMM_Open(fileName)) < 0) { printf("Checkpoint 6:\n"); printf("FMM_Open() returned %d!\n\n", fd); } // Elegxos an i thesi fd tou pinaka anoixtwn arxeiwn einai pleon egkyri kataxwrisi arxeiou printf("Checking for validity of position %d of open files array...\n\n", fd); if (FMM_IsValid(fd) != TRUE) { printf("Checkpoint 7:\n"); printf("FMM_IsValid() returned FALSE!\n\n"); } // Anaktisi pliroforias gia to arxeio me vasi to onoma strcpy(finfo.filename, fileName); printf("Retrieving file information by name...\n\n"); if ((rvalue = FMM_GetFileInfoByName(&finfo)) != BFE_OK) { printf("Checkpoint 8:\n"); printf("FMM_GetFileInfoByName() returned %d!\n\n", rvalue); } printFileInfo(&finfo); // Anaktisi pliroforias gia to arxeio me vasi to fd finfo.fd = fd; printf("Retrieving file information by file descriptor...\n\n"); if ((rvalue = FMM_GetFileInfoByFD(&finfo)) != BFE_OK) { printf("Checkpoint 9:\n"); printf("FMM_GetFileInfoByFD() returned %d!\n\n", rvalue); } printFileInfo(&finfo); // Dimiourgia enos neou block sti mnimi memset(buffer, 0, BF_BLOCK_SIZE); offset = 0; strcpy(athlete, "Periklis Iakovakis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Pyrros Dimas"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Helena Isinbayeva"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Blanka Vlasic"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); // Grapsimo aytou tou block sto arxeio sti thesi blockNumInFile fp = finfo.fp; blockNumInFile1 = 5; printf("Writing block to disk...\n\n"); if ((rvalue = HDF_WriteBlock(blockNumInFile1, buffer, fp)) != BFE_OK) { printf("Checkpoint 10:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Enimerwsi tis epikefalidas tou arxeiou oti to block ayto einai pleon egkyro blockNumInHeader1 = blockNumInFile1 - 1; // PROSOXH SE AYTO!!! printf("Updating header...\n\n"); if ((rvalue = FMM_HEAD_Set(fd, blockNumInHeader1, TRUE)) != BFE_OK) { printf("Checkpoint 11:"); printf("FMM_HEAD_Set() returned %d!\n\n",rvalue); } // Elegxos an enimerwthike i epikefalida printf("Checking header...\n\n"); if (FMM_HEAD_IsValid(fd, blockNumInHeader1) == FALSE) { printf("Checkpoint 12:"); printf("FMM_HEAD_isValid() returned %d!\n\n",rvalue); } // Anaktisi tou prwtou egkyrou block tou arxeiou printf("Retrieving first valid block...\n\n"); if ((firstValid = FMM_HEAD_GetFirstBlock(fd)) == BFE_EOF) { printf("Checkpoint 13:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("firstValid = %d, it should be equal to %d\n\n", firstValid, blockNumInHeader1); // Dimiourgia enos 2ou neou block sti mnimi memset(buffer, 0, BF_BLOCK_SIZE); offset = 0; strcpy(athlete, "Vasilis Spanoulis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); offset += ATHLETE_SIZE; strcpy(athlete, "Sofoklis Sxortsianitis"); memcpy(buffer + offset, athlete, ATHLETE_SIZE); // Grapsimo aytou tou block sto arxeio sti thesi blockNumInFile blockNumInFile2 = 3; printf("Writing block to disk...\n\n"); if ((rvalue = HDF_WriteBlock(blockNumInFile2, buffer, fp)) != BFE_OK) { printf("Checkpoint 14:"); printf("HDF_WriteBlock() returned %d!\n\n",rvalue); } // Enimerwsi tis epikefalidas tou arxeiou oti to block ayto einai pleon egkyro blockNumInHeader2 = blockNumInFile2 - 1; // PROSOXH SE AYTO!!! printf("Updating header...\n\n"); if ((rvalue = FMM_HEAD_Set(fd, blockNumInHeader2, TRUE)) != BFE_OK) { printf("Checkpoint 15:"); printf("FMM_HEAD_Set() returned %d!\n\n",rvalue); } // Anaktisi tou prwtou egkyrou block tou arxeiou printf("Retrieving first valid block...\n\n"); if ((firstValid = FMM_HEAD_GetFirstBlock(fd)) == BFE_EOF) { printf("Checkpoint 16:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("firstValid = %d, it should be equal to %d\n\n", firstValid, blockNumInHeader2); // Anaktisi tou prwtou egkyrou block tou arxeiou meta apo to firstValid printf("Retrieving first valid block after block %d...\n\n", firstValid); if ((secondValid = FMM_HEAD_GetNextBlock(fd, firstValid)) == BFE_EOF) { printf("Checkpoint 17:"); printf("FMM_HEAD_GetFirstBlock returned %d!\n\n",rvalue); } printf("secondValid = %d, it should be equal to %d\n\n", secondValid, blockNumInHeader1); // Kleisimo arxeiou printf("Closing file %s...\n\n", fileName); if ((rvalue = FMM_Close(fd)) != BFE_OK) { printf("Checkpoint 19:\n"); printf("FMM_Close() returned %d!\n\n", rvalue); } free(buffer); return 0; }