ext2fs - A few bug fixes and syntax adjustments.
[dragonfly.git] / sbin / restore / interactive.c
blob75f435abf8cc3abc845d6a303a876273dbd4e920
1 /*
2 * Copyright (c) 1985, 1993
3 * The Regents of the University of California. All rights reserved.
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.
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.
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.10 2005/11/06 12:49:25 swildner Exp $
38 #include <sys/param.h>
39 #include <sys/stat.h>
41 #include <dirent.h>
42 #include <setjmp.h>
43 #include <glob.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
52 #include "restore.h"
53 #include "extern.h"
55 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
58 * Things to handle interruptions.
60 static int runshell;
61 static jmp_buf reset;
62 static char *nextarg = NULL;
65 * Structure and routines associated with listing directories.
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 */
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 */
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 *);
91 * Read and execute commands from the terminal.
93 void
94 runcmdshell(void)
96 struct entry *np;
97 ufs1_ino_t ino;
98 struct arglist arglist;
99 char curdir[MAXPATHLEN];
100 char name[MAXPATHLEN];
101 char cmd[BUFSIZ];
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);
119 nextarg = NULL;
120 volno = 0;
122 runshell = 1;
123 getcmd(curdir, cmd, name, sizeof(name), &arglist);
124 switch (cmd[0]) {
126 * Add elements to the extraction list.
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;
139 * Change working directory.
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;
151 strcpy(curdir, name);
152 break;
154 * Delete elements from the extraction list.
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;
164 treescan(name, np->e_ino, deletefile);
165 break;
167 * Extract the requested list.
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;
180 * List available commands.
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;
206 * List a directory.
208 case 'l':
209 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
210 goto bad;
211 printlist(name, curdir);
212 break;
214 * Print current directory.
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;
225 * Quit.
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;
236 * Toggle verbose mode.
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;
246 fprintf(stderr, "verbose mode on\n");
247 vflag++;
248 break;
250 * Just restore requested directory modes.
252 case 's':
253 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
254 goto bad;
255 setdirmodes(FORCE);
256 break;
258 * Print out dump header information.
260 case 'w':
261 if (strncmp(cmd, "what", strlen(cmd)) != 0)
262 goto bad;
263 printdumpinfo();
264 break;
266 * Turn on debugging.
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;
276 fprintf(stderr, "debugging mode on\n");
277 dflag++;
278 break;
280 * Unknown command.
282 default:
283 bad:
284 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
285 break;
287 goto loop;
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.
301 static void
302 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
304 char *cp;
305 static char input[BUFSIZ];
306 char output[BUFSIZ];
307 # define rawname input /* save space by reusing input buffer */
310 * Check to see if still processing arguments.
312 if (ap->argcnt > 0)
313 goto retnext;
314 if (nextarg != NULL)
315 goto getnext;
317 * Read a command line and trim off trailing white space.
319 do {
320 fprintf(stderr, "restore > ");
321 fflush(stderr);
322 if (fgets(input, BUFSIZ, terminal) == NULL) {
323 strcpy(cmd, "quit");
324 return;
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';
331 * Copy the command into "cmd".
333 cp = copynext(input, cmd);
334 ap->cmd = cmd;
336 * If no argument, use curdir as the default.
338 if (*cp == '\0') {
339 strncpy(name, curdir, size);
340 name[size - 1] = '\0';
341 return;
343 nextarg = cp;
345 * Find the next argument.
347 getnext:
348 cp = copynext(nextarg, rawname);
349 if (*cp == '\0')
350 nextarg = NULL;
351 else
352 nextarg = cp;
354 * If it is an absolute pathname, canonicalize it and return it.
356 if (rawname[0] == '/') {
357 canon(rawname, name, size);
358 } else {
360 * For relative pathnames, prepend the current directory to
361 * it then canonicalize and return it.
363 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
364 canon(output, name, size);
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;
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);
380 # undef rawname
384 * Strip off the next token of the input.
386 static char *
387 copynext(char *input, char *output)
389 char *cp, *bp;
390 char quote;
392 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
393 /* skip to argument */;
394 bp = output;
395 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
397 * Handle back slashes.
399 if (*cp == '\\') {
400 if (*++cp == '\0') {
401 fprintf(stderr,
402 "command lines cannot be continued\n");
403 continue;
405 *bp++ = *cp++;
406 continue;
409 * The usual unquoted case.
411 if (*cp != '\'' && *cp != '"') {
412 *bp++ = *cp++;
413 continue;
416 * Handle single and double quotes.
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;
427 *bp = '\0';
428 return (cp);
432 * Canonicalize file names to always start with ``./'' and
433 * remove any embedded "." and ".." components.
435 void
436 canon(char *rawname, char *canonname, int len)
438 char *cp, *np;
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);
451 strcat(canonname, rawname);
453 * Eliminate multiple and trailing '/'s
455 for (cp = np = canonname; *np != '\0'; cp++) {
456 *cp = *np++;
457 while (*cp == '/' && *np == '/')
458 np++;
460 *cp = '\0';
461 if (*--cp == '/')
462 *cp = '\0';
464 * Eliminate extraneous "." and ".." from pathnames.
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;
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;
487 * Do an "ls" style listing of a directory
489 static void
490 printlist(char *name, char *basename)
492 struct afile *fp, *list, *listp = NULL;
493 struct direct *dp;
494 struct afile single;
495 RST_DIR *dirp;
496 int entries, len, namelen;
497 char locname[MAXPATHLEN + 1];
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);
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;
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++;
550 rst_closedir(dirp);
551 if (entries == 0) {
552 fprintf(stderr, "\n");
553 free(list);
554 return;
556 qsort((char *)list, entries, sizeof(struct afile), fcmp);
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);
568 * Read the contents of a directory.
570 static void
571 mkentry(char *name, struct direct *dp, struct afile *fp)
573 char *cp;
574 struct entry *np;
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) {
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;
598 case DT_LNK:
599 fp->postfix = '@';
600 break;
602 case DT_FIFO:
603 case DT_SOCK:
604 fp->postfix = '=';
605 break;
607 case DT_CHR:
608 case DT_BLK:
609 fp->postfix = '#';
610 break;
612 case DT_WHT:
613 fp->postfix = '%';
614 break;
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;
624 return;
628 * Print out a pretty listing of a directory
630 static void
631 formatf(struct afile *list, int nentry)
633 struct afile *fp, *endlist;
634 int width, bigino, haveprefix, havepostfix;
635 int i, j, w, precision = 0, columns, lines;
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;
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;
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;
673 if (haveprefix) {
674 putc(fp->prefix, stderr);
675 fp->len++;
677 fprintf(stderr, "%s", fp->fname);
678 if (havepostfix) {
679 putc(fp->postfix, stderr);
680 fp->len++;
682 if (fp + lines >= endlist) {
683 fprintf(stderr, "\n");
684 break;
686 for (w = fp->len; w < width; w++)
687 putc(' ', stderr);
693 * Skip over directory entries that are not on the tape
695 * First have to get definition of a dirent.
697 #undef DIRBLKSIZ
699 struct dirent *
700 glob_readdir(RST_DIR *dirp)
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;
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;
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);
725 * Return st_mode information in response to stat or lstat calls
727 static int
728 glob_stat(const char *name, struct stat *stp)
730 struct direct *dp;
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);
744 * Comparison routine for qsort.
746 static int
747 fcmp(const void *f1, const void *f2)
749 return (strcmp(((struct afile *)f1)->fname,
750 ((struct afile *)f2)->fname));
754 * respond to interrupts
756 void
757 onintr(int signo)
759 if (command == 'i' && runshell)
760 longjmp(reset, 1);
761 if (reply("restore interrupted, continue") == FAIL)
762 done(1);