Home | History | Annotate | Line # | Download | only in makefs
msdos.c revision 1.12
      1 /*	$NetBSD: msdos.c,v 1.12 2013/01/30 17:29:25 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Christos Zoulas.
      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  * 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.
     21  *
     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.
     33  */
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(__lint)
     40 __RCSID("$NetBSD: msdos.c,v 1.12 2013/01/30 17:29:25 christos Exp $");
     41 #endif	/* !__lint */
     42 
     43 #include <sys/param.h>
     44 
     45 #if !HAVE_NBTOOL_CONFIG_H
     46 #include <sys/mount.h>
     47 #endif
     48 
     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>
     59 
     60 #include <ffs/buf.h>
     61 #include <fs/msdosfs/denode.h>
     62 #include "makefs.h"
     63 #include "msdos.h"
     64 #include "mkfs_msdos.h"
     65 
     66 static int msdos_populate_dir(const char *, struct denode *, fsnode *,
     67     fsnode *, fsinfo_t *);
     68 
     69 void
     70 msdos_prep_opts(fsinfo_t *fsopts)
     71 {
     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,						\
     88 },
     89 ALLOPTS
     90 #undef AOPT
     91 		{ .name = NULL }
     92 	};
     93 
     94 	fsopts->fs_specific = msdos_opt;
     95 	fsopts->fs_options = copy_opts(msdos_options);
     96 }
     97 
     98 void
     99 msdos_cleanup_opts(fsinfo_t *fsopts)
    100 {
    101 	free(fsopts->fs_specific);
    102 	free(fsopts->fs_options);
    103 }
    104 
    105 int
    106 msdos_parse_opts(const char *option, fsinfo_t *fsopts)
    107 {
    108 	struct msdos_options *msdos_opt = fsopts->fs_specific;
    109 	option_t *msdos_options = fsopts->fs_options;
    110 
    111 	int rv;
    112 
    113 	assert(option != NULL);
    114 	assert(fsopts != NULL);
    115 	assert(msdos_opt != NULL);
    116 
    117 	if (debug & DEBUG_FS_PARSE_OPTS)
    118 		printf("msdos_parse_opts: got `%s'\n", option);
    119 
    120 	rv = set_option(msdos_options, option, NULL, 0);
    121 	if (rv == -1)
    122 		return rv;
    123 
    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;
    131 }
    132 
    133 
    134 void
    135 msdos_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
    136 {
    137 	struct msdos_options *msdos_opt = fsopts->fs_specific;
    138 	struct vnode vp, rootvp;
    139 	struct timeval	start;
    140 	struct msdosfsmount *pmp;
    141 
    142 	assert(image != NULL);
    143 	assert(dir != NULL);
    144 	assert(root != NULL);
    145 	assert(fsopts != NULL);
    146 
    147 	/*
    148 	 * XXX: pick up other options from the msdos specific ones?
    149 	 * Is minsize right here?
    150 	 */
    151 	msdos_opt->create_size = MAX(msdos_opt->create_size, fsopts->minsize);
    152 	if (msdos_opt->bytes_per_sector == 0) {
    153 		if (fsopts->sectorsize == 0)
    154 			fsopts->sectorsize = 512;
    155 		msdos_opt->bytes_per_sector = fsopts->sectorsize;
    156 	} else if (fsopts->sectorsize == 0) {
    157 		fsopts->sectorsize = msdos_opt->bytes_per_sector;
    158 	} else if (fsopts->sectorsize != msdos_opt->bytes_per_sector) {
    159 		err(1, "inconsistent sectorsize -S %u"
    160 		    "!= -o bytes_per_sector %u",
    161 		    fsopts->sectorsize, msdos_opt->bytes_per_sector);
    162 	}
    163 
    164 		/* create image */
    165 	printf("Creating `%s'\n", image);
    166 	TIMER_START(start);
    167 	if (mkfs_msdos(image, NULL, msdos_opt) == -1)
    168 		return;
    169 	TIMER_RESULTS(start, "mkfs_msdos");
    170 
    171 	vp.fd = open(image, O_RDWR);
    172 	vp.fs = msdos_opt;
    173 
    174 	if ((pmp = msdosfs_mount(&vp, 0)) == NULL)
    175 		err(1, "msdosfs_mount");
    176 
    177 	if (msdosfs_root(pmp, &rootvp) != 0)
    178 		err(1, "msdosfs_root");
    179 
    180 	if (debug & DEBUG_FS_MAKEFS)
    181 		printf("msdos_makefs: image %s directory %s root %p\n",
    182 		    image, dir, root);
    183 
    184 		/* populate image */
    185 	printf("Populating `%s'\n", image);
    186 	TIMER_START(start);
    187 	if (msdos_populate_dir(dir, VTODE(&rootvp), root, root, fsopts) == -1)
    188 		errx(1, "Image file `%s' not created.", image);
    189 	TIMER_RESULTS(start, "msdos_populate_dir");
    190 
    191 	if (debug & DEBUG_FS_MAKEFS)
    192 		putchar('\n');
    193 
    194 		/* ensure no outstanding buffers remain */
    195 	if (debug & DEBUG_FS_MAKEFS)
    196 		bcleanup();
    197 
    198 	printf("Image `%s' complete\n", image);
    199 }
    200 
    201 static int
    202 msdos_populate_dir(const char *path, struct denode *dir, fsnode *root,
    203     fsnode *parent, fsinfo_t *fsopts)
    204 {
    205 	fsnode *cur;
    206 	char pbuf[MAXPATHLEN];
    207 
    208 	assert(dir != NULL);
    209 	assert(root != NULL);
    210 	assert(fsopts != NULL);
    211 
    212 	for (cur = root->next; cur != NULL; cur = cur->next) {
    213 		if ((size_t)snprintf(pbuf, sizeof(pbuf), "%s/%s", path,
    214 		    cur->name) >= sizeof(pbuf)) {
    215 			warnx("path %s too long", pbuf);
    216 			return -1;
    217 		}
    218 
    219 		if ((cur->inode->flags & FI_ALLOCATED) == 0) {
    220 			cur->inode->flags |= FI_ALLOCATED;
    221 			if (cur != root) {
    222 				fsopts->curinode++;
    223 				cur->inode->ino = fsopts->curinode;
    224 				cur->parent = parent;
    225 			}
    226 		}
    227 
    228 		if (cur->inode->flags & FI_WRITTEN) {
    229 			continue;	// hard link
    230 		}
    231 		cur->inode->flags |= FI_WRITTEN;
    232 
    233 		if (cur->child) {
    234 			struct denode *de;
    235 			if ((de = msdosfs_mkdire(pbuf, dir, cur)) == NULL) {
    236 				warn("msdosfs_mkdire %s", pbuf);
    237 				return -1;
    238 			}
    239 			if (msdos_populate_dir(pbuf, de, cur->child, cur,
    240 			    fsopts) == -1) {
    241 				warn("msdos_populate_dir %s", pbuf);
    242 				return -1;
    243 			}
    244 			continue;
    245 		} else if (!S_ISREG(cur->type)) {
    246 			warnx("skipping non-regular file %s/%s", cur->path,
    247 			    cur->name);
    248 			continue;
    249 		}
    250 		if (msdosfs_mkfile(pbuf, dir, cur) == NULL) {
    251 			warn("msdosfs_mkfile %s", pbuf);
    252 			return -1;
    253 		}
    254 	}
    255 	return 0;
    256 }
    257