void NVMAntiCacheDB::writeBlock(const std::string tableName, uint32_t blockId, const int tupleCount, const char* data, const long size, const int evictedTupleCount) { VOLT_TRACE("free blocks: %d", getFreeBlocks()); if (getFreeBlocks() == 0) { VOLT_WARN("No free space in ACID %d for blockid %u with blocksize %ld", m_ACID, blockId, size); throw FullBackingStoreException(((int32_t)m_ACID << 16) & blockId, 0); } uint32_t index = getFreeNVMBlockIndex(); VOLT_TRACE("block index: %u", index); char* block = getNVMBlock(index); long bufsize; char* buffer = new char [tableName.size() + 1 + size]; memset(buffer, 0, tableName.size() + 1 + size); bufsize = tableName.size() + 1; memcpy(buffer, tableName.c_str(), bufsize); memcpy(buffer + bufsize, data, size); bufsize += size; memcpy(block, buffer, bufsize); delete[] buffer; VOLT_DEBUG("Writing NVM Block: ID = %u, index = %u, tupleCount = %d, size = %ld, tableName = %s", blockId, index, tupleCount, bufsize, tableName.c_str()); m_blocksEvicted++; if (!isBlockMerge()) { tupleInBlock[blockId] = tupleCount; evictedTupleInBlock[blockId] = evictedTupleCount; blockSize[blockId] = bufsize; m_bytesEvicted += static_cast<int32_t>((int64_t)bufsize * evictedTupleCount / tupleCount); } else { m_bytesEvicted += static_cast<int32_t>(bufsize); } m_blockMap.insert(std::pair<uint32_t, std::pair<int, int32_t> >(blockId, std::pair<uint32_t, int32_t>(index, static_cast<int32_t>(bufsize)))); m_monoBlockID++; // FIXME: I'm hacking!!!!!!!!!!!!!!!!!!!!!!!!! pushBlockLRU(blockId); }
int putFile(SuperBlock *super_block, FILE *fp, char *file_name, char *new_file_name){ FILE *new_fp = fopen(file_name, "rb"); if(new_fp == NULL){ fprintf(stderr,"File not found.\n"); return -1; } int file_size_bytes, block_size, bytes_read, file_size_blocks; file_size_bytes = block_size = bytes_read = file_size_blocks = 0; block_size = ntohs((*super_block).block_size); char block[block_size]; char *FAT_blocks = NULL; readFATBlocks(&FAT_blocks, super_block, fp); while((bytes_read = fread(block, 1, sizeof(block), new_fp)) > 0 ){ //read in data of (at most) one block size file_size_bytes += bytes_read; //found file size (in blocks) file_size_blocks++; } int free_blocks[file_size_blocks]; if(getFreeBlocks(FAT_blocks, free_blocks, super_block, file_size_blocks) < 0){ printf("No free space on disk.\n"); exit(EXIT_FAILURE); } int block_index, size, first_block; //get file size first_block = -1; size = block_size; //allocate one block fseek(new_fp, 0, SEEK_SET); for(block_index = 0; block_index < file_size_blocks; block_index++){ memset(block, 0, sizeof(block)); if(block_index == (file_size_blocks - 2)){ //last case where you may want to read in less than 512 bytes size = file_size_bytes - ((file_size_blocks - 1) * block_size); } fread(block, sizeof(block), 1, new_fp); fseek(fp, free_blocks[block_index] * 512, SEEK_SET); //move to correct mem location fwrite(block, size, 1, fp); //write one block if(first_block < 0) { first_block = free_blocks[block_index]; } } //write the fat blocks to FAT table writeFATBlocks(first_block, free_blocks, super_block, fp, file_size_blocks); writeFileEntry(super_block, fp, first_block, file_name, new_file_name, file_size_blocks, file_size_bytes); fprintf(stderr,"File succsessfully put in disk image.\n"); fclose(new_fp); return 0; }
static void uae4all_disk_real_write(int num) { unsigned new_crc=savedisk_get_checksum(uae4all_disk_memory[num],MAX_DISK_LEN); if (new_crc!=uae4all_disk_actual_crc[num]) { void *buff=uae4all_disk_memory[num]; void *buff_patch=uae4all_extra_buffer; memset(buff_patch,0,MAX_DISK_LEN); unsigned changed=savedisk_get_changes(buff,MAX_DISK_LEN,buff_patch,uae4all_disk_orig[num]); if ((changed)&&(changed<MAX_DISK_LEN)) { char *namefile=get_namefile(num); void *bc=calloc(1,MAX_COMP_SIZE); unsigned long sizecompressed=MAX_COMP_SIZE; //int compress2(Bytef * dest, uLongf * destLen, const Bytef * source, uLong sourceLen, int level); int retc=compress2((Bytef *)bc,&sizecompressed,(const Bytef *)uae4all_extra_buffer,changed,Z_BEST_COMPRESSION); if (retc>=0) { unsigned usado=0; { FILE *f=fopen(namefile,"rb"); if (f) { fseek(f,0,SEEK_END); usado=ftell(f); fclose(f); usado/=512; } } if ( ((getFreeBlocks()+usado)*512) >=(sizecompressed+VMUFILE_PAD)) { eliminate_file(namefile); FILE *f=fopen(namefile,"wb"); if (f) { rebuild_paquete(prefs_df[num], sizecompressed, (unsigned char*) bc, f); fwrite((void *)&sizecompressed,1,4,f); fwrite(bc,1,sizecompressed,f); fclose(f); } } } free(bc); uae4all_disk_actual_crc[num]=new_crc; #ifdef GP2X sync(); #endif } } }
int main(int argc, char** argv) { FILE *fp; char *name = new char[IDENT_SIZE]; int size; int blockcount; int FATstarts; int FATblocks; int rootdirstart; int rootdirblocks; int freeblocks; int reservedblocks; int allocatedblocks; if ((fp=fopen(argv[1],"r"))) { printf("Super block information: \n"); //getName(fp,name); //printf("File system identifier: %s\n", name); size = getBlockSize(fp); printf("Block size: %d\n", size); blockcount = getBlockCount(fp); printf("Block count: %d\n", blockcount); FATstarts = getFATstart(fp); printf("FAT starts: %d\n", FATstarts); FATblocks = getFATblocks(fp); printf("FAT blocks: %d\n", FATblocks); rootdirstart = getRootDirStart(fp); printf("Root directory start: %d\n", rootdirstart); rootdirblocks = getRootDirBlocks(fp); printf("Root directory blocks: %d\n", rootdirblocks); printf("\nFAT information: \n"); freeblocks = getFreeBlocks(fp); printf("Free Blocks: %d\n", freeblocks); reservedblocks = getReservedBlocks(fp); printf("Reserved Blocks: %d\n", reservedblocks); allocatedblocks = getAllocatedBlocks(fp); printf("Allocated Blocks: %d\n", allocatedblocks); } else printf("Fail to open the image file.\n"); free(name); fclose(fp); return 0; }
int getAllocatedBlocks(FILE *fp){ int bc = getBlockCount(fp); int rb = getReservedBlocks(fp); int fb = getFreeBlocks(fp); return bc - rb - fb; }
/** * Initialized the super block ,inode block and data blocks of the file system * *@param inp String input given in the terminal *@throws Exception when unkown block is reached, invalid block is traveresed * */ void InitializeFS :: createFileSystem(string inp){ superBlock sb; iNode node,rootNode; Directory rootDirectory,filler; unsigned short freeHeadChain; try{ if(checkParameters(inp)){ file.open(getFileSystemPath().append("fsaccess").c_str(),ios::binary | ios::app); if (file.is_open()){ //Initializing the file system sb.isize = getInodesBlock(); //1 Block is the directory for root node and another block is the head of the free chain list sb.fsize = getFreeBlocks(); sb.ninode = getNumOfInodes(); sb.nfree = 100; for(int i=0;i<100;i++){ sb.free[i] = (getFreeBlocksIndex() + i); } file.write((char *)&sb,BLOCK_SIZE); //Setting up root node rootNode.flags = 0x1800; rootNode.addr[0] = (1+ getInodesBlock())*BLOCK_SIZE; file.write((char *)&rootNode,getSizeOfInode()); //Writing inodes block to the file system for(int i=2;i<=getNumOfInodes();i++){ file.write((char *)&node,getSizeOfInode()); } //Padding empty characters to complete block if(calculateInodePadding() !=0 ){ char *iNodeBuffer = new char[calculateInodePadding()]; file.write((char *)&iNodeBuffer,calculateInodePadding()); } //Writing Root Directory MetaDirectory::Instance()->setNumInodes(getNumOfInodes()); MetaDirectory::Instance()->setPath(getFileSystemPath().append("fsaccess")); MetaDirectory::Instance()->dirState(); //Setting './' character rootDirectory.inodeNumber=1; strcpy(rootDirectory.fileName,"."); file.write((char *)&rootDirectory,sizeof(rootDirectory)); //Setting '..' character rootDirectory.inodeNumber=1; strcpy(rootDirectory.fileName,".."); file.write((char *)&rootDirectory,sizeof(rootDirectory)); //Setting the remaining directory entries for(int j=3; j<=numDirectoryEntry; j++){ file.write((char *)&filler,sizeof(filler)); } //Empty Char buffer char *buffer = new char[BLOCK_SIZE]; char *headChainBuffer; //Writing free data blocks //If the number of free blocks are <100 no need to set free head chain values if((getFreeBlocks() - getFreeBlocksIndex()) < 100){ for(int i=getFreeBlocksIndex();i<=getFreeBlocks();i++){ file.write((char *)&buffer,BLOCK_SIZE); } }else { //Checks for every 100th block and assigns the head chain values for(int i=getFreeBlocksIndex();i<=getFreeBlocks();i++){ //To assign the head chain value link to the next free list . two condition are checked here //Wether the cursor position is traversed 100 blocks starting from the index and //Making sure the head chain don't point to blocks > than our free data blocks if((i % 100 == getFreeBlocksIndex()) && ((getFreeBlocks() - i) >= 100) ){ freeHeadChain = i+100; file.write((char *)&freeHeadChain,2); headChainBuffer = new char[BLOCK_SIZE - 2]; file.write((char *)&headChainBuffer,BLOCK_SIZE - 2); }else{ file.write((char *)&buffer,BLOCK_SIZE); } } } cout <<"!!File System Created Successfully!!" <<endl; }else{ cout << "!!Invalid file system path!!" <<endl; } file.close(); }else{ return; } }catch(exception& e){ cout <<"Exception at createFileSystem method" <<endl; } }
/** * Calculates the starting index for free data blocks.'1' is added to point to the starting position * of data blocks * * @return freeBlockIndex Starting index where the free data blocks start */ int InitializeFS :: getFreeBlocksIndex(void){ int freeBlockIndex; freeBlockIndex = getNumOfBlocks() - getFreeBlocks() + 1; return freeBlockIndex; }