void Ext2FS::initFsRoot() { readSuperBlock(); _blockSize = 1024 << _sb->log_block_size; int i = (_sb->block_count / _sb->blocks_per_group) + ((_sb->block_count % _sb->blocks_per_group) ? 1 : 0); int j = (_sb->inodes_count / _sb->inodes_per_group) + ((_sb->inodes_count % _sb->inodes_per_group) ? 1 : 0); _groupNumber = (i > j) ? i : j; readGroupBlock(); struct file *rootFile = getRoot(); struct filePrivateData *data = (struct filePrivateData*)kmalloc(sizeof(struct filePrivateData)); rootFile->name = (char*)kmalloc(strlen("/") + 1); strcpy(rootFile->name, "/"); data->inum = EXT2_INUM_ROOT; data->inode = readInode(EXT2_INUM_ROOT); rootFile->content = 0; rootFile->privateData = (void*)data; rootFile->parent = rootFile; rootFile->leaf = getDirEntries(rootFile); rootFile->next = 0; rootFile->prev = 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; }
int readf(fd_t* stream, char* data, int size) { superBlock_t* spB; inode_t* nodep; if (stream) { readSuperBlock(stream->inodeBlockNum); readInode() return 0; } else return -1;
/** * A test program for testing file system operations. * * @param args - a list of arguments */ int main(int argc, char* argv[]) { int error; superBlock_t superBlock; superBlock_t* spB = &superBlock; inode_t node; inode_t* nodep = &node; initializeInode(nodep); char path[25] = "storageDisk"; if((error = format( 100000, path))) { printf("There was an error formatting the disk\n"); } printf("\nHello World\n"); //openDiskFile(path); readSuperBlock(spB); printSupBlock(spB); readInode(nodep, 1); printInodesTest(nodep); //closeDiskFile(); return 0; }
int main () { int rc; /* Return code */ int fd; /* Device descriptor */ int sector; /* IN: sector to read */ struct partition partitions[16]; struct ext2_group_desc groupdescriptors[32]; struct ext2_inode inode; struct ext2_super_block superblock; int block_size; int dir_in_location; int partition_start; int start = 446; int inode_num; int dir_count; int gd_count; int partition_count; /* Open the device */ fd = open ( "disk", O_RDWR ); if ( fd == -1 ) { perror ( "Could not open device file" ); exit(-1); } /* Read the partitions */ partition_count = getPartitions(fd, partitions); partition_start = partitions[0].start_sect; /* Read the Superblock */ superblock = readSuperBlock(fd, partition_start); if (superblock.s_magic == EXT2_SUPER_MAGIC) { printf("\nSuperblock Magic Number: %x (Correct)", superblock.s_magic); } else { printf("\nSuperblock Magic Number: %x (Incorrect)", superblock.s_magic); } block_size = pow(2, superblock.s_log_block_size) * 1024; printf("\nBlock Size: %d\n", block_size); /* Read the group descriptors */ gd_count = getGroupDescriptors(fd, partition_start, groupdescriptors); /* Read the root inode */ inode_num = 2; inode = getInode(fd, inode_num, groupdescriptors, partition_start, superblock.s_inodes_per_group); printf("\nRoot Inode File Mode: "); printFileMode(inode.i_mode); /* See if root inode is allocated */ if (checkAllocation(fd, 1, inode_num, groupdescriptors, partition_start, superblock.s_blocks_per_group, superblock.s_inodes_per_group) == 1) { printf("\nInode Allocated\n"); } else { printf("\nInode Not Allocated\n"); } /* Read the data for root inode */ printf("\nRoot Inode Directory Data..."); dir_in_location = partition_start + (inode.i_block[0] * 2); dir_count = printDirs(fd, dir_in_location); /* Read the oz inode */ inode_num = 28; inode = getInode(fd, inode_num, groupdescriptors, partition_start, superblock.s_inodes_per_group); /* Read the data for oz inode */ printf("\n\nOz Directory Data..."); dir_in_location = partition_start + (inode.i_block[0] * 2); dir_count = printDirs(fd, dir_in_location); /* Read the ohmy.txt inode */ inode_num = 4021; inode = getInode(fd, inode_num, groupdescriptors, partition_start, superblock.s_inodes_per_group); printf("\n\nohmy.txt Inode Data Blocks: %d", inode.i_blocks); /* See if ohmy.txt's blocks are allocated */ if (checkBlockAllocation(fd, inode.i_block, groupdescriptors, partition_start, superblock.s_blocks_per_group, superblock.s_inodes_per_group) == 1) { printf("\nAll Blocks Allocated"); } else { printf("\nNot All Blocks Allocated"); } /* Read the glinda inode */ inode_num = 30; inode = getInode(fd, inode_num, groupdescriptors, partition_start, superblock.s_inodes_per_group); printf("\n\nglinda Inode File Mode: "); printFileMode(inode.i_mode); printf("\nglinda Link: "); printFastSymbolic(fd, inode_num, inode.i_size, groupdescriptors, partition_start, superblock.s_inodes_per_group); close(fd); return 0; }