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