]>
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
41 * $Id: interactive.c,v 1.5 1999/10/11 13:31:12 stelian Exp $
44 #include <sys/param.h>
48 #include <linux/ext2_fs.h>
49 #include <bsdcompat.h>
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/dir.h>
53 #endif /* __linux__ */
54 #include <protocols/dumprestore.h>
57 #include <compaterr.h>
59 #include <compatglob.h>
65 #include <ext2fs/ext2fs.h>
66 extern char * __progname
;
72 #define round(a, b) (((a) + (b) - 1) / (b) * (b))
75 * Things to handle interruptions.
79 static char *nextarg
= NULL
;
82 * Structure and routines associated with listing directories.
85 ino_t fnum
; /* inode number of file */
86 char *fname
; /* file name */
87 short len
; /* name length */
88 char prefix
; /* prefix character */
89 char postfix
; /* postfix character */
92 int freeglob
; /* glob structure needs to be freed */
93 int argcnt
; /* next globbed argument to return */
94 glob_t glob
; /* globbing information */
95 char *cmd
; /* the current command */
98 static char *copynext
__P((char *, char *));
99 static int fcmp
__P((const void *, const void *));
100 static void formatf
__P((struct afile
*, int));
101 static void getcmd
__P((char *, char *, char *, int, struct arglist
*));
102 struct dirent
*glob_readdir
__P((RST_DIR
*dirp
));
103 static int glob_stat
__P((const char *, struct stat
*));
104 static void mkentry
__P((char *, struct direct
*, struct afile
*));
105 static void printlist
__P((char *, char *));
108 * Read and execute commands from the terminal.
113 register struct entry
*np
;
115 struct arglist arglist
;
116 char curdir
[MAXPATHLEN
];
117 char name
[MAXPATHLEN
];
120 arglist
.freeglob
= 0;
122 arglist
.glob
.gl_flags
= GLOB_ALTDIRFUNC
;
123 arglist
.glob
.gl_opendir
= (void *)rst_opendir
;
124 arglist
.glob
.gl_readdir
= (void *)glob_readdir
;
125 arglist
.glob
.gl_closedir
= (void *)rst_closedir
;
126 arglist
.glob
.gl_lstat
= glob_stat
;
127 arglist
.glob
.gl_stat
= glob_stat
;
128 canon("/", curdir
, sizeof(curdir
));
130 if (setjmp(reset
) != 0) {
131 if (arglist
.freeglob
!= 0) {
132 arglist
.freeglob
= 0;
134 globfree(&arglist
.glob
);
140 getcmd(curdir
, cmd
, name
, sizeof(name
), &arglist
);
143 * Add elements to the extraction list.
146 if (strncmp(cmd
, "add", strlen(cmd
)) != 0)
148 ino
= dirlookup(name
);
153 treescan(name
, ino
, addfile
);
156 * Change working directory.
159 if (strncmp(cmd
, "cd", strlen(cmd
)) != 0)
161 ino
= dirlookup(name
);
164 if (inodetype(ino
) == LEAF
) {
165 fprintf(stderr
, "%s: not a directory\n", name
);
168 (void) strncpy(curdir
, name
, sizeof(curdir
));
169 curdir
[sizeof(curdir
) - 1] = '\0';
172 * Delete elements from the extraction list.
175 if (strncmp(cmd
, "delete", strlen(cmd
)) != 0)
177 np
= lookupname(name
);
178 if (np
== NULL
|| (np
->e_flags
& NEW
) == 0) {
179 fprintf(stderr
, "%s: not on extraction list\n", name
);
182 treescan(name
, np
->e_ino
, deletefile
);
185 * Extract the requested list.
188 if (strncmp(cmd
, "extract", strlen(cmd
)) != 0)
198 * List available commands.
201 if (strncmp(cmd
, "help", strlen(cmd
)) != 0)
204 fprintf(stderr
, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
205 "Available commands are:\n",
206 "\tls [arg] - list directory\n",
207 "\tcd arg - change directory\n",
208 "\tpwd - print current directory\n",
209 "\tadd [arg] - add `arg' to list of",
210 " files to be extracted\n",
211 "\tdelete [arg] - delete `arg' from",
212 " list of files to be extracted\n",
213 "\textract - extract requested files\n",
214 "\tsetmodes - set modes of requested directories\n",
215 "\tquit - immediately exit program\n",
216 "\twhat - list dump header information\n",
217 "\tverbose - toggle verbose flag",
218 " (useful with ``ls'')\n",
219 "\thelp or `?' - print this list\n",
220 "If no `arg' is supplied, the current",
221 " directory is used\n");
227 if (strncmp(cmd
, "ls", strlen(cmd
)) != 0)
229 printlist(name
, curdir
);
232 * Print current directory.
235 if (strncmp(cmd
, "pwd", strlen(cmd
)) != 0)
237 if (curdir
[1] == '\0')
238 fprintf(stderr
, "/\n");
240 fprintf(stderr
, "%s\n", &curdir
[1]);
246 if (strncmp(cmd
, "quit", strlen(cmd
)) != 0)
250 if (strncmp(cmd
, "xit", strlen(cmd
)) != 0)
254 * Toggle verbose mode.
257 if (strncmp(cmd
, "verbose", strlen(cmd
)) != 0)
260 fprintf(stderr
, "verbose mode off\n");
264 fprintf(stderr
, "verbose mode on\n");
268 * Just restore requested directory modes.
271 if (strncmp(cmd
, "setmodes", strlen(cmd
)) != 0)
276 * Print out dump header information.
279 if (strncmp(cmd
, "what", strlen(cmd
)) != 0)
287 if (strncmp(cmd
, "Debug", strlen(cmd
)) != 0)
290 fprintf(stderr
, "debugging mode off\n");
294 fprintf(stderr
, "debugging mode on\n");
302 fprintf(stderr
, "%s: unknown command; type ? for help\n", cmd
);
309 * Read and parse an interactive command.
310 * The first word on the line is assigned to "cmd". If
311 * there are no arguments on the command line, then "curdir"
312 * is returned as the argument. If there are arguments
313 * on the line they are returned one at a time on each
314 * successive call to getcmd. Each argument is first assigned
315 * to "name". If it does not start with "/" the pathname in
316 * "curdir" is prepended to it. Finally "canon" is called to
317 * eliminate any embedded ".." components.
320 getcmd(char *curdir
, char *cmd
, char *name
, int size
, struct arglist
*ap
)
323 static char input
[BUFSIZ
];
325 # define rawname input /* save space by reusing input buffer */
328 * Check to see if still processing arguments.
335 * Read a command line and trim off trailing white space.
338 fprintf(stderr
, "%s > ", __progname
);
339 (void) fflush(stderr
);
340 (void) fgets(input
, BUFSIZ
, terminal
);
341 } while (!feof(terminal
) && input
[0] == '\n');
342 if (feof(terminal
)) {
343 (void) strcpy(cmd
, "quit");
346 for (cp
= &input
[strlen(input
) - 2]; *cp
== ' ' || *cp
== '\t'; cp
--)
347 /* trim off trailing white space and newline */;
350 * Copy the command into "cmd".
352 cp
= copynext(input
, cmd
);
355 * If no argument, use curdir as the default.
358 (void) strncpy(name
, curdir
, size
);
359 name
[size
- 1] = '\0';
364 * Find the next argument.
367 cp
= copynext(nextarg
, rawname
);
373 * If it is an absolute pathname, canonicalize it and return it.
375 if (rawname
[0] == '/') {
376 canon(rawname
, name
, size
);
379 * For relative pathnames, prepend the current directory to
380 * it then canonicalize and return it.
382 snprintf(output
, sizeof(output
), "%s/%s", curdir
, rawname
);
383 canon(output
, name
, size
);
385 if (glob(name
, GLOB_ALTDIRFUNC
, NULL
, &ap
->glob
) < 0)
386 fprintf(stderr
, "%s: out of memory\n", ap
->cmd
);
387 if (ap
->glob
.gl_pathc
== 0)
390 ap
->argcnt
= ap
->glob
.gl_pathc
;
393 strncpy(name
, ap
->glob
.gl_pathv
[ap
->glob
.gl_pathc
- ap
->argcnt
], size
);
394 name
[size
- 1] = '\0';
395 if (--ap
->argcnt
== 0) {
403 * Strip off the next token of the input.
406 copynext(char *input
, char *output
)
408 register char *cp
, *bp
;
411 for (cp
= input
; *cp
== ' ' || *cp
== '\t'; cp
++)
412 /* skip to argument */;
414 while (*cp
!= ' ' && *cp
!= '\t' && *cp
!= '\0') {
416 * Handle back slashes.
421 "command lines cannot be continued\n");
428 * The usual unquoted case.
430 if (*cp
!= '\'' && *cp
!= '"') {
435 * Handle single and double quotes.
438 while (*cp
!= quote
&& *cp
!= '\0')
439 *bp
++ = *cp
++ | 0200;
441 fprintf(stderr
, "missing %c\n", quote
);
451 * Canonicalize file names to always start with ``./'' and
452 * remove any embedded "." and ".." components.
455 canon(char *rawname
, char *canonname
, int len
)
457 register char *cp
, *np
;
459 if (strcmp(rawname
, ".") == 0 || strncmp(rawname
, "./", 2) == 0)
460 (void) strcpy(canonname
, "");
461 else if (rawname
[0] == '/')
462 (void) strcpy(canonname
, ".");
464 (void) strcpy(canonname
, "./");
465 if (strlen(canonname
) + strlen(rawname
) >= len
)
466 errx(1, "canonname: not enough buffer space");
468 (void) strcat(canonname
, rawname
);
470 * Eliminate multiple and trailing '/'s
472 for (cp
= np
= canonname
; *np
!= '\0'; cp
++) {
474 while (*cp
== '/' && *np
== '/')
481 * Eliminate extraneous "." and ".." from pathnames.
483 for (np
= canonname
; *np
!= '\0'; ) {
486 while (*np
!= '/' && *np
!= '\0')
488 if (np
- cp
== 1 && *cp
== '.') {
490 (void) strcpy(cp
, np
);
493 if (np
- cp
== 2 && strncmp(cp
, "..", 2) == 0) {
495 while (cp
> &canonname
[1] && *--cp
!= '/')
496 /* find beginning of name */;
497 (void) strcpy(cp
, np
);
504 * Do an "ls" style listing of a directory
507 printlist(char *name
, char *basename
)
509 register struct afile
*fp
, *list
, *listp
= NULL
;
510 register struct direct
*dp
;
513 int entries
, len
, namelen
;
514 char locname
[MAXPATHLEN
+ 1];
516 dp
= pathsearch(name
);
517 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
518 (!vflag
&& dp
->d_ino
== WINO
))
520 if ((dirp
= rst_opendir(name
)) == NULL
) {
523 mkentry(name
, dp
, list
);
524 len
= strlen(basename
) + 1;
525 if (strlen(name
) - len
> single
.len
) {
526 freename(single
.fname
);
527 single
.fname
= savename(&name
[len
]);
528 single
.len
= strlen(single
.fname
);
532 while ((dp
= rst_readdir(dirp
)))
535 list
= (struct afile
*)malloc(entries
* sizeof(struct afile
));
537 fprintf(stderr
, "ls: out of memory\n");
540 if ((dirp
= rst_opendir(name
)) == NULL
)
541 panic("directory reopen failed\n");
542 fprintf(stderr
, "%s:\n", name
);
545 namelen
= snprintf(locname
, sizeof(locname
), "%s/", name
);
546 if (namelen
>= sizeof(locname
))
547 namelen
= sizeof(locname
) - 1;
548 while ((dp
= rst_readdir(dirp
))) {
551 if (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0)
553 if (!vflag
&& (dp
->d_ino
== WINO
||
554 strcmp(dp
->d_name
, ".") == 0 ||
555 strcmp(dp
->d_name
, "..") == 0))
557 locname
[namelen
] = '\0';
558 if (namelen
+ strlen(dp
->d_name
) >= MAXPATHLEN
) {
559 fprintf(stderr
, "%s%s: name exceeds %d char\n",
560 locname
, dp
->d_name
, MAXPATHLEN
);
562 (void) strncat(locname
, dp
->d_name
,
563 (int)strlen(dp
->d_name
));
564 mkentry(locname
, dp
, listp
++);
570 fprintf(stderr
, "\n");
574 qsort((char *)list
, entries
, sizeof(struct afile
), fcmp
);
576 formatf(list
, entries
);
578 for (fp
= listp
- 1; fp
>= list
; fp
--)
580 fprintf(stderr
, "\n");
586 * Read the contents of a directory.
589 mkentry(char *name
, struct direct
*dp
, struct afile
*fp
)
594 fp
->fnum
= dp
->d_ino
;
595 fp
->fname
= savename(dp
->d_name
);
596 for (cp
= fp
->fname
; *cp
; cp
++)
597 if (!vflag
&& (*cp
< ' ' || *cp
>= 0177))
599 fp
->len
= cp
- fp
->fname
;
600 if (dflag
&& TSTINO(fp
->fnum
, dumpmap
) == 0)
602 else if ((np
= lookupname(name
)) != NULL
&& (np
->e_flags
& NEW
))
609 fprintf(stderr
, "Warning: undefined file type %d\n",
631 /* no need for this */
639 if (inodetype(dp
->d_ino
) == NODE
)
649 * Print out a pretty listing of a directory
652 formatf(struct afile
*list
, int nentry
)
654 register struct afile
*fp
, *endlist
;
655 int width
, bigino
, haveprefix
, havepostfix
;
656 int i
, j
, w
, precision
= 0, columns
, lines
;
662 endlist
= &list
[nentry
];
663 for (fp
= &list
[0]; fp
< endlist
; fp
++) {
664 if (bigino
< fp
->fnum
)
668 if (fp
->prefix
!= ' ')
670 if (fp
->postfix
!= ' ')
678 for (precision
= 0, i
= bigino
; i
> 0; i
/= 10)
680 width
+= precision
+ 1;
683 columns
= 81 / width
;
686 lines
= (nentry
+ columns
- 1) / columns
;
687 for (i
= 0; i
< lines
; i
++) {
688 for (j
= 0; j
< columns
; j
++) {
689 fp
= &list
[j
* lines
+ i
];
691 fprintf(stderr
, "%*ld ", precision
, fp
->fnum
);
692 fp
->len
+= precision
+ 1;
695 putc(fp
->prefix
, stderr
);
698 fprintf(stderr
, "%s", fp
->fname
);
700 putc(fp
->postfix
, stderr
);
703 if (fp
+ lines
>= endlist
) {
704 fprintf(stderr
, "\n");
707 for (w
= fp
->len
; w
< width
; w
++)
714 * Skip over directory entries that are not on the tape
716 * First have to get definition of a dirent.
718 * For Linux the dirent struct is now included from bsdcompat.h
724 #endif /* ! __linux__ */
727 glob_readdir(RST_DIR
*dirp
)
730 static struct dirent adirent
;
732 while ((dp
= rst_readdir(dirp
)) != NULL
) {
733 if (!vflag
&& dp
->d_ino
== WINO
)
735 if (dflag
|| TSTINO(dp
->d_ino
, dumpmap
))
740 adirent
.d_fileno
= dp
->d_ino
;
741 memmove(adirent
.d_name
, dp
->d_name
, dp
->d_namlen
+ 1);
746 * Return st_mode information in response to stat or lstat calls
749 glob_stat(const char *name
, struct stat
*stp
)
751 register struct direct
*dp
;
752 dp
= pathsearch(name
);
753 if (dp
== NULL
|| (!dflag
&& TSTINO(dp
->d_ino
, dumpmap
) == 0) ||
754 (!vflag
&& dp
->d_ino
== WINO
))
756 if (inodetype(dp
->d_ino
) == NODE
)
757 stp
->st_mode
= IFDIR
;
759 stp
->st_mode
= IFREG
;
764 * Comparison routine for qsort.
767 fcmp(const void *f1
, const void *f2
)
769 return (strcmp(((struct afile
*)f1
)->fname
,
770 ((struct afile
*)f2
)->fname
));
774 * respond to interrupts
779 int save_errno
= errno
;
781 if (command
== 'i' && runshell
)
783 if (reply("restore interrupted, continue") == FAIL
)