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