NetBSD/usr.sbin/makefs/v7fs.c

189 lines
4.5 KiB
C
Raw Normal View History

/* $NetBSD: v7fs.c,v 1.3 2011/08/10 11:31:49 uch Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by UCHIYAMA Yasushi.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
#include <sys/cdefs.h>
#if defined(__RCSID) && !defined(__lint)
__RCSID("$NetBSD: v7fs.c,v 1.3 2011/08/10 11:31:49 uch Exp $");
#endif /* !__lint */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include "makefs.h"
#include "v7fs.h"
#include "v7fs_impl.h"
#include "v7fs_makefs.h"
#include "newfs_v7fs.h"
static v7fs_opt_t v7fs_opts;
2011-07-19 22:29:41 +04:00
#ifndef HAVE_NBTOOL_CONFIG_H
#include "progress.h"
static bool progress_bar_enable;
2011-07-19 22:29:41 +04:00
#endif
int v7fs_newfs_verbose;
void
v7fs_prep_opts(fsinfo_t *fsopts)
{
fsopts->fs_specific = &v7fs_opts;
}
void
v7fs_cleanup_opts(fsinfo_t *fsopts)
{
/*NO-OP*/
}
int
v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
{
static option_t v7fs_options[] = {
{ "pdp", &v7fs_opts.pdp_endian, false, true, "PDP endian" },
{ "progress", &v7fs_opts.progress, false, true,
"Progress bar" },
{ .name = NULL }
};
set_option(v7fs_options, option, "1");
return 1;
}
void
v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts)
{
struct v7fs_mount_device v7fs_mount;
int fd, endian, error = 1;
v7fs_newfs_verbose = debug;
2011-07-19 22:29:41 +04:00
#ifndef HAVE_NBTOOL_CONFIG_H
if ((progress_bar_enable = v7fs_opts.progress)) {
progress_switch(progress_bar_enable);
progress_init();
progress(&(struct progress_arg){ .cdev = image });
}
2011-07-19 22:29:41 +04:00
#endif
/* Determine filesystem image size */
v7fs_estimate(dir, root, fsopts);
printf("Calculated size of `%s': %lld bytes, %ld inodes\n",
image, (long long)fsopts->size, (long)fsopts->inodes);
if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) {
err(EXIT_FAILURE, "%s", image);
}
if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) {
goto err_exit;
}
if (write(fd, &fd, 1) != 1) {
goto err_exit;
}
if (lseek(fd, 0, SEEK_SET) == -1) {
goto err_exit;
}
fsopts->fd = fd;
v7fs_mount.device.fd = fd;
#if !defined BYTE_ORDER
#error
#endif
#if BYTE_ORDER == LITTLE_ENDIAN
if (fsopts->needswap)
endian = BIG_ENDIAN;
else
endian = LITTLE_ENDIAN;
#else
if (fsopts->needswap)
endian = LITTLE_ENDIAN;
else
endian = BIG_ENDIAN;
#endif
if (v7fs_opts.pdp_endian) {
endian = PDP_ENDIAN;
}
v7fs_mount.endian = endian;
v7fs_mount.sectors = fsopts->size >> V7FS_BSHIFT;
if (v7fs_newfs(&v7fs_mount, fsopts->inodes) != 0) {
goto err_exit;
}
if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) {
error = 2; /* some files couldn't add */
goto err_exit;
}
close(fd);
return;
err_exit:
close(fd);
err(error, "%s", image);
}
void
progress(const struct progress_arg *p)
{
2011-07-19 22:29:41 +04:00
#ifndef HAVE_NBTOOL_CONFIG_H
static struct progress_arg Progress;
static char cdev[32];
static char label[32];
if (!progress_bar_enable)
return;
if (p) {
Progress = *p;
if (p->cdev)
strcpy(cdev, p->cdev);
if (p->label)
strcpy(label, p->label);
}
if (!Progress.tick)
return;
if (++Progress.cnt > Progress.tick) {
Progress.cnt = 0;
Progress.total++;
progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE);
}
2011-07-19 22:29:41 +04:00
#endif
}