]>
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
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.6 1999/10/13 09:57:20 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
;
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",
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(char *curdir
, char *cmd
, char *name
, int size
, struct arglist
*ap
)
325 static char input
[BUFSIZ
];
327 # define rawname input /* save space by reusing input buffer */
330 * Check to see if still processing arguments.
337 * Read a command line and trim off trailing white space.
340 fprintf(stderr
, "%s > ", __progname
);
341 (void) fflush(stderr
);
342 (void) fgets(input
, BUFSIZ
, terminal
);
343 } while (!feof(terminal
) && input
[0] == '\n');
344 if (feof(terminal
)) {
345 (void) strcpy(cmd
, "quit");
348 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
349 /* trim off trailing white space and newline */;
352 * Copy the command into "cmd".
354 cp
= copynext(input
, cmd
);
357 * If no argument, use curdir as the default.
360 (void) strncpy(name
, curdir
, size
);
361 name
[size
- 1] = '\0';
366 * Find the next argument.
369 cp
= copynext(nextarg
, rawname
);
375 * If it is an absolute pathname, canonicalize it and return it.
377 if (rawname
[0] == '/') {
378 canon(rawname
, name
, size
);
381 * For relative pathnames, prepend the current directory to
382 * it then canonicalize and return it.
384 snprintf(output
, sizeof(output
), "%s/%s", curdir
, rawname
);
385 canon(output
, name
, size
);
387 if (glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
) < 0)
388 fprintf(stderr
, "%s: out of memory\n", ap
->cmd
);
389 if (ap
->glob
.gl_pathc
== 0)
392 ap
->argcnt
= ap
->glob
.gl_pathc
;
395 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
396 name
[size
- 1] = '\0';
397 if (--ap
->argcnt
== 0) {
405 * Strip off the next token of the input.
408 copynext(char *input
, char *output
)
410 register char *cp
, *bp
;
413 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
414 /* skip to argument */;
416 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
418 * Handle back slashes.
423 "command lines cannot be continued\n");
430 * The usual unquoted case.
432 if (*cp
!= '\'' && *cp
!= '"') {
437 * Handle single and double quotes.
440 while (*cp
!= quote
&& *cp
!= '\0')
441 *bp
++ = *cp
++ | 0200;
443 fprintf(stderr
, "missing %c\n", quote
);
453 * Canonicalize file names to always start with ``./'' and
454 * remove any embedded "." and ".." components.
457 canon(char *rawname
, char *canonname
, int len
)
459 register char *cp
, *np
;
461 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
462 (void) strcpy(canonname
, "");
463 else if (rawname
[0] == '/')
464 (void) strcpy(canonname
, ".");
466 (void) strcpy(canonname
, "./");
467 if (strlen(canonname
) + strlen(rawname
) >= len
)
468 errx(1, "canonname: not enough buffer space");
470 (void) strcat(canonname
, rawname
);
472 * Eliminate multiple and trailing '/'s
474 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
476 while (*cp
== '/' && *np
== '/')
483 * Eliminate extraneous "." and ".." from pathnames.
485 for (np
= canonname
; *np
!= '\0'; ) {
488 while (*np
!= '/' && *np
!= '\0')
490 if (np
- cp
== 1 && *cp
== '.') {
492 (void) strcpy(cp
, np
);
495 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
497 while (cp
> &canonname
[1] && *--cp
!= '/')
498 /* find beginning of name */;
499 (void) strcpy(cp
, np
);
506 * Do an "ls" style listing of a directory
509 printlist(char *name
, char *basename
)
511 register struct afile
*fp
, *list
, *listp
= NULL
;
512 register struct direct
*dp
;
515 int entries
, len
, namelen
;
516 char locname
[MAXPATHLEN
+ 1];
518 dp
= pathsearch(name
);
519 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
520 (!vflag
&& dp
->d_ino
== WINO
))
522 if ((dirp
= rst_opendir(name
)) == NULL
) {
525 mkentry(name
, dp
, list
);
526 len
= strlen(basename
) + 1;
527 if (strlen(name
) - len
> single
.len
) {
528 freename(single
.fname
);
529 single
.fname
= savename(&name
[len
]);
530 single
.len
= strlen(single
.fname
);
534 while ((dp
= rst_readdir(dirp
)))
537 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
539 fprintf(stderr
, "ls: out of memory\n");
542 if ((dirp
= rst_opendir(name
)) == NULL
)
543 panic("directory reopen failed\n");
544 fprintf(stderr
, "%s:\n", name
);
547 namelen
= snprintf(locname
, sizeof(locname
), "%s/", name
);
548 if (namelen
>= sizeof(locname
))
549 namelen
= sizeof(locname
) - 1;
550 while ((dp
= rst_readdir(dirp
))) {
553 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
555 if (!vflag
&& (dp
->d_ino
== WINO
||
556 strcmp(dp
->d_name
, ".") == 0 ||
557 strcmp(dp
->d_name
, "..") == 0))
559 locname
[namelen
] = '\0';
560 if (namelen
+ strlen(dp
->d_name
) >= MAXPATHLEN
) {
561 fprintf(stderr
, "%s%s: name exceeds %d char\n",
562 locname
, dp
->d_name
, MAXPATHLEN
);
564 (void) strncat(locname
, dp
->d_name
,
565 (int)strlen(dp
->d_name
));
566 mkentry(locname
, dp
, listp
++);
572 fprintf(stderr
, "\n");
576 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
578 formatf(list
, entries
);
580 for (fp
= listp
- 1; fp
>= list
; fp
--)
582 fprintf(stderr
, "\n");
588 * Read the contents of a directory.
591 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
596 fp
->fnum
= dp
->d_ino
;
597 fp
->fname
= savename(dp
->d_name
);
598 for (cp
= fp
->fname
; *cp
; cp
++)
599 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
601 fp
->len
= cp
- fp
->fname
;
602 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
604 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
611 fprintf(stderr
, "Warning: undefined file type %d\n",
633 /* no need for this */
641 if (inodetype(dp
->d_ino
) == NODE
)
651 * Print out a pretty listing of a directory
654 formatf(struct afile
*list
, int nentry
)
656 register struct afile
*fp
, *endlist
;
657 int width
, bigino
, haveprefix
, havepostfix
;
658 int i
, j
, w
, precision
= 0, columns
, lines
;
664 endlist
= &list
[nentry
];
665 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
666 if (bigino
< fp
->fnum
)
670 if (fp
->prefix
!= ' ')
672 if (fp
->postfix
!= ' ')
680 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
682 width
+= precision
+ 1;
685 columns
= 81 / width
;
688 lines
= (nentry
+ columns
- 1) / columns
;
689 for (i
= 0; i
< lines
; i
++) {
690 for (j
= 0; j
< columns
; j
++) {
691 fp
= &list
[j
* lines
+ i
];
693 fprintf(stderr
, "%*ld ", precision
, fp
->fnum
);
694 fp
->len
+= precision
+ 1;
697 putc(fp
->prefix
, stderr
);
700 fprintf(stderr
, "%s", fp
->fname
);
702 putc(fp
->postfix
, stderr
);
705 if (fp
+ lines
>= endlist
) {
706 fprintf(stderr
, "\n");
709 for (w
= fp
->len
; w
< width
; w
++)
716 * Skip over directory entries that are not on the tape
718 * First have to get definition of a dirent.
720 * For Linux the dirent struct is now included from bsdcompat.h
726 #endif /* ! __linux__ */
729 glob_readdir(RST_DIR
*dirp
)
732 static struct dirent adirent
;
734 while ((dp
= rst_readdir(dirp
)) != NULL
) {
735 if (!vflag
&& dp
->d_ino
== WINO
)
737 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
742 adirent
.d_fileno
= dp
->d_ino
;
743 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
748 * Return st_mode information in response to stat or lstat calls
751 glob_stat(const char *name
, struct stat
*stp
)
753 register struct direct
*dp
;
754 dp
= pathsearch(name
);
755 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
756 (!vflag
&& dp
->d_ino
== WINO
))
758 if (inodetype(dp
->d_ino
) == NODE
)
759 stp
->st_mode
= IFDIR
;
761 stp
->st_mode
= IFREG
;
766 * Comparison routine for qsort.
769 fcmp(const void *f1
, const void *f2
)
771 return (strcmp(((struct afile
*)f1
)->fname
,
772 ((struct afile
*)f2
)->fname
));
776 * respond to interrupts
781 int save_errno
= errno
;
783 if (command
== 'i' && runshell
)
785 if (reply("restore interrupted, continue") == FAIL
)