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