Home | History | Annotate | Line # | Download | only in makefs
v7fs.c revision 1.3.2.1
      1  1.3.2.1   yamt /*	$NetBSD: v7fs.c,v 1.3.2.1 2014/05/22 11:43:05 yamt Exp $	*/
      2      1.1    uch 
      3      1.1    uch /*-
      4      1.1    uch  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5      1.1    uch  * All rights reserved.
      6      1.1    uch  *
      7      1.1    uch  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1    uch  * by UCHIYAMA Yasushi.
      9      1.1    uch  *
     10      1.1    uch  * Redistribution and use in source and binary forms, with or without
     11      1.1    uch  * modification, are permitted provided that the following conditions
     12      1.1    uch  * are met:
     13      1.1    uch  * 1. Redistributions of source code must retain the above copyright
     14      1.1    uch  *    notice, this list of conditions and the following disclaimer.
     15      1.1    uch  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1    uch  *    notice, this list of conditions and the following disclaimer in the
     17      1.1    uch  *    documentation and/or other materials provided with the distribution.
     18      1.1    uch  *
     19      1.1    uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1    uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1    uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1    uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1    uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1    uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1    uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1    uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1    uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1    uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1    uch  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1    uch  */
     31      1.1    uch 
     32      1.1    uch #if HAVE_NBTOOL_CONFIG_H
     33      1.1    uch #include "nbtool_config.h"
     34      1.1    uch #endif
     35      1.1    uch 
     36      1.1    uch #include <sys/cdefs.h>
     37      1.1    uch #if defined(__RCSID) && !defined(__lint)
     38  1.3.2.1   yamt __RCSID("$NetBSD: v7fs.c,v 1.3.2.1 2014/05/22 11:43:05 yamt Exp $");
     39      1.1    uch #endif	/* !__lint */
     40      1.1    uch 
     41      1.1    uch #include <stdio.h>
     42      1.1    uch #include <stdlib.h>
     43      1.1    uch #include <unistd.h>
     44      1.1    uch #include <string.h>
     45      1.1    uch #include <fcntl.h>
     46  1.3.2.1   yamt #include <util.h>
     47      1.1    uch 
     48      1.1    uch #include "makefs.h"
     49      1.1    uch #include "v7fs.h"
     50      1.1    uch #include "v7fs_impl.h"
     51      1.1    uch #include "v7fs_makefs.h"
     52      1.1    uch #include "newfs_v7fs.h"
     53      1.1    uch 
     54      1.2  joerg 
     55      1.2  joerg #ifndef HAVE_NBTOOL_CONFIG_H
     56      1.2  joerg #include "progress.h"
     57      1.1    uch static bool progress_bar_enable;
     58      1.2  joerg #endif
     59      1.3    uch int v7fs_newfs_verbose;
     60      1.1    uch 
     61      1.1    uch void
     62      1.1    uch v7fs_prep_opts(fsinfo_t *fsopts)
     63      1.1    uch {
     64  1.3.2.1   yamt 	v7fs_opt_t *v7fs_opts = ecalloc(1, sizeof(*v7fs_opts));
     65  1.3.2.1   yamt 	const option_t v7fs_options[] = {
     66  1.3.2.1   yamt 		{ 'p', "pdp", &v7fs_opts->pdp_endian, OPT_INT32, false, true,
     67  1.3.2.1   yamt 		    "PDP endian" },
     68  1.3.2.1   yamt 		{ 'P', "progress", &v7fs_opts->progress, OPT_INT32, false, true,
     69  1.3.2.1   yamt 		  "Progress bar" },
     70  1.3.2.1   yamt 		{ .name = NULL }
     71  1.3.2.1   yamt 	};
     72      1.1    uch 
     73  1.3.2.1   yamt 	fsopts->fs_specific = v7fs_opts;
     74  1.3.2.1   yamt 	fsopts->fs_options = copy_opts(v7fs_options);
     75      1.1    uch }
     76      1.1    uch 
     77      1.1    uch void
     78      1.1    uch v7fs_cleanup_opts(fsinfo_t *fsopts)
     79      1.1    uch {
     80  1.3.2.1   yamt 	free(fsopts->fs_specific);
     81  1.3.2.1   yamt 	free(fsopts->fs_options);
     82      1.1    uch }
     83      1.1    uch 
     84      1.1    uch int
     85      1.1    uch v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
     86      1.1    uch {
     87      1.1    uch 
     88  1.3.2.1   yamt 	return set_option_var(fsopts->fs_options, option, "1", NULL, 0) != -1;
     89      1.1    uch }
     90      1.1    uch 
     91      1.1    uch void
     92      1.1    uch v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
     93      1.1    uch {
     94      1.1    uch 	struct v7fs_mount_device v7fs_mount;
     95      1.1    uch 	int fd, endian, error = 1;
     96  1.3.2.1   yamt 	v7fs_opt_t *v7fs_opts = fsopts->fs_specific;
     97      1.1    uch 
     98      1.3    uch 	v7fs_newfs_verbose = debug;
     99      1.2  joerg #ifndef HAVE_NBTOOL_CONFIG_H
    100  1.3.2.1   yamt 	if ((progress_bar_enable = v7fs_opts->progress)) {
    101      1.1    uch 		progress_switch(progress_bar_enable);
    102      1.1    uch 		progress_init();
    103      1.1    uch 		progress(&(struct progress_arg){ .cdev = image });
    104      1.1    uch 	}
    105      1.2  joerg #endif
    106      1.1    uch 
    107      1.1    uch 	/* Determine filesystem image size */
    108      1.1    uch 	v7fs_estimate(dir, root, fsopts);
    109      1.1    uch 	printf("Calculated size of `%s': %lld bytes, %ld inodes\n",
    110      1.1    uch 	    image, (long long)fsopts->size, (long)fsopts->inodes);
    111      1.1    uch 
    112      1.1    uch 	if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
    113      1.1    uch 		err(EXIT_FAILURE, "%s", image);
    114      1.1    uch 	}
    115      1.1    uch 	if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) {
    116      1.1    uch 		goto err_exit;
    117      1.1    uch 	}
    118      1.1    uch 	if (write(fd, &fd, 1) != 1) {
    119      1.1    uch 		goto err_exit;
    120      1.1    uch 	}
    121      1.1    uch 	if (lseek(fd, 0, SEEK_SET) == -1) {
    122      1.1    uch 		goto err_exit;
    123      1.1    uch 	}
    124      1.1    uch 	fsopts->fd = fd;
    125      1.1    uch 	v7fs_mount.device.fd = fd;
    126      1.1    uch 
    127      1.1    uch #if !defined BYTE_ORDER
    128      1.1    uch #error
    129      1.1    uch #endif
    130      1.1    uch #if BYTE_ORDER == LITTLE_ENDIAN
    131      1.1    uch 	if (fsopts->needswap)
    132      1.1    uch 		endian = BIG_ENDIAN;
    133      1.1    uch 	else
    134      1.1    uch 		endian = LITTLE_ENDIAN;
    135      1.1    uch #else
    136      1.1    uch 	if (fsopts->needswap)
    137      1.1    uch 		endian = LITTLE_ENDIAN;
    138      1.1    uch 	else
    139      1.1    uch 		endian = BIG_ENDIAN;
    140      1.1    uch #endif
    141  1.3.2.1   yamt 	if (v7fs_opts->pdp_endian) {
    142      1.1    uch 		endian = PDP_ENDIAN;
    143      1.1    uch 	}
    144      1.1    uch 
    145      1.1    uch 	v7fs_mount.endian = endian;
    146      1.1    uch 	v7fs_mount.sectors = fsopts->size >> V7FS_BSHIFT;
    147      1.1    uch 	if (v7fs_newfs(&v7fs_mount, fsopts->inodes) != 0) {
    148      1.1    uch 		goto err_exit;
    149      1.1    uch 	}
    150      1.1    uch 
    151      1.1    uch 	if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) {
    152      1.1    uch 		error = 2;	/* some files couldn't add */
    153      1.1    uch 		goto err_exit;
    154      1.1    uch 	}
    155      1.1    uch 
    156      1.1    uch 	close(fd);
    157      1.1    uch 	return;
    158      1.1    uch 
    159      1.1    uch  err_exit:
    160      1.1    uch 	close(fd);
    161      1.1    uch 	err(error, "%s", image);
    162      1.1    uch }
    163      1.1    uch 
    164      1.1    uch void
    165      1.1    uch progress(const struct progress_arg *p)
    166      1.1    uch {
    167      1.2  joerg #ifndef HAVE_NBTOOL_CONFIG_H
    168      1.1    uch 	static struct progress_arg Progress;
    169      1.1    uch 	static char cdev[32];
    170      1.1    uch 	static char label[32];
    171      1.1    uch 
    172      1.1    uch 	if (!progress_bar_enable)
    173      1.1    uch 		return;
    174      1.1    uch 
    175      1.1    uch 	if (p) {
    176      1.1    uch 		Progress = *p;
    177      1.1    uch 		if (p->cdev)
    178      1.1    uch 			strcpy(cdev, p->cdev);
    179      1.1    uch 		if (p->label)
    180      1.1    uch 			strcpy(label, p->label);
    181      1.1    uch 	}
    182      1.1    uch 
    183      1.1    uch 	if (!Progress.tick)
    184      1.1    uch 		return;
    185      1.1    uch 	if (++Progress.cnt > Progress.tick) {
    186      1.1    uch 		Progress.cnt = 0;
    187      1.1    uch 		Progress.total++;
    188      1.1    uch 		progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE);
    189      1.1    uch 	}
    190      1.2  joerg #endif
    191      1.1    uch }
    192