Author: nieklinnenbank Date: Mon Jun 29 10:08:43 2009 New Revision: 169 Added: trunk/srv/filesystem/ext2/Ext2Create.h Modified: trunk/boot/boot.ext2desc trunk/srv/filesystem/ext2/Ext2Create.cpp trunk/srv/filesystem/ext2/Ext2FileSystem.h trunk/srv/filesystem/ext2/Ext2SuperBlock.h Log: Added Ext2Create class to create new Extended 2 FileSystems. It currently can allocate, fill and write a superblock to an output image. /usr/bin/file correctly identifies it as an ext2 filesystem image. Modified: trunk/boot/boot.ext2desc ============================================================================== --- trunk/boot/boot.ext2desc (original) +++ trunk/boot/boot.ext2desc Mon Jun 29 10:08:43 2009 @@ -85,7 +85,6 @@ ./srv/filesystem/ext2/Ext2Group.h ./srv/filesystem/ext2/Ext2Directory.h ./srv/filesystem/ext2/SConscript -./srv/filesystem/ext2/Main.cpp ./srv/filesystem/ext2/Ext2FileSystem.h ./srv/filesystem/ext2/Ext2FileSystem.cpp ./srv/filesystem/virtual/VirtualFileSystem.cpp Modified: trunk/srv/filesystem/ext2/Ext2Create.cpp ============================================================================== --- trunk/srv/filesystem/ext2/Ext2Create.cpp (original) +++ trunk/srv/filesystem/ext2/Ext2Create.cpp Mon Jun 29 10:08:43 2009 @@ -16,13 +16,132 @@ */ #include <BitMap.h> +#include <List.h> +#include <String.h> #include "Ext2SuperBlock.h" #include "Ext2Inode.h" #include "Ext2Group.h" +#include "Ext2FileSystem.h" +#include "Ext2Create.h" #include <stdio.h> #include <stdlib.h> +#include <string.h> +#include <errno.h> -int main(int argc, char **argv) +Ext2Create::Ext2Create() +{ + prog = ZERO; + image = ZERO; + super = ZERO; + blockSize = EXT2_MIN_BLOCK_SIZE; + totalInodes = 1024 * 64; + freeInodes = totalInodes; + totalBlocks = 128; + freeBlocks = totalBlocks; + fragmentSize = EXT2_MIN_FRAG_SIZE; +} + +int Ext2Create::create() { + FILE *fp; + + assert(image != ZERO); + assert(prog != ZERO); + + /* Allocate and initialize the superblock. */ + super = initSuperBlock(); + + /* Open output image file. */ + if ((fp = fopen(image, "w")) == NULL) + { + printf("%s: failed to fopen `%s': %s\r\n", + prog, image, strerror(errno)); + return EXIT_FAILURE; + } + /* Seek to second block. */ + if (fseek(fp, blockSize, SEEK_SET) != 0) + { + printf("%s: failed to seek `%s' to %x: %s\r\n", + prog, image, blockSize, strerror(errno)); + return EXIT_FAILURE; + } + /* Write superblock. */ + if (fwrite(super, sizeof(*super), 1, fp) != 1) + { + printf("%s: failed to fwrite `%s': %s\r\n", + prog, image, strerror(errno)); + return EXIT_FAILURE; + } + /* Cleanup. */ + fclose(fp); + delete super; + + /* All done. */ return EXIT_SUCCESS; +} + +void Ext2Create::setProgram(char *progName) +{ + this->prog = progName; +} + +void Ext2Create::setImage(char *imageName) +{ + this->image = imageName; +} + +Ext2SuperBlock * Ext2Create::initSuperBlock() +{ + Ext2SuperBlock *sb = new Ext2SuperBlock; + sb->inodesCount = totalInodes; + sb->blocksCount = totalBlocks; + sb->reservedBlocksCount = ZERO; + sb->freeBlocksCount = freeBlocks; + sb->freeInodesCount = freeInodes; + sb->firstDataBlock = 1; + sb->log2BlockSize = blockSize >> 10; + sb->log2FragmentSize = fragmentSize >> 10; + sb->blocksPerGroup = 256; + sb->fragmentsPerGroup = 128; + sb->inodesPerGroup = 2048; + sb->mountTime = ZERO; + sb->writeTime = ZERO; + sb->mountCount = ZERO; + sb->maximumMountCount = 32; + sb->magic = EXT2_SUPER_MAGIC; + sb->state = EXT2_VALID_FS; + sb->errors = EXT2_ERRORS_CONTINUE; + sb->minorRevision = ZERO; + sb->lastCheck = ZERO; + sb->checkInterval = 3600 * 24 * 7; + sb->creatorOS = EXT2_OS_FREENOS; + sb->majorRevision = EXT2_CURRENT_REV; + sb->defaultReservedUid = ZERO; + sb->defaultReservedGid = ZERO; + return sb; +} + +int main(int argc, char **argv) +{ + Ext2Create fs; + + /* Verify command-line arguments. */ + if (argc < 3) + { + printf("usage: %s IMAGE DIRECTORY [OPTIONS...]\r\n" + "Creates a new Extended 2 FileSystem\r\n" + "\r\n" + "-h Show this help message.\r\n"+ "-e PATTERN Exclude matching files from the created filesystem\r\n"
+ "-b SIZE Block size.\r\n" + "-n INODES Number of inodes.\r\n", + argv[0]); + return EXIT_FAILURE; + } + /* Process command-line arguments. */ + fs.setProgram(argv[0]); + fs.setImage(argv[1]); + + /* Create a new Extended 2 FileSystem. */ + return fs.create(); } Added: trunk/srv/filesystem/ext2/Ext2Create.h ============================================================================== --- (empty file) +++ trunk/srv/filesystem/ext2/Ext2Create.h Mon Jun 29 10:08:43 2009 @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2009 Niek Linnenbank + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef __FILESYSTEM_EXT2CREATE_H +#define __FILESYSTEM_EXT2CREATE_H + +#include <BitMap.h> +#include <List.h> +#include <String.h> +#include "Ext2SuperBlock.h" +#include "Ext2Inode.h" +#include "Ext2Group.h" +#include "Ext2FileSystem.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> + +/** + * Class for creating new Extended 2 FileSystems. + */ +class Ext2Create +{ + public: + + /** + * Class constructor. + */ + Ext2Create(); + + /** + * Creates a new Extended 2 FileSystem. + * @return EXIT_SUCCESS if successfull and EXIT_FAILURE otherwise. + */ + int create(); + + /** + * Set the program name we are invoked with. + * @param progName program name. + */ + void setProgram(char *progName); + + /** + * Set the output image file name. + * @param imageName Image name to use. + */ + void setImage(char *imageName); + + private: + + /** + * Allocate and initialize a superblock. + */ + Ext2SuperBlock * initSuperBlock(); + + /** Program name we are invoked with. */ + char *prog; + + /** Path to the output image. */ + char *image; + + /** Pointer to the superblock. */ + Ext2SuperBlock *super; + + /** List of file patterns to ignore. */ + List<String> excludes; + + /** Size of each block. */ + Size blockSize; + + /** The total number of Inode available. */ + Size totalInodes; + + /** Number of free Inodes. */ + Size freeInodes; + + /** The total number of blocks available. */ + Size totalBlocks; + + /** Number of free blocks. */ + Size freeBlocks; + + /** Size of a fragment. */ + Size fragmentSize; +}; + +#endif /* __FILESYSTEM_EXT2CREATE_H */ Modified: trunk/srv/filesystem/ext2/Ext2FileSystem.h ============================================================================== --- trunk/srv/filesystem/ext2/Ext2FileSystem.h (original) +++ trunk/srv/filesystem/ext2/Ext2FileSystem.h Mon Jun 29 10:08:43 2009 @@ -33,6 +33,7 @@ #ifndef __FILESYSTEM_EXT2FILESYSTEM_H #define __FILESYSTEM_EXT2FILESYSTEM_H +#ifndef __HOST__ #include <FileSystem.h> #include <FileSystemMessage.h> #include <FileSystemPath.h> @@ -45,6 +46,7 @@ #include "Ext2SuperBlock.h" #include "Ext2Inode.h" #include "Ext2Group.h" +#endif /* __HOST__ */ /** * @defgroup ext2 ext2fs (Extended 2 Filesystem) @@ -318,6 +320,8 @@ * @} */ +#ifndef __HOST__ + /** * Second Extended FileSystem (ext2). * @see http://e2fsprogs.sourceforge.net/ext2.html @@ -418,6 +422,8 @@ /** Inode cache. */ HashTable<Integer<u32>,Ext2Inode> inodes; }; + +#endif /* __HOST__ */ /** * @} Modified: trunk/srv/filesystem/ext2/Ext2SuperBlock.h ============================================================================== --- trunk/srv/filesystem/ext2/Ext2SuperBlock.h (original) +++ trunk/srv/filesystem/ext2/Ext2SuperBlock.h Mon Jun 29 10:08:43 2009 @@ -15,19 +15,19 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -/* - * linux/include/linux/ext2_fs.h - * - * Copyright (C) 1992, 1993, 1994, 1995 - * Remy Card (card@xxxxxxxxxxx) - * Laboratoire MASI - Institut Blaise Pascal - * Universite Pierre et Marie Curie (Paris VI) - * - * from - * - * linux/include/linux/minix_fs.h - * - * Copyright (C) 1991, 1992 Linus Torvalds +/* + * linux/include/linux/ext2_fs.h + * + * Copyright (C) 1992, 1993, 1994, 1995 + * Remy Card (card@xxxxxxxxxxx) + * Laboratoire MASI - Institut Blaise Pascal + * Universite Pierre et Marie Curie (Paris VI) + * + * from + * + * linux/include/linux/minix_fs.h + * + * Copyright (C) 1991, 1992 Linus Torvalds */ #ifndef __FILESYSTEM_EXT2SUPERBLOCK_H