Home | History | Annotate | Line # | Download | only in makefs
      1  1.1     ttoth /*-
      2  1.1     ttoth  * Copyright (c) 2012 Department of Software Engineering,
      3  1.1     ttoth  *		      University of Szeged, Hungary
      4  1.1     ttoth  * Copyright (c) 2012 Tamas Toth <ttoth (at) inf.u-szeged.hu>
      5  1.1     ttoth  * All rights reserved.
      6  1.1     ttoth  *
      7  1.1     ttoth  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     ttoth  * by the Department of Software Engineering, University of Szeged, Hungary
      9  1.1     ttoth  *
     10  1.1     ttoth  * Redistribution and use in source and binary forms, with or without
     11  1.1     ttoth  * modification, are permitted provided that the following conditions
     12  1.1     ttoth  * are met:
     13  1.1     ttoth  * 1. Redistributions of source code must retain the above copyright
     14  1.1     ttoth  *    notice, this list of conditions and the following disclaimer.
     15  1.1     ttoth  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     ttoth  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     ttoth  *    documentation and/or other materials provided with the distribution.
     18  1.1     ttoth  *
     19  1.1     ttoth  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1     ttoth  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1     ttoth  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1     ttoth  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1     ttoth  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  1.1     ttoth  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  1.1     ttoth  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  1.1     ttoth  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  1.1     ttoth  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1     ttoth  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1     ttoth  * SUCH DAMAGE.
     30  1.1     ttoth  */
     31  1.1     ttoth 
     32  1.1     ttoth #if HAVE_NBTOOL_CONFIG_H
     33  1.1     ttoth #include "nbtool_config.h"
     34  1.1     ttoth #endif
     35  1.1     ttoth 
     36  1.1     ttoth #include <sys/param.h>
     37  1.1     ttoth 
     38  1.1     ttoth #include <assert.h>
     39  1.1     ttoth #include <fcntl.h>
     40  1.1     ttoth #include <stdio.h>
     41  1.1     ttoth #include <stdlib.h>
     42  1.1     ttoth #include <string.h>
     43  1.1     ttoth #include <unistd.h>
     44  1.7  christos #include <util.h>
     45  1.1     ttoth 
     46  1.1     ttoth #include "makefs.h"
     47  1.1     ttoth #include "chfs_makefs.h"
     48  1.1     ttoth 
     49  1.1     ttoth #include "chfs/chfs_mkfs.h"
     50  1.1     ttoth 
     51  1.1     ttoth static void chfs_validate(const char *, fsnode *, fsinfo_t *);
     52  1.1     ttoth static int chfs_create_image(const char *, fsinfo_t *);
     53  1.1     ttoth static int chfs_populate_dir(const char *, fsnode *, fsnode *, fsinfo_t *);
     54  1.1     ttoth 
     55  1.1     ttoth 
     56  1.1     ttoth void
     57  1.1     ttoth chfs_prep_opts(fsinfo_t *fsopts)
     58  1.1     ttoth {
     59  1.7  christos 	chfs_opt_t *chfs_opts = ecalloc(1, sizeof(*chfs_opts));
     60  1.7  christos 
     61  1.7  christos 	const option_t chfs_options[] = {
     62  1.7  christos 		{ 'p', "pagesize", &chfs_opts->pagesize, OPT_INT32,
     63  1.7  christos 		  1, INT_MAX, "page size" },
     64  1.7  christos 		{ 'e', "eraseblock", &chfs_opts->eraseblock, OPT_INT32,
     65  1.7  christos 		  1, INT_MAX, "eraseblock size" },
     66  1.7  christos 		{ 'm', "mediatype", &chfs_opts->mediatype, OPT_INT32,
     67  1.7  christos 		  0, 1, "type of the media, 0 (nor) or 1 (nand)" },
     68  1.7  christos 		{ .name = NULL }
     69  1.7  christos 	};
     70  1.7  christos 
     71  1.7  christos 	chfs_opts->pagesize = -1;
     72  1.7  christos 	chfs_opts->eraseblock = -1;
     73  1.7  christos 	chfs_opts->mediatype = -1;
     74  1.7  christos 
     75  1.1     ttoth 	fsopts->size = 0;
     76  1.7  christos 	fsopts->fs_specific = chfs_opts;
     77  1.7  christos 	fsopts->fs_options = copy_opts(chfs_options);
     78  1.1     ttoth }
     79  1.1     ttoth 
     80  1.1     ttoth void
     81  1.1     ttoth chfs_cleanup_opts(fsinfo_t *fsopts)
     82  1.1     ttoth {
     83  1.7  christos 	free(fsopts->fs_specific);
     84  1.7  christos 	free(fsopts->fs_options);
     85  1.1     ttoth }
     86  1.1     ttoth 
     87  1.1     ttoth int
     88  1.1     ttoth chfs_parse_opts(const char *option, fsinfo_t *fsopts)
     89  1.1     ttoth {
     90  1.1     ttoth 
     91  1.1     ttoth 	assert(option != NULL);
     92  1.1     ttoth 	assert(fsopts != NULL);
     93  1.1     ttoth 
     94  1.8  christos 	return set_option(fsopts->fs_options, option, NULL, 0) != -1;
     95  1.1     ttoth }
     96  1.1     ttoth 
     97  1.1     ttoth void
     98  1.1     ttoth chfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
     99  1.1     ttoth {
    100  1.1     ttoth 	struct timeval	start;
    101  1.1     ttoth 
    102  1.1     ttoth 	assert(image != NULL);
    103  1.1     ttoth 	assert(dir != NULL);
    104  1.1     ttoth 	assert(root != NULL);
    105  1.9  riastrad 	assert(fsopts != NULL);
    106  1.1     ttoth 
    107  1.1     ttoth 	TIMER_START(start);
    108  1.1     ttoth 	chfs_validate(dir, root, fsopts);
    109  1.1     ttoth 	TIMER_RESULTS(start, "chfs_validate");
    110  1.1     ttoth 
    111  1.1     ttoth 	printf("Creating `%s'\n", image);
    112  1.1     ttoth 	TIMER_START(start);
    113  1.1     ttoth 	if (chfs_create_image(image, fsopts) == -1) {
    114  1.1     ttoth 		errx(EXIT_FAILURE, "Image file `%s' not created", image);
    115  1.1     ttoth 	}
    116  1.1     ttoth 	TIMER_RESULTS(start, "chfs_create_image");
    117  1.9  riastrad 
    118  1.1     ttoth 	fsopts->curinode = CHFS_ROOTINO;
    119  1.1     ttoth 	root->inode->ino = CHFS_ROOTINO;
    120  1.1     ttoth 
    121  1.1     ttoth 	printf("Populating `%s'\n", image);
    122  1.1     ttoth 	TIMER_START(start);
    123  1.1     ttoth 	write_eb_header(fsopts);
    124  1.1     ttoth 	if (!chfs_populate_dir(dir, root, root, fsopts)) {
    125  1.1     ttoth 		errx(EXIT_FAILURE, "Image file `%s' not populated", image);
    126  1.1     ttoth 	}
    127  1.1     ttoth 	TIMER_RESULTS(start, "chfs_populate_dir");
    128  1.1     ttoth 
    129  1.1     ttoth 	padblock(fsopts);
    130  1.1     ttoth 
    131  1.1     ttoth 	if (close(fsopts->fd) == -1) {
    132  1.1     ttoth 		err(EXIT_FAILURE, "Closing `%s'", image);
    133  1.1     ttoth 	}
    134  1.1     ttoth 	fsopts->fd = -1;
    135  1.1     ttoth 
    136  1.1     ttoth 	printf("Image `%s' complete\n", image);
    137  1.1     ttoth }
    138  1.1     ttoth 
    139  1.1     ttoth static void
    140  1.1     ttoth chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
    141  1.1     ttoth {
    142  1.7  christos 	chfs_opt_t *chfs_opts;
    143  1.1     ttoth 	assert(dir != NULL);
    144  1.1     ttoth 	assert(root != NULL);
    145  1.1     ttoth 	assert(fsopts != NULL);
    146  1.1     ttoth 
    147  1.7  christos 	chfs_opts = fsopts->fs_specific;
    148  1.7  christos 
    149  1.7  christos 	if (chfs_opts->pagesize == -1) {
    150  1.7  christos 		chfs_opts->pagesize = DEFAULT_PAGESIZE;
    151  1.1     ttoth 	}
    152  1.7  christos 	if (chfs_opts->eraseblock == -1) {
    153  1.7  christos 		chfs_opts->eraseblock = DEFAULT_ERASEBLOCK;
    154  1.1     ttoth 	}
    155  1.7  christos 	if (chfs_opts->mediatype == -1) {
    156  1.7  christos 		chfs_opts->mediatype = DEFAULT_MEDIATYPE;
    157  1.1     ttoth 	}
    158  1.1     ttoth }
    159  1.1     ttoth 
    160  1.1     ttoth static int
    161  1.1     ttoth chfs_create_image(const char *image, fsinfo_t *fsopts)
    162  1.1     ttoth {
    163  1.1     ttoth 	assert(image != NULL);
    164  1.1     ttoth 	assert(fsopts != NULL);
    165  1.9  riastrad 
    166  1.1     ttoth 	if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
    167  1.1     ttoth 		warn("Can't open `%s' for writing", image);
    168  1.1     ttoth 		return -1;
    169  1.1     ttoth 	}
    170  1.1     ttoth 
    171  1.1     ttoth 	return fsopts->fd;
    172  1.1     ttoth }
    173  1.1     ttoth 
    174  1.1     ttoth static int
    175  1.1     ttoth chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent,
    176  1.1     ttoth     fsinfo_t *fsopts)
    177  1.1     ttoth {
    178  1.1     ttoth 	fsnode *cur;
    179  1.1     ttoth 	char path[MAXPATHLEN + 1];
    180  1.1     ttoth 
    181  1.1     ttoth 	assert(dir != NULL);
    182  1.1     ttoth 	assert(root != NULL);
    183  1.9  riastrad 	assert(fsopts != NULL);
    184  1.9  riastrad 
    185  1.1     ttoth 	for (cur = root->next; cur != NULL; cur = cur->next) {
    186  1.1     ttoth 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
    187  1.1     ttoth 			cur->inode->flags |= FI_ALLOCATED;
    188  1.1     ttoth 			if (cur != root) {
    189  1.1     ttoth 				fsopts->curinode++;
    190  1.1     ttoth 				cur->inode->ino = fsopts->curinode;
    191  1.1     ttoth 				cur->parent = parent;
    192  1.1     ttoth 			}
    193  1.1     ttoth 		}
    194  1.1     ttoth 
    195  1.1     ttoth 		if (cur->inode->flags & FI_WRITTEN) {
    196  1.1     ttoth 			continue;	// hard link
    197  1.1     ttoth 		}
    198  1.1     ttoth 		cur->inode->flags |= FI_WRITTEN;
    199  1.1     ttoth 
    200  1.1     ttoth 		write_vnode(fsopts, cur);
    201  1.1     ttoth 		write_dirent(fsopts, cur);
    202  1.1     ttoth 		if (!S_ISDIR(cur->type & S_IFMT)) {
    203  1.1     ttoth 			write_file(fsopts, cur, dir);
    204  1.1     ttoth 		}
    205  1.1     ttoth 	}
    206  1.9  riastrad 
    207  1.1     ttoth 	for (cur = root; cur != NULL; cur = cur->next) {
    208  1.1     ttoth 		if (cur->child == NULL) {
    209  1.1     ttoth 			continue;
    210  1.1     ttoth 		}
    211  1.3  christos 		if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
    212  1.3  christos 		    cur->name) >= sizeof(path)) {
    213  1.1     ttoth 			errx(EXIT_FAILURE, "Pathname too long");
    214  1.1     ttoth 		}
    215  1.1     ttoth 		if (!chfs_populate_dir(path, cur->child, cur, fsopts)) {
    216  1.1     ttoth 			return 0;
    217  1.1     ttoth 		}
    218  1.1     ttoth 	}
    219  1.1     ttoth 
    220  1.1     ttoth 	return 1;
    221  1.1     ttoth }
    222