1 /* 2 * Copyright (c) 1985, 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 #if 0 36 static char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95"; 37 #endif 38 static const char rcsid[] = 39 "$Id: interactive.c,v 1.5 1998/07/28 06:20:08 charnier Exp $"; 40 #endif /* not lint */ 41 42 #include <sys/param.h> 43 #include <sys/stat.h> 44 45 #include <ufs/ufs/dinode.h> 46 #include <ufs/ufs/dir.h> 47 #include <protocols/dumprestore.h> 48 49 #include <setjmp.h> 50 #include <glob.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 55 #include "restore.h" 56 #include "extern.h" 57 58 #define round(a, b) (((a) + (b) - 1) / (b) * (b)) 59 60 /* 61 * Things to handle interruptions. 62 */ 63 static int runshell; 64 static jmp_buf reset; 65 static char *nextarg = NULL; 66 67 /* 68 * Structure and routines associated with listing directories. 69 */ 70 struct afile { 71 ino_t fnum; /* inode number of file */ 72 char *fname; /* file name */ 73 short len; /* name length */ 74 char prefix; /* prefix character */ 75 char postfix; /* postfix character */ 76 }; 77 struct arglist { 78 int freeglob; /* glob structure needs to be freed */ 79 int argcnt; /* next globbed argument to return */ 80 glob_t glob; /* globbing information */ 81 char *cmd; /* the current command */ 82 }; 83 84 static char *copynext __P((char *, char *)); 85 static int fcmp __P((const void *, const void *)); 86 static void formatf __P((struct afile *, int)); 87 static void getcmd __P((char *, char *, char *, int, struct arglist *)); 88 struct dirent *glob_readdir __P((RST_DIR *dirp)); 89 static int glob_stat __P((const char *, struct stat *)); 90 static void mkentry __P((char *, struct direct *, struct afile *)); 91 static void printlist __P((char *, char *)); 92 93 /* 94 * Read and execute commands from the terminal. 95 */ 96 void 97 runcmdshell() 98 { 99 register struct entry *np; 100 ino_t ino; 101 struct arglist arglist; 102 char curdir[MAXPATHLEN]; 103 char name[MAXPATHLEN]; 104 char cmd[BUFSIZ]; 105 106 arglist.freeglob = 0; 107 arglist.argcnt = 0; 108 arglist.glob.gl_flags = GLOB_ALTDIRFUNC; 109 arglist.glob.gl_opendir = (void *)rst_opendir; 110 arglist.glob.gl_readdir = (void *)glob_readdir; 111 arglist.glob.gl_closedir = (void *)rst_closedir; 112 arglist.glob.gl_lstat = glob_stat; 113 arglist.glob.gl_stat = glob_stat; 114 canon("/", curdir, sizeof(curdir)); 115 loop: 116 if (setjmp(reset) != 0) { 117 if (arglist.freeglob != 0) { 118 arglist.freeglob = 0; 119 arglist.argcnt = 0; 120 globfree(&arglist.glob); 121 } 122 nextarg = NULL; 123 volno = 0; 124 } 125 runshell = 1; 126 getcmd(curdir, cmd, name, sizeof(name), &arglist); 127 switch (cmd[0]) { 128 /* 129 * Add elements to the extraction list. 130 */ 131 case 'a': 132 if (strncmp(cmd, "add", strlen(cmd)) != 0) 133 goto bad; 134 ino = dirlookup(name); 135 if (ino == 0) 136 break; 137 if (mflag) 138 pathcheck(name); 139 treescan(name, ino, addfile); 140 break; 141 /* 142 * Change working directory. 143 */ 144 case 'c': 145 if (strncmp(cmd, "cd", strlen(cmd)) != 0) 146 goto bad; 147 ino = dirlookup(name); 148 if (ino == 0) 149 break; 150 if (inodetype(ino) == LEAF) { 151 fprintf(stderr, "%s: not a directory\n", name); 152 break; 153 } 154 (void) strcpy(curdir, name); 155 break; 156 /* 157 * Delete elements from the extraction list. 158 */ 159 case 'd': 160 if (strncmp(cmd, "delete", strlen(cmd)) != 0) 161 goto bad; 162 np = lookupname(name); 163 if (np == NULL || (np->e_flags & NEW) == 0) { 164 fprintf(stderr, "%s: not on extraction list\n", name); 165 break; 166 } 167 treescan(name, np->e_ino, deletefile); 168 break; 169 /* 170 * Extract the requested list. 171 */ 172 case 'e': 173 if (strncmp(cmd, "extract", strlen(cmd)) != 0) 174 goto bad; 175 createfiles(); 176 createlinks(); 177 setdirmodes(0); 178 if (dflag) 179 checkrestore(); 180 volno = 0; 181 break; 182 /* 183 * List available commands. 184 */ 185 case 'h': 186 if (strncmp(cmd, "help", strlen(cmd)) != 0) 187 goto bad; 188 case '?': 189 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s", 190 "Available commands are:\n", 191 "\tls [arg] - list directory\n", 192 "\tcd arg - change directory\n", 193 "\tpwd - print current directory\n", 194 "\tadd [arg] - add `arg' to list of", 195 " files to be extracted\n", 196 "\tdelete [arg] - delete `arg' from", 197 " list of files to be extracted\n", 198 "\textract - extract requested files\n", 199 "\tsetmodes - set modes of requested directories\n", 200 "\tquit - immediately exit program\n", 201 "\twhat - list dump header information\n", 202 "\tverbose - toggle verbose flag", 203 " (useful with ``ls'')\n", 204 "\thelp or `?' - print this list\n", 205 "If no `arg' is supplied, the current", 206 " directory is used\n"); 207 break; 208 /* 209 * List a directory. 210 */ 211 case 'l': 212 if (strncmp(cmd, "ls", strlen(cmd)) != 0) 213 goto bad; 214 printlist(name, curdir); 215 break; 216 /* 217 * Print current directory. 218 */ 219 case 'p': 220 if (strncmp(cmd, "pwd", strlen(cmd)) != 0) 221 goto bad; 222 if (curdir[1] == '\0') 223 fprintf(stderr, "/\n"); 224 else 225 fprintf(stderr, "%s\n", &curdir[1]); 226 break; 227 /* 228 * Quit. 229 */ 230 case 'q': 231 if (strncmp(cmd, "quit", strlen(cmd)) != 0) 232 goto bad; 233 return; 234 case 'x': 235 if (strncmp(cmd, "xit", strlen(cmd)) != 0) 236 goto bad; 237 return; 238 /* 239 * Toggle verbose mode. 240 */ 241 case 'v': 242 if (strncmp(cmd, "verbose", strlen(cmd)) != 0) 243 goto bad; 244 if (vflag) { 245 fprintf(stderr, "verbose mode off\n"); 246 vflag = 0; 247 break; 248 } 249 fprintf(stderr, "verbose mode on\n"); 250 vflag++; 251 break; 252 /* 253 * Just restore requested directory modes. 254 */ 255 case 's': 256 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0) 257 goto bad; 258 setdirmodes(FORCE); 259 break; 260 /* 261 * Print out dump header information. 262 */ 263 case 'w': 264 if (strncmp(cmd, "what", strlen(cmd)) != 0) 265 goto bad; 266 printdumpinfo(); 267 break; 268 /* 269 * Turn on debugging. 270 */ 271 case 'D': 272 if (strncmp(cmd, "Debug", strlen(cmd)) != 0) 273 goto bad; 274 if (dflag) { 275 fprintf(stderr, "debugging mode off\n"); 276 dflag = 0; 277 break; 278 } 279 fprintf(stderr, "debugging mode on\n"); 280 dflag++; 281 break; 282 /* 283 * Unknown command. 284 */ 285 default: 286 bad: 287 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd); 288 break; 289 } 290 goto loop; 291 } 292 293 /* 294 * Read and parse an interactive command. 295 * The first word on the line is assigned to "cmd". If 296 * there are no arguments on the command line, then "curdir" 297 * is returned as the argument. If there are arguments 298 * on the line they are returned one at a time on each 299 * successive call to getcmd. Each argument is first assigned 300 * to "name". If it does not start with "/" the pathname in 301 * "curdir" is prepended to it. Finally "canon" is called to 302 * eliminate any embedded ".." components. 303 */ 304 static void 305 getcmd(curdir, cmd, name, size, ap) 306 char *curdir, *cmd, *name; 307 struct arglist *ap; 308 int size; 309 { 310 register char *cp; 311 static char input[BUFSIZ]; 312 char output[BUFSIZ]; 313 # define rawname input /* save space by reusing input buffer */ 314 315 /* 316 * Check to see if still processing arguments. 317 */ 318 if (ap->argcnt > 0) 319 goto retnext; 320 if (nextarg != NULL) 321 goto getnext; 322 /* 323 * Read a command line and trim off trailing white space. 324 */ 325 do { 326 fprintf(stderr, "restore > "); 327 (void) fflush(stderr); 328 (void) fgets(input, BUFSIZ, terminal); 329 } while (!feof(terminal) && input[0] == '\n'); 330 if (feof(terminal)) { 331 (void) strcpy(cmd, "quit"); 332 return; 333 } 334 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 335 /* trim off trailing white space and newline */; 336 *++cp = '\0'; 337 /* 338 * Copy the command into "cmd". 339 */ 340 cp = copynext(input, cmd); 341 ap->cmd = cmd; 342 /* 343 * If no argument, use curdir as the default. 344 */ 345 if (*cp == '\0') { 346 (void) strcpy(name, curdir); 347 return; 348 } 349 nextarg = cp; 350 /* 351 * Find the next argument. 352 */ 353 getnext: 354 cp = copynext(nextarg, rawname); 355 if (*cp == '\0') 356 nextarg = NULL; 357 else 358 nextarg = cp; 359 /* 360 * If it is an absolute pathname, canonicalize it and return it. 361 */ 362 if (rawname[0] == '/') { 363 canon(rawname, name, size); 364 } else { 365 /* 366 * For relative pathnames, prepend the current directory to 367 * it then canonicalize and return it. 368 */ 369 snprintf(output, sizeof(output), "%s/%s", curdir, rawname); 370 canon(output, name, size); 371 } 372 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0) 373 fprintf(stderr, "%s: out of memory\n", ap->cmd); 374 if (ap->glob.gl_pathc == 0) 375 return; 376 ap->freeglob = 1; 377 ap->argcnt = ap->glob.gl_pathc; 378 379 retnext: 380 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]); 381 if (--ap->argcnt == 0) { 382 ap->freeglob = 0; 383 globfree(&ap->glob); 384 } 385 # undef rawname 386 } 387 388 /* 389 * Strip off the next token of the input. 390 */ 391 static char * 392 copynext(input, output) 393 char *input, *output; 394 { 395 register char *cp, *bp; 396 char quote; 397 398 for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 399 /* skip to argument */; 400 bp = output; 401 while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 402 /* 403 * Handle back slashes. 404 */ 405 if (*cp == '\\') { 406 if (*++cp == '\0') { 407 fprintf(stderr, 408 "command lines cannot be continued\n"); 409 continue; 410 } 411 *bp++ = *cp++; 412 continue; 413 } 414 /* 415 * The usual unquoted case. 416 */ 417 if (*cp != '\'' && *cp != '"') { 418 *bp++ = *cp++; 419 continue; 420 } 421 /* 422 * Handle single and double quotes. 423 */ 424 quote = *cp++; 425 while (*cp != quote && *cp != '\0') 426 *bp++ = *cp++ | 0200; 427 if (*cp++ == '\0') { 428 fprintf(stderr, "missing %c\n", quote); 429 cp--; 430 continue; 431 } 432 } 433 *bp = '\0'; 434 return (cp); 435 } 436 437 /* 438 * Canonicalize file names to always start with ``./'' and 439 * remove any embedded "." and ".." components. 440 */ 441 void 442 canon(rawname, canonname, len) 443 char *rawname, *canonname; 444 int len; 445 { 446 register char *cp, *np; 447 448 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 449 (void) strcpy(canonname, ""); 450 else if (rawname[0] == '/') 451 (void) strcpy(canonname, "."); 452 else 453 (void) strcpy(canonname, "./"); 454 if (strlen(canonname) + strlen(rawname) >= len) { 455 fprintf(stderr, "canonname: not enough buffer space\n"); 456 done(1); 457 } 458 459 (void) strcat(canonname, rawname); 460 /* 461 * Eliminate multiple and trailing '/'s 462 */ 463 for (cp = np = canonname; *np != '\0'; cp++) { 464 *cp = *np++; 465 while (*cp == '/' && *np == '/') 466 np++; 467 } 468 *cp = '\0'; 469 if (*--cp == '/') 470 *cp = '\0'; 471 /* 472 * Eliminate extraneous "." and ".." from pathnames. 473 */ 474 for (np = canonname; *np != '\0'; ) { 475 np++; 476 cp = np; 477 while (*np != '/' && *np != '\0') 478 np++; 479 if (np - cp == 1 && *cp == '.') { 480 cp--; 481 (void) strcpy(cp, np); 482 np = cp; 483 } 484 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 485 cp--; 486 while (cp > &canonname[1] && *--cp != '/') 487 /* find beginning of name */; 488 (void) strcpy(cp, np); 489 np = cp; 490 } 491 } 492 } 493 494 /* 495 * Do an "ls" style listing of a directory 496 */ 497 static void 498 printlist(name, basename) 499 char *name; 500 char *basename; 501 { 502 register struct afile *fp, *list, *listp; 503 register struct direct *dp; 504 struct afile single; 505 RST_DIR *dirp; 506 int entries, len, namelen; 507 char locname[MAXPATHLEN + 1]; 508 509 dp = pathsearch(name); 510 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 511 (!vflag && dp->d_ino == WINO)) 512 return; 513 if ((dirp = rst_opendir(name)) == NULL) { 514 entries = 1; 515 list = &single; 516 mkentry(name, dp, list); 517 len = strlen(basename) + 1; 518 if (strlen(name) - len > single.len) { 519 freename(single.fname); 520 single.fname = savename(&name[len]); 521 single.len = strlen(single.fname); 522 } 523 } else { 524 entries = 0; 525 while ((dp = rst_readdir(dirp))) 526 entries++; 527 rst_closedir(dirp); 528 list = (struct afile *)malloc(entries * sizeof(struct afile)); 529 if (list == NULL) { 530 fprintf(stderr, "ls: out of memory\n"); 531 return; 532 } 533 if ((dirp = rst_opendir(name)) == NULL) 534 panic("directory reopen failed\n"); 535 fprintf(stderr, "%s:\n", name); 536 entries = 0; 537 listp = list; 538 (void) strncpy(locname, name, MAXPATHLEN); 539 (void) strncat(locname, "/", MAXPATHLEN); 540 namelen = strlen(locname); 541 while ((dp = rst_readdir(dirp))) { 542 if (dp == NULL) 543 break; 544 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) 545 continue; 546 if (!vflag && (dp->d_ino == WINO || 547 strcmp(dp->d_name, ".") == 0 || 548 strcmp(dp->d_name, "..") == 0)) 549 continue; 550 locname[namelen] = '\0'; 551 if (namelen + dp->d_namlen >= MAXPATHLEN) { 552 fprintf(stderr, "%s%s: name exceeds %d char\n", 553 locname, dp->d_name, MAXPATHLEN); 554 } else { 555 (void) strncat(locname, dp->d_name, 556 (int)dp->d_namlen); 557 mkentry(locname, dp, listp++); 558 entries++; 559 } 560 } 561 rst_closedir(dirp); 562 if (entries == 0) { 563 fprintf(stderr, "\n"); 564 free(list); 565 return; 566 } 567 qsort((char *)list, entries, sizeof(struct afile), fcmp); 568 } 569 formatf(list, entries); 570 if (dirp != NULL) { 571 for (fp = listp - 1; fp >= list; fp--) 572 freename(fp->fname); 573 fprintf(stderr, "\n"); 574 free(list); 575 } 576 } 577 578 /* 579 * Read the contents of a directory. 580 */ 581 static void 582 mkentry(name, dp, fp) 583 char *name; 584 struct direct *dp; 585 register struct afile *fp; 586 { 587 char *cp; 588 struct entry *np; 589 590 fp->fnum = dp->d_ino; 591 fp->fname = savename(dp->d_name); 592 for (cp = fp->fname; *cp; cp++) 593 if (!vflag && (*cp < ' ' || *cp >= 0177)) 594 *cp = '?'; 595 fp->len = cp - fp->fname; 596 if (dflag && TSTINO(fp->fnum, dumpmap) == 0) 597 fp->prefix = '^'; 598 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW)) 599 fp->prefix = '*'; 600 else 601 fp->prefix = ' '; 602 switch(dp->d_type) { 603 604 default: 605 fprintf(stderr, "Warning: undefined file type %d\n", 606 dp->d_type); 607 /* fall through */ 608 case DT_REG: 609 fp->postfix = ' '; 610 break; 611 612 case DT_LNK: 613 fp->postfix = '@'; 614 break; 615 616 case DT_FIFO: 617 case DT_SOCK: 618 fp->postfix = '='; 619 break; 620 621 case DT_CHR: 622 case DT_BLK: 623 fp->postfix = '#'; 624 break; 625 626 case DT_WHT: 627 fp->postfix = '%'; 628 break; 629 630 case DT_UNKNOWN: 631 case DT_DIR: 632 if (inodetype(dp->d_ino) == NODE) 633 fp->postfix = '/'; 634 else 635 fp->postfix = ' '; 636 break; 637 } 638 return; 639 } 640 641 /* 642 * Print out a pretty listing of a directory 643 */ 644 static void 645 formatf(list, nentry) 646 register struct afile *list; 647 int nentry; 648 { 649 register struct afile *fp, *endlist; 650 int width, bigino, haveprefix, havepostfix; 651 int i, j, w, precision, columns, lines; 652 653 width = 0; 654 haveprefix = 0; 655 havepostfix = 0; 656 bigino = ROOTINO; 657 endlist = &list[nentry]; 658 for (fp = &list[0]; fp < endlist; fp++) { 659 if (bigino < fp->fnum) 660 bigino = fp->fnum; 661 if (width < fp->len) 662 width = fp->len; 663 if (fp->prefix != ' ') 664 haveprefix = 1; 665 if (fp->postfix != ' ') 666 havepostfix = 1; 667 } 668 if (haveprefix) 669 width++; 670 if (havepostfix) 671 width++; 672 if (vflag) { 673 for (precision = 0, i = bigino; i > 0; i /= 10) 674 precision++; 675 width += precision + 1; 676 } 677 width++; 678 columns = 81 / width; 679 if (columns == 0) 680 columns = 1; 681 lines = (nentry + columns - 1) / columns; 682 for (i = 0; i < lines; i++) { 683 for (j = 0; j < columns; j++) { 684 fp = &list[j * lines + i]; 685 if (vflag) { 686 fprintf(stderr, "%*d ", precision, fp->fnum); 687 fp->len += precision + 1; 688 } 689 if (haveprefix) { 690 putc(fp->prefix, stderr); 691 fp->len++; 692 } 693 fprintf(stderr, "%s", fp->fname); 694 if (havepostfix) { 695 putc(fp->postfix, stderr); 696 fp->len++; 697 } 698 if (fp + lines >= endlist) { 699 fprintf(stderr, "\n"); 700 break; 701 } 702 for (w = fp->len; w < width; w++) 703 putc(' ', stderr); 704 } 705 } 706 } 707 708 /* 709 * Skip over directory entries that are not on the tape 710 * 711 * First have to get definition of a dirent. 712 */ 713 #undef DIRBLKSIZ 714 #include <dirent.h> 715 #undef d_ino 716 717 struct dirent * 718 glob_readdir(dirp) 719 RST_DIR *dirp; 720 { 721 struct direct *dp; 722 static struct dirent adirent; 723 724 while ((dp = rst_readdir(dirp)) != NULL) { 725 if (!vflag && dp->d_ino == WINO) 726 continue; 727 if (dflag || TSTINO(dp->d_ino, dumpmap)) 728 break; 729 } 730 if (dp == NULL) 731 return (NULL); 732 adirent.d_fileno = dp->d_ino; 733 adirent.d_namlen = dp->d_namlen; 734 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1); 735 return (&adirent); 736 } 737 738 /* 739 * Return st_mode information in response to stat or lstat calls 740 */ 741 static int 742 glob_stat(name, stp) 743 const char *name; 744 struct stat *stp; 745 { 746 register struct direct *dp; 747 748 dp = pathsearch(name); 749 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 750 (!vflag && dp->d_ino == WINO)) 751 return (-1); 752 if (inodetype(dp->d_ino) == NODE) 753 stp->st_mode = IFDIR; 754 else 755 stp->st_mode = IFREG; 756 return (0); 757 } 758 759 /* 760 * Comparison routine for qsort. 761 */ 762 static int 763 fcmp(f1, f2) 764 register const void *f1, *f2; 765 { 766 return (strcmp(((struct afile *)f1)->fname, 767 ((struct afile *)f2)->fname)); 768 } 769 770 /* 771 * respond to interrupts 772 */ 773 void 774 onintr(signo) 775 int signo; 776 { 777 if (command == 'i' && runshell) 778 longjmp(reset, 1); 779 if (reply("restore interrupted, continue") == FAIL) 780 done(1); 781 } 782