xref: /original-bsd/sbin/restore/dirs.c (revision 331bfa8d)
1 /*
2  * Copyright (c) 1983 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)dirs.c	5.15 (Berkeley) 02/26/91";
10 #endif /* not lint */
11 
12 #include "restore.h"
13 #include <protocols/dumprestore.h>
14 #include <sys/file.h>
15 #include <ufs/dir.h>
16 #include "pathnames.h"
17 
18 /*
19  * Symbol table of directories read from tape.
20  */
21 #define HASHSIZE	1000
22 #define INOHASH(val) (val % HASHSIZE)
23 struct inotab {
24 	struct inotab *t_next;
25 	ino_t	t_ino;
26 	daddr_t	t_seekpt;
27 	long t_size;
28 };
29 static struct inotab *inotab[HASHSIZE];
30 extern struct inotab *inotablookup();
31 extern struct inotab *allocinotab();
32 
33 /*
34  * Information retained about directories.
35  */
36 struct modeinfo {
37 	ino_t ino;
38 	struct timeval timep[2];
39 	short mode;
40 	short uid;
41 	short gid;
42 };
43 
44 /*
45  * Definitions for library routines operating on directories.
46  */
47 #undef DIRBLKSIZ
48 #define DIRBLKSIZ 1024
49 struct dirdesc {
50 	int	dd_fd;
51 	long	dd_loc;
52 	long	dd_size;
53 	char	dd_buf[DIRBLKSIZ];
54 };
55 extern DIR *opendirfile();
56 extern off_t rst_telldir();
57 extern void rst_seekdir();
58 
59 /*
60  * Global variables for this file.
61  */
62 static daddr_t	seekpt;
63 static FILE	*df, *mf;
64 static DIR	*dirp;
65 static char	dirfile[32] = "#";	/* No file */
66 static char	modefile[32] = "#";	/* No file */
67 static char	dot[2] = ".";		/* So it can be modified */
68 extern ino_t	search();
69 struct direct 	*rst_readdir();
70 extern void 	rst_seekdir();
71 
72 /*
73  * Format of old style directories.
74  */
75 #define ODIRSIZ 14
76 struct odirect {
77 	u_short	d_ino;
78 	char	d_name[ODIRSIZ];
79 };
80 
81 /*
82  *	Extract directory contents, building up a directory structure
83  *	on disk for extraction by name.
84  *	If genmode is requested, save mode, owner, and times for all
85  *	directories on the tape.
86  */
87 extractdirs(genmode)
88 	int genmode;
89 {
90 	register int i;
91 	register struct dinode *ip;
92 	struct inotab *itp;
93 	struct direct nulldir;
94 	int putdir(), null();
95 
96 	vprintf(stdout, "Extract directories from tape\n");
97 	(void) sprintf(dirfile, "%s/rstdir%d", _PATH_TMP, dumpdate);
98 	df = fopen(dirfile, "w");
99 	if (df == 0) {
100 		fprintf(stderr,
101 		    "restore: %s - cannot create directory temporary\n",
102 		    dirfile);
103 		perror("fopen");
104 		done(1);
105 	}
106 	if (genmode != 0) {
107 		(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
108 		mf = fopen(modefile, "w");
109 		if (mf == 0) {
110 			fprintf(stderr,
111 			    "restore: %s - cannot create modefile \n",
112 			    modefile);
113 			perror("fopen");
114 			done(1);
115 		}
116 	}
117 	nulldir.d_ino = 0;
118 	nulldir.d_namlen = 1;
119 	(void) strcpy(nulldir.d_name, "/");
120 	nulldir.d_reclen = DIRSIZ(&nulldir);
121 	for (;;) {
122 		curfile.name = "<directory file - name unknown>";
123 		curfile.action = USING;
124 		ip = curfile.dip;
125 		if (ip == NULL || (ip->di_mode & IFMT) != IFDIR) {
126 			(void) fclose(df);
127 			dirp = opendirfile(dirfile);
128 			if (dirp == NULL)
129 				perror("opendirfile");
130 			if (mf != NULL)
131 				(void) fclose(mf);
132 			i = dirlookup(dot);
133 			if (i == 0)
134 				panic("Root directory is not on tape\n");
135 			return;
136 		}
137 		itp = allocinotab(curfile.ino, ip, seekpt);
138 		getfile(putdir, null);
139 		putent(&nulldir);
140 		flushent();
141 		itp->t_size = seekpt - itp->t_seekpt;
142 	}
143 }
144 
145 /*
146  * skip over all the directories on the tape
147  */
148 skipdirs()
149 {
150 
151 	while ((curfile.dip->di_mode & IFMT) == IFDIR) {
152 		skipfile();
153 	}
154 }
155 
156 /*
157  *	Recursively find names and inumbers of all files in subtree
158  *	pname and pass them off to be processed.
159  */
160 treescan(pname, ino, todo)
161 	char *pname;
162 	ino_t ino;
163 	long (*todo)();
164 {
165 	register struct inotab *itp;
166 	register struct direct *dp;
167 	register struct entry *np;
168 	int namelen;
169 	daddr_t bpt;
170 	char locname[MAXPATHLEN + 1];
171 
172 	itp = inotablookup(ino);
173 	if (itp == NULL) {
174 		/*
175 		 * Pname is name of a simple file or an unchanged directory.
176 		 */
177 		(void) (*todo)(pname, ino, LEAF);
178 		return;
179 	}
180 	/*
181 	 * Pname is a dumped directory name.
182 	 */
183 	if ((*todo)(pname, ino, NODE) == FAIL)
184 		return;
185 	/*
186 	 * begin search through the directory
187 	 * skipping over "." and ".."
188 	 */
189 	(void) strncpy(locname, pname, MAXPATHLEN);
190 	(void) strncat(locname, "/", MAXPATHLEN);
191 	namelen = strlen(locname);
192 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
193 	dp = rst_readdir(dirp); /* "." */
194 	if (dp != NULL && strcmp(dp->d_name, ".") == 0)
195 		dp = rst_readdir(dirp); /* ".." */
196 	else
197 		fprintf(stderr, "Warning: `.' missing from directory %s\n",
198 			pname);
199 	if (dp != NULL && strcmp(dp->d_name, "..") == 0)
200 		dp = rst_readdir(dirp); /* first real entry */
201 	else
202 		fprintf(stderr, "Warning: `..' missing from directory %s\n",
203 			pname);
204 	bpt = rst_telldir(dirp);
205 	/*
206 	 * a zero inode signals end of directory
207 	 */
208 	while (dp != NULL && dp->d_ino != 0) {
209 		locname[namelen] = '\0';
210 		if (namelen + dp->d_namlen >= MAXPATHLEN) {
211 			fprintf(stderr, "%s%s: name exceeds %d char\n",
212 				locname, dp->d_name, MAXPATHLEN);
213 		} else {
214 			(void) strncat(locname, dp->d_name, (int)dp->d_namlen);
215 			treescan(locname, dp->d_ino, todo);
216 			rst_seekdir(dirp, bpt, itp->t_seekpt);
217 		}
218 		dp = rst_readdir(dirp);
219 		bpt = rst_telldir(dirp);
220 	}
221 	if (dp == NULL)
222 		fprintf(stderr, "corrupted directory: %s.\n", locname);
223 }
224 
225 /*
226  * Search the directory tree rooted at inode ROOTINO
227  * for the path pointed at by n
228  */
229 ino_t
230 psearch(n)
231 	char	*n;
232 {
233 	register char *cp, *cp1;
234 	ino_t ino;
235 	char c;
236 
237 	ino = ROOTINO;
238 	if (*(cp = n) == '/')
239 		cp++;
240 next:
241 	cp1 = cp + 1;
242 	while (*cp1 != '/' && *cp1)
243 		cp1++;
244 	c = *cp1;
245 	*cp1 = 0;
246 	ino = search(ino, cp);
247 	if (ino == 0) {
248 		*cp1 = c;
249 		return(0);
250 	}
251 	*cp1 = c;
252 	if (c == '/') {
253 		cp = cp1+1;
254 		goto next;
255 	}
256 	return(ino);
257 }
258 
259 /*
260  * search the directory inode ino
261  * looking for entry cp
262  */
263 ino_t
264 search(inum, cp)
265 	ino_t	inum;
266 	char	*cp;
267 {
268 	register struct direct *dp;
269 	register struct inotab *itp;
270 	int len;
271 
272 	itp = inotablookup(inum);
273 	if (itp == NULL)
274 		return(0);
275 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
276 	len = strlen(cp);
277 	do {
278 		dp = rst_readdir(dirp);
279 		if (dp == NULL || dp->d_ino == 0)
280 			return (0);
281 	} while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
282 	return(dp->d_ino);
283 }
284 
285 /*
286  * Put the directory entries in the directory file
287  */
288 putdir(buf, size)
289 	char *buf;
290 	int size;
291 {
292 	struct direct cvtbuf;
293 	register struct odirect *odp;
294 	struct odirect *eodp;
295 	register struct direct *dp;
296 	long loc, i;
297 	extern int Bcvt;
298 
299 	if (cvtflag) {
300 		eodp = (struct odirect *)&buf[size];
301 		for (odp = (struct odirect *)buf; odp < eodp; odp++)
302 			if (odp->d_ino != 0) {
303 				dcvt(odp, &cvtbuf);
304 				putent(&cvtbuf);
305 			}
306 	} else {
307 		for (loc = 0; loc < size; ) {
308 			dp = (struct direct *)(buf + loc);
309 			if (Bcvt) {
310 				swabst("l2s", (char *) dp);
311 			}
312 			i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
313 			if ((dp->d_reclen & 0x3) != 0 ||
314 			    dp->d_reclen > i ||
315 			    dp->d_reclen < DIRSIZ(dp) ||
316 			    dp->d_namlen > MAXNAMLEN) {
317 				vprintf(stdout, "Mangled directory\n");
318 				loc += i;
319 				continue;
320 			}
321 			loc += dp->d_reclen;
322 			if (dp->d_ino != 0) {
323 				putent(dp);
324 			}
325 		}
326 	}
327 }
328 
329 /*
330  * These variables are "local" to the following two functions.
331  */
332 char dirbuf[DIRBLKSIZ];
333 long dirloc = 0;
334 long prev = 0;
335 
336 /*
337  * add a new directory entry to a file.
338  */
339 putent(dp)
340 	struct direct *dp;
341 {
342 	dp->d_reclen = DIRSIZ(dp);
343 	if (dirloc + dp->d_reclen > DIRBLKSIZ) {
344 		((struct direct *)(dirbuf + prev))->d_reclen =
345 		    DIRBLKSIZ - prev;
346 		(void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
347 		dirloc = 0;
348 	}
349 	bcopy((char *)dp, dirbuf + dirloc, (long)dp->d_reclen);
350 	prev = dirloc;
351 	dirloc += dp->d_reclen;
352 }
353 
354 /*
355  * flush out a directory that is finished.
356  */
357 flushent()
358 {
359 
360 	((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
361 	(void) fwrite(dirbuf, (int)dirloc, 1, df);
362 	seekpt = ftell(df);
363 	dirloc = 0;
364 }
365 
366 dcvt(odp, ndp)
367 	register struct odirect *odp;
368 	register struct direct *ndp;
369 {
370 
371 	bzero((char *)ndp, (long)(sizeof *ndp));
372 	ndp->d_ino =  odp->d_ino;
373 	(void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
374 	ndp->d_namlen = strlen(ndp->d_name);
375 	ndp->d_reclen = DIRSIZ(ndp);
376 }
377 
378 /*
379  * Seek to an entry in a directory.
380  * Only values returned by rst_telldir should be passed to rst_seekdir.
381  * This routine handles many directories in a single file.
382  * It takes the base of the directory in the file, plus
383  * the desired seek offset into it.
384  */
385 void
386 rst_seekdir(dirp, loc, base)
387 	register DIR *dirp;
388 	daddr_t loc, base;
389 {
390 
391 	if (loc == rst_telldir(dirp))
392 		return;
393 	loc -= base;
394 	if (loc < 0)
395 		fprintf(stderr, "bad seek pointer to rst_seekdir %d\n", loc);
396 	(void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
397 	dirp->dd_loc = loc & (DIRBLKSIZ - 1);
398 	if (dirp->dd_loc != 0)
399 		dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
400 }
401 
402 /*
403  * get next entry in a directory.
404  */
405 struct direct *
406 rst_readdir(dirp)
407 	register DIR *dirp;
408 {
409 	register struct direct *dp;
410 
411 	for (;;) {
412 		if (dirp->dd_loc == 0) {
413 			dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
414 			    DIRBLKSIZ);
415 			if (dirp->dd_size <= 0) {
416 				dprintf(stderr, "error reading directory\n");
417 				return NULL;
418 			}
419 		}
420 		if (dirp->dd_loc >= dirp->dd_size) {
421 			dirp->dd_loc = 0;
422 			continue;
423 		}
424 		dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
425 		if (dp->d_reclen == 0 ||
426 		    dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
427 			dprintf(stderr, "corrupted directory: bad reclen %d\n",
428 				dp->d_reclen);
429 			return NULL;
430 		}
431 		dirp->dd_loc += dp->d_reclen;
432 		if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
433 			continue;
434 		if (dp->d_ino >= maxino) {
435 			dprintf(stderr, "corrupted directory: bad inum %d\n",
436 				dp->d_ino);
437 			continue;
438 		}
439 		return (dp);
440 	}
441 }
442 
443 /*
444  * Simulate the opening of a directory
445  */
446 DIR *
447 rst_opendir(name)
448 	char *name;
449 {
450 	struct inotab *itp;
451 	ino_t ino;
452 
453 	if ((ino = dirlookup(name)) > 0 &&
454 	    (itp = inotablookup(ino)) != NULL) {
455 		rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
456 		return (dirp);
457 	}
458 	return (0);
459 }
460 
461 /*
462  * Simulate finding the current offset in the directory.
463  */
464 off_t
465 rst_telldir(dirp)
466 	DIR *dirp;
467 {
468 	off_t lseek();
469 
470 	return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);
471 }
472 
473 /*
474  * Open a directory file.
475  */
476 DIR *
477 opendirfile(name)
478 	char *name;
479 {
480 	register DIR *dirp;
481 	register int fd;
482 
483 	if ((fd = open(name, 0)) == -1)
484 		return NULL;
485 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
486 		close (fd);
487 		return NULL;
488 	}
489 	dirp->dd_fd = fd;
490 	dirp->dd_loc = 0;
491 	return dirp;
492 }
493 
494 /*
495  * Set the mode, owner, and times for all new or changed directories
496  */
497 setdirmodes()
498 {
499 	FILE *mf;
500 	struct modeinfo node;
501 	struct entry *ep;
502 	char *cp;
503 
504 	vprintf(stdout, "Set directory mode, owner, and times.\n");
505 	(void) sprintf(modefile, "%s/rstmode%d", _PATH_TMP, dumpdate);
506 	mf = fopen(modefile, "r");
507 	if (mf == NULL) {
508 		perror("fopen");
509 		fprintf(stderr, "cannot open mode file %s\n", modefile);
510 		fprintf(stderr, "directory mode, owner, and times not set\n");
511 		return;
512 	}
513 	clearerr(mf);
514 	for (;;) {
515 		(void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
516 		if (feof(mf))
517 			break;
518 		ep = lookupino(node.ino);
519 		if (command == 'i' || command == 'x') {
520 			if (ep == NIL)
521 				continue;
522 			if (ep->e_flags & EXISTED) {
523 				ep->e_flags &= ~NEW;
524 				continue;
525 			}
526 			if (node.ino == ROOTINO &&
527 		   	    reply("set owner/mode for '.'") == FAIL)
528 				continue;
529 		}
530 		if (ep == NIL) {
531 			panic("cannot find directory inode %d\n", node.ino);
532 		} else {
533 			cp = myname(ep);
534 			(void) chown(cp, node.uid, node.gid);
535 			(void) chmod(cp, node.mode);
536 			utimes(cp, node.timep);
537 			ep->e_flags &= ~NEW;
538 		}
539 	}
540 	if (ferror(mf))
541 		panic("error setting directory modes\n");
542 	(void) fclose(mf);
543 }
544 
545 /*
546  * Generate a literal copy of a directory.
547  */
548 genliteraldir(name, ino)
549 	char *name;
550 	ino_t ino;
551 {
552 	register struct inotab *itp;
553 	int ofile, dp, i, size;
554 	char buf[BUFSIZ];
555 
556 	itp = inotablookup(ino);
557 	if (itp == NULL)
558 		panic("Cannot find directory inode %d named %s\n", ino, name);
559 	if ((ofile = creat(name, 0666)) < 0) {
560 		fprintf(stderr, "%s: ", name);
561 		(void) fflush(stderr);
562 		perror("cannot create file");
563 		return (FAIL);
564 	}
565 	rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
566 	dp = dup(dirp->dd_fd);
567 	for (i = itp->t_size; i > 0; i -= BUFSIZ) {
568 		size = i < BUFSIZ ? i : BUFSIZ;
569 		if (read(dp, buf, (int) size) == -1) {
570 			fprintf(stderr,
571 				"write error extracting inode %d, name %s\n",
572 				curfile.ino, curfile.name);
573 			perror("read");
574 			done(1);
575 		}
576 		if (!Nflag && write(ofile, buf, (int) size) == -1) {
577 			fprintf(stderr,
578 				"write error extracting inode %d, name %s\n",
579 				curfile.ino, curfile.name);
580 			perror("write");
581 			done(1);
582 		}
583 	}
584 	(void) close(dp);
585 	(void) close(ofile);
586 	return (GOOD);
587 }
588 
589 /*
590  * Determine the type of an inode
591  */
592 inodetype(ino)
593 	ino_t ino;
594 {
595 	struct inotab *itp;
596 
597 	itp = inotablookup(ino);
598 	if (itp == NULL)
599 		return (LEAF);
600 	return (NODE);
601 }
602 
603 /*
604  * Allocate and initialize a directory inode entry.
605  * If requested, save its pertinent mode, owner, and time info.
606  */
607 struct inotab *
608 allocinotab(ino, dip, seekpt)
609 	ino_t ino;
610 	struct dinode *dip;
611 	daddr_t seekpt;
612 {
613 	register struct inotab	*itp;
614 	struct modeinfo node;
615 
616 	itp = (struct inotab *)calloc(1, sizeof(struct inotab));
617 	if (itp == 0)
618 		panic("no memory directory table\n");
619 	itp->t_next = inotab[INOHASH(ino)];
620 	inotab[INOHASH(ino)] = itp;
621 	itp->t_ino = ino;
622 	itp->t_seekpt = seekpt;
623 	if (mf == NULL)
624 		return(itp);
625 	node.ino = ino;
626 	node.timep[0].tv_sec = dip->di_atime;
627 	node.timep[0].tv_usec = 0;
628 	node.timep[1].tv_sec = dip->di_mtime;
629 	node.timep[1].tv_usec = 0;
630 	node.mode = dip->di_mode;
631 	node.uid = dip->di_uid;
632 	node.gid = dip->di_gid;
633 	(void) fwrite((char *)&node, 1, sizeof(struct modeinfo), mf);
634 	return(itp);
635 }
636 
637 /*
638  * Look up an inode in the table of directories
639  */
640 struct inotab *
641 inotablookup(ino)
642 	ino_t	ino;
643 {
644 	register struct inotab *itp;
645 
646 	for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
647 		if (itp->t_ino == ino)
648 			return(itp);
649 	return ((struct inotab *)0);
650 }
651 
652 /*
653  * Clean up and exit
654  */
655 done(exitcode)
656 	int exitcode;
657 {
658 
659 	closemt();
660 	if (modefile[0] != '#')
661 		(void) unlink(modefile);
662 	if (dirfile[0] != '#')
663 		(void) unlink(dirfile);
664 	exit(exitcode);
665 }
666