xref: /csrg-svn/sbin/restore/utilities.c (revision 11923)
110315Smckusick /* Copyright (c) 1983 Regents of the University of California */
210315Smckusick 
310315Smckusick #ifndef lint
4*11923Smckusick static char sccsid[] = "@(#)utilities.c	3.10	(Berkeley)	83/04/16";
510315Smckusick #endif
610315Smckusick 
710315Smckusick #include "restore.h"
810315Smckusick 
910315Smckusick /*
1010315Smckusick  * Insure that all the components of a pathname exist.
1110315Smckusick  */
1211645Smckusick pathcheck(name)
1310315Smckusick 	char *name;
1410315Smckusick {
1510315Smckusick 	register char *cp;
1610315Smckusick 	struct entry *ep;
1711312Smckusick 	char *start;
1810315Smckusick 
1910315Smckusick 	start = index(name, '/');
2011312Smckusick 	if (start == 0)
2111645Smckusick 		return;
2210315Smckusick 	for (cp = start; *cp != '\0'; cp++) {
2310315Smckusick 		if (*cp != '/')
2410315Smckusick 			continue;
2510315Smckusick 		*cp = '\0';
2610315Smckusick 		ep = lookupname(name);
2710315Smckusick 		if (ep == NIL) {
2811645Smckusick 			ep = addentry(name, ep->e_ino, NODE);
2911645Smckusick 			ep->e_flags |= KEEP;
3010315Smckusick 			newnode(ep);
3110315Smckusick 		}
3210315Smckusick 		*cp = '/';
3310315Smckusick 	}
3410315Smckusick }
3510315Smckusick 
3610315Smckusick /*
3710315Smckusick  * Change a name to a unique temporary name.
3810315Smckusick  */
3910315Smckusick mktempname(ep)
4010315Smckusick 	register struct entry *ep;
4110315Smckusick {
4211645Smckusick 	char oldname[MAXPATHLEN];
4310315Smckusick 
4410315Smckusick 	if (ep->e_flags & TMPNAME)
4510315Smckusick 		badentry(ep, "mktempname: called with TMPNAME");
4610315Smckusick 	ep->e_flags |= TMPNAME;
4710315Smckusick 	strcpy(oldname, myname(ep));
4811645Smckusick 	freename(ep->e_name);
4911645Smckusick 	ep->e_name = savename(gentempname(ep));
5011645Smckusick 	ep->e_namlen = strlen(ep->e_name);
5110315Smckusick 	renameit(oldname, myname(ep));
5210315Smckusick }
5310315Smckusick 
5410315Smckusick /*
5511645Smckusick  * Generate a temporary name for an entry.
5611645Smckusick  */
5711645Smckusick char *
5811645Smckusick gentempname(ep)
5911645Smckusick 	struct entry *ep;
6011645Smckusick {
6111645Smckusick 	static char name[MAXPATHLEN];
6211645Smckusick 	struct entry *np;
6311645Smckusick 	long i = 0;
6411645Smckusick 
6511645Smckusick 	for (np = lookupino(ep->e_ino); np != NIL && np != ep; np = np->e_links)
6611645Smckusick 		i++;
6711645Smckusick 	if (np == NIL)
6811645Smckusick 		badentry(ep, "not on ino list");
6911733Smckusick 	(void) sprintf(name, "%s%d%d", TMPHDR, i, ep->e_ino);
7011645Smckusick 	return (name);
7111645Smckusick }
7211645Smckusick 
7311645Smckusick /*
7410315Smckusick  * Rename a file or directory.
7510315Smckusick  */
7610315Smckusick renameit(from, to)
7710315Smckusick 	char *from, *to;
7810315Smckusick {
7910315Smckusick 	if (rename(from, to) < 0) {
80*11923Smckusick 		fprintf(stderr, "Warning: cannot rename %s to %s\n", from, to);
81*11923Smckusick 		perror("rename");
82*11923Smckusick 		return;
8310315Smckusick 	}
8410315Smckusick 	vprintf(stdout, "rename %s to %s\n", from, to);
8510315Smckusick }
8610315Smckusick 
8710315Smckusick /*
8810315Smckusick  * Create a new node (directory).
8910315Smckusick  */
9010315Smckusick newnode(np)
9110315Smckusick 	struct entry *np;
9210315Smckusick {
9310315Smckusick 	char *cp;
9410315Smckusick 
9510315Smckusick 	if (np->e_type != NODE)
9610315Smckusick 		badentry(np, "newnode: not a node");
9710315Smckusick 	cp = myname(np);
9811312Smckusick 	if (mkdir(cp, 0777) < 0) {
9911740Smckusick 		fprintf(stderr, "Warning: ");
10011740Smckusick 		fflush(stderr);
10111740Smckusick 		perror(cp);
10211740Smckusick 		return;
10310315Smckusick 	}
10410315Smckusick 	vprintf(stdout, "Make node %s\n", cp);
10510315Smckusick }
10610315Smckusick 
10710315Smckusick /*
10810315Smckusick  * Remove an old node (directory).
10910315Smckusick  */
11010315Smckusick removenode(ep)
11110315Smckusick 	register struct entry *ep;
11210315Smckusick {
11310315Smckusick 	char *cp;
11410315Smckusick 
11510315Smckusick 	if (ep->e_type != NODE)
11610315Smckusick 		badentry(ep, "removenode: not a node");
11710315Smckusick 	if (ep->e_entries != NIL)
11810315Smckusick 		badentry(ep, "removenode: non-empty directory");
119*11923Smckusick 	ep->e_flags |= REMOVED;
120*11923Smckusick 	ep->e_flags &= ~TMPNAME;
12110315Smckusick 	cp = myname(ep);
12210315Smckusick 	if (rmdir(cp) < 0) {
123*11923Smckusick 		fprintf(stderr, "Warning: ");
124*11923Smckusick 		fflush(stderr);
125*11923Smckusick 		perror(cp);
126*11923Smckusick 		return;
12710315Smckusick 	}
12810315Smckusick 	vprintf(stdout, "Remove node %s\n", cp);
12910315Smckusick }
13010315Smckusick 
13110315Smckusick /*
13210315Smckusick  * Remove a leaf.
13310315Smckusick  */
13410315Smckusick removeleaf(ep)
13510315Smckusick 	register struct entry *ep;
13610315Smckusick {
13710315Smckusick 	char *cp;
13810315Smckusick 
13910315Smckusick 	if (ep->e_type != LEAF)
14010315Smckusick 		badentry(ep, "removeleaf: not a leaf");
141*11923Smckusick 	ep->e_flags |= REMOVED;
142*11923Smckusick 	ep->e_flags &= ~TMPNAME;
14310315Smckusick 	cp = myname(ep);
14410315Smckusick 	if (unlink(cp) < 0) {
145*11923Smckusick 		fprintf(stderr, "Warning: ");
146*11923Smckusick 		fflush(stderr);
147*11923Smckusick 		perror(cp);
148*11923Smckusick 		return;
14910315Smckusick 	}
15010315Smckusick 	vprintf(stdout, "Remove leaf %s\n", cp);
15110315Smckusick }
15210315Smckusick 
15310315Smckusick /*
15410315Smckusick  * Create a link.
15510315Smckusick  */
15610315Smckusick linkit(existing, new, type)
15710315Smckusick 	char *existing, *new;
15810315Smckusick 	int type;
15910315Smckusick {
16010315Smckusick 
16110315Smckusick 	if (type == SYMLINK) {
16210315Smckusick 		if (symlink(existing, new) < 0) {
163*11923Smckusick 			fprintf(stderr,
164*11923Smckusick 				"Warning: cannot create symbolic link %s->%s\n",
16510315Smckusick 				new, existing);
166*11923Smckusick 			perror("symlink");
167*11923Smckusick 			return;
16810315Smckusick 		}
16910315Smckusick 	} else if (type == HARDLINK) {
17010315Smckusick 		if (link(existing, new) < 0) {
171*11923Smckusick 			fprintf(stderr,
172*11923Smckusick 				"Warning: cannot create hard link %s->%s\n",
17310315Smckusick 				new, existing);
174*11923Smckusick 			perror("link");
175*11923Smckusick 			return;
17610315Smckusick 		}
17710315Smckusick 	} else {
17810315Smckusick 		panic("linkit: unknown type %d\n", type);
17910315Smckusick 	}
18010315Smckusick 	vprintf(stdout, "Create %s link %s->%s\n",
18110315Smckusick 		type == SYMLINK ? "symbolic" : "hard", new, existing);
18210315Smckusick }
18310315Smckusick 
18410315Smckusick /*
18510315Smckusick  * find lowest number file (above "start") that needs to be extracted
18610315Smckusick  */
18710315Smckusick ino_t
18810315Smckusick lowerbnd(start)
18910315Smckusick 	ino_t start;
19010315Smckusick {
19110315Smckusick 	register struct entry *ep;
19210315Smckusick 
19310315Smckusick 	for ( ; start < maxino; start++) {
19410315Smckusick 		ep = lookupino(start);
19510315Smckusick 		if (ep == NIL)
19610315Smckusick 			continue;
19711645Smckusick 		if (ep->e_flags & (NEW|EXTRACT))
19810315Smckusick 			return (start);
19910315Smckusick 	}
20010315Smckusick 	return (start);
20110315Smckusick }
20210315Smckusick 
20310315Smckusick /*
20410315Smckusick  * find highest number file (below "start") that needs to be extracted
20510315Smckusick  */
20610315Smckusick ino_t
20710315Smckusick upperbnd(start)
20810315Smckusick 	ino_t start;
20910315Smckusick {
21010315Smckusick 	register struct entry *ep;
21110315Smckusick 
21210315Smckusick 	for ( ; start > ROOTINO; start--) {
21310315Smckusick 		ep = lookupino(start);
21410315Smckusick 		if (ep == NIL)
21510315Smckusick 			continue;
21611645Smckusick 		if (ep->e_flags & (NEW|EXTRACT))
21710315Smckusick 			return (start);
21810315Smckusick 	}
21910315Smckusick 	return (start);
22010315Smckusick }
22110315Smckusick 
22210315Smckusick /*
22310315Smckusick  * report on a badly formed entry
22410315Smckusick  */
22510315Smckusick badentry(ep, msg)
22610315Smckusick 	register struct entry *ep;
22710315Smckusick 	char *msg;
22810315Smckusick {
22910315Smckusick 
23010315Smckusick 	fprintf(stderr, "bad entry: %s\n", msg);
23110315Smckusick 	fprintf(stderr, "name: %s\n", myname(ep));
23210315Smckusick 	fprintf(stderr, "parent name %s\n", myname(ep->e_parent));
23310315Smckusick 	if (ep->e_sibling != NIL)
23410315Smckusick 		fprintf(stderr, "sibling name: %s\n", myname(ep->e_sibling));
23510315Smckusick 	if (ep->e_entries != NIL)
23610315Smckusick 		fprintf(stderr, "next entry name: %s\n", myname(ep->e_entries));
23710315Smckusick 	if (ep->e_links != NIL)
23810315Smckusick 		fprintf(stderr, "next link name: %s\n", myname(ep->e_links));
23911645Smckusick 	if (ep->e_next != NIL)
24011645Smckusick 		fprintf(stderr, "next hashchain name: %s\n", myname(ep->e_next));
24110315Smckusick 	fprintf(stderr, "entry type: %s\n",
24210315Smckusick 		ep->e_type == NODE ? "NODE" : "LEAF");
24310315Smckusick 	fprintf(stderr, "inode number: %ld\n", ep->e_ino);
24411898Smckusick 	panic("flags: %s\n", flagvalues(ep));
24511898Smckusick }
24611898Smckusick 
24711898Smckusick /*
24811898Smckusick  * Construct a string indicating the active flag bits of an entry.
24911898Smckusick  */
25011898Smckusick char *
25111898Smckusick flagvalues(ep)
25211898Smckusick 	register struct entry *ep;
25311898Smckusick {
25411898Smckusick 	static char flagbuf[BUFSIZ];
25511898Smckusick 
25611898Smckusick 	(void) strcpy(flagbuf, "|NIL");
25710315Smckusick 	flagbuf[0] = '\0';
25810315Smckusick 	if (ep->e_flags & REMOVED)
25911898Smckusick 		(void) strcat(flagbuf, "|REMOVED");
26010315Smckusick 	if (ep->e_flags & TMPNAME)
26111898Smckusick 		(void) strcat(flagbuf, "|TMPNAME");
26210315Smckusick 	if (ep->e_flags & EXTRACT)
26311898Smckusick 		(void) strcat(flagbuf, "|EXTRACT");
26410315Smckusick 	if (ep->e_flags & NEW)
26511898Smckusick 		(void) strcat(flagbuf, "|NEW");
26610315Smckusick 	if (ep->e_flags & KEEP)
26711898Smckusick 		(void) strcat(flagbuf, "|KEEP");
26811898Smckusick 	return (&flagbuf[1]);
26910315Smckusick }
27010315Smckusick 
27110315Smckusick /*
27211321Smckusick  * canonicalize file names to always start with ``./''
27311321Smckusick  */
27411321Smckusick canon(rawname, canonname)
27511321Smckusick 	char *rawname, *canonname;
27611321Smckusick {
27711321Smckusick 	int len;
27811321Smckusick 
27911321Smckusick 	if (strcmp(rawname, ".") == 0 || strncmp(rawname, "./", 2) == 0)
28011645Smckusick 		(void) strcpy(canonname, "");
28111321Smckusick 	else if (rawname[0] == '/')
28211645Smckusick 		(void) strcpy(canonname, ".");
28311321Smckusick 	else
28411645Smckusick 		(void) strcpy(canonname, "./");
28511645Smckusick 	(void) strcat(canonname, rawname);
28611321Smckusick 	len = strlen(canonname) - 1;
28711321Smckusick 	if (canonname[len] == '/')
28811321Smckusick 		canonname[len] = '\0';
28911321Smckusick }
29011321Smckusick 
29111321Smckusick /*
29210315Smckusick  * elicit a reply
29310315Smckusick  */
29410315Smckusick reply(question)
29510315Smckusick 	char *question;
29610315Smckusick {
29710315Smckusick 	char c;
29810315Smckusick 
29910315Smckusick 	fprintf(stderr, "%s? ", question);
30010315Smckusick 	do	{
30110315Smckusick 		fprintf(stderr, "[yn] ");
30210315Smckusick 		c = getchar();
30310315Smckusick 		while (c != '\n' && getchar() != '\n')
30410315Smckusick 			/* void */;
30510315Smckusick 	} while (c != 'y' && c != 'n');
30610315Smckusick 	if (c == 'y')
30710315Smckusick 		return (GOOD);
30810315Smckusick 	return (FAIL);
30910315Smckusick }
310