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.6 2004/02/04 17:40:01 joerg 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(char *, char *); 81 static int fcmp(const void *, const void *); 82 static void formatf(struct afile *, int); 83 static void getcmd(char *, char *, char *, int, struct arglist *); 84 struct dirent *glob_readdir(RST_DIR *dirp); 85 static int glob_stat(const char *, struct stat *); 86 static void mkentry(char *, struct direct *, struct afile *); 87 static void printlist(char *, char *); 88 89 /* 90 * Read and execute commands from the terminal. 91 */ 92 void 93 runcmdshell(void) 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(char *curdir, char *cmd, char *name, int size, struct arglist *ap) 302 { 303 register char *cp; 304 static char input[BUFSIZ]; 305 char output[BUFSIZ]; 306 # define rawname input /* save space by reusing input buffer */ 307 308 /* 309 * Check to see if still processing arguments. 310 */ 311 if (ap->argcnt > 0) 312 goto retnext; 313 if (nextarg != NULL) 314 goto getnext; 315 /* 316 * Read a command line and trim off trailing white space. 317 */ 318 do { 319 fprintf(stderr, "restore > "); 320 (void) fflush(stderr); 321 if (fgets(input, BUFSIZ, terminal) == NULL) { 322 strcpy(cmd, "quit"); 323 return; 324 } 325 } while (input[0] == '\n'); 326 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) 327 /* trim off trailing white space and newline */; 328 *++cp = '\0'; 329 /* 330 * Copy the command into "cmd". 331 */ 332 cp = copynext(input, cmd); 333 ap->cmd = cmd; 334 /* 335 * If no argument, use curdir as the default. 336 */ 337 if (*cp == '\0') { 338 (void) strncpy(name, curdir, size); 339 name[size - 1] = '\0'; 340 return; 341 } 342 nextarg = cp; 343 /* 344 * Find the next argument. 345 */ 346 getnext: 347 cp = copynext(nextarg, rawname); 348 if (*cp == '\0') 349 nextarg = NULL; 350 else 351 nextarg = cp; 352 /* 353 * If it is an absolute pathname, canonicalize it and return it. 354 */ 355 if (rawname[0] == '/') { 356 canon(rawname, name, size); 357 } else { 358 /* 359 * For relative pathnames, prepend the current directory to 360 * it then canonicalize and return it. 361 */ 362 snprintf(output, sizeof(output), "%s/%s", curdir, rawname); 363 canon(output, name, size); 364 } 365 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0) 366 fprintf(stderr, "%s: out of memory\n", ap->cmd); 367 if (ap->glob.gl_pathc == 0) 368 return; 369 ap->freeglob = 1; 370 ap->argcnt = ap->glob.gl_pathc; 371 372 retnext: 373 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size); 374 name[size - 1] = '\0'; 375 if (--ap->argcnt == 0) { 376 ap->freeglob = 0; 377 globfree(&ap->glob); 378 } 379 # undef rawname 380 } 381 382 /* 383 * Strip off the next token of the input. 384 */ 385 static char * 386 copynext(char *input, char *output) 387 { 388 register char *cp, *bp; 389 char quote; 390 391 for (cp = input; *cp == ' ' || *cp == '\t'; cp++) 392 /* skip to argument */; 393 bp = output; 394 while (*cp != ' ' && *cp != '\t' && *cp != '\0') { 395 /* 396 * Handle back slashes. 397 */ 398 if (*cp == '\\') { 399 if (*++cp == '\0') { 400 fprintf(stderr, 401 "command lines cannot be continued\n"); 402 continue; 403 } 404 *bp++ = *cp++; 405 continue; 406 } 407 /* 408 * The usual unquoted case. 409 */ 410 if (*cp != '\'' && *cp != '"') { 411 *bp++ = *cp++; 412 continue; 413 } 414 /* 415 * Handle single and double quotes. 416 */ 417 quote = *cp++; 418 while (*cp != quote && *cp != '\0') 419 *bp++ = *cp++ | 0200; 420 if (*cp++ == '\0') { 421 fprintf(stderr, "missing %c\n", quote); 422 cp--; 423 continue; 424 } 425 } 426 *bp = '\0'; 427 return (cp); 428 } 429 430 /* 431 * Canonicalize file names to always start with ``./'' and 432 * remove any embedded "." and ".." components. 433 */ 434 void 435 canon(char *rawname, char *canonname, int len) 436 { 437 register char *cp, *np; 438 439 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0) 440 (void) strcpy(canonname, ""); 441 else if (rawname[0] == '/') 442 (void) strcpy(canonname, "."); 443 else 444 (void) strcpy(canonname, "./"); 445 if (strlen(canonname) + strlen(rawname) >= len) { 446 fprintf(stderr, "canonname: not enough buffer space\n"); 447 done(1); 448 } 449 450 (void) strcat(canonname, rawname); 451 /* 452 * Eliminate multiple and trailing '/'s 453 */ 454 for (cp = np = canonname; *np != '\0'; cp++) { 455 *cp = *np++; 456 while (*cp == '/' && *np == '/') 457 np++; 458 } 459 *cp = '\0'; 460 if (*--cp == '/') 461 *cp = '\0'; 462 /* 463 * Eliminate extraneous "." and ".." from pathnames. 464 */ 465 for (np = canonname; *np != '\0'; ) { 466 np++; 467 cp = np; 468 while (*np != '/' && *np != '\0') 469 np++; 470 if (np - cp == 1 && *cp == '.') { 471 cp--; 472 (void) strcpy(cp, np); 473 np = cp; 474 } 475 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) { 476 cp--; 477 while (cp > &canonname[1] && *--cp != '/') 478 /* find beginning of name */; 479 (void) strcpy(cp, np); 480 np = cp; 481 } 482 } 483 } 484 485 /* 486 * Do an "ls" style listing of a directory 487 */ 488 static void 489 printlist(char *name, char *basename) 490 { 491 register struct afile *fp, *list, *listp = NULL; 492 register struct direct *dp; 493 struct afile single; 494 RST_DIR *dirp; 495 int entries, len, namelen; 496 char locname[MAXPATHLEN + 1]; 497 498 dp = pathsearch(name); 499 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 500 (!vflag && dp->d_ino == WINO)) 501 return; 502 if ((dirp = rst_opendir(name)) == NULL) { 503 entries = 1; 504 list = &single; 505 mkentry(name, dp, list); 506 len = strlen(basename) + 1; 507 if (strlen(name) - len > single.len) { 508 freename(single.fname); 509 single.fname = savename(&name[len]); 510 single.len = strlen(single.fname); 511 } 512 } else { 513 entries = 0; 514 while ((dp = rst_readdir(dirp))) 515 entries++; 516 rst_closedir(dirp); 517 list = (struct afile *)malloc(entries * sizeof(struct afile)); 518 if (list == NULL) { 519 fprintf(stderr, "ls: out of memory\n"); 520 return; 521 } 522 if ((dirp = rst_opendir(name)) == NULL) 523 panic("directory reopen failed\n"); 524 fprintf(stderr, "%s:\n", name); 525 entries = 0; 526 listp = list; 527 (void) strncpy(locname, name, MAXPATHLEN); 528 (void) strncat(locname, "/", MAXPATHLEN); 529 namelen = strlen(locname); 530 while ((dp = rst_readdir(dirp))) { 531 if (dp == NULL) 532 break; 533 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) 534 continue; 535 if (!vflag && (dp->d_ino == WINO || 536 strcmp(dp->d_name, ".") == 0 || 537 strcmp(dp->d_name, "..") == 0)) 538 continue; 539 locname[namelen] = '\0'; 540 if (namelen + dp->d_namlen >= MAXPATHLEN) { 541 fprintf(stderr, "%s%s: name exceeds %d char\n", 542 locname, dp->d_name, MAXPATHLEN); 543 } else { 544 (void) strncat(locname, dp->d_name, 545 (int)dp->d_namlen); 546 mkentry(locname, dp, listp++); 547 entries++; 548 } 549 } 550 rst_closedir(dirp); 551 if (entries == 0) { 552 fprintf(stderr, "\n"); 553 free(list); 554 return; 555 } 556 qsort((char *)list, entries, sizeof(struct afile), fcmp); 557 } 558 formatf(list, entries); 559 if (dirp != NULL) { 560 for (fp = listp - 1; fp >= list; fp--) 561 freename(fp->fname); 562 fprintf(stderr, "\n"); 563 free(list); 564 } 565 } 566 567 /* 568 * Read the contents of a directory. 569 */ 570 static void 571 mkentry(char *name, struct direct *dp, 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 = lookupname(name)) != 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_WHT: 613 fp->postfix = '%'; 614 break; 615 616 case DT_UNKNOWN: 617 case DT_DIR: 618 if (inodetype(dp->d_ino) == NODE) 619 fp->postfix = '/'; 620 else 621 fp->postfix = ' '; 622 break; 623 } 624 return; 625 } 626 627 /* 628 * Print out a pretty listing of a directory 629 */ 630 static void 631 formatf(register struct afile *list, int nentry) 632 { 633 register struct afile *fp, *endlist; 634 int width, bigino, haveprefix, havepostfix; 635 int i, j, w, precision = 0, columns, lines; 636 637 width = 0; 638 haveprefix = 0; 639 havepostfix = 0; 640 bigino = ROOTINO; 641 endlist = &list[nentry]; 642 for (fp = &list[0]; fp < endlist; fp++) { 643 if (bigino < fp->fnum) 644 bigino = fp->fnum; 645 if (width < fp->len) 646 width = fp->len; 647 if (fp->prefix != ' ') 648 haveprefix = 1; 649 if (fp->postfix != ' ') 650 havepostfix = 1; 651 } 652 if (haveprefix) 653 width++; 654 if (havepostfix) 655 width++; 656 if (vflag) { 657 for (precision = 0, i = bigino; i > 0; i /= 10) 658 precision++; 659 width += precision + 1; 660 } 661 width++; 662 columns = 81 / width; 663 if (columns == 0) 664 columns = 1; 665 lines = (nentry + columns - 1) / columns; 666 for (i = 0; i < lines; i++) { 667 for (j = 0; j < columns; j++) { 668 fp = &list[j * lines + i]; 669 if (vflag) { 670 fprintf(stderr, "%*d ", precision, fp->fnum); 671 fp->len += precision + 1; 672 } 673 if (haveprefix) { 674 putc(fp->prefix, stderr); 675 fp->len++; 676 } 677 fprintf(stderr, "%s", fp->fname); 678 if (havepostfix) { 679 putc(fp->postfix, stderr); 680 fp->len++; 681 } 682 if (fp + lines >= endlist) { 683 fprintf(stderr, "\n"); 684 break; 685 } 686 for (w = fp->len; w < width; w++) 687 putc(' ', stderr); 688 } 689 } 690 } 691 692 /* 693 * Skip over directory entries that are not on the tape 694 * 695 * First have to get definition of a dirent. 696 */ 697 #undef DIRBLKSIZ 698 #include <dirent.h> 699 #undef d_ino 700 701 struct dirent * 702 glob_readdir(RST_DIR *dirp) 703 { 704 struct direct *dp; 705 static struct dirent adirent; 706 707 while ((dp = rst_readdir(dirp)) != NULL) { 708 if (!vflag && dp->d_ino == WINO) 709 continue; 710 if (dflag || TSTINO(dp->d_ino, dumpmap)) 711 break; 712 } 713 if (dp == NULL) 714 return (NULL); 715 adirent.d_fileno = dp->d_ino; 716 adirent.d_namlen = dp->d_namlen; 717 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1); 718 return (&adirent); 719 } 720 721 /* 722 * Return st_mode information in response to stat or lstat calls 723 */ 724 static int 725 glob_stat(const char *name, struct stat *stp) 726 { 727 register struct direct *dp; 728 729 dp = pathsearch(name); 730 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) || 731 (!vflag && dp->d_ino == WINO)) 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(register const void *f1, register const void *f2) 745 { 746 return (strcmp(((struct afile *)f1)->fname, 747 ((struct afile *)f2)->fname)); 748 } 749 750 /* 751 * respond to interrupts 752 */ 753 void 754 onintr(int signo) 755 { 756 if (command == 'i' && runshell) 757 longjmp(reset, 1); 758 if (reply("restore interrupted, continue") == FAIL) 759 done(1); 760 } 761