1 /* $NetBSD: main.c,v 1.14 1997/07/06 08:51:30 lukem 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 #ifndef lint 37 static char copyright[] = 38 "@(#) 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.3 (Berkeley) 9/13/94"; 45 #else 46 static char rcsid[] = "$NetBSD: main.c,v 1.14 1997/07/06 08:51:30 lukem Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 53 #include <ufs/ffs/fs.h> 54 #include <ufs/ufs/dinode.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 65 #include "restore.h" 66 #include "extern.h" 67 68 extern char *__progname; /* from crt0.o */ 69 70 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0; 71 int hflag = 1, mflag = 1, Nflag = 0; 72 char command = '\0'; 73 long dumpnum = 1; 74 long volno = 0; 75 long ntrec; 76 char *dumpmap; 77 char *usedinomap; 78 ino_t maxino; 79 time_t dumptime; 80 time_t dumpdate; 81 FILE *terminal; 82 char *tmpdir; 83 84 static void obsolete __P((int *, char **[])); 85 static void usage __P((void)); 86 87 int 88 main(argc, argv) 89 int argc; 90 char *argv[]; 91 { 92 int ch; 93 ino_t ino; 94 char *inputdev; 95 char *symtbl = "./restoresymtable"; 96 char *p, name[MAXPATHLEN]; 97 98 if (argc < 2) 99 usage(); 100 101 if ((inputdev = getenv("TAPE")) == NULL) 102 inputdev = _PATH_DEFTAPE; 103 if ((tmpdir = getenv("TMPDIR")) == NULL) 104 tmpdir = _PATH_TMP; 105 obsolete(&argc, &argv); 106 while ((ch = getopt(argc, argv, "b:cdf:himNRrs:tvxy")) != EOF) 107 switch(ch) { 108 case 'b': 109 /* Change default tape blocksize. */ 110 bflag = 1; 111 ntrec = strtol(optarg, &p, 10); 112 if (*p) 113 errx(1, "illegal blocksize -- %s", optarg); 114 if (ntrec <= 0) 115 errx(1, "block size must be greater than 0"); 116 break; 117 case 'c': 118 cvtflag = 1; 119 break; 120 case 'd': 121 dflag = 1; 122 break; 123 case 'f': 124 inputdev = optarg; 125 break; 126 case 'h': 127 hflag = 0; 128 break; 129 case 'i': 130 case 'R': 131 case 'r': 132 case 't': 133 case 'x': 134 if (command != '\0') 135 errx(1, 136 "%c and %c options are mutually exclusive", 137 ch, command); 138 command = ch; 139 break; 140 case 'm': 141 mflag = 0; 142 break; 143 case 'N': 144 Nflag = 1; 145 break; 146 case 's': 147 /* Dumpnum (skip to) for multifile dump tapes. */ 148 dumpnum = strtol(optarg, &p, 10); 149 if (*p) 150 errx(1, "illegal dump number -- %s", optarg); 151 if (dumpnum <= 0) 152 errx(1, "dump number must be greater than 0"); 153 break; 154 case 'v': 155 vflag = 1; 156 break; 157 case 'y': 158 yflag = 1; 159 break; 160 default: 161 usage(); 162 } 163 argc -= optind; 164 argv += optind; 165 166 if (command == '\0') 167 errx(1, "none of i, R, r, t or x options specified"); 168 169 if (signal(SIGINT, onintr) == SIG_IGN) 170 (void) signal(SIGINT, SIG_IGN); 171 if (signal(SIGTERM, onintr) == SIG_IGN) 172 (void) signal(SIGTERM, SIG_IGN); 173 setlinebuf(stderr); 174 175 atexit(cleanup); 176 177 setinput(inputdev); 178 179 if (argc == 0) { 180 argc = 1; 181 *--argv = "."; 182 } 183 184 switch (command) { 185 /* 186 * Interactive mode. 187 */ 188 case 'i': 189 setup(); 190 extractdirs(1); 191 initsymtable(NULL); 192 runcmdshell(); 193 break; 194 /* 195 * Incremental restoration of a file system. 196 */ 197 case 'r': 198 setup(); 199 if (dumptime > 0) { 200 /* 201 * This is an incremental dump tape. 202 */ 203 vprintf(stdout, "Begin incremental restore\n"); 204 initsymtable(symtbl); 205 extractdirs(1); 206 removeoldleaves(); 207 vprintf(stdout, "Calculate node updates.\n"); 208 treescan(".", ROOTINO, nodeupdates); 209 findunreflinks(); 210 removeoldnodes(); 211 } else { 212 /* 213 * This is a level zero dump tape. 214 */ 215 vprintf(stdout, "Begin level 0 restore\n"); 216 initsymtable((char *)0); 217 extractdirs(1); 218 vprintf(stdout, "Calculate extraction list.\n"); 219 treescan(".", ROOTINO, nodeupdates); 220 } 221 createleaves(symtbl); 222 createlinks(); 223 setdirmodes(FORCE); 224 checkrestore(); 225 if (dflag) { 226 vprintf(stdout, "Verify the directory structure\n"); 227 treescan(".", ROOTINO, verifyfile); 228 } 229 dumpsymtable(symtbl, (long)1); 230 break; 231 /* 232 * Resume an incremental file system restoration. 233 */ 234 case 'R': 235 initsymtable(symtbl); 236 skipmaps(); 237 skipdirs(); 238 createleaves(symtbl); 239 createlinks(); 240 setdirmodes(FORCE); 241 checkrestore(); 242 dumpsymtable(symtbl, (long)1); 243 break; 244 /* 245 * List contents of tape. 246 */ 247 case 't': 248 setup(); 249 extractdirs(0); 250 initsymtable((char *)0); 251 while (argc--) { 252 canon(*argv++, name); 253 ino = dirlookup(name); 254 if (ino == 0) 255 continue; 256 treescan(name, ino, listfile); 257 } 258 break; 259 /* 260 * Batch extraction of tape contents. 261 */ 262 case 'x': 263 setup(); 264 extractdirs(1); 265 initsymtable((char *)0); 266 while (argc--) { 267 canon(*argv++, name); 268 ino = dirlookup(name); 269 if (ino == 0) 270 continue; 271 if (mflag) 272 pathcheck(name); 273 treescan(name, ino, addfile); 274 } 275 createfiles(); 276 createlinks(); 277 setdirmodes(0); 278 if (dflag) 279 checkrestore(); 280 break; 281 } 282 exit(0); 283 /* NOTREACHED */ 284 } 285 286 static void 287 usage() 288 { 289 290 (void)fprintf(stderr, 291 "usage: %s -i [-chmvy] [-b blocksize] [-f file] [-s fileno]\n", 292 __progname); 293 (void)fprintf(stderr, 294 "\t%s -R [-cvy] [-b blocksize] [-f file] [-s fileno]\n", 295 __progname); 296 (void)fprintf(stderr, 297 "\t%s -r [-cvy] [-b blocksize] [-f file] [-s fileno]\n", 298 __progname); 299 (void)fprintf(stderr, 300 "\t%s -t [-chvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n", 301 __progname); 302 (void)fprintf(stderr, 303 "\t%s -x [-chmvy] [-b blocksize] [-f file] [-s fileno] [file ...]\n", 304 __progname); 305 exit(1); 306 } 307 308 /* 309 * obsolete -- 310 * Change set of key letters and ordered arguments into something 311 * getopt(3) will like. 312 */ 313 static void 314 obsolete(argcp, argvp) 315 int *argcp; 316 char **argvp[]; 317 { 318 int argc, flags; 319 char *ap, **argv, *flagsp, **nargv, *p; 320 321 /* Setup. */ 322 argv = *argvp; 323 argc = *argcp; 324 325 /* Return if no arguments or first argument has leading dash. */ 326 ap = argv[1]; 327 if (argc == 1 || *ap == '-') 328 return; 329 330 /* Allocate space for new arguments. */ 331 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 332 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 333 err(1, NULL); 334 335 *nargv++ = *argv; 336 argv += 2; 337 338 for (flags = 0; *ap; ++ap) { 339 switch (*ap) { 340 case 'b': 341 case 'f': 342 case 's': 343 if (*argv == NULL) { 344 warnx("option requires an argument -- %c", *ap); 345 usage(); 346 } 347 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 348 err(1, NULL); 349 nargv[0][0] = '-'; 350 nargv[0][1] = *ap; 351 (void)strcpy(&nargv[0][2], *argv); 352 ++argv; 353 ++nargv; 354 break; 355 default: 356 if (!flags) { 357 *p++ = '-'; 358 flags = 1; 359 } 360 *p++ = *ap; 361 break; 362 } 363 } 364 365 /* Terminate flags. */ 366 if (flags) { 367 *p = '\0'; 368 *nargv++ = flagsp; 369 } 370 371 /* Copy remaining arguments. */ 372 while ((*nargv++ = *argv++) != NULL) 373 ; 374 375 /* Update argument count. */ 376 *argcp = nargv - *argvp - 1; 377 } 378