xref: /netbsd-src/usr.sbin/makefs/chfs.c (revision bf2ad7248906fbda5c0a68bac7ee290dcd2d2b38)
10751a667Sttoth /*-
20751a667Sttoth  * Copyright (c) 2012 Department of Software Engineering,
30751a667Sttoth  *		      University of Szeged, Hungary
40751a667Sttoth  * Copyright (c) 2012 Tamas Toth <ttoth@inf.u-szeged.hu>
50751a667Sttoth  * All rights reserved.
60751a667Sttoth  *
70751a667Sttoth  * This code is derived from software contributed to The NetBSD Foundation
80751a667Sttoth  * by the Department of Software Engineering, University of Szeged, Hungary
90751a667Sttoth  *
100751a667Sttoth  * Redistribution and use in source and binary forms, with or without
110751a667Sttoth  * modification, are permitted provided that the following conditions
120751a667Sttoth  * are met:
130751a667Sttoth  * 1. Redistributions of source code must retain the above copyright
140751a667Sttoth  *    notice, this list of conditions and the following disclaimer.
150751a667Sttoth  * 2. Redistributions in binary form must reproduce the above copyright
160751a667Sttoth  *    notice, this list of conditions and the following disclaimer in the
170751a667Sttoth  *    documentation and/or other materials provided with the distribution.
180751a667Sttoth  *
190751a667Sttoth  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
200751a667Sttoth  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
210751a667Sttoth  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
220751a667Sttoth  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
230751a667Sttoth  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
240751a667Sttoth  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
250751a667Sttoth  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
260751a667Sttoth  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
270751a667Sttoth  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280751a667Sttoth  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290751a667Sttoth  * SUCH DAMAGE.
300751a667Sttoth  */
310751a667Sttoth 
320751a667Sttoth #if HAVE_NBTOOL_CONFIG_H
330751a667Sttoth #include "nbtool_config.h"
340751a667Sttoth #endif
350751a667Sttoth 
360751a667Sttoth #include <sys/param.h>
370751a667Sttoth 
380751a667Sttoth #include <assert.h>
390751a667Sttoth #include <fcntl.h>
400751a667Sttoth #include <stdio.h>
410751a667Sttoth #include <stdlib.h>
420751a667Sttoth #include <string.h>
430751a667Sttoth #include <unistd.h>
44e4989541Schristos #include <util.h>
450751a667Sttoth 
460751a667Sttoth #include "makefs.h"
470751a667Sttoth #include "chfs_makefs.h"
480751a667Sttoth 
490751a667Sttoth #include "chfs/chfs_mkfs.h"
500751a667Sttoth 
510751a667Sttoth static void chfs_validate(const char *, fsnode *, fsinfo_t *);
520751a667Sttoth static int chfs_create_image(const char *, fsinfo_t *);
530751a667Sttoth static int chfs_populate_dir(const char *, fsnode *, fsnode *, fsinfo_t *);
540751a667Sttoth 
550751a667Sttoth 
560751a667Sttoth void
chfs_prep_opts(fsinfo_t * fsopts)570751a667Sttoth chfs_prep_opts(fsinfo_t *fsopts)
580751a667Sttoth {
59e4989541Schristos 	chfs_opt_t *chfs_opts = ecalloc(1, sizeof(*chfs_opts));
600751a667Sttoth 
61e4989541Schristos 	const option_t chfs_options[] = {
62e4989541Schristos 		{ 'p', "pagesize", &chfs_opts->pagesize, OPT_INT32,
63e4989541Schristos 		  1, INT_MAX, "page size" },
64e4989541Schristos 		{ 'e', "eraseblock", &chfs_opts->eraseblock, OPT_INT32,
65e4989541Schristos 		  1, INT_MAX, "eraseblock size" },
66e4989541Schristos 		{ 'm', "mediatype", &chfs_opts->mediatype, OPT_INT32,
67e4989541Schristos 		  0, 1, "type of the media, 0 (nor) or 1 (nand)" },
68e4989541Schristos 		{ .name = NULL }
69e4989541Schristos 	};
70e4989541Schristos 
71e4989541Schristos 	chfs_opts->pagesize = -1;
72e4989541Schristos 	chfs_opts->eraseblock = -1;
73e4989541Schristos 	chfs_opts->mediatype = -1;
74e4989541Schristos 
75e4989541Schristos 	fsopts->size = 0;
76e4989541Schristos 	fsopts->fs_specific = chfs_opts;
77e4989541Schristos 	fsopts->fs_options = copy_opts(chfs_options);
780751a667Sttoth }
790751a667Sttoth 
800751a667Sttoth void
chfs_cleanup_opts(fsinfo_t * fsopts)810751a667Sttoth chfs_cleanup_opts(fsinfo_t *fsopts)
820751a667Sttoth {
83e4989541Schristos 	free(fsopts->fs_specific);
84e4989541Schristos 	free(fsopts->fs_options);
850751a667Sttoth }
860751a667Sttoth 
870751a667Sttoth int
chfs_parse_opts(const char * option,fsinfo_t * fsopts)880751a667Sttoth chfs_parse_opts(const char *option, fsinfo_t *fsopts)
890751a667Sttoth {
900751a667Sttoth 
910751a667Sttoth 	assert(option != NULL);
920751a667Sttoth 	assert(fsopts != NULL);
930751a667Sttoth 
94*50d02345Schristos 	return set_option(fsopts->fs_options, option, NULL, 0) != -1;
950751a667Sttoth }
960751a667Sttoth 
970751a667Sttoth void
chfs_makefs(const char * image,const char * dir,fsnode * root,fsinfo_t * fsopts)980751a667Sttoth chfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
990751a667Sttoth {
1000751a667Sttoth 	struct timeval	start;
1010751a667Sttoth 
1020751a667Sttoth 	assert(image != NULL);
1030751a667Sttoth 	assert(dir != NULL);
1040751a667Sttoth 	assert(root != NULL);
1050751a667Sttoth 	assert(fsopts != NULL);
1060751a667Sttoth 
1070751a667Sttoth 	TIMER_START(start);
1080751a667Sttoth 	chfs_validate(dir, root, fsopts);
1090751a667Sttoth 	TIMER_RESULTS(start, "chfs_validate");
1100751a667Sttoth 
1110751a667Sttoth 	printf("Creating `%s'\n", image);
1120751a667Sttoth 	TIMER_START(start);
1130751a667Sttoth 	if (chfs_create_image(image, fsopts) == -1) {
1140751a667Sttoth 		errx(EXIT_FAILURE, "Image file `%s' not created", image);
1150751a667Sttoth 	}
1160751a667Sttoth 	TIMER_RESULTS(start, "chfs_create_image");
1170751a667Sttoth 
1180751a667Sttoth 	fsopts->curinode = CHFS_ROOTINO;
1190751a667Sttoth 	root->inode->ino = CHFS_ROOTINO;
1200751a667Sttoth 
1210751a667Sttoth 	printf("Populating `%s'\n", image);
1220751a667Sttoth 	TIMER_START(start);
1230751a667Sttoth 	write_eb_header(fsopts);
1240751a667Sttoth 	if (!chfs_populate_dir(dir, root, root, fsopts)) {
1250751a667Sttoth 		errx(EXIT_FAILURE, "Image file `%s' not populated", image);
1260751a667Sttoth 	}
1270751a667Sttoth 	TIMER_RESULTS(start, "chfs_populate_dir");
1280751a667Sttoth 
1290751a667Sttoth 	padblock(fsopts);
1300751a667Sttoth 
1310751a667Sttoth 	if (close(fsopts->fd) == -1) {
1320751a667Sttoth 		err(EXIT_FAILURE, "Closing `%s'", image);
1330751a667Sttoth 	}
1340751a667Sttoth 	fsopts->fd = -1;
1350751a667Sttoth 
1360751a667Sttoth 	printf("Image `%s' complete\n", image);
1370751a667Sttoth }
1380751a667Sttoth 
1390751a667Sttoth static void
chfs_validate(const char * dir,fsnode * root,fsinfo_t * fsopts)1400751a667Sttoth chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
1410751a667Sttoth {
142e4989541Schristos 	chfs_opt_t *chfs_opts;
1430751a667Sttoth 	assert(dir != NULL);
1440751a667Sttoth 	assert(root != NULL);
1450751a667Sttoth 	assert(fsopts != NULL);
1460751a667Sttoth 
147e4989541Schristos 	chfs_opts = fsopts->fs_specific;
148e4989541Schristos 
149e4989541Schristos 	if (chfs_opts->pagesize == -1) {
150e4989541Schristos 		chfs_opts->pagesize = DEFAULT_PAGESIZE;
1510751a667Sttoth 	}
152e4989541Schristos 	if (chfs_opts->eraseblock == -1) {
153e4989541Schristos 		chfs_opts->eraseblock = DEFAULT_ERASEBLOCK;
1540751a667Sttoth 	}
155e4989541Schristos 	if (chfs_opts->mediatype == -1) {
156e4989541Schristos 		chfs_opts->mediatype = DEFAULT_MEDIATYPE;
1570751a667Sttoth 	}
1580751a667Sttoth }
1590751a667Sttoth 
1600751a667Sttoth static int
chfs_create_image(const char * image,fsinfo_t * fsopts)1610751a667Sttoth chfs_create_image(const char *image, fsinfo_t *fsopts)
1620751a667Sttoth {
1630751a667Sttoth 	assert(image != NULL);
1640751a667Sttoth 	assert(fsopts != NULL);
1650751a667Sttoth 
1660751a667Sttoth 	if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
1670751a667Sttoth 		warn("Can't open `%s' for writing", image);
1680751a667Sttoth 		return -1;
1690751a667Sttoth 	}
1700751a667Sttoth 
1710751a667Sttoth 	return fsopts->fd;
1720751a667Sttoth }
1730751a667Sttoth 
1740751a667Sttoth static int
chfs_populate_dir(const char * dir,fsnode * root,fsnode * parent,fsinfo_t * fsopts)1750751a667Sttoth chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent,
1760751a667Sttoth     fsinfo_t *fsopts)
1770751a667Sttoth {
1780751a667Sttoth 	fsnode *cur;
1790751a667Sttoth 	char path[MAXPATHLEN + 1];
1800751a667Sttoth 
1810751a667Sttoth 	assert(dir != NULL);
1820751a667Sttoth 	assert(root != NULL);
1830751a667Sttoth 	assert(fsopts != NULL);
1840751a667Sttoth 
1850751a667Sttoth 	for (cur = root->next; cur != NULL; cur = cur->next) {
1860751a667Sttoth 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
1870751a667Sttoth 			cur->inode->flags |= FI_ALLOCATED;
1880751a667Sttoth 			if (cur != root) {
1890751a667Sttoth 				fsopts->curinode++;
1900751a667Sttoth 				cur->inode->ino = fsopts->curinode;
1910751a667Sttoth 				cur->parent = parent;
1920751a667Sttoth 			}
1930751a667Sttoth 		}
1940751a667Sttoth 
1950751a667Sttoth 		if (cur->inode->flags & FI_WRITTEN) {
1960751a667Sttoth 			continue;	// hard link
1970751a667Sttoth 		}
1980751a667Sttoth 		cur->inode->flags |= FI_WRITTEN;
1990751a667Sttoth 
2000751a667Sttoth 		write_vnode(fsopts, cur);
2010751a667Sttoth 		write_dirent(fsopts, cur);
2020751a667Sttoth 		if (!S_ISDIR(cur->type & S_IFMT)) {
2030751a667Sttoth 			write_file(fsopts, cur, dir);
2040751a667Sttoth 		}
2050751a667Sttoth 	}
2060751a667Sttoth 
2070751a667Sttoth 	for (cur = root; cur != NULL; cur = cur->next) {
2080751a667Sttoth 		if (cur->child == NULL) {
2090751a667Sttoth 			continue;
2100751a667Sttoth 		}
211b825b96bSchristos 		if ((size_t)snprintf(path, sizeof(path), "%s/%s", dir,
212b825b96bSchristos 		    cur->name) >= sizeof(path)) {
2130751a667Sttoth 			errx(EXIT_FAILURE, "Pathname too long");
2140751a667Sttoth 		}
2150751a667Sttoth 		if (!chfs_populate_dir(path, cur->child, cur, fsopts)) {
2160751a667Sttoth 			return 0;
2170751a667Sttoth 		}
2180751a667Sttoth 	}
2190751a667Sttoth 
2200751a667Sttoth 	return 1;
2210751a667Sttoth }
222