#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.9 2025/12/21 23:55:14 nia Exp $");
#endif
#if defined(HAVE_SYS_ENDIAN_H) || !defined(HAVE_NBTOOL_CONFIG_H)
#include <sys/endian.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <util.h>
#include "makefs.h"
#include "v7fs.h"
#include "v7fs_impl.h"
#include "v7fs_makefs.h"
#include "newfs_v7fs.h"
#ifndef HAVE_NBTOOL_CONFIG_H
#include "progress.h"
static bool progress_bar_enable;
#endif
int v7fs_newfs_verbose;
void
v7fs_prep_opts(fsinfo_t *fsopts)
{
v7fs_opt_t *v7fs_opts = ecalloc(1, sizeof(*v7fs_opts));
const option_t v7fs_options[] = {
{ 'p', "pdp", &v7fs_opts->pdp_endian, OPT_INT32, false, true,
"PDP endian" },
{ 'P', "progress", &v7fs_opts->progress, OPT_INT32, false, true,
"Progress bar" },
{ .name = NULL }
};
fsopts->fs_specific = v7fs_opts;
fsopts->fs_options = copy_opts(v7fs_options);
}
void
v7fs_cleanup_opts(fsinfo_t *fsopts)
{
free(fsopts->fs_specific);
free(fsopts->fs_options);
}
int
v7fs_parse_opts(const char *option, fsinfo_t *fsopts)
{
return set_option_var(fsopts->fs_options, option, "1", NULL, 0) != -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_opt_t *v7fs_opts = fsopts->fs_specific;
v7fs_newfs_verbose = debug;
#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 });
}
#endif
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;
goto err_exit;
}
close(fd);
return;
err_exit:
close(fd);
err(error, "%s", image);
}
void
progress(const struct progress_arg *p)
{
#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);
}
#endif
}