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