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