etc/services - sync with NetBSD-8
[minix.git] / usr.sbin / makefs / msdos.c
blob5a7b10310caf57a845ecb364d8fdec338ebbf093
1 /* $NetBSD: msdos.c,v 1.14 2013/02/03 03:21:21 christos Exp $ */
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
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 * 3. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 #if HAVE_NBTOOL_CONFIG_H
35 #include "nbtool_config.h"
36 #endif
38 #include <sys/cdefs.h>
39 #if defined(__RCSID) && !defined(__lint)
40 __RCSID("$NetBSD: msdos.c,v 1.14 2013/02/03 03:21:21 christos Exp $");
41 #endif /* !__lint */
43 #include <sys/param.h>
45 #if !HAVE_NBTOOL_CONFIG_H
46 #include <sys/mount.h>
47 #endif
49 #include <assert.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <dirent.h>
58 #include <util.h>
60 #include <ffs/buf.h>
61 #include <fs/msdosfs/denode.h>
62 #include "makefs.h"
63 #include "msdos.h"
64 #include "mkfs_msdos.h"
66 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
67 fsnode *, fsinfo_t *);
69 void
70 msdos_prep_opts(fsinfo_t *fsopts)
72 struct msdos_options *msdos_opt = ecalloc(1, sizeof(*msdos_opt));
73 const option_t msdos_options[] = {
74 #define AOPT(_opt, _type, _name, _min, _desc) { \
75 .letter = _opt, \
76 .name = # _name, \
77 .type = _min == -1 ? OPT_STRPTR : \
78 (_min == -2 ? OPT_BOOL : \
79 (sizeof(_type) == 1 ? OPT_INT8 : \
80 (sizeof(_type) == 2 ? OPT_INT16 : \
81 (sizeof(_type) == 4 ? OPT_INT32 : OPT_INT64)))), \
82 .value = &msdos_opt->_name, \
83 .minimum = _min, \
84 .maximum = sizeof(_type) == 1 ? 0xff : \
85 (sizeof(_type) == 2 ? 0xffff : \
86 (sizeof(_type) == 4 ? 0xffffffff : 0xffffffffffffffffLL)), \
87 .desc = _desc, \
89 ALLOPTS
90 #undef AOPT
91 { .name = NULL }
94 fsopts->fs_specific = msdos_opt;
95 fsopts->fs_options = copy_opts(msdos_options);
98 void
99 msdos_cleanup_opts(fsinfo_t *fsopts)
101 free(fsopts->fs_specific);
102 free(fsopts->fs_options);
106 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
108 struct msdos_options *msdos_opt = fsopts->fs_specific;
109 option_t *msdos_options = fsopts->fs_options;
111 int rv;
113 assert(option != NULL);
114 assert(fsopts != NULL);
115 assert(msdos_opt != NULL);
117 if (debug & DEBUG_FS_PARSE_OPTS)
118 printf("msdos_parse_opts: got `%s'\n", option);
120 rv = set_option(msdos_options, option, NULL, 0);
121 if (rv == -1)
122 return rv;
124 if (strcmp(msdos_options[rv].name, "volume_id") == 0)
125 msdos_opt->volume_id_set = 1;
126 else if (strcmp(msdos_options[rv].name, "media_descriptor") == 0)
127 msdos_opt->media_descriptor_set = 1;
128 else if (strcmp(msdos_options[rv].name, "hidden_sectors") == 0)
129 msdos_opt->hidden_sectors_set = 1;
130 return 1;
134 void
135 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
137 struct msdos_options *msdos_opt = fsopts->fs_specific;
138 struct vnode vp, rootvp;
139 struct timeval start;
140 struct msdosfsmount *pmp;
142 assert(image != NULL);
143 assert(dir != NULL);
144 assert(root != NULL);
145 assert(fsopts != NULL);
148 * XXX: pick up other options from the msdos specific ones?
149 * Is minsize right here?
151 msdos_opt->create_size = MAX(msdos_opt->create_size, fsopts->minsize);
152 msdos_opt->offset = fsopts->offset;
153 if (msdos_opt->bytes_per_sector == 0) {
154 if (fsopts->sectorsize == -1)
155 fsopts->sectorsize = 512;
156 msdos_opt->bytes_per_sector = fsopts->sectorsize;
157 } else if (fsopts->sectorsize == -1) {
158 fsopts->sectorsize = msdos_opt->bytes_per_sector;
159 } else if (fsopts->sectorsize != msdos_opt->bytes_per_sector) {
160 err(1, "inconsistent sectorsize -S %u"
161 "!= -o bytes_per_sector %u",
162 fsopts->sectorsize, msdos_opt->bytes_per_sector);
165 /* create image */
166 printf("Creating `%s'\n", image);
167 TIMER_START(start);
168 if (mkfs_msdos(image, NULL, msdos_opt) == -1)
169 return;
170 TIMER_RESULTS(start, "mkfs_msdos");
172 fsopts->fd = open(image, O_RDWR);
173 vp.fs = fsopts;
175 if ((pmp = msdosfs_mount(&vp, 0)) == NULL)
176 err(1, "msdosfs_mount");
178 if (msdosfs_root(pmp, &rootvp) != 0)
179 err(1, "msdosfs_root");
181 if (debug & DEBUG_FS_MAKEFS)
182 printf("msdos_makefs: image %s directory %s root %p\n",
183 image, dir, root);
185 /* populate image */
186 printf("Populating `%s'\n", image);
187 TIMER_START(start);
188 if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
189 errx(1, "Image file `%s' not created.", image);
190 TIMER_RESULTS(start, "msdos_populate_dir");
192 if (debug & DEBUG_FS_MAKEFS)
193 putchar('\n');
195 /* ensure no outstanding buffers remain */
196 if (debug & DEBUG_FS_MAKEFS)
197 bcleanup();
199 printf("Image `%s' complete\n", image);
202 static int
203 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
204 fsnode *parent, fsinfo_t *fsopts)
206 fsnode *cur;
207 char pbuf[MAXPATHLEN];
209 assert(dir != NULL);
210 assert(root != NULL);
211 assert(fsopts != NULL);
213 for (cur = root->next; cur != NULL; cur = cur->next) {
214 if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
215 cur->name) >= sizeof(pbuf)) {
216 warnx("path %s too long", pbuf);
217 return -1;
220 if ((cur->inode->flags & FI_ALLOCATED) == 0) {
221 cur->inode->flags |= FI_ALLOCATED;
222 if (cur != root) {
223 fsopts->curinode++;
224 cur->inode->ino = fsopts->curinode;
225 cur->parent = parent;
229 if (cur->inode->flags & FI_WRITTEN) {
230 continue; // hard link
232 cur->inode->flags |= FI_WRITTEN;
234 if (cur->child) {
235 struct denode *de;
236 if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
237 warn("msdosfs_mkdire %s", pbuf);
238 return -1;
240 if (msdos_populate_dir(pbuf, de, cur->child, cur,
241 fsopts) == -1) {
242 warn("msdos_populate_dir %s", pbuf);
243 return -1;
245 continue;
246 } else if (!S_ISREG(cur->type)) {
247 warnx("skipping non-regular file %s/%s", cur->path,
248 cur->name);
249 continue;
251 if (msdosfs_mkfile(pbuf, dir, cur) == NULL) {
252 warn("msdosfs_mkfile %s", pbuf);
253 return -1;
256 return 0;