Author: nieklinnenbank Date: Tue Jun 30 16:25:36 2009 New Revision: 174 Modified: trunk/include/List.h trunk/srv/filesystem/ext2/Ext2Create.cpp trunk/srv/filesystem/ext2/Ext2Create.h Log: Ext2Create now writes Ext2Group descriptors to an image. The /sbin/dumpe2fs utility from e2fsprogs now correctly displays the superblock information of the generated image. Also added the List::count() function which returns the number of items in the List. Modified: trunk/include/List.h ============================================================================== --- trunk/include/List.h (original) +++ trunk/include/List.h Tue Jun 30 16:25:36 2009 @@ -57,9 +57,8 @@ /** * Class constructor. */ - List() + List() : headNode(0), tailNode(0), nodeCount(0) { - headNode = tailNode = 0; } /** @@ -90,6 +89,7 @@ if (!tailNode) tailNode = n; headNode = n; + nodeCount++; } /** @@ -107,6 +107,7 @@ if (!headNode) headNode = n; tailNode = n; + nodeCount++; } /** @@ -132,7 +133,8 @@ if (i == tailNode) tailNode = i->prev; - + + nodeCount--; delete i; break; } @@ -171,6 +173,7 @@ delete i; } headNode = tailNode = ZERO; + nodeCount = ZERO; } /** @@ -199,9 +202,21 @@ { return headNode ? false : true; } + + /** + * Get the number of items. + * @return The number of items on the List. + */ + Size count() const + { + return nodeCount; + } /** Head of the List. */ ListNode<T> *headNode, *tailNode; + + /** Number of items currently in the List. */ + Size nodeCount; }; #endif /* __LIST_H */ Modified: trunk/srv/filesystem/ext2/Ext2Create.cpp ============================================================================== --- trunk/srv/filesystem/ext2/Ext2Create.cpp (original) +++ trunk/srv/filesystem/ext2/Ext2Create.cpp Tue Jun 30 16:25:36 2009 @@ -17,6 +17,7 @@ #include <BitMap.h> #include <List.h> +#include <ListIterator.h> #include <String.h> #include "Ext2SuperBlock.h" #include "Ext2Inode.h" @@ -139,9 +140,52 @@ return EXIT_SUCCESS; } +void Ext2Create::inputToGroup(List<Ext2Group> *list, + Ext2InputFile *file) +{ + Ext2Group *group; + + /* Find a group descriptor with space. */ + for (ListIterator<Ext2Group> j(list); j.hasNext(); j++) + { + if (j.current()->freeInodesCount && + j.current()->freeBlocksCount >= file->inode.blocks) + { + j.current()->freeInodesCount--; + j.current()->freeBlocksCount -= file->inode.blocks; + return; + } + } + /* Allocate a new Ext2Group. */ + group = new Ext2Group; + group->blockBitmap = blockSize * 3; + group->inodeBitmap = blockSize * 3; + group->inodeTable = blockSize * 3; + group->freeBlocksCount = EXT2CREATE_BLOCKS_PER_GROUP; + group->freeInodesCount = EXT2CREATE_INODES_PER_GROUP - 1; + group->usedDirsCount = ZERO; + + /* Add it to the list. */ + list->insertTail(group); +} + +void Ext2Create::createGroups(List<Ext2Group> *list, + Ext2InputFile *file) +{ + /* Add the file itself. */ + inputToGroup(list, file); + + /* Loop childs. */ + for (ListIterator<Ext2InputFile> i(file->childs); i.hasNext(); i++) + { + inputToGroup(list, i.current()); + } +} + int Ext2Create::writeImage() { FILE *fp; + List<Ext2Group> groups; assert(image != ZERO); assert(prog != ZERO); @@ -152,9 +196,16 @@ /* Add the input directory contents. */ readInput(input, inputRoot); + /* Update block count. */ + super->blocksCount += (super->inodesCount / blockSize) + 1; + + /* Generate group descriptors. */ + createGroups(&groups, inputRoot); + /* Debug out. */- printf( "Writing Extended 2 FileSystem to `%s' (blocksize=%u inodes=%u)\r\n",
- image, blockSize, super->inodesCount); + printf( "Writing Extended 2 FileSystem to `%s' " + "(blocksize=%u inodes=%u groups=%u)\r\n", + image, blockSize, super->inodesCount, groups.count()); /* Open output image file. */ if ((fp = fopen(image, "w")) == NULL) @@ -177,6 +228,19 @@ prog, image, strerror(errno)); return EXIT_FAILURE; } + /* Seek to the next block. */ + fseek(fp, (super->firstDataBlock + 1) * blockSize, + SEEK_SET); + + /* Write group descriptors. */ + for (ListIterator<Ext2Group> i(&groups); i.hasNext(); i++) + { + fwrite(i.current(), sizeof(Ext2Group), 1, fp); + } + /* Dummy. */ + fseek(fp, blockSize * 3, SEEK_SET); + fwrite(" ", 1, 1, fp); + /* Cleanup. */ fclose(fp); delete super; @@ -209,8 +273,8 @@ sb->freeBlocksCount = 0; sb->freeInodesCount = 0; sb->firstDataBlock = 1; - sb->log2BlockSize = blockSize >> 10; - sb->log2FragmentSize = fragmentSize >> 10; + sb->log2BlockSize = blockSize >> 11; + sb->log2FragmentSize = fragmentSize >> 11; sb->blocksPerGroup = EXT2CREATE_BLOCKS_PER_GROUP; sb->fragmentsPerGroup = EXT2CREATE_FRAGS_PER_GROUP; sb->inodesPerGroup = EXT2CREATE_INODES_PER_GROUP; Modified: trunk/srv/filesystem/ext2/Ext2Create.h ============================================================================== --- trunk/srv/filesystem/ext2/Ext2Create.h (original) +++ trunk/srv/filesystem/ext2/Ext2Create.h Tue Jun 30 16:25:36 2009 @@ -115,6 +115,20 @@ * @return Pointer to the newly created Ext2InputFile. */ Ext2InputFile * addInputFile(char *inputFile, Ext2InputFile *parent); + + /** + * Create group descriptors and add the given input files. + * @param list List to store group descriptors in. + * @param file Input file and childs to add. + */ + void createGroups(List<Ext2Group> *list, Ext2InputFile *file); + + /** + * Add the given input file to any group. + * @param list List of available group descriptors. + * @param file The file to add. + */ + void inputToGroup(List<Ext2Group> *list, Ext2InputFile *file); /** * Allocate and initialize a superblock.