]>
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
10 * Copyright (c) 1985, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 static char sccsid
[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
46 static const char rcsid
[] =
47 "$Id: interactive.c,v 1.2 1999/10/11 12:53:23 stelian Exp $";
50 #include <sys/param.h>
54 #include <linux/ext2_fs.h>
55 #include <bsdcompat.h>
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ufs/dir.h>
59 #endif /* __linux__ */
60 #include <protocols/dumprestore.h>
69 #include <ext2fs/ext2fs.h>
75 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
78 * Things to handle interruptions.
82 static char *nextarg
= NULL
;
85 * Structure and routines associated with listing directories.
88 ino_t fnum
; /* inode number of file */
89 char *fname
; /* file name */
90 short len
; /* name length */
91 char prefix
; /* prefix character */
92 char postfix
; /* postfix character */
95 int freeglob
; /* glob structure needs to be freed */
96 int argcnt
; /* next globbed argument to return */
97 glob_t glob
; /* globbing information */
98 char *cmd
; /* the current command */
101 static char *copynext
__P((char *, char *));
102 static int fcmp
__P((const void *, const void *));
103 static void formatf
__P((struct afile
*, int));
104 static void getcmd
__P((char *, char *, char *, int, struct arglist
*));
105 struct dirent
*glob_readdir
__P((RST_DIR
*dirp
));
106 static int glob_stat
__P((const char *, struct stat
*));
107 static void mkentry
__P((char *, struct direct
*, struct afile
*));
108 static void printlist
__P((char *, char *));
111 * Read and execute commands from the terminal.
116 register struct entry
*np
;
118 struct arglist arglist
;
119 char curdir
[MAXPATHLEN
];
120 char name
[MAXPATHLEN
];
123 arglist
.freeglob
= 0;
125 arglist
.glob
.gl_flags
= GLOB_ALTDIRFUNC
;
126 arglist
.glob
.gl_opendir
= (void *)rst_opendir
;
127 arglist
.glob
.gl_readdir
= (void *)glob_readdir
;
128 arglist
.glob
.gl_closedir
= (void *)rst_closedir
;
129 arglist
.glob
.gl_lstat
= glob_stat
;
130 arglist
.glob
.gl_stat
= glob_stat
;
131 canon("/", curdir
, sizeof(curdir
));
133 if (setjmp(reset
) != 0) {
134 if (arglist
.freeglob
!= 0) {
135 arglist
.freeglob
= 0;
137 globfree(&arglist
.glob
);
143 getcmd(curdir
, cmd
, name
, sizeof(name
), &arglist
);
146 * Add elements to the extraction list.
149 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
151 ino
= dirlookup(name
);
156 treescan(name
, ino
, addfile
);
159 * Change working directory.
162 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
164 ino
= dirlookup(name
);
167 if (inodetype(ino
) == LEAF
) {
168 fprintf(stderr
, "%s: not a directory\n", name
);
171 (void) strcpy(curdir
, name
);
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",
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 "\thelp or `?' - print this list\n",
222 "If no `arg' is supplied, the current",
223 " directory is used\n");
229 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
231 printlist(name
, curdir
);
234 * Print current directory.
237 if (strncmp(cmd
, "pwd", strlen(cmd
)) != 0)
239 if (curdir
[1] == '\0')
240 fprintf(stderr
, "/\n");
242 fprintf(stderr
, "%s\n", &curdir
[1]);
248 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
252 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
256 * Toggle verbose mode.
259 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
262 fprintf(stderr
, "verbose mode off\n");
266 fprintf(stderr
, "verbose mode on\n");
270 * Just restore requested directory modes.
273 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
278 * Print out dump header information.
281 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
289 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
292 fprintf(stderr
, "debugging mode off\n");
296 fprintf(stderr
, "debugging mode on\n");
304 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
311 * Read and parse an interactive command.
312 * The first word on the line is assigned to "cmd". If
313 * there are no arguments on the command line, then "curdir"
314 * is returned as the argument. If there are arguments
315 * on the line they are returned one at a time on each
316 * successive call to getcmd. Each argument is first assigned
317 * to "name". If it does not start with "/" the pathname in
318 * "curdir" is prepended to it. Finally "canon" is called to
319 * eliminate any embedded ".." components.
322 getcmd(curdir
, cmd
, name
, size
, ap
)
323 char *curdir
, *cmd
, *name
;
328 static char input
[BUFSIZ
];
330 # define rawname input /* save space by reusing input buffer */
333 * Check to see if still processing arguments.
340 * Read a command line and trim off trailing white space.
343 fprintf(stderr
, "restore > ");
344 (void) fflush(stderr
);
345 (void) fgets(input
, BUFSIZ
, terminal
);
346 } while (!feof(terminal
) && input
[0] == '\n');
347 if (feof(terminal
)) {
348 (void) strcpy(cmd
, "quit");
351 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
352 /* trim off trailing white space and newline */;
355 * Copy the command into "cmd".
357 cp
= copynext(input
, cmd
);
360 * If no argument, use curdir as the default.
363 (void) strncpy(name
, curdir
, size
);
364 name
[size
- 1] = '\0';
369 * Find the next argument.
372 cp
= copynext(nextarg
, rawname
);
378 * If it is an absolute pathname, canonicalize it and return it.
380 if (rawname
[0] == '/') {
381 canon(rawname
, name
, size
);
384 * For relative pathnames, prepend the current directory to
385 * it then canonicalize and return it.
387 snprintf(output
, sizeof(output
), "%s/%s", curdir
, rawname
);
388 canon(output
, name
, size
);
390 if (glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
) < 0)
391 fprintf(stderr
, "%s: out of memory\n", ap
->cmd
);
392 if (ap
->glob
.gl_pathc
== 0)
395 ap
->argcnt
= ap
->glob
.gl_pathc
;
398 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
399 name
[size
- 1] = '\0';
400 if (--ap
->argcnt
== 0) {
408 * Strip off the next token of the input.
411 copynext(input
, output
)
412 char *input
, *output
;
414 register char *cp
, *bp
;
417 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
418 /* skip to argument */;
420 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
422 * Handle back slashes.
427 "command lines cannot be continued\n");
434 * The usual unquoted case.
436 if (*cp
!= '\'' && *cp
!= '"') {
441 * Handle single and double quotes.
444 while (*cp
!= quote
&& *cp
!= '\0')
445 *bp
++ = *cp
++ | 0200;
447 fprintf(stderr
, "missing %c\n", quote
);
457 * Canonicalize file names to always start with ``./'' and
458 * remove any embedded "." and ".." components.
461 canon(rawname
, canonname
, len
)
462 char *rawname
, *canonname
;
465 register char *cp
, *np
;
467 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
468 (void) strcpy(canonname
, "");
469 else if (rawname
[0] == '/')
470 (void) strcpy(canonname
, ".");
472 (void) strcpy(canonname
, "./");
473 if (strlen(canonname
) + strlen(rawname
) >= len
) {
474 fprintf(stderr
, "canonname: not enough buffer space\n");
478 (void) strcat(canonname
, rawname
);
480 * Eliminate multiple and trailing '/'s
482 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
484 while (*cp
== '/' && *np
== '/')
491 * Eliminate extraneous "." and ".." from pathnames.
493 for (np
= canonname
; *np
!= '\0'; ) {
496 while (*np
!= '/' && *np
!= '\0')
498 if (np
- cp
== 1 && *cp
== '.') {
500 (void) strcpy(cp
, np
);
503 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
505 while (cp
> &canonname
[1] && *--cp
!= '/')
506 /* find beginning of name */;
507 (void) strcpy(cp
, np
);
514 * Do an "ls" style listing of a directory
517 printlist(name
, basename
)
521 register struct afile
*fp
, *list
, *listp
=NULL
;
522 register struct direct
*dp
;
525 int entries
, len
, namelen
;
526 char locname
[MAXPATHLEN
+ 1];
528 dp
= pathsearch(name
);
529 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
530 (!vflag
&& dp
->d_ino
== WINO
))
532 if ((dirp
= rst_opendir(name
)) == NULL
) {
535 mkentry(name
, dp
, list
);
536 len
= strlen(basename
) + 1;
537 if (strlen(name
) - len
> single
.len
) {
538 freename(single
.fname
);
539 single
.fname
= savename(&name
[len
]);
540 single
.len
= strlen(single
.fname
);
544 while ((dp
= rst_readdir(dirp
)))
547 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
549 fprintf(stderr
, "ls: out of memory\n");
552 if ((dirp
= rst_opendir(name
)) == NULL
)
553 panic("directory reopen failed\n");
554 fprintf(stderr
, "%s:\n", name
);
557 (void) strncpy(locname
, name
, MAXPATHLEN
);
558 (void) strncat(locname
, "/", MAXPATHLEN
);
559 namelen
= strlen(locname
);
560 while ((dp
= rst_readdir(dirp
))) {
563 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
565 if (!vflag
&& (dp
->d_ino
== WINO
||
566 strcmp(dp
->d_name
, ".") == 0 ||
567 strcmp(dp
->d_name
, "..") == 0))
569 locname
[namelen
] = '\0';
570 if (namelen
+ dp
->d_namlen
>= MAXPATHLEN
) {
571 fprintf(stderr
, "%s%s: name exceeds %d char\n",
572 locname
, dp
->d_name
, MAXPATHLEN
);
574 (void) strncat(locname
, dp
->d_name
,
576 mkentry(locname
, dp
, listp
++);
582 fprintf(stderr
, "\n");
586 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
588 formatf(list
, entries
);
590 for (fp
= listp
- 1; fp
>= list
; fp
--)
592 fprintf(stderr
, "\n");
598 * Read the contents of a directory.
601 mkentry(name
, dp
, fp
)
604 register struct afile
*fp
;
609 fp
->fnum
= dp
->d_ino
;
610 fp
->fname
= savename(dp
->d_name
);
611 for (cp
= fp
->fname
; *cp
; cp
++)
612 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
614 fp
->len
= cp
- fp
->fname
;
615 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
617 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
624 fprintf(stderr
, "Warning: undefined file type %d\n",
651 if (inodetype(dp
->d_ino
) == NODE
)
661 * Print out a pretty listing of a directory
664 formatf(list
, nentry
)
665 register struct afile
*list
;
668 register struct afile
*fp
, *endlist
;
669 int width
, bigino
, haveprefix
, havepostfix
;
670 int i
, j
, w
, precision
=0, columns
, lines
;
676 endlist
= &list
[nentry
];
677 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
678 if (bigino
< fp
->fnum
)
682 if (fp
->prefix
!= ' ')
684 if (fp
->postfix
!= ' ')
692 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
694 width
+= precision
+ 1;
697 columns
= 81 / width
;
700 lines
= (nentry
+ columns
- 1) / columns
;
701 for (i
= 0; i
< lines
; i
++) {
702 for (j
= 0; j
< columns
; j
++) {
703 fp
= &list
[j
* lines
+ i
];
705 fprintf(stderr
, "%*ld ", precision
, fp
->fnum
);
706 fp
->len
+= precision
+ 1;
709 putc(fp
->prefix
, stderr
);
712 fprintf(stderr
, "%s", fp
->fname
);
714 putc(fp
->postfix
, stderr
);
717 if (fp
+ lines
>= endlist
) {
718 fprintf(stderr
, "\n");
721 for (w
= fp
->len
; w
< width
; w
++)
728 * Skip over directory entries that are not on the tape
730 * First have to get definition of a dirent.
734 off_t d_off
; /* offset of next disk dir entry */
735 unsigned long d_fileno
; /* file number of entry */
736 unsigned short d_reclen
; /* length of this record */
737 unsigned short d_namlen
; /* length of string in d_name */
738 char d_name
[255+1]; /* name (up to MAXNAMLEN + 1) */
740 #else /* __linux__ */
744 #endif /* __linux__ */
751 static struct dirent adirent
;
753 while ((dp
= rst_readdir(dirp
)) != NULL
) {
754 if (!vflag
&& dp
->d_ino
== WINO
)
756 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
761 adirent
.d_fileno
= dp
->d_ino
;
762 adirent
.d_namlen
= dp
->d_namlen
;
763 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
768 * Return st_mode information in response to stat or lstat calls
775 register struct direct
*dp
;
777 dp
= pathsearch(name
);
778 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
779 (!vflag
&& dp
->d_ino
== WINO
))
781 if (inodetype(dp
->d_ino
) == NODE
)
782 stp
->st_mode
= IFDIR
;
784 stp
->st_mode
= IFREG
;
789 * Comparison routine for qsort.
793 register const void *f1
, *f2
;
795 return (strcmp(((struct afile
*)f1
)->fname
,
796 ((struct afile
*)f2
)->fname
));
800 * respond to interrupts
806 if (command
== 'i' && runshell
)
808 if (reply("restore interrupted, continue") == FAIL
)