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