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