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
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)interactive.c 8.5 (Berkeley) 5/1/95
30 * $FreeBSD: src/sbin/restore/interactive.c,v 1.8.2.1 2001/01/03 14:36:08 iedowse Exp $
31 * $DragonFly: src/sbin/restore/interactive.c,v 1.10 2005/11/06 12:49:25 swildner Exp $
34 #include <sys/param.h>
44 #include <vfs/ufs/dinode.h>
45 #include <vfs/ufs/dir.h>
46 #include <protocols/dumprestore.h>
51 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
54 * Things to handle interruptions.
58 static char *nextarg = NULL;
61 * Structure and routines associated with listing directories.
64 ufs1_ino_t fnum; /* inode number of file */
65 char *fname; /* file name */
66 short len; /* name length */
67 char prefix; /* prefix character */
68 char postfix; /* postfix character */
71 int freeglob; /* glob structure needs to be freed */
72 int argcnt; /* next globbed argument to return */
73 glob_t glob; /* globbing information */
74 char *cmd; /* the current command */
77 static char *copynext(char *, char *);
78 static int fcmp(const void *, const void *);
79 static void formatf(struct afile *, int);
80 static void getcmd(char *, char *, char *, int, struct arglist *);
81 struct dirent *glob_readdir(RST_DIR *dirp);
82 static int glob_stat(const char *, struct stat *);
83 static void mkentry(char *, struct direct *, struct afile *);
84 static void printlist(char *, char *);
87 * Read and execute commands from the terminal.
94 struct arglist arglist;
95 char curdir[MAXPATHLEN];
96 char name[MAXPATHLEN];
101 arglist.glob.gl_flags = GLOB_ALTDIRFUNC;
102 arglist.glob.gl_opendir = (void *)rst_opendir;
103 arglist.glob.gl_readdir = (void *)glob_readdir;
104 arglist.glob.gl_closedir = (void *)rst_closedir;
105 arglist.glob.gl_lstat = glob_stat;
106 arglist.glob.gl_stat = glob_stat;
107 canon("/", curdir, sizeof(curdir));
109 if (setjmp(reset) != 0) {
110 if (arglist.freeglob != 0) {
111 arglist.freeglob = 0;
113 globfree(&arglist.glob);
119 getcmd(curdir, cmd, name, sizeof(name), &arglist);
122 * Add elements to the extraction list.
125 if (strncmp(cmd, "add", strlen(cmd)) != 0)
127 ino = dirlookup(name);
132 treescan(name, ino, addfile);
135 * Change working directory.
138 if (strncmp(cmd, "cd", strlen(cmd)) != 0)
140 ino = dirlookup(name);
143 if (inodetype(ino) == LEAF) {
144 fprintf(stderr, "%s: not a directory\n", name);
147 strcpy(curdir, name);
150 * Delete elements from the extraction list.
153 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
155 np = lookupname(name);
156 if (np == NULL || (np->e_flags & NEW) == 0) {
157 fprintf(stderr, "%s: not on extraction list\n", name);
160 treescan(name, np->e_ino, deletefile);
163 * Extract the requested list.
166 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
176 * List available commands.
179 if (strncmp(cmd, "help", strlen(cmd)) != 0)
182 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
183 "Available commands are:\n",
184 "\tls [arg] - list directory\n",
185 "\tcd arg - change directory\n",
186 "\tpwd - print current directory\n",
187 "\tadd [arg] - add `arg' to list of",
188 " files to be extracted\n",
189 "\tdelete [arg] - delete `arg' from",
190 " list of files to be extracted\n",
191 "\textract - extract requested files\n",
192 "\tsetmodes - set modes of requested directories\n",
193 "\tquit - immediately exit program\n",
194 "\twhat - list dump header information\n",
195 "\tverbose - toggle verbose flag",
196 " (useful with ``ls'')\n",
197 "\thelp or `?' - print this list\n",
198 "If no `arg' is supplied, the current",
199 " directory is used\n");
205 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
207 printlist(name, curdir);
210 * Print current directory.
213 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
215 if (curdir[1] == '\0')
216 fprintf(stderr, "/\n");
218 fprintf(stderr, "%s\n", &curdir[1]);
224 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
228 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
232 * Toggle verbose mode.
235 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
238 fprintf(stderr, "verbose mode off\n");
242 fprintf(stderr, "verbose mode on\n");
246 * Just restore requested directory modes.
249 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
254 * Print out dump header information.
257 if (strncmp(cmd, "what", strlen(cmd)) != 0)
265 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
268 fprintf(stderr, "debugging mode off\n");
272 fprintf(stderr, "debugging mode on\n");
280 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
287 * Read and parse an interactive command.
288 * The first word on the line is assigned to "cmd". If
289 * there are no arguments on the command line, then "curdir"
290 * is returned as the argument. If there are arguments
291 * on the line they are returned one at a time on each
292 * successive call to getcmd. Each argument is first assigned
293 * to "name". If it does not start with "/" the pathname in
294 * "curdir" is prepended to it. Finally "canon" is called to
295 * eliminate any embedded ".." components.
298 getcmd(char *curdir, char *cmd, char *name, int size, struct arglist *ap)
301 static char input[BUFSIZ];
303 # define rawname input /* save space by reusing input buffer */
306 * Check to see if still processing arguments.
313 * Read a command line and trim off trailing white space.
316 fprintf(stderr, "restore > ");
318 if (fgets(input, BUFSIZ, terminal) == NULL) {
322 } while (input[0] == '\n');
323 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
324 /* trim off trailing white space and newline */;
327 * Copy the command into "cmd".
329 cp = copynext(input, cmd);
332 * If no argument, use curdir as the default.
335 strncpy(name, curdir, size);
336 name[size - 1] = '\0';
341 * Find the next argument.
344 cp = copynext(nextarg, rawname);
350 * If it is an absolute pathname, canonicalize it and return it.
352 if (rawname[0] == '/') {
353 canon(rawname, name, size);
356 * For relative pathnames, prepend the current directory to
357 * it then canonicalize and return it.
359 snprintf(output, sizeof(output), "%s/%s", curdir, rawname);
360 canon(output, name, size);
362 if (glob(name, GLOB_ALTDIRFUNC, NULL, &ap->glob) < 0)
363 fprintf(stderr, "%s: out of memory\n", ap->cmd);
364 if (ap->glob.gl_pathc == 0)
367 ap->argcnt = ap->glob.gl_pathc;
370 strncpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt], size);
371 name[size - 1] = '\0';
372 if (--ap->argcnt == 0) {
380 * Strip off the next token of the input.
383 copynext(char *input, char *output)
388 for (cp = input; *cp == ' ' || *cp == '\t'; cp++)
389 /* skip to argument */;
391 while (*cp != ' ' && *cp != '\t' && *cp != '\0') {
393 * Handle back slashes.
398 "command lines cannot be continued\n");
405 * The usual unquoted case.
407 if (*cp != '\'' && *cp != '"') {
412 * Handle single and double quotes.
415 while (*cp != quote && *cp != '\0')
416 *bp++ = *cp++ | 0200;
418 fprintf(stderr, "missing %c\n", quote);
428 * Canonicalize file names to always start with ``./'' and
429 * remove any embedded "." and ".." components.
432 canon(char *rawname, char *canonname, int len)
436 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
437 strcpy(canonname, "");
438 else if (rawname[0] == '/')
439 strcpy(canonname, ".");
441 strcpy(canonname, "./");
442 if (strlen(canonname) + strlen(rawname) >= len) {
443 fprintf(stderr, "canonname: not enough buffer space\n");
447 strcat(canonname, rawname);
449 * Eliminate multiple and trailing '/'s
451 for (cp = np = canonname; *np != '\0'; cp++) {
453 while (*cp == '/' && *np == '/')
460 * Eliminate extraneous "." and ".." from pathnames.
462 for (np = canonname; *np != '\0'; ) {
465 while (*np != '/' && *np != '\0')
467 if (np - cp == 1 && *cp == '.') {
472 if (np - cp == 2 && strncmp(cp, "..", 2) == 0) {
474 while (cp > &canonname[1] && *--cp != '/')
475 /* find beginning of name */;
483 * Do an "ls" style listing of a directory
486 printlist(char *name, char *basename)
488 struct afile *fp, *list, *listp = NULL;
492 int entries, len, namelen;
493 char locname[MAXPATHLEN + 1];
495 dp = pathsearch(name);
496 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
497 (!vflag && dp->d_ino == WINO))
499 if ((dirp = rst_opendir(name)) == NULL) {
502 mkentry(name, dp, list);
503 len = strlen(basename) + 1;
504 if (strlen(name) - len > single.len) {
505 freename(single.fname);
506 single.fname = savename(&name[len]);
507 single.len = strlen(single.fname);
511 while ((dp = rst_readdir(dirp)))
514 list = (struct afile *)malloc(entries * sizeof(struct afile));
516 fprintf(stderr, "ls: out of memory\n");
519 if ((dirp = rst_opendir(name)) == NULL)
520 panic("directory reopen failed\n");
521 fprintf(stderr, "%s:\n", name);
524 strncpy(locname, name, MAXPATHLEN);
525 strncat(locname, "/", MAXPATHLEN);
526 namelen = strlen(locname);
527 while ((dp = rst_readdir(dirp))) {
530 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
532 if (!vflag && (dp->d_ino == WINO ||
533 strcmp(dp->d_name, ".") == 0 ||
534 strcmp(dp->d_name, "..") == 0))
536 locname[namelen] = '\0';
537 if (namelen + dp->d_namlen >= MAXPATHLEN) {
538 fprintf(stderr, "%s%s: name exceeds %d char\n",
539 locname, dp->d_name, MAXPATHLEN);
541 strncat(locname, dp->d_name, (int)dp->d_namlen);
542 mkentry(locname, dp, listp++);
548 fprintf(stderr, "\n");
552 qsort((char *)list, entries, sizeof(struct afile), fcmp);
554 formatf(list, entries);
556 for (fp = listp - 1; fp >= list; fp--)
558 fprintf(stderr, "\n");
564 * Read the contents of a directory.
567 mkentry(char *name, struct direct *dp, struct afile *fp)
572 fp->fnum = dp->d_ino;
573 fp->fname = savename(dp->d_name);
574 for (cp = fp->fname; *cp; cp++)
575 if (!vflag && (*cp < ' ' || *cp >= 0177))
577 fp->len = cp - fp->fname;
578 if (dflag && TSTINO(fp->fnum, dumpmap) == 0)
580 else if ((np = lookupname(name)) != NULL && (np->e_flags & NEW))
587 fprintf(stderr, "Warning: undefined file type %d\n",
614 if (inodetype(dp->d_ino) == NODE)
624 * Print out a pretty listing of a directory
627 formatf(struct afile *list, int nentry)
629 struct afile *fp, *endlist;
630 int width, bigino, haveprefix, havepostfix;
631 int i, j, w, precision = 0, columns, lines;
637 endlist = &list[nentry];
638 for (fp = &list[0]; fp < endlist; fp++) {
639 if (bigino < fp->fnum)
643 if (fp->prefix != ' ')
645 if (fp->postfix != ' ')
653 for (precision = 0, i = bigino; i > 0; i /= 10)
655 width += precision + 1;
658 columns = 81 / width;
661 lines = (nentry + columns - 1) / columns;
662 for (i = 0; i < lines; i++) {
663 for (j = 0; j < columns; j++) {
664 fp = &list[j * lines + i];
666 fprintf(stderr, "%*d ", precision, fp->fnum);
667 fp->len += precision + 1;
670 putc(fp->prefix, stderr);
673 fprintf(stderr, "%s", fp->fname);
675 putc(fp->postfix, stderr);
678 if (fp + lines >= endlist) {
679 fprintf(stderr, "\n");
682 for (w = fp->len; w < width; w++)
689 * Skip over directory entries that are not on the tape
691 * First have to get definition of a dirent.
696 glob_readdir(RST_DIR *dirp)
699 ADIRENT_STORAGE_LEN = _DIRENT_RECLEN(NAME_MAX),
702 uint8_t storage[ADIRENT_STORAGE_LEN];
703 struct dirent alignment;
708 while ((dp = rst_readdir(dirp)) != NULL) {
709 if (!vflag && dp->d_ino == WINO)
711 if (dflag || TSTINO(dp->d_ino, dumpmap))
716 adp = (struct dirent *)&adirent;
717 adp->d_ino = dp->d_ino;
718 adp->d_namlen = dp->d_namlen;
719 strcpy(adp->d_name, dp->d_name);
724 * Return st_mode information in response to stat or lstat calls
727 glob_stat(const char *name, struct stat *stp)
731 dp = pathsearch(name);
732 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
733 (!vflag && dp->d_ino == WINO))
735 if (inodetype(dp->d_ino) == NODE)
736 stp->st_mode = IFDIR;
738 stp->st_mode = IFREG;
743 * Comparison routine for qsort.
746 fcmp(const void *f1, const void *f2)
748 return (strcmp(((struct afile *)f1)->fname,
749 ((struct afile *)f2)->fname));
753 * respond to interrupts
758 if (command == 'i' && runshell)
760 if (reply("restore interrupted, continue") == FAIL)