Author: nieklinnenbank Date: Tue Jun 30 12:15:26 2009 New Revision: 172 Modified: trunk/srv/filesystem/ext2/Ext2Create.cpp trunk/srv/filesystem/ext2/Ext2Create.h Log: Ext2Create is now able to procude a list containing all input files. Additionally, Ext2Create::writeImage() is now used to write the output file. Modified: trunk/srv/filesystem/ext2/Ext2Create.cpp ============================================================================== --- trunk/srv/filesystem/ext2/Ext2Create.cpp (original) +++ trunk/srv/filesystem/ext2/Ext2Create.cpp Tue Jun 30 12:15:26 2009 @@ -25,8 +25,11 @@ #include "Ext2Create.h" #include <stdio.h> #include <stdlib.h> +#include <dirent.h> #include <string.h> #include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> Ext2Create::Ext2Create() { @@ -43,14 +46,100 @@ int Ext2Create::create() { - FILE *fp; - assert(image != ZERO); assert(prog != ZERO); /* Allocate and initialize the superblock. */ super = initSuperBlock(); + /* Write the final image. */ + return writeImage(); +} ++Ext2InputFile * Ext2Create::addInputFile(char *inputFile, Ext2InputFile *parent)
+{ + Ext2InputFile *file; + struct stat st; + + /* Stat the input file. */ + if (stat(inputFile, &st) != 0) + { + printf("%s: failed to stat() `%s': %s\r\n", + prog, inputFile, strerror(errno)); + exit(EXIT_FAILURE); + } + /* Create new input file. */ + file = new Ext2InputFile; + file->mode = st.st_mode; + file->size = st.st_size; + file->userId = st.st_uid; + file->groupId = st.st_gid; + strncpy(file->name, inputFile, EXT2_NAME_LEN); + file->name[EXT2_NAME_LEN - 1] = 0; + + /* Debug out. */ + printf("%s mode=%x size=%lu userId=%u groupId=%u\r\n", + file->name, file->mode, file->size, + file->userId, file->groupId); + + /* Add it to the parent, or make it root. */ + if (parent) + parent->childs.insertTail(file); + else + inputRoot = file; + + /* All done. */ + return file; +} + +int Ext2Create::readInput(char *directory, Ext2InputFile *parent) +{ + DIR *dir; + Ext2InputFile *file; + struct dirent *entry; + char path[EXT2_NAME_LEN]; + + /* Open the local directory. */ + if ((dir = opendir(directory)) == NULL) + { + return EXIT_FAILURE; + } + /* Add all entries. */ + while ((entry = readdir(dir)) != NULL) + { + /* Skip hidden. */ + if (entry->d_name[0] != '.') + { + snprintf(path, sizeof(path), "%s/%s", directory, entry->d_name); + file = addInputFile(path, parent); + + /* Traverse it in case of a directory. */ + if (S_ISDIR(file->mode)) + { + readInput(path, file); + } + } + } + /* Cleanup. */ + closedir(dir); + + /* Done. */ + return EXIT_SUCCESS; +} + +int Ext2Create::writeImage() +{ + FILE *fp; + + assert(image != ZERO); + assert(prog != ZERO); + + /* Construct the input root. */ + addInputFile(input, ZERO); + + /* Add the input directory contents. */ + readInput(input, inputRoot); + /* Open output image file. */ if ((fp = fopen(image, "w")) == NULL) { @@ -78,7 +167,7 @@ /* All done. */ return EXIT_SUCCESS; -} +} void Ext2Create::setProgram(char *progName) { @@ -90,6 +179,11 @@ this->image = imageName; } +void Ext2Create::setInput(char *inputName) +{ + this->input = inputName; +} + Ext2SuperBlock * Ext2Create::initSuperBlock() { Ext2SuperBlock *sb = new Ext2SuperBlock; @@ -141,6 +235,7 @@ /* Process command-line arguments. */ fs.setProgram(argv[0]); fs.setImage(argv[1]); + fs.setInput(argv[2]); /* Create a new Extended 2 FileSystem. */ return fs.create(); Modified: trunk/srv/filesystem/ext2/Ext2Create.h ============================================================================== --- trunk/srv/filesystem/ext2/Ext2Create.h (original) +++ trunk/srv/filesystem/ext2/Ext2Create.h Tue Jun 30 12:15:26 2009 @@ -29,6 +29,32 @@ #include <stdlib.h> #include <string.h> #include <errno.h> +#include <sys/types.h> + +/** + * Describes an input file to be placed on the new filesystem. + */ +typedef struct Ext2InputFile +{ + /** Name of the file. */ + char name[EXT2_NAME_LEN]; + + /** Mode of the file. */ + mode_t mode; + + /** Size of the file. */ + size_t size; + + /** User identity. */ + uid_t userId; + + /** Group identity. */ + gid_t groupId; + + /** List of childs. */ + List<Ext2InputFile> childs; +} +Ext2InputFile; /** * Class for creating new Extended 2 FileSystems. @@ -49,6 +75,20 @@ int create(); /** + * Traverse the input directory. + * @param directory Path of the local directory to traverse. + * @param parent Parent directory or ZERO if none. + * @return EXIT_SUCCESS if successfull and EXIT_FAILURE otherwise. + */ + int readInput(char *directory, Ext2InputFile *parent); + + /** + * Writes the final image to disk. + * @return EXIT_SUCCESS if successfull and EXIT_FAILURE otherwise. + */ + int writeImage(); + + /** * Set the program name we are invoked with. * @param progName program name. */ @@ -60,7 +100,21 @@ */ void setImage(char *imageName); + /** + * Set the input directory name. + * @param inputName Input directory to use. + */ + void setInput(char *inputName); + private: + + /** + * Adds the given input file to it's parent. + * @param inputFile Path of the local file to add. + * @param parent Pointer to an Ext2InputFile. + * @return Pointer to the newly created Ext2InputFile. + */ + Ext2InputFile * addInputFile(char *inputFile, Ext2InputFile *parent); /** * Allocate and initialize a superblock. @@ -73,8 +127,14 @@ /** Path to the output image. */ char *image; + /** Path to the input directory. */ + char *input; + /** Pointer to the superblock. */ Ext2SuperBlock *super; + + /** Contains all files to be written into the new filesystem. */ + Ext2InputFile *inputRoot; /** List of file patterns to ignore. */ List<String> excludes;