1 /* $NetBSD: v7fs.c,v 1.1 2011/07/18 08:58:38 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.1 2011/07/18 08:58:38 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 #include "progress.h" 53 54 static v7fs_opt_t v7fs_opts; 55 static bool progress_bar_enable; 56 bool verbose; 57 58 void 59 v7fs_prep_opts(fsinfo_t *fsopts) 60 { 61 62 fsopts->fs_specific = &v7fs_opts; 63 } 64 65 void 66 v7fs_cleanup_opts(fsinfo_t *fsopts) 67 { 68 /*NO-OP*/ 69 } 70 71 int 72 v7fs_parse_opts(const char *option, fsinfo_t *fsopts) 73 { 74 static option_t v7fs_options[] = { 75 { "pdp", &v7fs_opts.pdp_endian, false, true, "PDP endian" }, 76 { "progress", &v7fs_opts.progress, false, true, 77 "Progress bar" }, 78 { .name = NULL } 79 }; 80 81 set_option(v7fs_options, option, "1"); 82 83 return 1; 84 } 85 86 void 87 v7fs_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) 88 { 89 struct v7fs_mount_device v7fs_mount; 90 int fd, endian, error = 1; 91 92 verbose = debug; 93 if ((progress_bar_enable = v7fs_opts.progress)) { 94 progress_switch(progress_bar_enable); 95 progress_init(); 96 progress(&(struct progress_arg){ .cdev = image }); 97 } 98 99 /* Determine filesystem image size */ 100 v7fs_estimate(dir, root, fsopts); 101 printf("Calculated size of `%s': %lld bytes, %ld inodes\n", 102 image, (long long)fsopts->size, (long)fsopts->inodes); 103 104 if ((fd = open(image, O_RDWR | O_CREAT | O_TRUNC, 0666)) == -1) { 105 err(EXIT_FAILURE, "%s", image); 106 } 107 if (lseek(fd, fsopts->size - 1, SEEK_SET) == -1) { 108 goto err_exit; 109 } 110 if (write(fd, &fd, 1) != 1) { 111 goto err_exit; 112 } 113 if (lseek(fd, 0, SEEK_SET) == -1) { 114 goto err_exit; 115 } 116 fsopts->fd = fd; 117 v7fs_mount.device.fd = fd; 118 119 #if !defined BYTE_ORDER 120 #error 121 #endif 122 #if BYTE_ORDER == LITTLE_ENDIAN 123 if (fsopts->needswap) 124 endian = BIG_ENDIAN; 125 else 126 endian = LITTLE_ENDIAN; 127 #else 128 if (fsopts->needswap) 129 endian = LITTLE_ENDIAN; 130 else 131 endian = BIG_ENDIAN; 132 #endif 133 if (v7fs_opts.pdp_endian) { 134 endian = PDP_ENDIAN; 135 } 136 137 v7fs_mount.endian = endian; 138 v7fs_mount.sectors = fsopts->size >> V7FS_BSHIFT; 139 if (v7fs_newfs(&v7fs_mount, fsopts->inodes) != 0) { 140 goto err_exit; 141 } 142 143 if (v7fs_populate(dir, root, fsopts, &v7fs_mount) != 0) { 144 error = 2; /* some files couldn't add */ 145 goto err_exit; 146 } 147 148 close(fd); 149 return; 150 151 err_exit: 152 close(fd); 153 err(error, "%s", image); 154 } 155 156 void 157 progress(const struct progress_arg *p) 158 { 159 static struct progress_arg Progress; 160 static char cdev[32]; 161 static char label[32]; 162 163 if (!progress_bar_enable) 164 return; 165 166 if (p) { 167 Progress = *p; 168 if (p->cdev) 169 strcpy(cdev, p->cdev); 170 if (p->label) 171 strcpy(label, p->label); 172 } 173 174 if (!Progress.tick) 175 return; 176 if (++Progress.cnt > Progress.tick) { 177 Progress.cnt = 0; 178 Progress.total++; 179 progress_bar(cdev, label, Progress.total, PROGRESS_BAR_GRANULE); 180 } 181 } 182