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