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