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, 1995, 1996
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 char sccsid[] = "@(#)interactive.c 8.5 (Berkeley) 5/1/95";
45 #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 #include <ufs/ffs/fs.h>
56 #endif /* __linux__ */
57 #include <protocols/dumprestore.h>
66 #include <ext2fs/ext2fs.h>
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 *, 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;
130 if (setjmp(reset) != 0) {
131 if (arglist.freeglob != 0) {
132 arglist.freeglob = 0;
134 globfree(&arglist.glob);
140 getcmd(curdir, cmd, 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) strcpy(curdir, name);
171 * Delete elements from the extraction list.
174 if (strncmp(cmd, "delete", strlen(cmd)) != 0)
176 np = lookupname(name);
177 if (np == NULL || (np->e_flags & NEW) == 0) {
178 fprintf(stderr, "%s: not on extraction list\n", name);
181 treescan(name, np->e_ino, deletefile);
184 * Extract the requested list.
187 if (strncmp(cmd, "extract", strlen(cmd)) != 0)
197 * List available commands.
200 if (strncmp(cmd, "help", strlen(cmd)) != 0)
203 fprintf(stderr, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
204 "Available commands are:\n",
205 "\tls [arg] - list directory\n",
206 "\tcd arg - change directory\n",
207 "\tpwd - print current directory\n",
208 "\tadd [arg] - add `arg' to list of",
209 " files to be extracted\n",
210 "\tdelete [arg] - delete `arg' from",
211 " list of files to be extracted\n",
212 "\textract - extract requested files\n",
213 "\tsetmodes - set modes of requested directories\n",
214 "\tquit - immediately exit program\n",
215 "\twhat - list dump header information\n",
216 "\tverbose - toggle verbose flag",
217 " (useful with ``ls'')\n",
218 "\thelp or `?' - print this list\n",
219 "If no `arg' is supplied, the current",
220 " directory is used\n");
226 if (strncmp(cmd, "ls", strlen(cmd)) != 0)
228 printlist(name, curdir);
231 * Print current directory.
234 if (strncmp(cmd, "pwd", strlen(cmd)) != 0)
236 if (curdir[1] == '\0')
237 fprintf(stderr, "/\n");
239 fprintf(stderr, "%s\n", &curdir[1]);
245 if (strncmp(cmd, "quit", strlen(cmd)) != 0)
249 if (strncmp(cmd, "xit", strlen(cmd)) != 0)
253 * Toggle verbose mode.
256 if (strncmp(cmd, "verbose", strlen(cmd)) != 0)
259 fprintf(stderr, "verbose mode off\n");
263 fprintf(stderr, "verbose mode on\n");
267 * Just restore requested directory modes.
270 if (strncmp(cmd, "setmodes", strlen(cmd)) != 0)
275 * Print out dump header information.
278 if (strncmp(cmd, "what", strlen(cmd)) != 0)
286 if (strncmp(cmd, "Debug", strlen(cmd)) != 0)
289 fprintf(stderr, "debugging mode off\n");
293 fprintf(stderr, "debugging mode on\n");
301 fprintf(stderr, "%s: unknown command; type ? for help\n", cmd);
308 * Read and parse an interactive command.
309 * The first word on the line is assigned to "cmd". If
310 * there are no arguments on the command line, then "curdir"
311 * is returned as the argument. If there are arguments
312 * on the line they are returned one at a time on each
313 * successive call to getcmd. Each argument is first assigned
314 * to "name". If it does not start with "/" the pathname in
315 * "curdir" is prepended to it. Finally "canon" is called to
316 * eliminate any embedded ".." components.
319 getcmd(curdir, cmd, name, ap)
320 char *curdir, *cmd, *name;
324 static char input[BUFSIZ];
326 # define rawname input /* save space by reusing input buffer */
329 * Check to see if still processing arguments.
336 * Read a command line and trim off trailing white space.
339 fprintf(stderr, "restore > ");
340 (void) fflush(stderr);
341 (void) fgets(input, BUFSIZ, terminal);
342 } while (!feof(terminal) && input[0] == '\n');
343 if (feof(terminal)) {
344 (void) strcpy(cmd, "quit");
347 for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--)
348 /* trim off trailing white space and newline */;
351 * Copy the command into "cmd".
353 cp = copynext(input, cmd);
356 * If no argument, use curdir as the default.
359 (void) strcpy(name, curdir);
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);
379 * For relative pathnames, prepend the current directory to
380 * it then canonicalize and return it.
382 (void) strcpy(output, curdir);
383 (void) strcat(output, "/");
384 (void) strcat(output, rawname);
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 strcpy(name, ap->glob.gl_pathv[ap->glob.gl_pathc - ap->argcnt]);
396 if (--ap->argcnt == 0) {
404 * Strip off the next token of the input.
407 copynext(input, output)
408 char *input, *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 imbedded "." and ".." components.
457 canon(rawname, canonname)
458 char *rawname, *canonname;
460 register char *cp, *np;
462 if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
463 (void) strcpy(canonname, "");
464 else if (rawname[0] == '/')
465 (void) strcpy(canonname, ".");
467 (void) strcpy(canonname, "./");
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(name, basename)
511 register struct afile *fp, *list, *listp;
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)) && (dp->d_ino != 0)) */
535 while (dp = rst_readdir(dirp))
538 list = (struct afile *)malloc(entries * sizeof(struct afile));
540 fprintf(stderr, "ls: out of memory\n");
543 if ((dirp = rst_opendir(name)) == NULL)
544 panic("directory reopen failed\n");
545 fprintf(stderr, "%s:\n", name);
548 (void) strncpy(locname, name, MAXPATHLEN);
549 (void) strncat(locname, "/", MAXPATHLEN);
550 namelen = strlen(locname);
551 while (dp = rst_readdir(dirp)) {
554 if (!dflag && TSTINO(dp->d_ino, dumpmap) == 0)
556 if (!vflag && (dp->d_ino == WINO ||
557 strcmp(dp->d_name, ".") == 0 ||
558 strcmp(dp->d_name, "..") == 0))
560 locname[namelen] = '\0';
561 if (namelen + dp->d_namlen >= MAXPATHLEN) {
562 fprintf(stderr, "%s%s: name exceeds %d char\n",
563 locname, dp->d_name, MAXPATHLEN);
565 (void) strncat(locname, dp->d_name,
567 mkentry(locname, dp, listp++);
573 fprintf(stderr, "\n");
577 qsort((char *)list, entries, sizeof(struct afile), fcmp);
579 formatf(list, entries);
581 for (fp = listp - 1; fp >= list; fp--)
583 fprintf(stderr, "\n");
589 * Read the contents of a directory.
592 mkentry(name, dp, fp)
595 register 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))
613 if (inodetype(dp->d_ino) == NODE)
617 #else /* __linux__ */
621 fprintf(stderr, "Warning: undefined file type %d\n",
648 if (inodetype(dp->d_ino) == NODE)
654 #endif /* __linux__ */
659 * Print out a pretty listing of a directory
662 formatf(list, nentry)
663 register struct afile *list;
666 register struct afile *fp, *endlist;
667 int width, bigino, haveprefix, havepostfix;
668 int i, j, w, precision, columns, lines;
674 endlist = &list[nentry];
675 for (fp = &list[0]; fp < endlist; fp++) {
676 if (bigino < fp->fnum)
680 if (fp->prefix != ' ')
682 if (fp->postfix != ' ')
690 for (precision = 0, i = bigino; i > 0; i /= 10)
692 width += precision + 1;
695 columns = 81 / width;
698 lines = (nentry + columns - 1) / columns;
699 for (i = 0; i < lines; i++) {
700 for (j = 0; j < columns; j++) {
701 fp = &list[j * lines + i];
703 fprintf(stderr, "%*d ", precision, fp->fnum);
704 fp->len += precision + 1;
707 putc(fp->prefix, stderr);
710 fprintf(stderr, "%s", fp->fname);
712 putc(fp->postfix, stderr);
715 if (fp + lines >= endlist) {
716 fprintf(stderr, "\n");
719 for (w = fp->len; w < width; w++)
726 * Skip over directory entries that are not on the tape
728 * First have to get definition of a dirent.
732 off_t d_off; /* offset of next disk dir entry */
733 unsigned long d_fileno; /* file number of entry */
734 unsigned short d_reclen; /* length of this record */
735 unsigned short d_namlen; /* length of string in d_name */
736 char d_name[255+1]; /* name (up to MAXNAMLEN + 1) */
738 #else /* __linux__ */
742 #endif /* __linux__ */
749 static struct dirent adirent;
751 while ((dp = rst_readdir(dirp)) != NULL) {
752 if (!vflag && dp->d_ino == WINO)
754 if (dflag || TSTINO(dp->d_ino, dumpmap))
759 adirent.d_fileno = dp->d_ino;
761 adirent.d_namlen = dp->d_namlen;
763 adirent.d_namlen = dp->d_namlen & 0xff;
765 memmove(adirent.d_name, dp->d_name, dp->d_namlen + 1);
770 * Return st_mode information in response to stat or lstat calls
777 register struct direct *dp;
779 dp = pathsearch(name);
780 if (dp == NULL || (!dflag && TSTINO(dp->d_ino, dumpmap) == 0) ||
781 (!vflag && dp->d_ino == WINO))
783 if (inodetype(dp->d_ino) == NODE)
784 stp->st_mode = IFDIR;
786 stp->st_mode = IFREG;
791 * Comparison routine for qsort.
795 register const void *f1, *f2;
797 return (strcmp(((struct afile *)f1)->fname,
798 ((struct afile *)f2)->fname));
802 * respond to interrupts
808 if (command == 'i' && runshell)
810 if (reply("restore interrupted, continue") == FAIL)