Created
October 10, 2010 19:48
-
-
Save tian2992/619509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: dataStructs.h | |
* Author: tian | |
* | |
* Created on 22 September 2010, 12:38 | |
*/ | |
#ifndef _DATASTRUCTS_H | |
#define _DATASTRUCTS_H | |
#include <iostream> | |
namespace ext2_fileSys{ | |
const int block_size = 150; | |
const int fileNameLimit = 10; | |
const int folderElementLimit = 10; | |
const int pointer_amount = 0; //WTF is wrong with them!!! | |
const int date_size = 20; | |
const int block_amount = (50000/150)+1; | |
/*! @struct dataBlock | |
* @brief Represents a fixed size datablock | |
* | |
**/ | |
struct dataBlock{ | |
char content[block_size]; | |
void clean(){ | |
for (int i=0; i<block_size;i++){ | |
this->content[i] = 0; | |
} | |
} | |
}; | |
#pragma pack(push) //Here's the magic | |
#pragma pack(1) | |
struct folderBlock { | |
char fileNames[folderElementLimit][fileNameLimit]; //!< 11 * 10 = 110 bytes | |
int directoryPointers[folderElementLimit]; //!< 4 * 10 = 40 bytes TODO: fix for 64bit architectures | |
bool filler[10]; | |
void clean(){ | |
for (int i = 0; i < fileNameLimit; i++){ //Cleaning up the filenames | |
for (int j = 0; j < folderElementLimit; j++){ | |
this->fileNames[i][j] = 0; | |
} | |
} | |
for (int k = 0; k < folderElementLimit; k++){ //Cleaning up the pointers | |
this->directoryPointers[k] = -1; | |
} | |
} | |
/*! | |
* @function set | |
* @brief sets the content of a folder Block | |
* | |
* */ | |
void set(const char fileNamesToSet[fileNameLimit][folderElementLimit], const int pointersToSet[folderElementLimit]) | |
{ | |
for (int i = 0; i < fileNameLimit; i++){ | |
for (int j = 0; j < folderElementLimit; j++){ | |
this->fileNames[i][j] = fileNamesToSet[i][j]; | |
} | |
} | |
for (int k = 0; k<folderElementLimit; k++){ | |
this->directoryPointers[k] = pointersToSet[k]; | |
} | |
} | |
}; //end of struct folderBlock | |
#pragma pack(pop) | |
struct inode_s{ | |
bool isDir; | |
int filesize_i; | |
int usedBlocks_i; //!<amount of used blocks | |
char accessDate [date_size]; | |
char creationDate [date_size]; | |
char modificationDate[date_size]; | |
int dataBlocks_p[pointer_amount]; //!<data block pointer | |
int indirectBlock_p; //!<indirect block pointer | |
void clean(){ | |
this->isDir=false; | |
this->filesize_i=-1; | |
this->usedBlocks_i=0; | |
for (int i=0; i<=date_size; i++){ | |
accessDate[i] = 0; | |
creationDate[i] = 0; | |
modificationDate[i] = 0; | |
} | |
for (int i=0; i<=pointer_amount; i++){ | |
dataBlocks_p[i] = -1; | |
} | |
indirectBlock_p = -1; | |
} | |
void to_s(){ | |
std::cout<<"Node: isDir?"; | |
} | |
}; | |
struct superBlock{ | |
int total_inodes; | |
int free_inodes; | |
int root_inode; | |
int total_blocks; | |
int free_blocks; | |
int size_of_block; | |
int rootBlock_pointer; | |
char lastModification[date_size]; | |
int amount_of_mounts; | |
char last_mount[date_size]; | |
int firstFreeInode; | |
int firstFreeBlock; | |
void clean(){ | |
total_inodes = -1; | |
free_inodes = -1; | |
root_inode = -1; | |
total_blocks = -1; | |
free_blocks = -1; | |
rootBlock_pointer = -1; | |
for (int i = 0; i < date_size;i++){ | |
lastModification[i] = -1; | |
last_mount[i] = -1; | |
} | |
amount_of_mounts = 0; | |
firstFreeBlock = 0; | |
firstFreeInode = 0; | |
} | |
}; | |
} | |
#endif /* _DATASTRUCTS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: ext2FileSystem.cpp | |
* Author: tian | |
* | |
* Created on 24 September 2010, 16:55 | |
*/ | |
#include <stdlib.h> | |
#include <fstream> | |
#include "ext2FileSystem.h" | |
#include "dataStructs.h" | |
#include <cstring> | |
#include <iostream> | |
#include <ctime> | |
using namespace ext2_fileSys; | |
using namespace std; | |
ext2FileSystem::ext2FileSystem() { | |
} | |
ext2FileSystem::ext2FileSystem(const ext2FileSystem& orig) { | |
} | |
ext2FileSystem::~ext2FileSystem() { | |
} | |
void ext2FileSystem::allocMemories(){ | |
// /* | |
superB = new superBlock; | |
Bitmap_I = new bool*[block_amount]; | |
Bitmap_B = new bool*[block_amount]; | |
InodeTable = new inode_s*[block_amount]; | |
// */ | |
/* | |
superB = (superBlock*) malloc(sizeof(superBlock)); | |
Bitmap_I = (bool**) malloc(block_amount); | |
Bitmap_B = (bool**) malloc(block_amount); | |
InodeTable = (inode_s**) malloc(sizeof(inode_s)*block_amount); | |
//DataTable = (dataBlock*) malloc(block_size*block_amount); | |
// */ | |
} | |
void ext2FileSystem::createFolderBlock(dataBlock *blockToShape, | |
int i_father, | |
int i_self){ | |
folderBlock *workingFolder = (folderBlock*)blockToShape; | |
workingFolder->clean(); | |
//setting up father and son directories | |
strcpy(workingFolder->fileNames[0],"."); | |
strcpy(workingFolder->fileNames[1],".."); | |
//now setting up the pointers | |
workingFolder->directoryPointers[0] = i_self; | |
workingFolder->directoryPointers[1] = i_father; | |
} | |
void ext2FileSystem::writeRawFile(char* data, int size, int amount,char* route){ | |
FILE *File; | |
File = fopen(route,"w+"); | |
fwrite((char*)data,size,1,File); | |
fclose(File); | |
} | |
/** | |
@brief Reads a raw file at route and places it on dumpPlace | |
*/ | |
void ext2FileSystem::readRawFile(char* dumpPlace, char* route, int size){ | |
FILE *File; | |
File = fopen(route,"r+"); | |
fread(dumpPlace,size,1,File); | |
fclose(File); | |
} | |
void ext2FileSystem::createINode( inode_s* p_inode, | |
bool b_isDir, | |
int filesize) | |
{ | |
//setting up isDir and filesize | |
p_inode->isDir=b_isDir; | |
if (b_isDir){ | |
p_inode->filesize_i = -1; | |
} | |
else { | |
p_inode->filesize_i = filesize; | |
} | |
//setting up the date | |
char dateString[20]; | |
time_t *currentTime_t = (time_t*) malloc(sizeof(time_t)); | |
time(currentTime_t); | |
tm *time_struct = localtime(currentTime_t); | |
strftime(dateString, 19, "%d/%m/%Y %H:%M",time_struct); // (dd/mm/yyyy hh:mm) | |
//actually copying the date inside | |
strcpy(p_inode->accessDate,dateString); | |
strcpy(p_inode->creationDate, dateString); | |
strcpy(p_inode->modificationDate, dateString); | |
} | |
void ext2FileSystem::createFilesystem(char* superBlockURL, | |
char* INodeBitmapURL, | |
char* BlockBitmapURL, | |
char* INodeTableURL , | |
char* BlockTableURL ) | |
{ | |
//We use the cf_ prefix on all local vars | |
//Setting up the SuperBlock | |
superBlock *cf_superB = new ext2_fileSys::superBlock; | |
cf_superB->clean(); | |
cf_superB->total_blocks = | |
cf_superB->total_inodes = | |
cf_superB->free_blocks = | |
cf_superB->free_inodes = | |
ext2_fileSys::block_amount; | |
//Writing the new SuperBlock | |
writeRawFile((char*)cf_superB,sizeof(superBlock),1,superBlockURL); | |
//Writing the InodeBitmap | |
bool cf_Bitmap_I[block_amount]; | |
for(int b = 1; b < block_amount; b++) | |
cf_Bitmap_I[b] = false; | |
cf_Bitmap_I[0] = true; | |
writeRawFile((char*)cf_Bitmap_I,sizeof(cf_Bitmap_I),1,INodeBitmapURL); | |
//Writing the BlockBitmap | |
bool cf_Bitmap_B[block_amount]; | |
for(int i = 1; i < block_amount; i++) | |
cf_Bitmap_B[i] = false; | |
writeRawFile((char*)cf_Bitmap_B,sizeof(cf_Bitmap_B),1,BlockBitmapURL); | |
//Creating the RootInode | |
inode_s *cf_rootInode = new inode_s; | |
cf_rootInode->clean(); | |
createINode(cf_rootInode,true,-1); | |
cf_rootInode->dataBlocks_p[0] = 0; | |
//Creating the InodeTable | |
inode_s cf_InodeTable[block_amount]; | |
cf_InodeTable[0] = *cf_rootInode; | |
for (int i=1; i<block_amount; i++) | |
cf_InodeTable[i].clean(); | |
writeRawFile((char*)cf_InodeTable,sizeof(inode_s)*block_amount,1,INodeTableURL); | |
//Creating the set of blocks | |
dataBlock cf_DataTable[block_amount]; | |
for (int i=1;i<block_amount;i++) | |
cf_DataTable[i].clean(); | |
//here we initialize the root | |
createFolderBlock(&cf_DataTable[0],0,0); | |
writeRawFile((char*)cf_DataTable,sizeof(dataBlock)*block_amount,1,BlockTableURL); | |
} | |
void ext2FileSystem::loadDataFiles(char* superBlockURL, | |
char* INodeBitmapURL, | |
char* BlockBitmapURL, | |
char* INodeTableURL , | |
char* BlockTableURL) | |
{ | |
allocMemories(); | |
readRawFile((char*)superB,superBlockURL,sizeof(superBlock)); | |
readRawFile((char*)Bitmap_I,INodeBitmapURL,block_amount); | |
readRawFile((char*)Bitmap_B,BlockBitmapURL,block_amount); | |
readRawFile((char*)InodeTable,INodeTableURL,sizeof(inode_s)*block_amount); | |
strcpy(dataBlockTableURL,BlockTableURL); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: ext2FileSystem.h | |
* Author: tian | |
* | |
* Created on 24 September 2010, 16:55 | |
*/ | |
#ifndef _EXT2FILESYSTEM_H | |
#define _EXT2FILESYSTEM_H | |
#include "dataStructs.h" | |
#include <stdlib.h> | |
#include <fstream> | |
using namespace ext2_fileSys; | |
class ext2FileSystem { | |
public: | |
ext2FileSystem(); | |
ext2FileSystem(const ext2FileSystem& orig); | |
virtual ~ext2FileSystem(); | |
void createFilesystem(char* superBlockURL, | |
char* INodeBitmapURL, | |
char* BlockBitmapURL, | |
char* INodeTableURL , | |
char* BlockTableURL ); | |
void writeRawFile(char* data, int size, int amount,char* route); | |
void readRawFile(char* dumpPlace, char* route,int size); | |
void createINode( ext2_fileSys::inode_s* p_inode, | |
bool b_isDir, | |
int filesize); | |
void createFolderBlock(ext2_fileSys::dataBlock *blockToShape, | |
int i_father, | |
int i_self); | |
void createFolder(ext2_fileSys::inode_s father_inode, | |
ext2_fileSys::inode_s self_inode, | |
ext2_fileSys::folderBlock father_block, | |
ext2_fileSys::dataBlock self_block); | |
void loadDataFiles(char* superBlockURL, | |
char* INodeBitmapURL, | |
char* BlockBitmapURL, | |
char* INodeTableURL , | |
char* BlockTableURL ); | |
void allocMemories(); | |
superBlock *superB; | |
bool **Bitmap_I; | |
bool **Bitmap_B; | |
inode_s **InodeTable; | |
char *dataBlockTableURL; | |
//ext2_fileSys::dataBlock *DataTable[ext2_fileSys::block_amount]; | |
private: | |
}; | |
#endif /* _EXT2FILESYSTEM_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: main.cpp | |
* Author: tian | |
* | |
*/ | |
#include <stdlib.h> | |
#include "dataStructs.h" | |
#include <iostream> | |
#include "ext2FileSystem.h" | |
#include <string.h> | |
#define ROUTE(x) "/home/tian/archo/"#x | |
ext2FileSystem *exo; | |
char routeSuperB[] = ROUTE(superBlock.sb); | |
char routeInodeB[] = ROUTE(InodeB.bitmap); | |
char routeBlockB[] = ROUTE(BlockB.bitmap); | |
char routeInodeT[] = ROUTE(INodeTa.table); | |
char routeBlockT[] = ROUTE(BlockTa.table); | |
int makeFilesystem(){ | |
exo = new ext2FileSystem; | |
exo->createFilesystem(routeSuperB, | |
routeInodeB, | |
routeBlockB, | |
routeInodeT, | |
routeBlockT); | |
return 0; | |
} | |
int loadFilesytem(){ | |
exo->loadDataFiles(routeSuperB, | |
routeInodeB, | |
routeBlockB, | |
routeInodeT, | |
routeBlockT); | |
} | |
int main(int argc, char** argv) { | |
std::cout<<"Welcome!\n"; | |
if (argc==3){ | |
if (strcmp(argv[1],"--init")==0 && strcmp(argv[2],"filesystem")==0){ | |
std::cout<<"Creating a Filesystem\n"; | |
makeFilesystem(); | |
} | |
} | |
loadFilesytem(); | |
return (EXIT_SUCCESS); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment