static int
getFreeBlock(){
	char bitMap[4*512];
	loadBitMap(bitMap);
	int j = 0;
	int i = 0;
	for( j = 0; j < 512*4 && bitMap[j] == 0x00 ; j++ ) {
	}	
	if( j == 512 * 4 ){
		return -1;	
	}
	int k = 0;

	for ( i = 0 ; i < sizeof(char) * 8 ; i++ ) {
		if ( ((bitMap[j] << i) & 0x80) == 0x80 ) {
			int aux = 0x1;
			for ( k = 0 ; k < sizeof(char)*8 - i - 1 ; k++ ) {
				aux <<= 1;
			}
			aux = ~aux;
			bitMap[j] &= aux;
			setBitMap(bitMap);
			return j * sizeof(char) * 8 + i;
		}
	}	
	return 0xCABA110;
}
static int
setFreeBlock(int blockNumber){
	if( blockNumber <= 0 || blockNumber > 2047 ){
		return -1;
	}
	
	char bitMap[4*512];
	loadBitMap(bitMap);
	
	int indexToModify = blockNumber / 8;
	int indexOfIndexToModify = blockNumber % 8;
	
	char newEntry;
	switch( indexOfIndexToModify ){
		case 0: newEntry = 0x80; break;
		case 1: newEntry = 0x40; break;
		case 2: newEntry = 0x20; break;
		case 3: newEntry = 0x10; break;
		case 4: newEntry = 0x08; break;
		case 5: newEntry = 0x04; break;
		case 6: newEntry = 0x02; break;
		case 7: newEntry = 0x01; break;
	}
	
	bitMap[indexToModify] |= newEntry;
	
	setBitMap(bitMap);
	
	return 0;
}
Пример #3
0
void BufferManager::setFileHeader(long pageNumber, bool flag)
{
	cout << "Update position: " << (pageNumber-1) << " in BitMap of DB File with flag: " << flag << "." << endl;
	setBitMap(dB_FILE_Header.bitMap, pageNumber - 1, flag);

	DBFileManager * dBFileManager = DBFileManager::getInstance();
	dBFileManager->setDBFileHeader(&dB_FILE_Header);
}
void 
init_fs() {
	char block[512] = {0};
	useSector(0, block, READ,1);

	if ( *((int*)block) == 0x12345678 ) {
	  	//printSomewhere(300, "POR ACA");
		root = kmalloc(sizeof(ENTRY));
		strcpy(root->name, "/");
		root->inodeNumber = 0;
		root->link = 0;
		int i;
		for ( i = 5 ; i < 2048 ; i++ ) {
			INODE * inodeAux = getInode(i-5);
			freeInodes[i-5] = inodeAux->attributes.free;
			free(inodeAux);
		}

		return;
	}
	*((int*)block) = 0x12345678;
	ENTRY rootEntry;
	strcpy(rootEntry.name , "/");
	rootEntry.inodeNumber = 0;
	rootEntry.link = 0;
	memcpy( block + sizeof(int), (char*)&rootEntry, sizeof(ENTRY));
	useSector(0, block, WRITE, 1);
	
	/*sete mapa d bits*/
	int i;
	char mapBlock[4*512];
	for ( i = 1 ; i < 4*512 ; i++ ) {
		mapBlock[i] = 0xFF;
	}
	mapBlock[0] = 0x7F;
	setBitMap(mapBlock);
	INODE inode;
	inode.attributes.free = 1;
	inode.blocks[0] = -1;
	for ( i = 5 ; i < 2048 ; i++ ) {
		if ( i == 5 ) {
			INODE rootInode;
			rootInode.blocks[0] = 0;
			rootInode.blocks[1] = -1;
			rootInode.attributes.prev_version = -1;
			rootInode.attributes.parentDir = 0;	
			int seconds = _clock(0);
			rootInode.attributes.creation_time.seconds = ((seconds&0xF0) >> 4) * 10 + (seconds&0x0F); 
			int minutes = _clock(2);
			rootInode.attributes.creation_time.minutes = ((minutes&0xF0) >> 4) * 10 + (minutes&0x0F); 
			int hours = _clock(4);
			rootInode.attributes.creation_time.hours = ((hours&0xF0) >> 4) * 10 + (hours&0x0F); 
			int day = _clock(7);
			rootInode.attributes.creation_time.day = ((day&0xF0) >> 4) * 10 + (day&0x0F);
			int month = _clock(8);
			rootInode.attributes.creation_time.month = ((month&0xF0) >> 4) * 10 + (month&0x0F); 
			int year = _clock(9);
			rootInode.attributes.creation_time.year = ((year&0xF0) >> 4) * 10 + (year&0x0F); 
			strcpy(rootInode.attributes.name , "/");	
			rootInode.attributes.size = 3 * sizeof(ENTRY);
			rootInode.attributes.free = 0;
			rootInode.attributes.alive = 1;
			char buffer[512] = {0};
			memcpy(buffer, (char*)&rootInode, 512);
			useSector(5, buffer, WRITE, 1);
		} else {