1 /* $NetBSD: dirs.c,v 1.55 2022/12/12 16:53:30 chs Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)dirs.c 8.7 (Berkeley) 5/1/95";
41 #else
42 __RCSID("$NetBSD: dirs.c,v 1.55 2022/12/12 16:53:30 chs Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50
51 #include <ufs/ufs/dinode.h>
52 #include <ufs/ufs/dir.h>
53 #include <ufs/ffs/fs.h>
54 #include <protocols/dumprestore.h>
55
56 #include <err.h>
57 #include <errno.h>
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63
64 #include <machine/endian.h>
65
66 #include "restore.h"
67 #include "extern.h"
68
69 /*
70 * Symbol table of directories read from tape.
71 */
72 #define HASHSIZE 1000
73 #define INOHASH(val) (val % HASHSIZE)
74 struct inotab {
75 struct inotab *t_next;
76 ino_t t_ino;
77 int32_t t_seekpt;
78 int32_t t_size;
79 };
80 static struct inotab *inotab[HASHSIZE];
81
82 /*
83 * Information retained about directories.
84 */
85 struct modeinfo {
86 ino_t ino;
87 struct timespec ctimep[2];
88 struct timespec mtimep[2];
89 mode_t mode;
90 uid_t uid;
91 gid_t gid;
92 u_int flags;
93 int extsize;
94 };
95
96 /*
97 * Definitions for library routines operating on directories.
98 */
99 #undef DIRBLKSIZ
100 #define DIRBLKSIZ 1024
101 struct rstdirdesc {
102 int dd_fd;
103 int32_t dd_loc;
104 int32_t dd_size;
105 char dd_buf[DIRBLKSIZ];
106 };
107
108 /*
109 * Global variables for this file.
110 */
111 static long seekpt;
112 static FILE *df, *mf;
113 static RST_DIR *dirp;
114 static char dirfile[MAXPATHLEN] = "#"; /* No file */
115 static char modefile[MAXPATHLEN] = "#"; /* No file */
116 static char dot[2] = "."; /* So it can be modified */
117
118 /*
119 * Format of old style directories.
120 */
121 #define ODIRSIZ 14
122 struct odirect {
123 u_short d_ino;
124 char d_name[ODIRSIZ];
125 };
126
127 static struct inotab *allocinotab(struct context *, long);
128 static void dcvt(struct odirect *, struct direct *);
129 static void flushent(void);
130 static struct inotab *inotablookup(ino_t);
131 static RST_DIR *opendirfile(const char *);
132 static void putdir(char *, size_t);
133 static void putdirattrs(char *, size_t);
134 static void putent(struct direct *);
135 static void rst_seekdir(RST_DIR *, long, long);
136 static long rst_telldir(RST_DIR *);
137 static struct direct *searchdir(ino_t, char *);
138 static void fail_dirtmp(char *) __dead;
139
140 /*
141 * Extract directory contents, building up a directory structure
142 * on disk for extraction by name.
143 * If genmode is requested, save mode, owner, and times for all
144 * directories on the tape.
145 */
146 void
extractdirs(int genmode)147 extractdirs(int genmode)
148 {
149 int i, dfd, mfd;
150 struct inotab *itp;
151 struct direct nulldir;
152
153 vprintf(stdout, "Extract directories from tape\n");
154 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%jd",
155 tmpdir, (intmax_t)dumpdate);
156 if (command != 'r' && command != 'R') {
157 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%jd-XXXXXX",
158 tmpdir, (intmax_t)dumpdate);
159 if ((dfd = mkstemp(dirfile)) == -1)
160 err(1, "cannot mkstemp temporary file %s", dirfile);
161 df = fdopen(dfd, "w");
162 }
163 else
164 df = fopen(dirfile, "w");
165 if (df == NULL)
166 err(1, "cannot open temporary file %s", dirfile);
167
168 if (genmode != 0) {
169 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd",
170 tmpdir, (intmax_t)dumpdate);
171 if (command != 'r' && command != 'R') {
172 (void) snprintf(modefile, sizeof(modefile),
173 "%s/rstmode%jd-XXXXXX", tmpdir, (intmax_t)dumpdate);
174 if ((mfd = mkstemp(modefile)) == -1)
175 err(1, "cannot mkstemp temporary file %s",
176 modefile);
177 mf = fdopen(mfd, "w");
178 }
179 else
180 mf = fopen(modefile, "w");
181 if (mf == NULL)
182 err(1, "cannot open temporary file %s", modefile);
183 }
184 nulldir.d_ino = 0;
185 nulldir.d_type = DT_DIR;
186 nulldir.d_namlen = 1;
187 (void) strcpy(nulldir.d_name, "/");
188 nulldir.d_reclen = UFS_DIRSIZ(0, &nulldir, 0);
189 for (;;) {
190 curfile.name = "<directory file - name unknown>";
191 curfile.action = USING;
192 if (curfile.mode == 0 || (curfile.mode & IFMT) != IFDIR)
193 break;
194 itp = allocinotab(&curfile, seekpt);
195 getfile(putdir, putdirattrs, xtrnull);
196 putent(&nulldir);
197 flushent();
198 itp->t_size = seekpt - itp->t_seekpt;
199 }
200 if (fclose(df) != 0)
201 fail_dirtmp(dirfile);
202 dirp = opendirfile(dirfile);
203 if (dirp == NULL)
204 fprintf(stderr, "opendirfile: %s\n", strerror(errno));
205 if (mf != NULL && fclose(mf) != 0)
206 fail_dirtmp(modefile);
207 i = dirlookup(dot);
208 if (i == 0)
209 panic("Root directory is not on tape\n");
210 }
211
212 /*
213 * skip over all the directories on the tape
214 */
215 void
skipdirs(void)216 skipdirs(void)
217 {
218
219 while (curfile.ino && (curfile.mode & IFMT) == IFDIR) {
220 skipfile();
221 }
222 }
223
224 /*
225 * Recursively find names and inumbers of all files in subtree
226 * pname and pass them off to be processed.
227 */
228 void
treescan(const char * pname,ino_t ino,long (* todo)(const char *,ino_t,int))229 treescan(const char *pname, ino_t ino, long (*todo)(const char *, ino_t, int))
230 {
231 struct inotab *itp;
232 struct direct *dp;
233 size_t namelen;
234 long bpt;
235 char locname[MAXPATHLEN + 1];
236
237 itp = inotablookup(ino);
238 if (itp == NULL) {
239 /*
240 * Pname is name of a simple file or an unchanged directory.
241 */
242 (void) (*todo)(pname, ino, LEAF);
243 return;
244 }
245 /*
246 * Pname is a dumped directory name.
247 */
248 if ((*todo)(pname, ino, NODE) == FAIL)
249 return;
250 /*
251 * begin search through the directory
252 * skipping over "." and ".."
253 */
254 (void) snprintf(locname, sizeof(locname), "%s/", pname);
255 namelen = strlen(locname);
256 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
257 dp = rst_readdir(dirp); /* "." */
258 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
259 dp = rst_readdir(dirp); /* ".." */
260 else
261 fprintf(stderr, "Warning: `.' missing from directory %s\n",
262 pname);
263 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
264 dp = rst_readdir(dirp); /* first real entry */
265 else
266 fprintf(stderr, "Warning: `..' missing from directory %s\n",
267 pname);
268 bpt = rst_telldir(dirp);
269 /*
270 * a zero inode signals end of directory
271 */
272 while (dp != NULL) {
273 locname[namelen] = '\0';
274 if (namelen + dp->d_namlen >= sizeof(locname)) {
275 fprintf(stderr, "%s%s: name exceeds %zu char\n",
276 locname, dp->d_name, sizeof(locname) - 1);
277 } else {
278 (void)strlcat(locname, dp->d_name, sizeof(locname));
279 treescan(locname, dp->d_ino, todo);
280 rst_seekdir(dirp, bpt, itp->t_seekpt);
281 }
282 dp = rst_readdir(dirp);
283 bpt = rst_telldir(dirp);
284 }
285 }
286
287 /*
288 * Lookup a pathname which is always assumed to start from the root inode.
289 */
290 struct direct *
pathsearch(const char * pathname)291 pathsearch(const char *pathname)
292 {
293 ino_t ino;
294 struct direct *dp;
295 char *path, *name, buffer[MAXPATHLEN];
296
297 strcpy(buffer, pathname);
298 path = buffer;
299 ino = UFS_ROOTINO;
300 while (*path == '/')
301 path++;
302 dp = NULL;
303 while ((name = strsep(&path, "/")) != NULL && *name != '\0') {
304 if ((dp = searchdir(ino, name)) == NULL)
305 return (NULL);
306 ino = dp->d_ino;
307 }
308 return (dp);
309 }
310
311 /*
312 * Lookup the requested name in directory inum.
313 * Return its inode number if found, zero if it does not exist.
314 */
315 static struct direct *
searchdir(ino_t inum,char * name)316 searchdir(ino_t inum, char *name)
317 {
318 struct direct *dp;
319 struct inotab *itp;
320 int len;
321
322 itp = inotablookup(inum);
323 if (itp == NULL)
324 return (NULL);
325 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
326 len = strlen(name);
327 do {
328 dp = rst_readdir(dirp);
329 if (dp == NULL)
330 return (NULL);
331 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
332 return (dp);
333 }
334
335 /*
336 * Put the directory entries in the directory file
337 */
338 static void
putdir(char * buf,size_t size)339 putdir(char *buf, size_t size)
340 {
341 struct direct cvtbuf;
342 struct odirect *odp;
343 struct odirect *eodp;
344 struct direct *dp;
345 size_t loc, i;
346
347 if (cvtflag) {
348 eodp = (struct odirect *)&buf[size];
349 for (odp = (struct odirect *)buf; odp < eodp; odp++)
350 if (odp->d_ino != 0) {
351 dcvt(odp, &cvtbuf);
352 putent(&cvtbuf);
353 }
354 } else {
355 for (loc = 0; loc < size; ) {
356 dp = (struct direct *)(buf + loc);
357 if (Bcvt) {
358 dp->d_ino = bswap32(dp->d_ino);
359 dp->d_reclen = bswap16(dp->d_reclen);
360 }
361 if (oldinofmt && dp->d_ino != 0) {
362 # if BYTE_ORDER == BIG_ENDIAN
363 if (Bcvt)
364 dp->d_namlen = dp->d_type;
365 # else
366 if (!Bcvt)
367 dp->d_namlen = dp->d_type;
368 # endif
369 dp->d_type = DT_UNKNOWN;
370 }
371 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
372 if ((dp->d_reclen & 0x3) != 0 ||
373 dp->d_reclen > i ||
374 dp->d_reclen < UFS_DIRSIZ(0, dp, 0) /* ||
375 dp->d_namlen > NAME_MAX */) {
376 vprintf(stdout, "Mangled directory: ");
377 if ((dp->d_reclen & 0x3) != 0)
378 vprintf(stdout,
379 "reclen not multiple of 4 ");
380 if (dp->d_reclen < UFS_DIRSIZ(0, dp, 0))
381 vprintf(stdout,
382 "reclen less than UFS_DIRSIZ (%d < %lu) ",
383 dp->d_reclen, (u_long)UFS_DIRSIZ(0, dp, 0));
384 #if 0 /* dp->d_namlen is a uint8_t, always < NAME_MAX */
385 if (dp->d_namlen > NAME_MAX)
386 vprintf(stdout,
387 "reclen name too big (%d > %d) ",
388 dp->d_namlen, NAME_MAX);
389 #endif
390 vprintf(stdout, "\n");
391 loc += i;
392 continue;
393 }
394 loc += dp->d_reclen;
395 if (dp->d_ino != 0) {
396 putent(dp);
397 }
398 }
399 }
400 }
401
402 /*
403 * These variables are "local" to the following two functions.
404 */
405 char dirbuf[DIRBLKSIZ];
406 long dirloc = 0;
407 long prev = 0;
408
409 /*
410 * add a new directory entry to a file.
411 */
412 static void
putent(struct direct * dp)413 putent(struct direct *dp)
414 {
415 dp->d_reclen = UFS_DIRSIZ(0, dp, 0);
416 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
417 ((struct direct *)(dirbuf + prev))->d_reclen =
418 DIRBLKSIZ - prev;
419 if (fwrite(dirbuf, DIRBLKSIZ, 1, df) != 1)
420 fail_dirtmp(dirfile);
421 dirloc = 0;
422 }
423 memmove(dirbuf + dirloc, dp, (long)dp->d_reclen);
424 prev = dirloc;
425 dirloc += dp->d_reclen;
426 }
427
428 /*
429 * flush out a directory that is finished.
430 */
431 static void
flushent(void)432 flushent(void)
433 {
434 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
435 if (fwrite(dirbuf, (int)dirloc, 1, df) != 1)
436 fail_dirtmp(dirfile);
437 seekpt = ftell(df);
438 dirloc = 0;
439 }
440
441 static void
dcvt(struct odirect * odp,struct direct * ndp)442 dcvt(struct odirect *odp, struct direct *ndp)
443 {
444
445 memset(ndp, 0, sizeof(*ndp));
446 if (Bcvt)
447 ndp->d_ino = bswap16(odp->d_ino);
448 else
449 ndp->d_ino = odp->d_ino;
450 ndp->d_type = DT_UNKNOWN;
451 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
452 ndp->d_namlen = strlen(ndp->d_name);
453 ndp->d_reclen = UFS_DIRSIZ(0, ndp, 0);
454 }
455
456 /*
457 * Save extended attributes for a directory entry to a file.
458 */
459 static void
putdirattrs(char * buf,size_t size)460 putdirattrs(char *buf, size_t size)
461 {
462
463 if (mf != NULL && fwrite(buf, size, 1, mf) != 1)
464 fail_dirtmp(modefile);
465 }
466
467 /*
468 * Seek to an entry in a directory.
469 * Only values returned by rst_telldir should be passed to rst_seekdir.
470 * This routine handles many directories in a single file.
471 * It takes the base of the directory in the file, plus
472 * the desired seek offset into it.
473 */
474 static void
rst_seekdir(RST_DIR * rdirp,long loc,long base)475 rst_seekdir(RST_DIR *rdirp, long loc, long base)
476 {
477
478 if (loc == rst_telldir(rdirp))
479 return;
480 loc -= base;
481 if (loc < 0)
482 fprintf(stderr, "bad seek pointer to rst_seekdir %d\n",
483 (int)loc);
484 (void) lseek(rdirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
485 rdirp->dd_loc = loc & (DIRBLKSIZ - 1);
486 if (rdirp->dd_loc != 0)
487 rdirp->dd_size = read(rdirp->dd_fd, rdirp->dd_buf, DIRBLKSIZ);
488 }
489
490 /*
491 * get next entry in a directory.
492 */
493 struct direct *
rst_readdir(RST_DIR * rdirp)494 rst_readdir(RST_DIR *rdirp)
495 {
496 struct direct *dp;
497
498 for (;;) {
499 if (rdirp->dd_loc == 0) {
500 rdirp->dd_size = read(rdirp->dd_fd, rdirp->dd_buf,
501 DIRBLKSIZ);
502 if (rdirp->dd_size <= 0) {
503 dprintf(stderr, "error reading directory\n");
504 return (NULL);
505 }
506 }
507 if (rdirp->dd_loc >= rdirp->dd_size) {
508 rdirp->dd_loc = 0;
509 continue;
510 }
511 dp = (struct direct *)(rdirp->dd_buf + rdirp->dd_loc);
512 if (dp->d_reclen == 0 ||
513 dp->d_reclen > DIRBLKSIZ + 1 - rdirp->dd_loc) {
514 dprintf(stderr, "corrupted directory: bad reclen %d\n",
515 dp->d_reclen);
516 return (NULL);
517 }
518 rdirp->dd_loc += dp->d_reclen;
519 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
520 return (NULL);
521 if (dp->d_ino >= maxino) {
522 dprintf(stderr, "corrupted directory: bad inum %d\n",
523 dp->d_ino);
524 continue;
525 }
526 return (dp);
527 }
528 }
529
530 /*
531 * Simulate the opening of a directory
532 */
533 RST_DIR *
rst_opendir(const char * name)534 rst_opendir(const char *name)
535 {
536 struct inotab *itp;
537 RST_DIR *rdirp;
538 ino_t ino;
539
540 if ((ino = dirlookup(name)) > 0 &&
541 (itp = inotablookup(ino)) != NULL) {
542 rdirp = opendirfile(dirfile);
543 rst_seekdir(rdirp, itp->t_seekpt, itp->t_seekpt);
544 return (rdirp);
545 }
546 return (NULL);
547 }
548
549 /*
550 * In our case, there is nothing to do when closing a directory.
551 */
552 void
rst_closedir(RST_DIR * rdirp)553 rst_closedir(RST_DIR *rdirp)
554 {
555
556 (void)close(rdirp->dd_fd);
557 free(rdirp);
558 return;
559 }
560
561 /*
562 * Simulate finding the current offset in the directory.
563 */
564 static long
rst_telldir(RST_DIR * rdirp)565 rst_telldir(RST_DIR *rdirp)
566 {
567 return ((long)lseek(rdirp->dd_fd,
568 (off_t)0, SEEK_CUR) - rdirp->dd_size + rdirp->dd_loc);
569 }
570
571 /*
572 * Open a directory file.
573 */
574 static RST_DIR *
opendirfile(const char * name)575 opendirfile(const char *name)
576 {
577 RST_DIR *rdirp;
578 int fd;
579
580 if ((fd = open(name, O_RDONLY)) == -1)
581 return (NULL);
582 if ((rdirp = malloc(sizeof(RST_DIR))) == NULL) {
583 (void)close(fd);
584 return (NULL);
585 }
586 rdirp->dd_fd = fd;
587 rdirp->dd_loc = 0;
588 return (rdirp);
589 }
590
591 /*
592 * Set the mode, owner, and times for all new or changed directories
593 */
594 void
setdirmodes(int flags)595 setdirmodes(int flags)
596 {
597 struct modeinfo node;
598 struct entry *ep;
599 char *cp, *buf;
600 int bufsize;
601 uid_t myuid;
602
603 vprintf(stdout, "Set directory mode, owner, and times.\n");
604 if (command == 'r' || command == 'R')
605 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd",
606 tmpdir, (intmax_t)dumpdate);
607 if (modefile[0] == '#') {
608 panic("modefile not defined\n");
609 fprintf(stderr, "directory mode, owner, and times not set\n");
610 return;
611 }
612 mf = fopen(modefile, "r");
613 if (mf == NULL) {
614 fprintf(stderr, "fopen: %s\n", strerror(errno));
615 fprintf(stderr, "cannot open mode file %s\n", modefile);
616 fprintf(stderr, "directory mode, owner, and times not set\n");
617 return;
618 }
619 clearerr(mf);
620 bufsize = 0;
621 buf = NULL;
622 myuid = getuid();
623 for (;;) {
624 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf);
625 if (ferror(mf)) {
626 warn("%s: cannot read modefile.", modefile);
627 fprintf(stderr, "Mode, owner, and times not set.\n");
628 break;
629 }
630 if (feof(mf))
631 break;
632 if (node.extsize > 0) {
633 if (bufsize < node.extsize) {
634 if (bufsize > 0)
635 free(buf);
636 if ((buf = malloc(node.extsize)) != NULL) {
637 bufsize = node.extsize;
638 } else {
639 bufsize = 0;
640 }
641 }
642 if (bufsize >= node.extsize) {
643 (void) fread(buf, 1, node.extsize, mf);
644 if (ferror(mf)) {
645 warn("%s: cannot read modefile.",
646 modefile);
647 fprintf(stderr, "Not all external ");
648 fprintf(stderr, "attributes set.\n");
649 break;
650 }
651 } else {
652 (void) fseek(mf, node.extsize, SEEK_CUR);
653 if (ferror(mf)) {
654 warn("%s: cannot seek in modefile.",
655 modefile);
656 fprintf(stderr, "Not all directory ");
657 fprintf(stderr, "attributes set.\n");
658 break;
659 }
660 }
661 }
662 ep = lookupino(node.ino);
663 if (command == 'i' || command == 'x') {
664 if (ep == NULL)
665 continue;
666 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
667 ep->e_flags &= ~NEW;
668 continue;
669 }
670 if (node.ino == UFS_ROOTINO && dotflag == 0)
671 continue;
672 }
673 if (ep == NULL) {
674 panic("cannot find directory inode %ju\n",
675 (uintmax_t)node.ino);
676 continue;
677 }
678 cp = myname(ep);
679 if (!Nflag) {
680 if (myuid != 0)
681 (void) chown(cp, myuid, node.gid);
682 else
683 (void) chown(cp, node.uid, node.gid);
684 (void) chmod(cp, node.mode);
685 if (node.extsize > 0) {
686 if (bufsize >= node.extsize) {
687 set_extattr(-1, cp, buf, node.extsize, SXA_FILE);
688 } else {
689 fprintf(stderr, "Cannot restore %s%s\n",
690 "extended attributes for ", cp);
691 }
692 }
693 (void) utimens(cp, node.ctimep);
694 (void) utimens(cp, node.mtimep);
695 if (Mtreefile) {
696 writemtree(cp, "dir",
697 node.uid, node.gid, node.mode,
698 node.flags);
699 } else
700 (void) chflags(cp, node.flags);
701 }
702 ep->e_flags &= ~NEW;
703 }
704 if (bufsize > 0)
705 free(buf);
706 (void) fclose(mf);
707 }
708
709 /*
710 * Generate a literal copy of a directory.
711 */
712 int
genliteraldir(const char * name,ino_t ino)713 genliteraldir(const char *name, ino_t ino)
714 {
715 struct inotab *itp;
716 int ofile, dp, i, size;
717 char buf[BUFSIZ];
718
719 itp = inotablookup(ino);
720 if (itp == NULL)
721 panic("Cannot find directory inode %ju named %s\n",
722 (uintmax_t)ino, name);
723 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
724 fprintf(stderr, "%s: ", name);
725 (void) fflush(stderr);
726 fprintf(stderr, "cannot create file: %s\n", strerror(errno));
727 return (FAIL);
728 }
729 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
730 dp = dup(dirp->dd_fd);
731 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
732 size = MIN(i, BUFSIZ);
733 if (read(dp, buf, (int) size) == -1) {
734 fprintf(stderr,
735 "write error extracting inode %ju, name %s\n",
736 (uintmax_t)curfile.ino, curfile.name);
737 fprintf(stderr, "read: %s\n", strerror(errno));
738 exit(1);
739 }
740 if (!Nflag && write(ofile, buf, (int) size) == -1) {
741 fprintf(stderr,
742 "write error extracting inode %ju, name %s\n",
743 (uintmax_t)curfile.ino, curfile.name);
744 fprintf(stderr, "write: %s\n", strerror(errno));
745 exit(1);
746 }
747 }
748 (void) close(dp);
749 (void) close(ofile);
750 return (GOOD);
751 }
752
753 /*
754 * Determine the type of an inode
755 */
756 int
inodetype(ino_t ino)757 inodetype(ino_t ino)
758 {
759 struct inotab *itp;
760
761 itp = inotablookup(ino);
762 if (itp == NULL)
763 return (LEAF);
764 return (NODE);
765 }
766
767 /*
768 * Allocate and initialize a directory inode entry.
769 * If requested, save its pertinent mode, owner, and time info.
770 */
771 static struct inotab *
allocinotab(struct context * ctxp,long aseekpt)772 allocinotab(struct context *ctxp, long aseekpt)
773 {
774 struct inotab *itp;
775 struct modeinfo node;
776
777 itp = calloc(1, sizeof(struct inotab));
778 if (itp == NULL)
779 panic("no memory for directory table\n");
780 itp->t_next = inotab[INOHASH(ctxp->ino)];
781 inotab[INOHASH(ctxp->ino)] = itp;
782 itp->t_ino = ctxp->ino;
783 itp->t_seekpt = aseekpt;
784 if (mf == NULL)
785 return (itp);
786 node.ino = ctxp->ino;
787 node.mtimep[0].tv_sec = ctxp->atime_sec;
788 node.mtimep[0].tv_nsec = ctxp->atime_nsec;
789 node.mtimep[1].tv_sec = ctxp->mtime_sec;
790 node.mtimep[1].tv_nsec = ctxp->mtime_nsec;
791 node.ctimep[0].tv_sec = ctxp->atime_sec;
792 node.ctimep[0].tv_nsec = ctxp->atime_nsec;
793 node.ctimep[1].tv_sec = ctxp->birthtime_sec;
794 node.ctimep[1].tv_nsec = ctxp->birthtime_nsec;
795 node.extsize = ctxp->extsize;
796 node.mode = ctxp->mode;
797 node.flags = ctxp->file_flags;
798 node.uid = ctxp->uid;
799 node.gid = ctxp->gid;
800 if (fwrite((char *)&node, sizeof(struct modeinfo), 1, mf) != 1)
801 fail_dirtmp(modefile);
802 return (itp);
803 }
804
805 /*
806 * Look up an inode in the table of directories
807 */
808 static struct inotab *
inotablookup(ino_t ino)809 inotablookup(ino_t ino)
810 {
811 struct inotab *itp;
812
813 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
814 if (itp->t_ino == ino)
815 return (itp);
816 return (NULL);
817 }
818
819 /*
820 * Clean up and exit
821 */
822 void
cleanup(void)823 cleanup(void)
824 {
825
826 closemt();
827 if (modefile[0] != '#') {
828 (void) truncate(modefile, 0);
829 (void) unlink(modefile);
830 }
831 if (dirfile[0] != '#') {
832 (void) truncate(dirfile, 0);
833 (void) unlink(dirfile);
834 }
835 }
836
837 /*
838 * Print out information about the failure to save directory,
839 * extended attribute, and mode information.
840 */
841 static void __dead
fail_dirtmp(char * filename)842 fail_dirtmp(char *filename)
843 {
844 warn("%s: cannot write directory database", filename);
845 if (errno == ENOSPC) {
846 fprintf(stderr, "Try making space in %s, %s\n%s\n", tmpdir,
847 "or set environment variable TMPDIR",
848 "to an alternate location with more disk space.");
849 }
850 exit(1);
851 }
852