]>
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.4 1999/10/11 13:08:09 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>
63 #include <compaterr.h>
65 #include <compatglob.h>
71 #include <ext2fs/ext2fs.h>
72 extern char * __progname
;
78 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
81 * Things to handle interruptions.
85 static char *nextarg
= NULL
;
88 * Structure and routines associated with listing directories.
91 ino_t fnum
; /* inode number of file */
92 char *fname
; /* file name */
93 short len
; /* name length */
94 char prefix
; /* prefix character */
95 char postfix
; /* postfix character */
98 int freeglob
; /* glob structure needs to be freed */
99 int argcnt
; /* next globbed argument to return */
100 glob_t glob
; /* globbing information */
101 char *cmd
; /* the current command */
104 static char *copynext
__P((char *, char *));
105 static int fcmp
__P((const void *, const void *));
106 static void formatf
__P((struct afile
*, int));
107 static void getcmd
__P((char *, char *, char *, int, struct arglist
*));
108 struct dirent
*glob_readdir
__P((RST_DIR
*dirp
));
109 static int glob_stat
__P((const char *, struct stat
*));
110 static void mkentry
__P((char *, struct direct
*, struct afile
*));
111 static void printlist
__P((char *, char *));
114 * Read and execute commands from the terminal.
119 register struct entry
*np
;
121 struct arglist arglist
;
122 char curdir
[MAXPATHLEN
];
123 char name
[MAXPATHLEN
];
126 arglist
.freeglob
= 0;
128 arglist
.glob
.gl_flags
= GLOB_ALTDIRFUNC
;
129 arglist
.glob
.gl_opendir
= (void *)rst_opendir
;
130 arglist
.glob
.gl_readdir
= (void *)glob_readdir
;
131 arglist
.glob
.gl_closedir
= (void *)rst_closedir
;
132 arglist
.glob
.gl_lstat
= glob_stat
;
133 arglist
.glob
.gl_stat
= glob_stat
;
134 canon("/", curdir
, sizeof(curdir
));
136 if (setjmp(reset
) != 0) {
137 if (arglist
.freeglob
!= 0) {
138 arglist
.freeglob
= 0;
140 globfree(&arglist
.glob
);
146 getcmd(curdir
, cmd
, name
, sizeof(name
), &arglist
);
149 * Add elements to the extraction list.
152 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
154 ino
= dirlookup(name
);
159 treescan(name
, ino
, addfile
);
162 * Change working directory.
165 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
167 ino
= dirlookup(name
);
170 if (inodetype(ino
) == LEAF
) {
171 fprintf(stderr
, "%s: not a directory\n", name
);
174 (void) strncpy(curdir
, name
, sizeof(curdir
));
175 curdir
[sizeof(curdir
) - 1] = '\0';
178 * Delete elements from the extraction list.
181 if (strncmp(cmd
, "delete", strlen(cmd
)) != 0)
183 np
= lookupname(name
);
184 if (np
== NULL
|| (np
->e_flags
& NEW
) == 0) {
185 fprintf(stderr
, "%s: not on extraction list\n", name
);
188 treescan(name
, np
->e_ino
, deletefile
);
191 * Extract the requested list.
194 if (strncmp(cmd
, "extract", strlen(cmd
)) != 0)
204 * List available commands.
207 if (strncmp(cmd
, "help", strlen(cmd
)) != 0)
210 fprintf(stderr
, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
211 "Available commands are:\n",
212 "\tls [arg] - list directory\n",
213 "\tcd arg - change directory\n",
214 "\tpwd - print current directory\n",
215 "\tadd [arg] - add `arg' to list of",
216 " files to be extracted\n",
217 "\tdelete [arg] - delete `arg' from",
218 " list of files to be extracted\n",
219 "\textract - extract requested files\n",
220 "\tsetmodes - set modes of requested directories\n",
221 "\tquit - immediately exit program\n",
222 "\twhat - list dump header information\n",
223 "\tverbose - toggle verbose flag",
224 " (useful with ``ls'')\n",
225 "\thelp or `?' - print this list\n",
226 "If no `arg' is supplied, the current",
227 " directory is used\n");
233 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
235 printlist(name
, curdir
);
238 * Print current directory.
241 if (strncmp(cmd
, "pwd", strlen(cmd
)) != 0)
243 if (curdir
[1] == '\0')
244 fprintf(stderr
, "/\n");
246 fprintf(stderr
, "%s\n", &curdir
[1]);
252 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
256 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
260 * Toggle verbose mode.
263 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
266 fprintf(stderr
, "verbose mode off\n");
270 fprintf(stderr
, "verbose mode on\n");
274 * Just restore requested directory modes.
277 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
282 * Print out dump header information.
285 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
293 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
296 fprintf(stderr
, "debugging mode off\n");
300 fprintf(stderr
, "debugging mode on\n");
308 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
315 * Read and parse an interactive command.
316 * The first word on the line is assigned to "cmd". If
317 * there are no arguments on the command line, then "curdir"
318 * is returned as the argument. If there are arguments
319 * on the line they are returned one at a time on each
320 * successive call to getcmd. Each argument is first assigned
321 * to "name". If it does not start with "/" the pathname in
322 * "curdir" is prepended to it. Finally "canon" is called to
323 * eliminate any embedded ".." components.
326 getcmd(char *curdir
, char *cmd
, char *name
, int size
, struct arglist
*ap
)
329 static char input
[BUFSIZ
];
331 # define rawname input /* save space by reusing input buffer */
334 * Check to see if still processing arguments.
341 * Read a command line and trim off trailing white space.
344 fprintf(stderr
, "%s > ", __progname
);
345 (void) fflush(stderr
);
346 (void) fgets(input
, BUFSIZ
, terminal
);
347 } while (!feof(terminal
) && input
[0] == '\n');
348 if (feof(terminal
)) {
349 (void) strcpy(cmd
, "quit");
352 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
353 /* trim off trailing white space and newline */;
356 * Copy the command into "cmd".
358 cp
= copynext(input
, cmd
);
361 * If no argument, use curdir as the default.
364 (void) strncpy(name
, curdir
, size
);
365 name
[size
- 1] = '\0';
370 * Find the next argument.
373 cp
= copynext(nextarg
, rawname
);
379 * If it is an absolute pathname, canonicalize it and return it.
381 if (rawname
[0] == '/') {
382 canon(rawname
, name
, size
);
385 * For relative pathnames, prepend the current directory to
386 * it then canonicalize and return it.
388 snprintf(output
, sizeof(output
), "%s/%s", curdir
, rawname
);
389 canon(output
, name
, size
);
391 if (glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
) < 0)
392 fprintf(stderr
, "%s: out of memory\n", ap
->cmd
);
393 if (ap
->glob
.gl_pathc
== 0)
396 ap
->argcnt
= ap
->glob
.gl_pathc
;
399 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
400 name
[size
- 1] = '\0';
401 if (--ap
->argcnt
== 0) {
409 * Strip off the next token of the input.
412 copynext(char *input
, char *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(char *rawname
, char *canonname
, int len
)
463 register char *cp
, *np
;
465 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
466 (void) strcpy(canonname
, "");
467 else if (rawname
[0] == '/')
468 (void) strcpy(canonname
, ".");
470 (void) strcpy(canonname
, "./");
471 if (strlen(canonname
) + strlen(rawname
) >= len
)
472 errx(1, "canonname: not enough buffer space");
474 (void) strcat(canonname
, rawname
);
476 * Eliminate multiple and trailing '/'s
478 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
480 while (*cp
== '/' && *np
== '/')
487 * Eliminate extraneous "." and ".." from pathnames.
489 for (np
= canonname
; *np
!= '\0'; ) {
492 while (*np
!= '/' && *np
!= '\0')
494 if (np
- cp
== 1 && *cp
== '.') {
496 (void) strcpy(cp
, np
);
499 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
501 while (cp
> &canonname
[1] && *--cp
!= '/')
502 /* find beginning of name */;
503 (void) strcpy(cp
, np
);
510 * Do an "ls" style listing of a directory
513 printlist(char *name
, char *basename
)
515 register struct afile
*fp
, *list
, *listp
= NULL
;
516 register struct direct
*dp
;
519 int entries
, len
, namelen
;
520 char locname
[MAXPATHLEN
+ 1];
522 dp
= pathsearch(name
);
523 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
524 (!vflag
&& dp
->d_ino
== WINO
))
526 if ((dirp
= rst_opendir(name
)) == NULL
) {
529 mkentry(name
, dp
, list
);
530 len
= strlen(basename
) + 1;
531 if (strlen(name
) - len
> single
.len
) {
532 freename(single
.fname
);
533 single
.fname
= savename(&name
[len
]);
534 single
.len
= strlen(single
.fname
);
538 while ((dp
= rst_readdir(dirp
)))
541 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
543 fprintf(stderr
, "ls: out of memory\n");
546 if ((dirp
= rst_opendir(name
)) == NULL
)
547 panic("directory reopen failed\n");
548 fprintf(stderr
, "%s:\n", name
);
551 namelen
= snprintf(locname
, sizeof(locname
), "%s/", name
);
552 if (namelen
>= sizeof(locname
))
553 namelen
= sizeof(locname
) - 1;
554 while ((dp
= rst_readdir(dirp
))) {
557 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
559 if (!vflag
&& (dp
->d_ino
== WINO
||
560 strcmp(dp
->d_name
, ".") == 0 ||
561 strcmp(dp
->d_name
, "..") == 0))
563 locname
[namelen
] = '\0';
564 if (namelen
+ strlen(dp
->d_name
) >= MAXPATHLEN
) {
565 fprintf(stderr
, "%s%s: name exceeds %d char\n",
566 locname
, dp
->d_name
, MAXPATHLEN
);
568 (void) strncat(locname
, dp
->d_name
,
569 (int)strlen(dp
->d_name
));
570 mkentry(locname
, dp
, listp
++);
576 fprintf(stderr
, "\n");
580 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
582 formatf(list
, entries
);
584 for (fp
= listp
- 1; fp
>= list
; fp
--)
586 fprintf(stderr
, "\n");
592 * Read the contents of a directory.
595 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
600 fp
->fnum
= dp
->d_ino
;
601 fp
->fname
= savename(dp
->d_name
);
602 for (cp
= fp
->fname
; *cp
; cp
++)
603 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
605 fp
->len
= cp
- fp
->fname
;
606 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
608 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
615 fprintf(stderr
, "Warning: undefined file type %d\n",
637 /* no need for this */
645 if (inodetype(dp
->d_ino
) == NODE
)
655 * Print out a pretty listing of a directory
658 formatf(struct afile
*list
, int nentry
)
660 register struct afile
*fp
, *endlist
;
661 int width
, bigino
, haveprefix
, havepostfix
;
662 int i
, j
, w
, precision
= 0, columns
, lines
;
668 endlist
= &list
[nentry
];
669 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
670 if (bigino
< fp
->fnum
)
674 if (fp
->prefix
!= ' ')
676 if (fp
->postfix
!= ' ')
684 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
686 width
+= precision
+ 1;
689 columns
= 81 / width
;
692 lines
= (nentry
+ columns
- 1) / columns
;
693 for (i
= 0; i
< lines
; i
++) {
694 for (j
= 0; j
< columns
; j
++) {
695 fp
= &list
[j
* lines
+ i
];
697 fprintf(stderr
, "%*ld ", precision
, fp
->fnum
);
698 fp
->len
+= precision
+ 1;
701 putc(fp
->prefix
, stderr
);
704 fprintf(stderr
, "%s", fp
->fname
);
706 putc(fp
->postfix
, stderr
);
709 if (fp
+ lines
>= endlist
) {
710 fprintf(stderr
, "\n");
713 for (w
= fp
->len
; w
< width
; w
++)
720 * Skip over directory entries that are not on the tape
722 * First have to get definition of a dirent.
724 * For Linux the dirent struct is now included from bsdcompat.h
730 #endif /* ! __linux__ */
733 glob_readdir(RST_DIR
*dirp
)
736 static struct dirent adirent
;
738 while ((dp
= rst_readdir(dirp
)) != NULL
) {
739 if (!vflag
&& dp
->d_ino
== WINO
)
741 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
746 adirent
.d_fileno
= dp
->d_ino
;
747 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
752 * Return st_mode information in response to stat or lstat calls
755 glob_stat(const char *name
, struct stat
*stp
)
757 register struct direct
*dp
;
758 dp
= pathsearch(name
);
759 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
760 (!vflag
&& dp
->d_ino
== WINO
))
762 if (inodetype(dp
->d_ino
) == NODE
)
763 stp
->st_mode
= IFDIR
;
765 stp
->st_mode
= IFREG
;
770 * Comparison routine for qsort.
773 fcmp(const void *f1
, const void *f2
)
775 return (strcmp(((struct afile
*)f1
)->fname
,
776 ((struct afile
*)f2
)->fname
));
780 * respond to interrupts
785 int save_errno
= errno
;
787 if (command
== 'i' && runshell
)
789 if (reply("restore interrupted, continue") == FAIL
)