1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 33 static const char copyright[] = 34 "@(#) 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 #endif /* not lint */ 40 41 #include <sys/cdefs.h> 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 45 #include <ufs/ufs/dinode.h> 46 #include <protocols/dumprestore.h> 47 48 #include <err.h> 49 #include <limits.h> 50 #include <locale.h> 51 #include <paths.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <unistd.h> 56 57 #include "restore.h" 58 #include "extern.h" 59 60 int bflag = 0, cvtflag = 0, dflag = 0, Dflag = 0, vflag = 0, yflag = 0; 61 int hflag = 1, mflag = 1, Nflag = 0; 62 int uflag = 0; 63 int pipecmd = 0; 64 char command = '\0'; 65 long dumpnum = 1; 66 long volno = 0; 67 long ntrec; 68 char *dumpmap; 69 char *usedinomap; 70 ino_t maxino; 71 time_t dumptime; 72 time_t dumpdate; 73 FILE *terminal; 74 75 static void obsolete(int *, char **[]); 76 static void usage(void) __dead2; 77 78 int 79 main(int argc, char *argv[]) 80 { 81 int ch; 82 ino_t ino; 83 char *inputdev; 84 char *symtbl = "./restoresymtable"; 85 char *p, name[MAXPATHLEN]; 86 87 /* Temp files should *not* be readable. We set permissions later. */ 88 (void) umask(077); 89 90 if (argc < 2) 91 usage(); 92 93 (void)setlocale(LC_ALL, ""); 94 95 inputdev = NULL; 96 obsolete(&argc, &argv); 97 while ((ch = getopt(argc, argv, "b:dDf:himNP:Rrs:tuvxy")) != -1) 98 switch(ch) { 99 case 'b': 100 /* Change default tape blocksize. */ 101 bflag = 1; 102 ntrec = strtol(optarg, &p, 10); 103 if (*p) 104 errx(1, "illegal blocksize -- %s", optarg); 105 if (ntrec <= 0) 106 errx(1, "block size must be greater than 0"); 107 break; 108 case 'd': 109 dflag = 1; 110 break; 111 case 'D': 112 Dflag = 1; 113 break; 114 case 'f': 115 if (pipecmd) 116 errx(1, 117 "-P and -f options are mutually exclusive"); 118 inputdev = optarg; 119 break; 120 case 'P': 121 if (!pipecmd && inputdev) 122 errx(1, 123 "-P and -f options are mutually exclusive"); 124 inputdev = optarg; 125 pipecmd = 1; 126 break; 127 case 'h': 128 hflag = 0; 129 break; 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 if (inputdev == NULL && (inputdev = getenv("TAPE")) == NULL) 180 inputdev = _PATH_DEFTAPE; 181 setinput(inputdev, pipecmd); 182 183 if (argc == 0) { 184 argc = 1; 185 *--argv = "."; 186 } 187 188 switch (command) { 189 /* 190 * Interactive mode. 191 */ 192 case 'i': 193 setup(); 194 extractdirs(1); 195 initsymtable(NULL); 196 runcmdshell(); 197 break; 198 /* 199 * Incremental restoration of a file system. 200 */ 201 case 'r': 202 setup(); 203 if (dumptime > 0) { 204 /* 205 * This is an incremental dump tape. 206 */ 207 vprintf(stdout, "Begin incremental restore\n"); 208 initsymtable(symtbl); 209 extractdirs(1); 210 removeoldleaves(); 211 vprintf(stdout, "Calculate node updates.\n"); 212 treescan(".", UFS_ROOTINO, nodeupdates); 213 findunreflinks(); 214 removeoldnodes(); 215 } else { 216 /* 217 * This is a level zero dump tape. 218 */ 219 vprintf(stdout, "Begin level 0 restore\n"); 220 initsymtable((char *)0); 221 extractdirs(1); 222 vprintf(stdout, "Calculate extraction list.\n"); 223 treescan(".", UFS_ROOTINO, nodeupdates); 224 } 225 createleaves(symtbl); 226 createlinks(); 227 setdirmodes(FORCE); 228 checkrestore(); 229 if (dflag) { 230 vprintf(stdout, "Verify the directory structure\n"); 231 treescan(".", UFS_ROOTINO, verifyfile); 232 } 233 dumpsymtable(symtbl, (long)1); 234 break; 235 /* 236 * Resume an incremental file system restoration. 237 */ 238 case 'R': 239 initsymtable(symtbl); 240 skipmaps(); 241 skipdirs(); 242 createleaves(symtbl); 243 createlinks(); 244 setdirmodes(FORCE); 245 checkrestore(); 246 dumpsymtable(symtbl, (long)1); 247 break; 248 /* 249 * List contents of tape. 250 */ 251 case 't': 252 setup(); 253 extractdirs(0); 254 initsymtable((char *)0); 255 while (argc--) { 256 canon(*argv++, name, sizeof(name)); 257 ino = dirlookup(name); 258 if (ino == 0) 259 continue; 260 treescan(name, ino, listfile); 261 } 262 break; 263 /* 264 * Batch extraction of tape contents. 265 */ 266 case 'x': 267 setup(); 268 extractdirs(1); 269 initsymtable((char *)0); 270 while (argc--) { 271 canon(*argv++, name, sizeof(name)); 272 ino = dirlookup(name); 273 if (ino == 0) 274 continue; 275 if (mflag) 276 pathcheck(name); 277 treescan(name, ino, addfile); 278 } 279 createfiles(); 280 createlinks(); 281 setdirmodes(0); 282 if (dflag) 283 checkrestore(); 284 break; 285 } 286 done(0); 287 /* NOTREACHED */ 288 } 289 290 static void 291 usage() 292 { 293 const char *const common = 294 "[-b blocksize] [-f file | -P pipecommand] [-s fileno]"; 295 const char *const fileell = "[file ...]"; 296 297 (void)fprintf(stderr, "usage:\t%s %s\n\t%s %s\n\t%s %s\n" 298 "\t%s %s %s\n\t%s %s %s\n", 299 "restore -i [-dhmNuvy]", common, 300 "restore -R [-dNuvy]", common, 301 "restore -r [-dNuvy]", common, 302 "restore -t [-dhNuvy]", common, fileell, 303 "restore -x [-dhmNuvy]", common, fileell); 304 done(1); 305 } 306 307 /* 308 * obsolete -- 309 * Change set of key letters and ordered arguments into something 310 * getopt(3) will like. 311 */ 312 static void 313 obsolete(int *argcp, char **argvp[]) 314 { 315 int argc, flags; 316 char *ap, **argv, *flagsp, **nargv, *p; 317 318 /* Setup. */ 319 argv = *argvp; 320 argc = *argcp; 321 322 /* Return if no arguments or first argument has leading dash. */ 323 ap = argv[1]; 324 if (argc == 1 || *ap == '-') 325 return; 326 327 /* Allocate space for new arguments. */ 328 if ((*argvp = nargv = malloc((argc + 1) * sizeof(char *))) == NULL || 329 (p = flagsp = malloc(strlen(ap) + 2)) == NULL) 330 err(1, NULL); 331 332 *nargv++ = *argv; 333 argv += 2, argc -= 2; 334 335 for (flags = 0; *ap; ++ap) { 336 switch (*ap) { 337 case 'b': 338 case 'f': 339 case 's': 340 if (*argv == NULL) { 341 warnx("option requires an argument -- %c", *ap); 342 usage(); 343 } 344 if ((nargv[0] = malloc(strlen(*argv) + 2 + 1)) == NULL) 345 err(1, NULL); 346 nargv[0][0] = '-'; 347 nargv[0][1] = *ap; 348 (void)strcpy(&nargv[0][2], *argv); 349 ++argv; 350 ++nargv; 351 break; 352 default: 353 if (!flags) { 354 *p++ = '-'; 355 flags = 1; 356 } 357 *p++ = *ap; 358 break; 359 } 360 } 361 362 /* Terminate flags. */ 363 if (flags) { 364 *p = '\0'; 365 *nargv++ = flagsp; 366 } else 367 free(flagsp); 368 369 /* Copy remaining arguments. */ 370 while ((*nargv++ = *argv++)); 371 372 /* Update argument count. */ 373 *argcp = nargv - *argvp - 1; 374 } 375