]>
git.wh0rd.org - dump.git/blob - restore/interactive.c
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <pop@cybercable.fr>, 1999-2000
9 * Copyright (c) 1985, 1993
10 * The Regents of the University of California. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char rcsid
[] =
43 "$Id: interactive.c,v 1.10 2000/05/28 17:50:27 stelian Exp $";
46 #include <sys/param.h>
50 #include <linux/ext2_fs.h>
51 #include <bsdcompat.h>
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ufs/dir.h>
55 #endif /* __linux__ */
56 #include <protocols/dumprestore.h>
59 #include <compaterr.h>
61 #include <compatglob.h>
67 #include <ext2fs/ext2fs.h>
68 extern char * __progname
;
74 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
77 * Things to handle interruptions.
81 static char *nextarg
= NULL
;
82 static int pflag
= 0; /* prompt mode */
84 * Structure and routines associated with listing directories.
87 ino_t fnum
; /* inode number of file */
88 char *fname
; /* file name */
89 short len
; /* name length */
90 char prefix
; /* prefix character */
91 char postfix
; /* postfix character */
94 int freeglob
; /* glob structure needs to be freed */
95 int argcnt
; /* next globbed argument to return */
96 glob_t glob
; /* globbing information */
97 char *cmd
; /* the current command */
100 static char *copynext
__P((char *, char *));
101 static int fcmp
__P((const void *, const void *));
102 static void formatf
__P((struct afile
*, int));
103 static void getcmd
__P((char *, char *, char *, int, struct arglist
*));
104 struct dirent
*glob_readdir
__P((RST_DIR
*dirp
));
105 static int glob_stat
__P((const char *, struct stat
*));
106 static void mkentry
__P((char *, struct direct
*, struct afile
*));
107 static void printlist
__P((char *, char *));
110 * Read and execute commands from the terminal.
115 register struct entry
*np
;
117 struct arglist arglist
;
118 char curdir
[MAXPATHLEN
];
119 char name
[MAXPATHLEN
];
122 arglist
.freeglob
= 0;
124 arglist
.glob
.gl_flags
= GLOB_ALTDIRFUNC
;
125 arglist
.glob
.gl_opendir
= (void *)rst_opendir
;
126 arglist
.glob
.gl_readdir
= (void *)glob_readdir
;
127 arglist
.glob
.gl_closedir
= (void *)rst_closedir
;
128 arglist
.glob
.gl_lstat
= glob_stat
;
129 arglist
.glob
.gl_stat
= glob_stat
;
130 canon("/", curdir
, sizeof(curdir
));
132 if (setjmp(reset
) != 0) {
133 if (arglist
.freeglob
!= 0) {
134 arglist
.freeglob
= 0;
136 globfree(&arglist
.glob
);
142 getcmd(curdir
, cmd
, name
, sizeof(name
), &arglist
);
145 * Add elements to the extraction list.
148 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
150 ino
= dirlookup(name
);
155 treescan(name
, ino
, addfile
);
158 * Change working directory.
161 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
163 ino
= dirlookup(name
);
166 if (inodetype(ino
) == LEAF
) {
167 fprintf(stderr
, "%s: not a directory\n", name
);
170 (void) strncpy(curdir
, name
, sizeof(curdir
));
171 curdir
[sizeof(curdir
) - 1] = '\0';
174 * Delete elements from the extraction list.
177 if (strncmp(cmd
, "delete", strlen(cmd
)) != 0)
179 np
= lookupname(name
);
180 if (np
== NULL
|| (np
->e_flags
& NEW
) == 0) {
181 fprintf(stderr
, "%s: not on extraction list\n", name
);
184 treescan(name
, np
->e_ino
, deletefile
);
187 * Extract the requested list.
190 if (strncmp(cmd
, "extract", strlen(cmd
)) != 0)
200 * List available commands.
203 if (strncmp(cmd
, "help", strlen(cmd
)) != 0)
206 fprintf(stderr
, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
207 "Available commands are:\n",
208 "\tls [arg] - list directory\n",
209 "\tcd arg - change directory\n",
210 "\tpwd - print current directory\n",
211 "\tadd [arg] - add `arg' to list of",
212 " files to be extracted\n",
213 "\tdelete [arg] - delete `arg' from",
214 " list of files to be extracted\n",
215 "\textract - extract requested files\n",
216 "\tsetmodes - set modes of requested directories\n",
217 "\tquit - immediately exit program\n",
218 "\twhat - list dump header information\n",
219 "\tverbose - toggle verbose flag",
220 " (useful with ``ls'')\n",
221 "\tprompt - toggle the prompt display\n",
222 "\thelp or `?' - print this list\n",
223 "If no `arg' is supplied, the current",
224 " directory is used\n");
230 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
232 printlist(name
, curdir
);
235 * Print current directory.
238 if (strncmp(cmd
, "pwd", strlen(cmd
)) == 0) {
239 if (curdir
[1] == '\0')
240 fprintf(stderr
, "/\n");
242 fprintf(stderr
, "%s\n", &curdir
[1]);
245 * Toggle prompt mode.
247 else if (strncmp(cmd
, "prompt", strlen(cmd
)) == 0) {
249 fprintf(stderr
, "prompt mode off\n");
253 fprintf(stderr
, "prompt mode on\n");
263 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
267 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
271 * Toggle verbose mode.
274 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
277 fprintf(stderr
, "verbose mode off\n");
281 fprintf(stderr
, "verbose mode on\n");
285 * Just restore requested directory modes.
288 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
293 * Print out dump header information.
296 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
304 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
307 fprintf(stderr
, "debugging mode off\n");
311 fprintf(stderr
, "debugging mode on\n");
319 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
326 * Read and parse an interactive command.
327 * The first word on the line is assigned to "cmd". If
328 * there are no arguments on the command line, then "curdir"
329 * is returned as the argument. If there are arguments
330 * on the line they are returned one at a time on each
331 * successive call to getcmd. Each argument is first assigned
332 * to "name". If it does not start with "/" the pathname in
333 * "curdir" is prepended to it. Finally "canon" is called to
334 * eliminate any embedded ".." components.
337 getcmd(char *curdir
, char *cmd
, char *name
, int size
, struct arglist
*ap
)
340 static char input
[BUFSIZ
];
342 # define rawname input /* save space by reusing input buffer */
345 * Check to see if still processing arguments.
352 * Read a command line and trim off trailing white space.
356 fprintf(stderr
, "%s:%s:%s > ",
359 curdir
[1] ? &curdir
[1] : "/");
361 fprintf(stderr
, "%s > ", __progname
);
362 (void) fflush(stderr
);
363 (void) fgets(input
, BUFSIZ
, terminal
);
364 } while (!feof(terminal
) && input
[0] == '\n');
365 if (feof(terminal
)) {
366 (void) strcpy(cmd
, "quit");
369 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
370 /* trim off trailing white space and newline */;
373 * Copy the command into "cmd".
375 cp
= copynext(input
, cmd
);
378 * If no argument, use curdir as the default.
381 (void) strncpy(name
, curdir
, size
);
382 name
[size
- 1] = '\0';
387 * Find the next argument.
390 cp
= copynext(nextarg
, rawname
);
396 * If it is an absolute pathname, canonicalize it and return it.
398 if (rawname
[0] == '/') {
399 canon(rawname
, name
, size
);
402 * For relative pathnames, prepend the current directory to
403 * it then canonicalize and return it.
405 snprintf(output
, sizeof(output
), "%s/%s", curdir
, rawname
);
406 canon(output
, name
, size
);
408 if (glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
) < 0)
409 fprintf(stderr
, "%s: out of memory\n", ap
->cmd
);
410 if (ap
->glob
.gl_pathc
== 0)
413 ap
->argcnt
= ap
->glob
.gl_pathc
;
416 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
417 name
[size
- 1] = '\0';
418 if (--ap
->argcnt
== 0) {
426 * Strip off the next token of the input.
429 copynext(char *input
, char *output
)
431 register char *cp
, *bp
;
434 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
435 /* skip to argument */;
437 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
439 * Handle back slashes.
444 "command lines cannot be continued\n");
451 * The usual unquoted case.
453 if (*cp
!= '\'' && *cp
!= '"') {
458 * Handle single and double quotes.
461 while (*cp
!= quote
&& *cp
!= '\0')
464 fprintf(stderr
, "missing %c\n", quote
);
474 * Canonicalize file names to always start with ``./'' and
475 * remove any embedded "." and ".." components.
478 canon(char *rawname
, char *canonname
, int len
)
480 register char *cp
, *np
;
482 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
483 (void) strcpy(canonname
, "");
484 else if (rawname
[0] == '/')
485 (void) strcpy(canonname
, ".");
487 (void) strcpy(canonname
, "./");
488 if (strlen(canonname
) + strlen(rawname
) >= len
)
489 errx(1, "canonname: not enough buffer space");
491 (void) strcat(canonname
, rawname
);
493 * Eliminate multiple and trailing '/'s
495 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
497 while (*cp
== '/' && *np
== '/')
504 * Eliminate extraneous "." and ".." from pathnames.
506 for (np
= canonname
; *np
!= '\0'; ) {
509 while (*np
!= '/' && *np
!= '\0')
511 if (np
- cp
== 1 && *cp
== '.') {
513 (void) strcpy(cp
, np
);
516 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
518 while (cp
> &canonname
[1] && *--cp
!= '/')
519 /* find beginning of name */;
520 (void) strcpy(cp
, np
);
527 * Do an "ls" style listing of a directory
530 printlist(char *name
, char *basename
)
532 register struct afile
*fp
, *list
, *listp
= NULL
;
533 register struct direct
*dp
;
536 int entries
, len
, namelen
;
537 char locname
[MAXPATHLEN
+ 1];
539 dp
= pathsearch(name
);
540 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
541 (!vflag
&& dp
->d_ino
== WINO
))
543 if ((dirp
= rst_opendir(name
)) == NULL
) {
546 mkentry(name
, dp
, list
);
547 len
= strlen(basename
) + 1;
548 if (strlen(name
) - len
> single
.len
) {
549 freename(single
.fname
);
550 single
.fname
= savename(&name
[len
]);
551 single
.len
= strlen(single
.fname
);
555 while ((dp
= rst_readdir(dirp
)))
558 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
560 fprintf(stderr
, "ls: out of memory\n");
563 if ((dirp
= rst_opendir(name
)) == NULL
)
564 panic("directory reopen failed\n");
565 fprintf(stderr
, "%s:\n", name
);
568 namelen
= snprintf(locname
, sizeof(locname
), "%s/", name
);
569 if (namelen
>= sizeof(locname
))
570 namelen
= sizeof(locname
) - 1;
571 while ((dp
= rst_readdir(dirp
))) {
574 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
576 if (!vflag
&& (dp
->d_ino
== WINO
||
577 strcmp(dp
->d_name
, ".") == 0 ||
578 strcmp(dp
->d_name
, "..") == 0))
580 locname
[namelen
] = '\0';
581 if (namelen
+ strlen(dp
->d_name
) >= MAXPATHLEN
) {
582 fprintf(stderr
, "%s%s: name exceeds %d char\n",
583 locname
, dp
->d_name
, MAXPATHLEN
);
585 (void) strncat(locname
, dp
->d_name
,
586 (int)strlen(dp
->d_name
));
587 mkentry(locname
, dp
, listp
++);
593 fprintf(stderr
, "\n");
597 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
599 formatf(list
, entries
);
601 for (fp
= listp
- 1; fp
>= list
; fp
--)
603 fprintf(stderr
, "\n");
609 * Read the contents of a directory.
612 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
617 fp
->fnum
= dp
->d_ino
;
618 fp
->fname
= savename(dp
->d_name
);
619 for (cp
= fp
->fname
; *cp
; cp
++)
620 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
622 fp
->len
= cp
- fp
->fname
;
623 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
625 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
632 fprintf(stderr
, "Warning: undefined file type %d\n",
654 /* no need for this */
662 if (inodetype(dp
->d_ino
) == NODE
)
672 * Print out a pretty listing of a directory
675 formatf(struct afile
*list
, int nentry
)
677 register struct afile
*fp
, *endlist
;
678 int width
, bigino
, haveprefix
, havepostfix
;
679 int i
, j
, w
, precision
= 0, columns
, lines
;
685 endlist
= &list
[nentry
];
686 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
687 if (bigino
< fp
->fnum
)
691 if (fp
->prefix
!= ' ')
693 if (fp
->postfix
!= ' ')
701 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
703 width
+= precision
+ 1;
706 columns
= 81 / width
;
709 lines
= (nentry
+ columns
- 1) / columns
;
710 for (i
= 0; i
< lines
; i
++) {
711 for (j
= 0; j
< columns
; j
++) {
712 fp
= &list
[j
* lines
+ i
];
714 fprintf(stderr
, "%*ld ", precision
, (long)fp
->fnum
);
715 fp
->len
+= precision
+ 1;
718 putc(fp
->prefix
, stderr
);
721 fprintf(stderr
, "%s", fp
->fname
);
723 putc(fp
->postfix
, stderr
);
726 if (fp
+ lines
>= endlist
) {
727 fprintf(stderr
, "\n");
730 for (w
= fp
->len
; w
< width
; w
++)
737 * Skip over directory entries that are not on the tape
739 * First have to get definition of a dirent.
741 * For Linux the dirent struct is now included from bsdcompat.h
747 #endif /* ! __linux__ */
750 glob_readdir(RST_DIR
*dirp
)
753 static struct dirent adirent
;
755 while ((dp
= rst_readdir(dirp
)) != NULL
) {
756 if (!vflag
&& dp
->d_ino
== WINO
)
758 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
763 adirent
.d_fileno
= dp
->d_ino
;
764 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
769 * Return st_mode information in response to stat or lstat calls
772 glob_stat(const char *name
, struct stat
*stp
)
774 register struct direct
*dp
;
775 dp
= pathsearch(name
);
776 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
777 (!vflag
&& dp
->d_ino
== WINO
))
779 if (inodetype(dp
->d_ino
) == NODE
)
780 stp
->st_mode
= IFDIR
;
782 stp
->st_mode
= IFREG
;
787 * Comparison routine for qsort.
790 fcmp(const void *f1
, const void *f2
)
792 return (strcmp(((struct afile
*)f1
)->fname
,
793 ((struct afile
*)f2
)->fname
));
797 * respond to interrupts
802 int save_errno
= errno
;
804 if (command
== 'i' && runshell
)
806 if (reply("restore interrupted, continue") == FAIL
)