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 const 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 #if 0 42 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/4/95"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/stat.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <protocols/dumprestore.h> 53 54 #include <err.h> 55 #include <paths.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "restore.h" 62 #include "extern.h" 63 64 int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0; 65 int hflag = 1, mflag = 1, Nflag = 0; 66 int uflag = 0; 67 int dokerberos = 0; 68 char command = '\0'; 69 long dumpnum = 1; 70 long volno = 0; 71 long ntrec; 72 char *dumpmap; 73 char *usedinomap; 74 ino_t maxino; 75 time_t dumptime; 76 time_t dumpdate; 77 FILE *terminal; 78 79 static void obsolete(int *, char **[]); 80 static void usage(void) __dead2; 81 82 int 83 main(int argc, 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:df:hikmNRrs:tuvxy" 102 #else 103 #define optlist "b:df:himNRrs:tuvxy" 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 'd': 117 dflag = 1; 118 break; 119 case 'f': 120 inputdev = optarg; 121 break; 122 case 'h': 123 hflag = 0; 124 break; 125 #ifdef KERBEROS 126 case 'k': 127 dokerberos = 1; 128 break; 129 #endif 130 case 'i': 131 case 'R': 132 case 'r': 133 case 't': 134 case 'x': 135 if (command != '\0') 136 errx(1, 137 "%c and %c options are mutually exclusive", 138 ch, command); 139 command = ch; 140 break; 141 case 'm': 142 mflag = 0; 143 break; 144 case 'N': 145 Nflag = 1; 146 break; 147 case 's': 148 /* Dumpnum (skip to) for multifile dump tapes. */ 149 dumpnum = strtol(optarg, &p, 10); 150 if (*p) 151 errx(1, "illegal dump number -- %s", optarg); 152 if (dumpnum <= 0) 153 errx(1, "dump number must be greater than 0"); 154 break; 155 case 'u': 156 uflag = 1; 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 filesystem. 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 filesystem 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 [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno]", 293 "restore -r [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]", 294 "restore -R [-cdkNuvy] [-b blocksize] [-f file] [-s fileno]", 295 "restore -x [-cdhkmNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]", 296 "restore -t [-cdhkNuvy] [-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(int *argcp, char **argvp[]) 307 { 308 int argc, flags; 309 char *ap, **argv, *flagsp, **nargv, *p; 310 311 /* Setup. */ 312 argv = *argvp; 313 argc = *argcp; 314 315 /* Return if no arguments or first argument has leading dash. */ 316 ap = argv[1]; 317 if (argc == 1 || *ap == '-') 318 return; 319 320 /* Allocate space for new arguments. */ 321 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 322 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 323 err(1, NULL); 324 325 *nargv++ = *argv; 326 argv += 2, argc -= 2; 327 328 for (flags = 0; *ap; ++ap) { 329 switch (*ap) { 330 case 'b': 331 case 'f': 332 case 's': 333 if (*argv == NULL) { 334 warnx("option requires an argument -- %c", *ap); 335 usage(); 336 } 337 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 338 err(1, NULL); 339 nargv[0][0] = '-'; 340 nargv[0][1] = *ap; 341 (void)strcpy(&nargv[0][2], *argv); 342 ++argv; 343 ++nargv; 344 break; 345 default: 346 if (!flags) { 347 *p++ = '-'; 348 flags = 1; 349 } 350 *p++ = *ap; 351 break; 352 } 353 } 354 355 /* Terminate flags. */ 356 if (flags) { 357 *p = '\0'; 358 *nargv++ = flagsp; 359 } 360 361 /* Copy remaining arguments. */ 362 while ((*nargv++ = *argv++)); 363 364 /* Update argument count. */ 365 *argcp = nargv - *argvp - 1; 366 } 367