chfs.c revision 1.1 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.1 ttoth
45 1.1 ttoth #include "makefs.h"
46 1.1 ttoth #include "chfs_makefs.h"
47 1.1 ttoth
48 1.1 ttoth #include "chfs/chfs_mkfs.h"
49 1.1 ttoth
50 1.1 ttoth static void chfs_validate(const char *, fsnode *, fsinfo_t *);
51 1.1 ttoth static int chfs_create_image(const char *, fsinfo_t *);
52 1.1 ttoth static int chfs_populate_dir(const char *, fsnode *, fsnode *, fsinfo_t *);
53 1.1 ttoth
54 1.1 ttoth chfs_opt_t chfs_opts;
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.1 ttoth fsopts->size = 0;
60 1.1 ttoth fsopts->fs_specific = &chfs_opts;
61 1.1 ttoth
62 1.1 ttoth chfs_opts.pagesize = -1;
63 1.1 ttoth chfs_opts.eraseblock = -1;
64 1.1 ttoth chfs_opts.mediatype = -1;
65 1.1 ttoth }
66 1.1 ttoth
67 1.1 ttoth void
68 1.1 ttoth chfs_cleanup_opts(fsinfo_t *fsopts)
69 1.1 ttoth {
70 1.1 ttoth
71 1.1 ttoth }
72 1.1 ttoth
73 1.1 ttoth int
74 1.1 ttoth chfs_parse_opts(const char *option, fsinfo_t *fsopts)
75 1.1 ttoth {
76 1.1 ttoth static const option_t chfs_options[] = {
77 1.1 ttoth { "pagesize", &chfs_opts.pagesize, 1, INT_MAX, "page size" },
78 1.1 ttoth { "eraseblock", &chfs_opts.eraseblock, 1, INT_MAX, "eraseblock size" },
79 1.1 ttoth { "mediatype", &chfs_opts.mediatype, 0, 1,
80 1.1 ttoth "type of the media, 0 (nor) or 1 (nand)" },
81 1.1 ttoth { .name = NULL }
82 1.1 ttoth };
83 1.1 ttoth
84 1.1 ttoth char *var, *val;
85 1.1 ttoth int retval;
86 1.1 ttoth
87 1.1 ttoth assert(option != NULL);
88 1.1 ttoth assert(fsopts != NULL);
89 1.1 ttoth
90 1.1 ttoth if ((var = strdup(option)) == NULL) {
91 1.1 ttoth err(EXIT_FAILURE, "Allocating memory for copy of option string");
92 1.1 ttoth }
93 1.1 ttoth retval = 0;
94 1.1 ttoth
95 1.1 ttoth if ((val = strchr(var, '=')) == NULL) {
96 1.1 ttoth warnx("Option `%s' doesn't contain a value", var);
97 1.1 ttoth goto leave_chfs_parse_opts;
98 1.1 ttoth }
99 1.1 ttoth *val++ = '\0';
100 1.1 ttoth
101 1.1 ttoth retval = set_option(chfs_options, var, val);
102 1.1 ttoth
103 1.1 ttoth leave_chfs_parse_opts:
104 1.1 ttoth free(var);
105 1.1 ttoth return retval;
106 1.1 ttoth }
107 1.1 ttoth
108 1.1 ttoth void
109 1.1 ttoth chfs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
110 1.1 ttoth {
111 1.1 ttoth struct timeval start;
112 1.1 ttoth
113 1.1 ttoth assert(image != NULL);
114 1.1 ttoth assert(dir != NULL);
115 1.1 ttoth assert(root != NULL);
116 1.1 ttoth assert(fsopts != NULL);
117 1.1 ttoth
118 1.1 ttoth TIMER_START(start);
119 1.1 ttoth chfs_validate(dir, root, fsopts);
120 1.1 ttoth TIMER_RESULTS(start, "chfs_validate");
121 1.1 ttoth
122 1.1 ttoth printf("Creating `%s'\n", image);
123 1.1 ttoth TIMER_START(start);
124 1.1 ttoth if (chfs_create_image(image, fsopts) == -1) {
125 1.1 ttoth errx(EXIT_FAILURE, "Image file `%s' not created", image);
126 1.1 ttoth }
127 1.1 ttoth TIMER_RESULTS(start, "chfs_create_image");
128 1.1 ttoth
129 1.1 ttoth fsopts->curinode = CHFS_ROOTINO;
130 1.1 ttoth root->inode->ino = CHFS_ROOTINO;
131 1.1 ttoth
132 1.1 ttoth printf("Populating `%s'\n", image);
133 1.1 ttoth TIMER_START(start);
134 1.1 ttoth write_eb_header(fsopts);
135 1.1 ttoth if (!chfs_populate_dir(dir, root, root, fsopts)) {
136 1.1 ttoth errx(EXIT_FAILURE, "Image file `%s' not populated", image);
137 1.1 ttoth }
138 1.1 ttoth TIMER_RESULTS(start, "chfs_populate_dir");
139 1.1 ttoth
140 1.1 ttoth padblock(fsopts);
141 1.1 ttoth
142 1.1 ttoth if (close(fsopts->fd) == -1) {
143 1.1 ttoth err(EXIT_FAILURE, "Closing `%s'", image);
144 1.1 ttoth }
145 1.1 ttoth fsopts->fd = -1;
146 1.1 ttoth
147 1.1 ttoth printf("Image `%s' complete\n", image);
148 1.1 ttoth }
149 1.1 ttoth
150 1.1 ttoth static void
151 1.1 ttoth chfs_validate(const char* dir, fsnode *root, fsinfo_t *fsopts)
152 1.1 ttoth {
153 1.1 ttoth assert(dir != NULL);
154 1.1 ttoth assert(root != NULL);
155 1.1 ttoth assert(fsopts != NULL);
156 1.1 ttoth
157 1.1 ttoth if (chfs_opts.pagesize == -1) {
158 1.1 ttoth chfs_opts.pagesize = DEFAULT_PAGESIZE;
159 1.1 ttoth }
160 1.1 ttoth if (chfs_opts.eraseblock == -1) {
161 1.1 ttoth chfs_opts.eraseblock = DEFAULT_ERASEBLOCK;
162 1.1 ttoth }
163 1.1 ttoth if (chfs_opts.mediatype == -1) {
164 1.1 ttoth chfs_opts.mediatype = DEFAULT_MEDIATYPE;
165 1.1 ttoth }
166 1.1 ttoth }
167 1.1 ttoth
168 1.1 ttoth static int
169 1.1 ttoth chfs_create_image(const char *image, fsinfo_t *fsopts)
170 1.1 ttoth {
171 1.1 ttoth assert(image != NULL);
172 1.1 ttoth assert(fsopts != NULL);
173 1.1 ttoth
174 1.1 ttoth if ((fsopts->fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
175 1.1 ttoth warn("Can't open `%s' for writing", image);
176 1.1 ttoth return -1;
177 1.1 ttoth }
178 1.1 ttoth
179 1.1 ttoth return fsopts->fd;
180 1.1 ttoth }
181 1.1 ttoth
182 1.1 ttoth static int
183 1.1 ttoth chfs_populate_dir(const char *dir, fsnode *root, fsnode *parent,
184 1.1 ttoth fsinfo_t *fsopts)
185 1.1 ttoth {
186 1.1 ttoth fsnode *cur;
187 1.1 ttoth char path[MAXPATHLEN + 1];
188 1.1 ttoth
189 1.1 ttoth assert(dir != NULL);
190 1.1 ttoth assert(root != NULL);
191 1.1 ttoth assert(fsopts != NULL);
192 1.1 ttoth
193 1.1 ttoth for (cur = root->next; cur != NULL; cur = cur->next) {
194 1.1 ttoth if ((cur->inode->flags & FI_ALLOCATED) == 0) {
195 1.1 ttoth cur->inode->flags |= FI_ALLOCATED;
196 1.1 ttoth if (cur != root) {
197 1.1 ttoth fsopts->curinode++;
198 1.1 ttoth cur->inode->ino = fsopts->curinode;
199 1.1 ttoth cur->parent = parent;
200 1.1 ttoth }
201 1.1 ttoth }
202 1.1 ttoth
203 1.1 ttoth if (cur->inode->flags & FI_WRITTEN) {
204 1.1 ttoth continue; // hard link
205 1.1 ttoth }
206 1.1 ttoth cur->inode->flags |= FI_WRITTEN;
207 1.1 ttoth
208 1.1 ttoth write_vnode(fsopts, cur);
209 1.1 ttoth write_dirent(fsopts, cur);
210 1.1 ttoth if (!S_ISDIR(cur->type & S_IFMT)) {
211 1.1 ttoth write_file(fsopts, cur, dir);
212 1.1 ttoth }
213 1.1 ttoth }
214 1.1 ttoth
215 1.1 ttoth for (cur = root; cur != NULL; cur = cur->next) {
216 1.1 ttoth if (cur->child == NULL) {
217 1.1 ttoth continue;
218 1.1 ttoth }
219 1.1 ttoth if (snprintf(path, sizeof(path), "%s/%s", dir, cur->name)
220 1.1 ttoth >= sizeof(path)) {
221 1.1 ttoth errx(EXIT_FAILURE, "Pathname too long");
222 1.1 ttoth }
223 1.1 ttoth if (!chfs_populate_dir(path, cur->child, cur, fsopts)) {
224 1.1 ttoth return 0;
225 1.1 ttoth }
226 1.1 ttoth }
227 1.1 ttoth
228 1.1 ttoth return 1;
229 1.1 ttoth }
230 1.1 ttoth
231