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. 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
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>
48 #include <vfs/ufs/dinode.h>
49 #include <vfs/ufs/dir.h>
50 #include <protocols/dumprestore.h>
55 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
58 * Things to handle interruptions.
62 static char *nextarg
= NULL
;
65 * Structure and routines associated with listing directories.
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 */
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.
98 struct arglist arglist
;
99 char curdir
[MAXPATHLEN
];
100 char name
[MAXPATHLEN
];
103 arglist
.freeglob
= 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
));
113 if (setjmp(reset
) != 0) {
114 if (arglist
.freeglob
!= 0) {
115 arglist
.freeglob
= 0;
117 globfree(&arglist
.glob
);
123 getcmd(curdir
, cmd
, name
, sizeof(name
), &arglist
);
126 * Add elements to the extraction list.
129 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
131 ino
= dirlookup(name
);
136 treescan(name
, ino
, addfile
);
139 * Change working directory.
142 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
144 ino
= dirlookup(name
);
147 if (inodetype(ino
) == LEAF
) {
148 fprintf(stderr
, "%s: not a directory\n", name
);
151 strcpy(curdir
, name
);
154 * Delete elements from the extraction list.
157 if (strncmp(cmd
, "delete", strlen(cmd
)) != 0)
159 np
= lookupname(name
);
160 if (np
== NULL
|| (np
->e_flags
& NEW
) == 0) {
161 fprintf(stderr
, "%s: not on extraction list\n", name
);
164 treescan(name
, np
->e_ino
, deletefile
);
167 * Extract the requested list.
170 if (strncmp(cmd
, "extract", strlen(cmd
)) != 0)
180 * List available commands.
183 if (strncmp(cmd
, "help", strlen(cmd
)) != 0)
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");
209 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
211 printlist(name
, curdir
);
214 * Print current directory.
217 if (strncmp(cmd
, "pwd", strlen(cmd
)) != 0)
219 if (curdir
[1] == '\0')
220 fprintf(stderr
, "/\n");
222 fprintf(stderr
, "%s\n", &curdir
[1]);
228 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
232 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
236 * Toggle verbose mode.
239 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
242 fprintf(stderr
, "verbose mode off\n");
246 fprintf(stderr
, "verbose mode on\n");
250 * Just restore requested directory modes.
253 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
258 * Print out dump header information.
261 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
269 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
272 fprintf(stderr
, "debugging mode off\n");
276 fprintf(stderr
, "debugging mode on\n");
284 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
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.
302 getcmd(char *curdir
, char *cmd
, char *name
, int size
, struct arglist
*ap
)
305 static char input
[BUFSIZ
];
307 # define rawname input /* save space by reusing input buffer */
310 * Check to see if still processing arguments.
317 * Read a command line and trim off trailing white space.
320 fprintf(stderr
, "restore > ");
322 if (fgets(input
, BUFSIZ
, terminal
) == NULL
) {
326 } while (input
[0] == '\n');
327 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
328 /* trim off trailing white space and newline */;
331 * Copy the command into "cmd".
333 cp
= copynext(input
, cmd
);
336 * If no argument, use curdir as the default.
339 strncpy(name
, curdir
, size
);
340 name
[size
- 1] = '\0';
345 * Find the next argument.
348 cp
= copynext(nextarg
, rawname
);
354 * If it is an absolute pathname, canonicalize it and return it.
356 if (rawname
[0] == '/') {
357 canon(rawname
, name
, size
);
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)
371 ap
->argcnt
= ap
->glob
.gl_pathc
;
374 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
375 name
[size
- 1] = '\0';
376 if (--ap
->argcnt
== 0) {
384 * Strip off the next token of the input.
387 copynext(char *input
, char *output
)
392 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
393 /* skip to argument */;
395 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
397 * Handle back slashes.
402 "command lines cannot be continued\n");
409 * The usual unquoted case.
411 if (*cp
!= '\'' && *cp
!= '"') {
416 * Handle single and double quotes.
419 while (*cp
!= quote
&& *cp
!= '\0')
420 *bp
++ = *cp
++ | 0200;
422 fprintf(stderr
, "missing %c\n", quote
);
432 * Canonicalize file names to always start with ``./'' and
433 * remove any embedded "." and ".." components.
436 canon(char *rawname
, char *canonname
, int len
)
440 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
441 strcpy(canonname
, "");
442 else if (rawname
[0] == '/')
443 strcpy(canonname
, ".");
445 strcpy(canonname
, "./");
446 if (strlen(canonname
) + strlen(rawname
) >= len
) {
447 fprintf(stderr
, "canonname: not enough buffer space\n");
451 strcat(canonname
, rawname
);
453 * Eliminate multiple and trailing '/'s
455 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
457 while (*cp
== '/' && *np
== '/')
464 * Eliminate extraneous "." and ".." from pathnames.
466 for (np
= canonname
; *np
!= '\0'; ) {
469 while (*np
!= '/' && *np
!= '\0')
471 if (np
- cp
== 1 && *cp
== '.') {
476 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
478 while (cp
> &canonname
[1] && *--cp
!= '/')
479 /* find beginning of name */;
487 * Do an "ls" style listing of a directory
490 printlist(char *name
, char *basename
)
492 struct afile
*fp
, *list
, *listp
= NULL
;
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
))
503 if ((dirp
= rst_opendir(name
)) == NULL
) {
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
);
515 while ((dp
= rst_readdir(dirp
)))
518 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
520 fprintf(stderr
, "ls: out of memory\n");
523 if ((dirp
= rst_opendir(name
)) == NULL
)
524 panic("directory reopen failed\n");
525 fprintf(stderr
, "%s:\n", name
);
528 strncpy(locname
, name
, MAXPATHLEN
);
529 strncat(locname
, "/", MAXPATHLEN
);
530 namelen
= strlen(locname
);
531 while ((dp
= rst_readdir(dirp
))) {
534 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
536 if (!vflag
&& (dp
->d_ino
== WINO
||
537 strcmp(dp
->d_name
, ".") == 0 ||
538 strcmp(dp
->d_name
, "..") == 0))
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
);
545 strncat(locname
, dp
->d_name
, (int)dp
->d_namlen
);
546 mkentry(locname
, dp
, listp
++);
552 fprintf(stderr
, "\n");
556 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
558 formatf(list
, entries
);
560 for (fp
= listp
- 1; fp
>= list
; fp
--)
562 fprintf(stderr
, "\n");
568 * Read the contents of a directory.
571 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
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))
581 fp
->len
= cp
- fp
->fname
;
582 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
584 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
591 fprintf(stderr
, "Warning: undefined file type %d\n",
618 if (inodetype(dp
->d_ino
) == NODE
)
628 * Print out a pretty listing of a directory
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
;
641 endlist
= &list
[nentry
];
642 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
643 if (bigino
< fp
->fnum
)
647 if (fp
->prefix
!= ' ')
649 if (fp
->postfix
!= ' ')
657 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
659 width
+= precision
+ 1;
662 columns
= 81 / width
;
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
];
670 fprintf(stderr
, "%*d ", precision
, fp
->fnum
);
671 fp
->len
+= precision
+ 1;
674 putc(fp
->prefix
, stderr
);
677 fprintf(stderr
, "%s", fp
->fname
);
679 putc(fp
->postfix
, stderr
);
682 if (fp
+ lines
>= endlist
) {
683 fprintf(stderr
, "\n");
686 for (w
= fp
->len
; w
< width
; w
++)
693 * Skip over directory entries that are not on the tape
695 * First have to get definition of a dirent.
700 glob_readdir(RST_DIR
*dirp
)
703 uint8_t storage
[_DIRENT_RECLEN(NAME_MAX
)];
704 struct dirent aligment
;
709 while ((dp
= rst_readdir(dirp
)) != NULL
) {
710 if (!vflag
&& dp
->d_ino
== WINO
)
712 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
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
);
725 * Return st_mode information in response to stat or lstat calls
728 glob_stat(const char *name
, struct stat
*stp
)
732 dp
= pathsearch(name
);
733 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
734 (!vflag
&& dp
->d_ino
== WINO
))
736 if (inodetype(dp
->d_ino
) == NODE
)
737 stp
->st_mode
= IFDIR
;
739 stp
->st_mode
= IFREG
;
744 * Comparison routine for qsort.
747 fcmp(const void *f1
, const void *f2
)
749 return (strcmp(((struct afile
*)f1
)->fname
,
750 ((struct afile
*)f2
)->fname
));
754 * respond to interrupts
759 if (command
== 'i' && runshell
)
761 if (reply("restore interrupted, continue") == FAIL
)