1 /* $NetBSD: main.c,v 1.19 1999/11/09 15:06:33 drochner Exp $ */ 2 3 /* 4 * Copyright (c) 1983, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/4/95"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.19 1999/11/09 15:06:33 drochner Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 53 #include <ufs/ufs/dinode.h> 54 #include <ufs/ffs/fs.h> 55 #include <protocols/dumprestore.h> 56 57 #include <err.h> 58 #include <errno.h> 59 #include <paths.h> 60 #include <signal.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 #include "restore.h" 67 #include "extern.h" 68 69 extern char *__progname; /* from crt0.o */ 70 71 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0; 72 int hflag = 1, mflag = 1, Nflag = 0; 73 char command = '\0'; 74 int32_t dumpnum = 1; 75 int32_t volno = 0; 76 int32_t ntrec; 77 char *dumpmap; 78 char *usedinomap; 79 ino_t maxino; 80 time_t dumptime; 81 time_t dumpdate; 82 FILE *terminal; 83 char *tmpdir; 84 85 int main __P((int, char *[])); 86 static void obsolete __P((int *, char **[])); 87 static void usage __P((void)); 88 89 int 90 main(argc, argv) 91 int argc; 92 char *argv[]; 93 { 94 int ch; 95 ino_t ino; 96 char *inputdev; 97 char *symtbl = "./restoresymtable"; 98 char *p, name[MAXPATHLEN]; 99 100 if (argc < 2) 101 usage(); 102 103 if ((inputdev = getenv("TAPE")) == NULL) 104 inputdev = _PATH_DEFTAPE; 105 if ((tmpdir = getenv("TMPDIR")) == NULL) 106 tmpdir = _PATH_TMP; 107 obsolete(&argc, &argv); 108 while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tuvxy")) != -1) 109 switch(ch) { 110 case 'b': 111 /* Change default tape blocksize. */ 112 bflag = 1; 113 ntrec = strtol(optarg, &p, 10); 114 if (*p) 115 errx(1, "illegal blocksize -- %s", optarg); 116 if (ntrec <= 0) 117 errx(1, "block size must be greater than 0"); 118 break; 119 case 'c': 120 cvtflag = 1; 121 break; 122 case 'd': 123 dflag = 1; 124 break; 125 case 'f': 126 inputdev = optarg; 127 break; 128 case 'h': 129 hflag = 0; 130 break; 131 case 'i': 132 case 'R': 133 case 'r': 134 case 't': 135 case 'x': 136 if (command != '\0') 137 errx(1, 138 "%c and %c options are mutually exclusive", 139 ch, command); 140 command = ch; 141 break; 142 case 'm': 143 mflag = 0; 144 break; 145 case 'N': 146 Nflag = 1; 147 break; 148 case 's': 149 /* Dumpnum (skip to) for multifile dump tapes. */ 150 dumpnum = strtol(optarg, &p, 10); 151 if (*p) 152 errx(1, "illegal dump number -- %s", optarg); 153 if (dumpnum <= 0) 154 errx(1, "dump number must be greater than 0"); 155 break; 156 case 'u': 157 uflag = 1; 158 break; 159 case 'v': 160 vflag = 1; 161 break; 162 case 'y': 163 yflag = 1; 164 break; 165 default: 166 usage(); 167 } 168 argc -= optind; 169 argv += optind; 170 171 if (command == '\0') 172 errx(1, "none of i, R, r, t or x options specified"); 173 174 if (signal(SIGINT, onintr) == SIG_IGN) 175 (void) signal(SIGINT, SIG_IGN); 176 if (signal(SIGTERM, onintr) == SIG_IGN) 177 (void) signal(SIGTERM, SIG_IGN); 178 setlinebuf(stderr); 179 180 atexit(cleanup); 181 182 setinput(inputdev); 183 184 if (argc == 0) { 185 argc = 1; 186 *--argv = "."; 187 } 188 189 switch (command) { 190 /* 191 * Interactive mode. 192 */ 193 case 'i': 194 setup(); 195 extractdirs(1); 196 initsymtable(NULL); 197 runcmdshell(); 198 break; 199 /* 200 * Incremental restoration of a file system. 201 */ 202 case 'r': 203 setup(); 204 if (dumptime > 0) { 205 /* 206 * This is an incremental dump tape. 207 */ 208 vprintf(stdout, "Begin incremental restore\n"); 209 initsymtable(symtbl); 210 extractdirs(1); 211 removeoldleaves(); 212 vprintf(stdout, "Calculate node updates.\n"); 213 treescan(".", ROOTINO, nodeupdates); 214 findunreflinks(); 215 removeoldnodes(); 216 } else { 217 /* 218 * This is a level zero dump tape. 219 */ 220 vprintf(stdout, "Begin level 0 restore\n"); 221 initsymtable((char *)0); 222 extractdirs(1); 223 vprintf(stdout, "Calculate extraction list.\n"); 224 treescan(".", ROOTINO, nodeupdates); 225 } 226 createleaves(symtbl); 227 createlinks(); 228 setdirmodes(FORCE); 229 checkrestore(); 230 if (dflag) { 231 vprintf(stdout, "Verify the directory structure\n"); 232 treescan(".", ROOTINO, verifyfile); 233 } 234 dumpsymtable(symtbl, (long)1); 235 break; 236 /* 237 * Resume an incremental file system restoration. 238 */ 239 case 'R': 240 initsymtable(symtbl); 241 skipmaps(); 242 skipdirs(); 243 createleaves(symtbl); 244 createlinks(); 245 setdirmodes(FORCE); 246 checkrestore(); 247 dumpsymtable(symtbl, (long)1); 248 break; 249 /* 250 * List contents of tape. 251 */ 252 case 't': 253 setup(); 254 extractdirs(0); 255 initsymtable((char *)0); 256 while (argc--) { 257 canon(*argv++, name); 258 ino = dirlookup(name); 259 if (ino == 0) 260 continue; 261 treescan(name, ino, listfile); 262 } 263 break; 264 /* 265 * Batch extraction of tape contents. 266 */ 267 case 'x': 268 setup(); 269 extractdirs(1); 270 initsymtable((char *)0); 271 while (argc--) { 272 canon(*argv++, name); 273 ino = dirlookup(name); 274 if (ino == 0) 275 continue; 276 if (mflag) 277 pathcheck(name); 278 treescan(name, ino, addfile); 279 } 280 createfiles(); 281 createlinks(); 282 setdirmodes(0); 283 if (dflag) 284 checkrestore(); 285 break; 286 } 287 exit(0); 288 /* NOTREACHED */ 289 } 290 291 static void 292 usage() 293 { 294 295 (void)fprintf(stderr, 296 "usage: %s -i [-cdhmvyN] [-b blocksize] [-f file] [-s fileno]\n", 297 __progname); 298 (void)fprintf(stderr, 299 "\t%s -R [-cdvyN] [-b blocksize] [-f file] [-s fileno]\n", 300 __progname); 301 (void)fprintf(stderr, 302 "\t%s -r [-cdvyN] [-b blocksize] [-f file] [-s fileno]\n", 303 __progname); 304 (void)fprintf(stderr, 305 "\t%s -t [-cdhvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n", 306 __progname); 307 (void)fprintf(stderr, 308 "\t%s -x [-cdhmvyN] [-b blocksize] [-f file] [-s fileno] [file ...]\n", 309 __progname); 310 exit(1); 311 } 312 313 /* 314 * obsolete -- 315 * Change set of key letters and ordered arguments into something 316 * getopt(3) will like. 317 */ 318 static void 319 obsolete(argcp, argvp) 320 int *argcp; 321 char **argvp[]; 322 { 323 int argc, flags; 324 char *ap, **argv, *flagsp, **nargv, *p; 325 326 /* Setup. */ 327 argv = *argvp; 328 argc = *argcp; 329 330 /* Return if no arguments or first argument has leading dash. */ 331 ap = argv[1]; 332 if (argc == 1 || *ap == '-') 333 return; 334 335 /* Allocate space for new arguments. */ 336 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 337 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 338 err(1, NULL); 339 340 *nargv++ = *argv; 341 argv += 2; 342 343 for (flags = 0; *ap; ++ap) { 344 switch (*ap) { 345 case 'b': 346 case 'f': 347 case 's': 348 if (*argv == NULL) { 349 warnx("option requires an argument -- %c", *ap); 350 usage(); 351 } 352 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 353 err(1, NULL); 354 nargv[0][0] = '-'; 355 nargv[0][1] = *ap; 356 (void)strcpy(&nargv[0][2], *argv); 357 ++argv; 358 ++nargv; 359 break; 360 default: 361 if (!flags) { 362 *p++ = '-'; 363 flags = 1; 364 } 365 *p++ = *ap; 366 break; 367 } 368 } 369 370 /* Terminate flags. */ 371 if (flags) { 372 *p = '\0'; 373 *nargv++ = flagsp; 374 } 375 376 /* Copy remaining arguments. */ 377 while ((*nargv++ = *argv++) != NULL) 378 ; 379 380 /* Update argument count. */ 381 *argcp = nargv - *argvp - 1; 382 } 383