1 #ifndef lint 2 static char sccsid[] = "@(#)utilities.c 3.16 (Berkeley) 83/12/30"; 3 #endif 4 5 /* Copyright (c) 1983 Regents of the University of California */ 6 7 #include "restore.h" 8 9 char *copynext(); 10 11 /* 12 * Insure that all the components of a pathname exist. 13 */ 14 pathcheck(name) 15 char *name; 16 { 17 register char *cp; 18 struct entry *ep; 19 char *start; 20 21 start = index(name, '/'); 22 if (start == 0) 23 return; 24 for (cp = start; *cp != '\0'; cp++) { 25 if (*cp != '/') 26 continue; 27 *cp = '\0'; 28 ep = lookupname(name); 29 if (ep == NIL) { 30 ep = addentry(name, psearch(name), NODE); 31 newnode(ep); 32 ep->e_flags |= NEW|KEEP; 33 } 34 *cp = '/'; 35 } 36 } 37 38 /* 39 * Change a name to a unique temporary name. 40 */ 41 mktempname(ep) 42 register struct entry *ep; 43 { 44 char oldname[MAXPATHLEN]; 45 46 if (ep->e_flags & TMPNAME) 47 badentry(ep, "mktempname: called with TMPNAME"); 48 ep->e_flags |= TMPNAME; 49 (void) strcpy(oldname, myname(ep)); 50 freename(ep->e_name); 51 ep->e_name = savename(gentempname(ep)); 52 ep->e_namlen = strlen(ep->e_name); 53 renameit(oldname, myname(ep)); 54 } 55 56 /* 57 * Generate a temporary name for an entry. 58 */ 59 char * 60 gentempname(ep) 61 struct entry *ep; 62 { 63 static char name[MAXPATHLEN]; 64 struct entry *np; 65 long i = 0; 66 67 for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links) 68 i++; 69 if (np == NIL) 70 badentry(ep, "not on ino list"); 71 (void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino); 72 return (name); 73 } 74 75 /* 76 * Rename a file or directory. 77 */ 78 renameit(from, to) 79 char *from, *to; 80 { 81 if (rename(from, to) < 0) { 82 fprintf(stderr, "Warning: cannot rename %s to %s", from, to); 83 (void) fflush(stderr); 84 perror(""); 85 return; 86 } 87 vprintf(stdout, "rename %s to %s\n", from, to); 88 } 89 90 /* 91 * Create a new node (directory). 92 */ 93 newnode(np) 94 struct entry *np; 95 { 96 char *cp; 97 98 if (np->e_type != NODE) 99 badentry(np, "newnode: not a node"); 100 cp = myname(np); 101 if (mkdir(cp, 0777) < 0) { 102 fprintf(stderr, "Warning: "); 103 (void) fflush(stderr); 104 perror(cp); 105 return; 106 } 107 vprintf(stdout, "Make node %s\n", cp); 108 } 109 110 /* 111 * Remove an old node (directory). 112 */ 113 removenode(ep) 114 register struct entry *ep; 115 { 116 char *cp; 117 118 if (ep->e_type != NODE) 119 badentry(ep, "removenode: not a node"); 120 if (ep->e_entries != NIL) 121 badentry(ep, "removenode: non-empty directory"); 122 ep->e_flags |= REMOVED; 123 ep->e_flags &= ~TMPNAME; 124 cp = myname(ep); 125 if (rmdir(cp) < 0) { 126 fprintf(stderr, "Warning: "); 127 (void) fflush(stderr); 128 perror(cp); 129 return; 130 } 131 vprintf(stdout, "Remove node %s\n", cp); 132 } 133 134 /* 135 * Remove a leaf. 136 */ 137 removeleaf(ep) 138 register struct entry *ep; 139 { 140 char *cp; 141 142 if (ep->e_type != LEAF) 143 badentry(ep, "removeleaf: not a leaf"); 144 ep->e_flags |= REMOVED; 145 ep->e_flags &= ~TMPNAME; 146 cp = myname(ep); 147 if (unlink(cp) < 0) { 148 fprintf(stderr, "Warning: "); 149 (void) fflush(stderr); 150 perror(cp); 151 return; 152 } 153 vprintf(stdout, "Remove leaf %s\n", cp); 154 } 155 156 /* 157 * Create a link. 158 */ 159 linkit(existing, new, type) 160 char *existing, *new; 161 int type; 162 { 163 164 if (type == SYMLINK) { 165 if (symlink(existing, new) < 0) { 166 fprintf(stderr, 167 "Warning: cannot create symbolic link %s->%s: ", 168 new, existing); 169 (void) fflush(stderr); 170 perror(""); 171 return (FAIL); 172 } 173 } else if (type == HARDLINK) { 174 if (link(existing, new) < 0) { 175 fprintf(stderr, 176 "Warning: cannot create hard link %s->%s: ", 177 new, existing); 178 (void) fflush(stderr); 179 perror(""); 180 return (FAIL); 181 } 182 } else { 183 panic("linkit: unknown type %d\n", type); 184 return (FAIL); 185 } 186 vprintf(stdout, "Create %s link %s->%s\n", 187 type == SYMLINK ? "symbolic" : "hard", new, existing); 188 return (GOOD); 189 } 190 191 /* 192 * find lowest number file (above "start") that needs to be extracted 193 */ 194 ino_t 195 lowerbnd(start) 196 ino_t start; 197 { 198 register struct entry *ep; 199 200 for ( ; start < maxino; start++) { 201 ep = lookupino(start); 202 if (ep == NIL || ep->e_type == NODE) 203 continue; 204 if (ep->e_flags & (NEW|EXTRACT)) 205 return (start); 206 } 207 return (start); 208 } 209 210 /* 211 * find highest number file (below "start") that needs to be extracted 212 */ 213 ino_t 214 upperbnd(start) 215 ino_t start; 216 { 217 register struct entry *ep; 218 219 for ( ; start > ROOTINO; start--) { 220 ep = lookupino(start); 221 if (ep == NIL || ep->e_type == NODE) 222 continue; 223 if (ep->e_flags & (NEW|EXTRACT)) 224 return (start); 225 } 226 return (start); 227 } 228 229 /* 230 * report on a badly formed entry 231 */ 232 badentry(ep, msg) 233 register struct entry *ep; 234 char *msg; 235 { 236 237 fprintf(stderr, "bad entry: %s\n", msg); 238 fprintf(stderr, "name: %s\n", myname(ep)); 239 fprintf(stderr, "parent name %s\n", myname(ep->e_parent)); 240 if (ep->e_sibling != NIL) 241 fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling)); 242 if (ep->e_entries != NIL) 243 fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries)); 244 if (ep->e_links != NIL) 245 fprintf(stderr, "next link name: %s\n", myname(ep->e_links)); 246 if (ep->e_next != NIL) 247 fprintf(stderr, "next hashchain name: %s\n", myname(ep->e_next)); 248 fprintf(stderr, "entry type: %s\n", 249 ep->e_type == NODE ? "NODE" : "LEAF"); 250 fprintf(stderr, "inode number: %ld\n", ep->e_ino); 251 panic("flags: %s\n", flagvalues(ep)); 252 } 253 254 /* 255 * Construct a string indicating the active flag bits of an entry. 256 */ 257 char * 258 flagvalues(ep) 259 register struct entry *ep; 260 { 261 static char flagbuf[BUFSIZ]; 262 263 (void) strcpy(flagbuf, "|NIL"); 264 flagbuf[0] = '\0'; 265 if (ep->e_flags & REMOVED) 266 (void) strcat(flagbuf, "|REMOVED"); 267 if (ep->e_flags & TMPNAME) 268 (void) strcat(flagbuf, "|TMPNAME"); 269 if (ep->e_flags & EXTRACT) 270 (void) strcat(flagbuf, "|EXTRACT"); 271 if (ep->e_flags & NEW) 272 (void) strcat(flagbuf, "|NEW"); 273 if (ep->e_flags & KEEP) 274 (void) strcat(flagbuf, "|KEEP"); 275 return (&flagbuf[1]); 276 } 277 278 /* 279 * Check to see if a name is on a dump tape. 280 */ 281 ino_t 282 dirlookup(name) 283 char *name; 284 { 285 ino_t ino; 286 287 ino = psearch(name); 288 if (ino == 0 || BIT(ino, dumpmap) == 0) 289 fprintf(stderr, "%s is not on tape\n", name); 290 return (ino); 291 } 292 293 /* 294 * Canonicalize file names to always start with ``./'' and 295 * remove any imbedded ".." components. 296 */ 297 canon(rawname, canonname) 298 char *rawname, *canonname; 299 { 300 register char *cp, *np; 301 int len; 302 303 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 304 (void) strcpy(canonname, ""); 305 else if (rawname[0] == '/') 306 (void) strcpy(canonname, "."); 307 else 308 (void) strcpy(canonname, "./"); 309 (void) strcat(canonname, rawname); 310 len = strlen(canonname) - 1; 311 if (canonname[len] == '/') 312 canonname[len] = '\0'; 313 /* 314 * Eliminate extraneous ".." from pathnames. 315 */ 316 for (np = canonname; *np != '\0'; ) { 317 np++; 318 cp = np; 319 while (*np != '/' && *np != '\0') 320 np++; 321 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 322 cp--; 323 while (cp > &canonname[1] && *--cp != '/') 324 /* find beginning of name */; 325 (void) strcpy(cp, np); 326 np = cp; 327 } 328 } 329 } 330 331 /* 332 * Elicit a reply. 333 */ 334 reply(question) 335 char *question; 336 { 337 char c; 338 339 fprintf(stderr, "%s? ", question); 340 do { 341 fprintf(stderr, "[yn] "); 342 (void) fflush(stderr); 343 c = getc(terminal); 344 while (c != '\n' && getc(terminal) != '\n') 345 /* void */; 346 } while (c != 'y' && c != 'n'); 347 if (c == 'y') 348 return (GOOD); 349 return (FAIL); 350 } 351 352 /* 353 * Read and parse an interactive command. 354 * The first word on the line is assigned to "cmd". If 355 * there are no arguments on the command line, then "curdir" 356 * is returned as the argument. If there are arguments 357 * on the line they are returned one at a time on each 358 * successive call to getcmd. Each argument is first assigned 359 * to "name". If it does not start with "/" the pathname in 360 * "curdir" is prepended to it. Finally "canon" is called to 361 * eliminate any embedded ".." components. 362 */ 363 getcmd(curdir, cmd, name) 364 char *curdir, *cmd, *name; 365 { 366 register char *cp; 367 static char *nextarg = NULL; 368 static char input[BUFSIZ]; 369 char output[BUFSIZ]; 370 # define rawname input /* save space by reusing input buffer */ 371 372 /* 373 * Check to see if still processing arguments. 374 */ 375 if (nextarg != NULL) 376 goto getnext; 377 /* 378 * Read a command line and trim off trailing white space. 379 */ 380 do { 381 fprintf(stderr, "restore > "); 382 (void) fflush(stderr); 383 (void) fgets(input, BUFSIZ, terminal); 384 } while (!feof(terminal) && input[0] == '\n'); 385 if (feof(terminal)) { 386 (void) strcpy(cmd, "quit"); 387 return; 388 } 389 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 390 /* trim off trailing white space and newline */; 391 *++cp = '\0'; 392 /* 393 * Copy the command into "cmd". 394 */ 395 cp = copynext(input, cmd); 396 /* 397 * If no argument, use curdir as the default. 398 */ 399 if (*cp == '\0') { 400 (void) strcpy(name, curdir); 401 return; 402 } 403 nextarg = cp; 404 /* 405 * Find the next argument. 406 */ 407 getnext: 408 cp = copynext(nextarg, rawname); 409 if (*cp == '\0') 410 nextarg = NULL; 411 else 412 nextarg = cp; 413 /* 414 * If it an absolute pathname, canonicalize it and return it. 415 */ 416 if (rawname[0] == '/') { 417 canon(rawname, name); 418 return; 419 } 420 /* 421 * For relative pathnames, prepend the current directory to 422 * it then canonicalize and return it. 423 */ 424 (void) strcpy(output, curdir); 425 (void) strcat(output, "/"); 426 (void) strcat(output, rawname); 427 canon(output, name); 428 # undef rawname 429 } 430 431 /* 432 * Strip off the next token of the input. 433 */ 434 char * 435 copynext(input, output) 436 char *input, *output; 437 { 438 register char *cp, *bp; 439 char quote; 440 441 for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 442 /* skip to argument */; 443 bp = output; 444 while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 445 /* 446 * Handle back slashes. 447 */ 448 if (*cp == '\\') { 449 if (*++cp == '\0') { 450 fprintf(stderr, 451 "command lines cannot be continued\n"); 452 continue; 453 } 454 *bp++ = *cp++; 455 continue; 456 } 457 /* 458 * The usual unquoted case. 459 */ 460 if (*cp != '\'' && *cp != '"') { 461 *bp++ = *cp++; 462 continue; 463 } 464 /* 465 * Handle single and double quotes. 466 */ 467 quote = *cp++; 468 while (*cp != quote && *cp != '\0') 469 *bp++ = *cp++; 470 if (*cp++ == '\0') { 471 fprintf(stderr, "missing %c\n", quote); 472 cp--; 473 continue; 474 } 475 } 476 *bp = '\0'; 477 return (cp); 478 } 479 480 /* 481 * respond to interrupts 482 */ 483 onintr() 484 { 485 if (reply("restore interrupted, continue") == FAIL) 486 done(1); 487 if (signal(SIGINT, onintr) == SIG_IGN) 488 (void) signal(SIGINT, SIG_IGN); 489 if (signal(SIGTERM, onintr) == SIG_IGN) 490 (void) signal(SIGTERM, SIG_IGN); 491 } 492 493 /* 494 * handle unexpected inconsistencies 495 */ 496 /* VARARGS1 */ 497 panic(msg, d1, d2) 498 char *msg; 499 long d1, d2; 500 { 501 502 fprintf(stderr, msg, d1, d2); 503 if (reply("abort") == GOOD) { 504 if (reply("dump core") == GOOD) 505 abort(); 506 done(1); 507 } 508 } 509