예제 #1
0
/**
 * Opens a binary file on the disk for storing data.
 */
fd_t* openf(char* name)
{
	fd_t * temp = NULL;
	superBlock_t* spB;
	inode_t* nodep;
	readSuperBlock(spB);
	int i=0, j=0;
	while (i<(spB->_numberOfInodes))
	{
	//printSupBlock(spB);
	readInode(nodep, j);
	if (name == nodep->_filename)
		//Exist !! open it!!
	{
		temp->inodeBlockNum = j;
		temp->fileptr = nodep;
		//returns file descriptor
	return 	temp;
	}
	else
	{
		i++;
		j++;
	}
	}
	//printInodesTest(nodep);
	// file not found, create one
	writeSuperBlock(spB);
	writeInode(nodep,spB->_firstBlockOfFreeList);
	return temp;
}
예제 #2
0
void write_root_dir(disk_t disk){
    int *pointers = calloc (1, sizeof (int));
    pointers[0] = '\0';
	Inode * node = writeInode(disk,1,pointers,true,"/");
	free(pointers);
	freeInode(node);
}
예제 #3
0
파일: rfs.c 프로젝트: karajrish/OS
//============== UFS INTERNAL LOW LEVEL ALGORITHMS =============
int readINode(int fd, int inodeNo, struct INode *inode){
	lseek(fd, INODEBLOCKSTART + inodeNo*sizeof(struct INode), SEEK_SET);
	printf("Reading inode number %d at %lu\n", inodeNo, INODEBLOCKSTART + inodeNo*sizeof(struct INode));
	read(fd, inode, sizeof(struct INode));
	inode->i_atime = time(NULL);
	writeInode(fd, inodeNo, inode);
}
예제 #4
0
파일: rfs.c 프로젝트: karajrish/OS
// Create a directory
int makeDir(int fd, char *dname, int uid, int gid, int attributes){
	printf("Creating new directory %s\n",dname);
	int parINodeNo, inodeNo;
	struct INode parent_in;
	struct DirEntry d;

	//Tokenize the dname, check for validity and find its parent directory
	parINodeNo = currDirINode;
	readINode(fd, parINodeNo, &parent_in);

	// Check if the dname already exists in the INode in
	if( fileExists(fd, dname, parent_in, &d)!=-1 ){
		printf("Directory already exists!\n\n");
		return -1;
	}

	// Initialize an inode and data block for new directory
	struct INode in;
	in.i_atime = 0;
	bzero(in.i_blocks, 13);
	in.i_blocks[0] = allocBlock(fd);
	in.i_gen = 0;
	in.i_gid = gid;
	in.i_uid = uid;
	in.i_nlinks = 0;
	in.i_mode = attributes;
	bzero(d.d_entry.d_name, MAXNAMELENGTH);
	strcpy(d.d_entry.d_name, ".");
	int allocatedINode = allocINode(fd, &in);
	d.d_entry.d_inode = allocatedINode;
	d.d_offset = 0;
	allocDirEntry(fd, &in, &d);
	bzero(d.d_entry.d_name, MAXNAMELENGTH);
	strcpy(d.d_entry.d_name, "..");
	d.d_entry.d_inode = parINodeNo;
	allocDirEntry(fd, &in, &d);
	writeInode(fd, allocatedINode, &in);

	// Add its DirEntry in its parent INode and rewrite Inode entry
	bzero(d.d_entry.d_name, MAXNAMELENGTH);
	strcpy(d.d_entry.d_name, dname);
	d.d_entry.d_inode = allocatedINode;
	allocDirEntry(fd, &parent_in, &d);
	writeInode(fd, parINodeNo, &parent_in);
	printf("\n");
	return 0;
}
예제 #5
0
파일: rfs.c 프로젝트: karajrish/OS
int removeOpenFile(int fd, int inodeNo){
	struct InCoreINode* temp = o.ofo_inode;
	while( temp->ic_ino!=inodeNo ){
		temp = temp->ic_next;
		if( temp==NULL ){
			return -1;
		}
	}
	temp->ic_prev->ic_next = temp->ic_next;
	temp->ic_next->ic_prev = temp->ic_prev;
	writeInode(fd, temp->ic_ino, &temp->ic_inode);
}
예제 #6
0
int format( int diskSizeInKB, char* path )
{
	int error, i;
    // create a disk
	if((error = createDisk(diskSizeInKB, path)))
	{
		printf("There was an error with creating the disk\n");
	}
	else
	{
    // complete implementation of this function
    // you must use the functions read/writeSuperblock() & read/writeInode()
	// formatting requires that you setup the superblock, write empty inodes,
	// and setup your free list for the remaining blocks
	superBlock_t supBlock;
	superBlock_t* spBlck = &supBlock;
	fd_t* fileTemp  = NULL;
	spBlck = intializeSupBlock(spBlck);
	inode_t* node;
	inode_t iNode[NUM_INODES];
	for(i = 0; i < NUM_INODES; i++)
	{
		initializeInode(&iNode[i]);
	}
	block_t* dataBlock = NULL;
	dataBlock = initializeBlockList(dataBlock);

	//read content to data blocks
	//write content to file or print out content
	//test if it prints out the content of the superBlock
	printSupBlock(spBlck);
	printInodesTest(&iNode[0]);
	writeSuperBlock(spBlck);
	node = &iNode[0];
	node->_flags = 4;
	node->_owner = 5;
	node->_filesize = 56;
	int i;
	for(i = 0; i < 12; i++)
	{
		node->_filename[i] = 2;
	}
	writeInode(node, 0);
	initializeFreeBlockList();
	//closeDiskFile();
	}


	return error;
}
예제 #7
0
//max name size = 15
//will modify block_map
Inode* writeInode(disk_t disk, int block, int * gpointers,bool directory, char * nametemp){
	int * blkmap = read_block_map(disk);
	blkmap[block] = 1;
	write_block_map(disk,blkmap);
	
	int size = arrayLength(gpointers);
	int maxsize = (disk->block_size-20-(ceil(log10(disk->size))*2)/(ceil(log10(disk->size))+1));
	int * pointers = malloc(sizeof(int) * (size+1));
	int i,j;
	int strlength = length(nametemp);
	char * name = malloc(sizeof(char)*  16);
	for(i = 0; i < 15 && nametemp[i] != '\0';i+=1){
		name[i] = nametemp[i];
	}
	name[i] = '\0';
	int * wpointers = malloc(sizeof(int) * (size + 1));
	for(i = 0; i < size; i+=1){
		pointers[i] = gpointers[i];
		wpointers[i] = gpointers[i];
	}
	wpointers[i] = '\0';
	pointers[i] = '\0';
	char * potSize;
	if(size > (maxsize)){ //-4 for 3 \n and 1 \0 - 16 for name length
		int * newpointers = malloc(sizeof(int) * (maxsize+1));
		int * extrapointers = malloc(sizeof(int) * (size-maxsize+1));
		for(i = 0; i < maxsize;i+=1){
			newpointers[i] = pointers[i];
		}
		newpointers[i] = '\0';
		free(wpointers);
		wpointers = newpointers;
		for(j = 0; j <size-maxsize;j+=1){
			extrapointers[j] = pointers[i];
			i+=1;
		}
		
		extrapointers[j] = '\0';
		int newblk;
		
		for(i = 0; i < disk->size;i+=1){
			if(blkmap[i] == 0){
				newblk = i;
				break;
			}
		}
		freeInode(writeInode(disk,newblk,extrapointers,directory,name));
	}
	unsigned char *strings[6];
	potSize = int2str(size);
	strings[0] = (directory)? "0" : potSize;

	char * list = intArray2charArray(wpointers);
	char * pntrs = malloc(sizeof(char) * (length(list)+2));
	pntrs[0] = '\n';

	pntrs[1] = '\0';
	strcat(pntrs,list);
	strings[1] = pntrs;
	strings[2] = "\n";
	strings[3] = name;
	strings[4] = "\n";
	strings[5] = NULL;
	
	unsigned char *databuf = calloc(disk->block_size, sizeof(unsigned char) );
	copy2buf(databuf,strings,0);
	writeblock(disk,block,databuf);
	free(list);
	free(pntrs);
	free(databuf);
	//free(name);
	free(blkmap);
	free(potSize);
	free(wpointers);
	return createInode(size,pointers,directory,block,name);
}
예제 #8
0
Inode * rewriteInode(disk_t disk, Inode* inode){
	deleteInode(disk,inode);
	Inode* newinode = writeInode(disk,inode->block,inode->pointers,inode->isDirectory,inode->name);
	freeInode(inode);
	return newinode;
}
예제 #9
0
파일: rfs.c 프로젝트: karajrish/OS
//============== UFS INTERFACE LAYER ==========================
int init_FS(int fd){
	// Boot block dummy block (Because no boot loader nothing...)
	bzero(nullbuf, BLOCKSIZE);
	write(fd, nullbuf, BLOCKSIZE);

	// Initialize variables
	int i;
	nsuperblocks = 1;
	ninodeblocks = 8;
	nbootblocks = 1;
	nrootdirblocks = 1;
	ndatablocks = TOTALBLOCKS - nsuperblocks - ninodeblocks - nbootblocks - nrootdirblocks;
	INODETABLESIZE = (ninodeblocks*BLOCKSIZE)/sizeof(struct INode);
	MAXDIRENTRIES = BLOCKSIZE/ sizeof(struct DirEntry);
	DATABLOCKSTART = BLOCKSIZE*(TOTALBLOCKS - ndatablocks);
	INODEBLOCKSTART = BLOCKSIZE*(nsuperblocks+nbootblocks);
	
	//Initialize super block
	strcpy(s.sb_vname, "root");
	s.sb_ninodes = (ninodeblocks*BLOCKSIZE)/sizeof(struct INode);
	s.sb_nblocks = ndatablocks;
	s.sb_nfreeblocks = s.sb_nblocks;
	s.sb_nfreeinodes = s.sb_ninodes;
	s.sb_flags = 0;
	bzero(s.sb_freeblocks, CACHESIZE);
	bzero(s.sb_freeinodes, CACHESIZE);
	s.sb_freeblockindex = CACHESIZE;
	s.sb_freeinodeindex = CACHESIZE;
	s.sb_chktime = time(NULL);
	s.sb_ctime = time(NULL);
	write(fd, &s, sizeof(struct SuperBlock));
	write(fd, nullbuf, (nsuperblocks*BLOCKSIZE) - sizeof(struct SuperBlock));
	printf("Superblock initialized!\n");

	// Write initialized list of inodes
	nullINode.i_size = 0;
	nullINode.i_atime = 0;
	nullINode.i_ctime = 0;
	nullINode.i_mtime = 0;
	bzero(nullINode.i_blocks, 13);
	nullINode.i_mode = 0;
	nullINode.i_uid = 0;
	nullINode.i_gid = 0;
	nullINode.i_gen = 0;
	nullINode.i_nlinks = 0;
	for(i=0; i<INODETABLESIZE; i++)
		write(fd, &nullINode, sizeof(struct INode));
	write(fd, &nullbuf, BLOCKSIZE%sizeof(struct INode));
	printf("Inodes initialized!\n");

	// Write initialized list of directory entries
	// Fill the remaining empty datablocks
	printf("%d\n",ndatablocks+nrootdirblocks );
	for(i=0; i<ndatablocks+nrootdirblocks; i++)
		write(fd, &nullbuf, BLOCKSIZE);
	printf("All data blocks initialized!\n");

	// Write free block information (data structures)
	struct INode in;
	in.i_atime = 0;
	bzero(in.i_blocks, 13);
	in.i_blocks[0] = allocBlock(fd);
	in.i_gen = 0;
	in.i_gid = 1;
	in.i_uid = 1;
	in.i_nlinks = 0;
	in.i_mode = 0;
	struct DirEntry d;
	bzero(d.d_entry.d_name, MAXNAMELENGTH);
	strcpy(d.d_entry.d_name,".");
	s.sb_rootdir = currDirINode = d.d_entry.d_inode = allocINode(fd, &in);
	d.d_offset = 0;
	allocDirEntry(fd, &in, &d);
	writeInode(fd, s.sb_rootdir, &in);
	updateSB(fd);
	printf("\n");
	o.ofo_inode = NULL;
	o.ofo_mode = o.ofo_ref = 0;
	return 0;
}