Debian Codegraph @

restore/dirs.c

raw symbols SCIP
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. 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 #include "config.h" // IWYU pragma: keep
44 #include "dirs.h"
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #include <dirent.h>
50 #include <stdint.h>
51 #include <sys/time.h>
52
53
54 #include "compat/include/bsdcompat.h"
55 #include "compat/include/protocols/dumprestore.h"
56
57 #include "compat/include/compaterr.h" // IWYU pragma: keep
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <fcntl.h>
63
64 #include <endian.h>
65
66 #include "restore.h"
67 #include "readtape.h"
68 #include "xattr.h"
69 #include "utilities.h"
70 #include "tape.h"
71 #include "symtab.h"
72 #include "restore_globals.h"
73
74 /*
75 * Symbol table of directories read from tape.
76 */
77 #define HASHSIZE 1000
78 #define INOHASH(val) (val % HASHSIZE)
79 struct inotab {
80 struct inotab *t_next;
81 dump_ino_t t_ino;
82 off_t t_seekpt;
83 off_t t_size;
84 };
85 static struct inotab *inotab[HASHSIZE];
86
87 /*
88 * Information retained about directories.
89 */
90 struct modeinfo {
91 dump_ino_t ino;
92 struct timeval timep[2];
93 mode_t mode;
94 uid_t uid;
95 gid_t gid;
96 unsigned int flags;
97 char xattr;
98 };
99
100 /*
101 * Definitions for library routines operating on directories.
102 */
103 #undef DIRBLKSIZ
104 #define DIRBLKSIZ 1024
105 struct rstdirdesc {
106 int dd_fd;
107 int32_t dd_loc;
108 int32_t dd_size;
109 char dd_buf[DIRBLKSIZ];
110 };
111
112 /*
113 * Global variables for this file.
114 */
115 static off_t seekpt;
116 static FILE *df, *mf;
117 static RST_DIR *dirp;
118 static char dirfile[MAXPATHLEN] = "#"; /* No file */
119 static char modefile[MAXPATHLEN] = "#"; /* No file */
120 static char dot[2] = "."; /* So it can be modified */
121
122 /*
123 * Format of old style directories.
124 */
125 #define ODIRSIZ 14
126 struct odirect {
127 u_short d_ino;
128 char d_name[ODIRSIZ];
129 };
130
131 static struct inotab *allocinotab (dump_ino_t, off_t);
132 static void savemodeinfo (dump_ino_t, struct new_bsd_inode *, char *);
133 static void dcvt (const struct odirect *, struct direct *);
134 static void flushent (void);
135 static struct inotab *inotablookup (dump_ino_t);
136 static RST_DIR *opendirfile (const char *);
137 static void putdir (const char *, off_t, size_t);
138 static void putent (struct direct *);
139 static void rst_seekdir (RST_DIR *, off_t, off_t);
140 static off_t rst_telldir (RST_DIR *);
141 static struct direct *searchdir (dump_ino_t, char *);
142
143 /*
144 * Extract directory contents, building up a directory structure
145 * on disk for extraction by name.
146 * If genmode is requested, save mode, owner, and times for all
147 * directories on the tape.
148 */
149 void
150 extractdirs(int genmode)
151 {
152 int i;
153 struct new_bsd_inode ip;
154 struct inotab *itp;
155 struct direct nulldir = {};
156 int fd;
157 char xattr[XATTR_MAXSIZE*2] = {0};
158 int xattr_found = 0;
159 dump_ino_t ino;
160
161 Vmsg("Extract directories from tape\n");
162 (void) snprintf(dirfile, sizeof(dirfile), "%s/rstdir%jd", tmpdir,
163 (intmax_t)dumpdate);
164 if (command != 'r' && command != 'R') {
165 (void) strncat(dirfile, "-XXXXXX",
166 sizeof(dirfile) - strlen(dirfile) - 1);
167 fd = mkstemp(dirfile);
168 } else
169 fd = open(dirfile, O_RDWR|O_CREAT|O_EXCL, 0666);
170 if (fd == -1 || (df = fdopen(fd, "w")) == NULL) {
171 if (fd != -1)
172 close(fd);
173 err(1, "cannot create directory temporary %s", dirfile);
174 }
175 if (genmode != 0) {
176 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd", tmpdir, (intmax_t)dumpdate);
177 if (command != 'r' && command != 'R') {
178 (void) strncat(modefile, "-XXXXXX",
179 sizeof(modefile) - strlen(modefile) - 1);
180 fd = mkstemp(modefile);
181 } else
182 fd = open(modefile, O_RDWR|O_CREAT|O_EXCL, 0666);
183 if (fd == -1 || (mf = fdopen(fd, "w")) == NULL) {
184 if (fd != -1)
185 close(fd);
186 err(1, "cannot create modefile %s", modefile);
187 }
188 }
189 nulldir.d_ino = 0;
190 nulldir.d_type = DT_DIR;
191 nulldir.d_namlen = 1;
192 nulldir.d_name[0] = '/';
193 nulldir.d_name[1] = '\0';
194 nulldir.d_reclen = DIRSIZ(0, &nulldir);
195 for (;;) {
196 curfile.name = "<directory file - name unknown>";
197 curfile.action = USING;
198 ino = curfile.ino;
199 if (curfile.dip == NULL || (curfile.dip->di_mode & IFMT) != IFDIR) {
200 if ( fclose(df) == EOF )
201 err(1, "cannot write to file %s", dirfile);
202 dirp = opendirfile(dirfile);
203 if (dirp == NULL)
204 warn("opendirfile");
205 if (mf != NULL && fclose(mf) == EOF )
206 err(1, "cannot write to file %s", dirfile);
207 i = dirlookup(dot);
208 if (i == 0)
209 panic("Root directory is not on tape\n");
210 return;
211 }
212 memcpy(&ip, curfile.dip, sizeof(ip));
213 itp = allocinotab(ino, seekpt);
214 getfile(putdir, xtrnull);
215 xattr_found = 0;
216 while (spcl.c_flags & DR_EXTATTRIBUTES) {
217 switch (spcl.c_extattributes) {
218 case EXT_MACOSFNDRINFO:
219 msg("MacOSX attributes not supported, skipping\n");
220 skipfile();
221 break;
222 case EXT_MACOSRESFORK:
223 msg("MacOSX attributes not supported, skipping\n");
224 skipfile();
225 break;
226 case EXT_XATTR:
227 if (readxattr(xattr+xattr_found*XATTR_MAXSIZE) == GOOD)
228 xattr_found = 1;
229 break;
230 }
231 }
232 if (xattr_found)
233 savemodeinfo(ino, &ip, xattr);
234 else
235 savemodeinfo(ino, &ip, NULL);
236 putent(&nulldir);
237 flushent();
238 itp->t_size = seekpt - itp->t_seekpt;
239 }
240 }
241
242 /*
243 * skip over all the directories on the tape
244 */
245 void
246 skipdirs(void)
247 {
248
249 while (curfile.dip && (curfile.dip->di_mode & IFMT) == IFDIR) {
250 skipfile();
251 }
252 }
253
254 /*
255 * Recursively find names and inumbers of all files in subtree
256 * pname and pass them off to be processed.
257 */
258 void
259 treescan(const char *pname, dump_ino_t ino, long (*todo) (const char *, dump_ino_t, int))
260 {
261 struct inotab *itp;
262 struct direct *dp;
263 off_t bpt;
264
265 itp = inotablookup(ino);
266 if (itp == NULL) {
267 /*
268 * Pname is name of a simple file or an unchanged directory.
269 */
270 (void) (*todo)(pname, ino, LEAF);
271 return;
272 }
273 /*
274 * Pname is a dumped directory name.
275 */
276 if ((*todo)(pname, ino, NODE) == FAIL)
277 return;
278 /*
279 * begin search through the directory
280 * skipping over "." and ".."
281 */
282 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
283 dp = rst_readdir(dirp); /* "." */
284 if (dp != NULL && strcmp(dp->d_name, ".") == 0)
285 dp = rst_readdir(dirp); /* ".." */
286 else
287 fprintf(stderr, "Warning: `.' missing from directory %s\n",
288 pname);
289 if (dp != NULL && strcmp(dp->d_name, "..") == 0)
290 dp = rst_readdir(dirp); /* first real entry */
291 else
292 fprintf(stderr, "Warning: `..' missing from directory %s\n",
293 pname);
294 bpt = rst_telldir(dirp);
295 /*
296 * a zero inode signals end of directory
297 */
298 while (dp != NULL) {
299 char locname[MAXPATHLEN];
300 size_t namelen = snprintf(locname, sizeof(locname), "%s/%.*s", pname, dp->d_namlen, dp->d_name);
301 if (namelen >= sizeof(locname)) {
302 fprintf(stderr, "%s/%.*s: name exceeds %d char\n",
303 locname, dp->d_namlen, dp->d_name, MAXPATHLEN);
304 } else {
305 treescan(locname, dp->d_ino, todo);
306 rst_seekdir(dirp, bpt, itp->t_seekpt);
307 }
308 dp = rst_readdir(dirp);
309 bpt = rst_telldir(dirp);
310 }
311 }
312
313 /*
314 * Lookup a pathname which is always assumed to start from the ROOTINO.
315 */
316 struct direct *
317 pathsearch(const char *pathname)
318 {
319 dump_ino_t ino;
320 struct direct *dp;
321 char *path, *name, buffer[MAXPATHLEN];
322
323 strcpy(buffer, pathname);
324 path = buffer;
325 ino = ROOTINO;
326 while (*path == '/')
327 path++;
328 dp = NULL;
329 while ((name = strsep(&path, "/")) != NULL && *name /* != NULL */) {
330 if ((dp = searchdir(ino, name)) == NULL)
331 return (NULL);
332 ino = dp->d_ino;
333 }
334 return (dp);
335 }
336
337 /*
338 * Lookup the requested name in directory inum.
339 * Return its inode number if found, zero if it does not exist.
340 */
341 static struct direct *
342 searchdir(dump_ino_t inum, char *name)
343 {
344 struct direct *dp;
345 struct inotab *itp;
346 int len;
347
348 itp = inotablookup(inum);
349 if (itp == NULL)
350 return (NULL);
351 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
352 len = strlen(name);
353 do {
354 dp = rst_readdir(dirp);
355 if (dp == NULL)
356 return (NULL);
357 } while (dp->d_namlen != len || strncmp(dp->d_name, name, len) != 0);
358 return (dp);
359 }
360
361 /*
362 * Put the directory entries in the directory file
363 */
364 static void
365 putdir(const char *buf, UNUSED(off_t pos), size_t size)
366 {
367 struct direct cvtbuf;
368 const struct odirect *odp;
369 const struct odirect *eodp;
370 long loc, i;
371
372 if (cvtflag && !ufs2flag) {
373 eodp = (const struct odirect *)&buf[size];
374 for (odp = (const struct odirect *)buf; odp < eodp; odp++)
375 if (odp->d_ino != 0) {
376 dcvt(odp, &cvtbuf);
377 putent(&cvtbuf);
378 }
379 } else {
380 for (loc = 0; loc < (ssize_t)size; ) {
381 memcpy(&cvtbuf, (const struct direct *)(buf + loc), sizeof cvtbuf);
382 Dmsg (DIRDEBUG, "reclen = %d, namlen = %d, type = %d\n",
383 cvtbuf.d_reclen, cvtbuf.d_namlen, cvtbuf.d_type);
384 if (Bcvt)
385 swabst("is", (u_char *) &cvtbuf);
386 if (oldinofmt && cvtbuf.d_ino != 0) {
387 # if BYTE_ORDER == BIG_ENDIAN
388 if (Bcvt)
389 cvtbuf.d_namlen = cvtbuf.d_type;
390 # else
391 if (!Bcvt)
392 cvtbuf.d_namlen = cvtbuf.d_type;
393 # endif
394 if (cvtbuf.d_namlen == 0 && cvtbuf.d_type != 0)
395 cvtbuf.d_namlen = cvtbuf.d_type;
396 cvtbuf.d_type = DT_UNKNOWN;
397 }
398 Dmsg (DIRDEBUG, "reclen = %d, namlen = %d, type = %d\n",
399 cvtbuf.d_reclen, cvtbuf.d_namlen, cvtbuf.d_type);
400 i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
401 if ((cvtbuf.d_reclen & 0x3) != 0 ||
402 cvtbuf.d_reclen > i ||
403 cvtbuf.d_reclen < DIRSIZ(0, &cvtbuf)
404 #if MAXNAMLEN < 255
405 || cvtbuf.d_namlen > MAXNAMLEN
406 #endif
407 ) {
408 Vmsg("Mangled directory: ");
409 if ((cvtbuf.d_reclen & 0x3) != 0)
410 Vmsg(
411 "reclen not multiple of 4 ");
412 if (cvtbuf.d_reclen < DIRSIZ(0, &cvtbuf))
413 Vmsg(
414 "reclen less than DIRSIZ (%d < %d) ",
415 cvtbuf.d_reclen, DIRSIZ(0, &cvtbuf));
416 #if MAXNAMLEN < 255
417 if (cvtbuf.d_namlen > MAXNAMLEN)
418 Vmsg(
419 "reclen name too big (%d > %d) ",
420 cvtbuf.d_namlen, MAXNAMLEN);
421 #endif
422 Vmsg("\n");
423 loc += i;
424 continue;
425 }
426 loc += cvtbuf.d_reclen;
427 if (cvtbuf.d_ino != 0) {
428 putent(&cvtbuf);
429 }
430 }
431 }
432 }
433
434 /*
435 * These variables are "local" to the following two functions.
436 */
437 static char dirbuf[DIRBLKSIZ];
438 static long dirloc = 0;
439 static long prev = 0;
440
441 /*
442 * add a new directory entry to a file.
443 */
444 static void
445 putent(struct direct *dp)
446 {
447 dp->d_reclen = DIRSIZ(0, dp);
448 if (dirloc + dp->d_reclen > DIRBLKSIZ) {
449 ((struct direct *)(dirbuf + prev))->d_reclen =
450 DIRBLKSIZ - prev;
451 if ( fwrite(dirbuf, DIRBLKSIZ, 1, df) != 1 )
452 err(1,"cannot write to file %s", dirfile);
453 dirloc = 0;
454 }
455 memmove(dirbuf + dirloc, dp, (size_t)dp->d_reclen);
456 prev = dirloc;
457 dirloc += dp->d_reclen;
458 }
459
460 /*
461 * flush out a directory that is finished.
462 */
463 static void
464 flushent(void)
465 {
466 ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
467 if ( fwrite(dirbuf, (int)dirloc, 1, df) != 1 )
468 err(1, "cannot write to file %s", dirfile);
469 seekpt = ftell(df);
470 if (seekpt == -1)
471 err(1, "cannot write to file %s", dirfile);
472 dirloc = 0;
473 }
474
475 static void
476 dcvt(const struct odirect *odp, struct direct *ndp)
477 {
478
479 memset(ndp, 0, (size_t)(sizeof *ndp));
480 ndp->d_ino = odp->d_ino;
481 ndp->d_type = DT_UNKNOWN;
482 (void) strncpy(ndp->d_name, odp->d_name, ODIRSIZ);
483 ndp->d_namlen = strlen(ndp->d_name);
484 ndp->d_reclen = DIRSIZ(0, ndp);
485 }
486
487 /*
488 * Seek to an entry in a directory.
489 * Only values returned by rst_telldir should be passed to rst_seekdir.
490 * This routine handles many directories in a single file.
491 * It takes the base of the directory in the file, plus
492 * the desired seek offset into it.
493 */
494 static void
495 rst_seekdir(RST_DIR *dirp, off_t loc, off_t base)
496 {
497
498 if (loc == rst_telldir(dirp))
499 return;
500 loc -= base;
501 if (loc < 0)
502 fprintf(stderr, "bad seek pointer to rst_seekdir %jd\n", (intmax_t)loc);
503 (void) lseek(dirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), SEEK_SET);
504 dirp->dd_loc = loc & (DIRBLKSIZ - 1);
505 if (dirp->dd_loc != 0)
506 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf, DIRBLKSIZ);
507 }
508
509 /*
510 * get next entry in a directory.
511 */
512 struct direct *
513 rst_readdir(RST_DIR *dirp)
514 {
515 struct direct *dp;
516
517 for (;;) {
518 if (dirp->dd_loc == 0) {
519 dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
520 DIRBLKSIZ);
521 if (dirp->dd_size <= 0) {
522 Dmsg(DEFAULT_DEBUG, "error reading directory\n");
523 return (NULL);
524 }
525 }
526 if (dirp->dd_loc >= dirp->dd_size) {
527 dirp->dd_loc = 0;
528 continue;
529 }
530 dp = (struct direct *)(dirp->dd_buf + dirp->dd_loc);
531 if (dp->d_reclen == 0 ||
532 dp->d_reclen > DIRBLKSIZ + 1 - dirp->dd_loc) {
533 Dmsg(DEFAULT_DEBUG, "corrupted directory: bad reclen %d\n",
534 dp->d_reclen);
535 return (NULL);
536 }
537 dirp->dd_loc += dp->d_reclen;
538 if (dp->d_ino == 0 && strcmp(dp->d_name, "/") == 0)
539 return (NULL);
540 if (dp->d_ino >= maxino) {
541 Dmsg(DEFAULT_DEBUG, "corrupted directory: bad inum %d\n",
542 dp->d_ino);
543 continue;
544 }
545 return (dp);
546 }
547 }
548
549 /*
550 * Simulate the opening of a directory
551 */
552 void *
553 rst_opendir(const char *name)
554 {
555 struct inotab *itp;
556 RST_DIR *dirp;
557 dump_ino_t ino;
558
559 if ((ino = dirlookup(name)) > 0 &&
560 (itp = inotablookup(ino)) != NULL) {
561 dirp = opendirfile(dirfile);
562 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
563 return (dirp);
564 }
565 return (NULL);
566 }
567
568 /*
569 * In our case, there is nothing to do when closing a directory.
570 */
571 void
572 rst_closedir(void *argp)
573 {
574 RST_DIR *dirp = argp;
575
576 (void)close(dirp->dd_fd);
577 free(dirp);
578 return;
579 }
580
581 /*
582 * Simulate finding the current offset in the directory.
583 */
584 static off_t
585 rst_telldir(RST_DIR *dirp)
586 {
587 return ((off_t)lseek(dirp->dd_fd,
588 (off_t)0, SEEK_CUR) - dirp->dd_size + dirp->dd_loc);
589 }
590
591 /*
592 * Open a directory file.
593 */
594 static RST_DIR *
595 opendirfile(const char *name)
596 {
597 RST_DIR *dirp;
598 int fd;
599
600 if ((fd = open(name, O_RDONLY)) == -1)
601 return (NULL);
602 if ((dirp = malloc(sizeof(RST_DIR))) == NULL) {
603 (void)close(fd);
604 return (NULL);
605 }
606 memset(dirp, 0, sizeof *dirp);
607 dirp->dd_fd = fd;
608 dirp->dd_loc = 0;
609 return (dirp);
610 }
611
612 /*
613 * Set the mode, owner, and times for all new or changed directories
614 */
615 void
616 setdirmodes(int flags)
617 {
618 if (Nflag)
619 return;
620 FILE *mf;
621 struct modeinfo node;
622 struct entry *ep;
623 char *cp;
624
625 Vmsg("Set directory mode, owner, and times.\n");
626 if (command == 'r' || command == 'R')
627 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%ju", tmpdir, (intmax_t)dumpdate);
628 if (modefile[0] == '#') {
629 panic("modefile not defined\n");
630 fprintf(stderr, "directory mode, owner, and times not set\n");
631 return;
632 }
633 mf = fopen(modefile, "r");
634 if (mf == NULL) {
635 warn("fopen");
636 fprintf(stderr, "cannot open mode file %s\n", modefile);
637 fprintf(stderr, "directory mode, owner, and times not set\n");
638 return;
639 }
640 clearerr(mf);
641 for (;;) {
642 char xattr[XATTR_MAXSIZE*2];
643 if (fread((char *)&node, sizeof(struct modeinfo), 1, mf) != 1) {
644 if (feof(mf))
645 break;
646 err(1, "fread");
647 }
648 if (node.xattr) {
649 if (fread(xattr, XATTR_MAXSIZE*2, 1, mf) != 1) {
650 if (feof(mf))
651 break;
652 err(1, "fread");
653 }
654 }
655 ep = lookupino(node.ino);
656 if (command == 'i' || command == 'x') {
657 if (ep == NULL)
658 continue;
659 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) {
660 ep->e_flags &= ~NEW;
661 continue;
662 }
663 if(node.ino == ROOTINO) {
664 if(flags & FORCENO)
665 continue;
666 if ((flags & FORCE) == 0 &&
667 reply("set owner/mode/attributes for '.'", 0) == FAIL)
668 continue;
669 }
670 }
671 if (ep == NULL) {
672 panic("cannot find directory inode %d\n", node.ino);
673 } else {
674 cp = myname(ep);
675 if (chown(cp, node.uid, node.gid) < 0)
676 warn("chown");
677 (void) chmod(cp, node.mode);
678 utimes(cp, node.timep);
679 if (node.xattr) {
680 xattr_extract(cp, xattr);
681 char* xattr2 = xattr + XATTR_MAXSIZE;
682 if (*(uint32_t*)xattr2 != 0) {
683 xattr_extract(cp, xattr2);
684 }
685 }
686 ep->e_flags &= ~NEW;
687 if (node.flags)
688 (void) lsetflags(cp, node.flags);
689 }
690 }
691 if (ferror(mf))
692 panic("error setting directory modes\n");
693 (void) fclose(mf);
694 }
695
696 /*
697 * In restore -C mode, tests the attributes for all directories
698 */
699 void
700 comparedirmodes(void)
701 {
702 FILE *mf;
703 struct modeinfo node;
704 struct entry *ep;
705 char *cp;
706
707 Vmsg("Compare directories modes, owner, attributes.\n");
708 if (modefile[0] == '#') {
709 panic("modefile not defined\n");
710 fprintf(stderr, "directory mode, owner, and times not set\n");
711 return;
712 }
713 mf = fopen(modefile, "r");
714 if (mf == NULL) {
715 warn("fopen");
716 fprintf(stderr, "cannot open mode file %s\n", modefile);
717 fprintf(stderr, "directory mode, owner, and times not set\n");
718 return;
719 }
720 clearerr(mf);
721 for (;;) {
722 char xattr[XATTR_MAXSIZE*2];
723 if (fread((char *)&node, sizeof(struct modeinfo), 1, mf) != 1) {
724 if (feof(mf))
725 break;
726 err(1, "fread");
727 }
728 if (node.xattr) {
729 if (fread(xattr, XATTR_MAXSIZE*2, 1, mf) != 1) {
730 if (feof(mf))
731 break;
732 err(1, "fread");
733 }
734 }
735 ep = lookupino(node.ino);
736 if (ep == NULL) {
737 panic("cannot find directory inode %d\n", node.ino);
738 } else {
739 struct stat sb;
740 unsigned long newflags;
741
742 cp = myname(ep);
743 if (lstat(cp, &sb) < 0) {
744 warn("unable to stat %s", cp);
745 do_compare_error;
746 continue;
747 }
748
749 Vmsg("comparing directory %s\n", cp);
750
751 if (sb.st_mode != node.mode) {
752 fprintf(stderr, "%s: mode changed from 0%o to 0%o.\n",
753 cp, node.mode & 07777, sb.st_mode & 07777);
754 do_compare_error;
755 }
756 if (sb.st_uid != node.uid) {
757 fprintf(stderr, "%s: uid changed from %d to %d.\n",
758 cp, node.uid, sb.st_uid);
759 do_compare_error;
760 }
761 if (sb.st_gid != node.gid) {
762 fprintf(stderr, "%s: gid changed from %d to %d.\n",
763 cp, node.gid, sb.st_gid);
764 do_compare_error;
765 }
766 if (lgetflags(cp, &newflags, 0) < 0) {
767 if (node.flags != 0) {
768 warn("%s: lgetflags failed", cp);
769 do_compare_error;
770 }
771 }
772 else {
773 if (newflags != node.flags) {
774 fprintf(stderr, "%s: flags changed from 0x%08x to 0x%08lx.\n",
775 cp, node.flags, newflags);
776 do_compare_error;
777 }
778 }
779 int xattr_count_all = 0;
780 int attr_compare_err = 0;
781 if (node.xattr) {
782 int xattr_count_thisblock;
783 if (xattr_compare(cp, xattr) == FAIL)
784 attr_compare_err=1;
785 xattr_count(xattr, &xattr_count_thisblock);
786 xattr_count_all += xattr_count_thisblock;
787 char* xattr2 = xattr + XATTR_MAXSIZE;
788 if (*(uint32_t*)xattr2 != 0) {
789 if (xattr_compare(cp, xattr2) == FAIL)
790 attr_compare_err=1;
791 xattr_count(xattr2, &xattr_count_thisblock);
792 xattr_count_all += xattr_count_thisblock;
793 }
794 }
795 int count_file = xattr_count_file(cp);
796 if (count_file < 0) {
797 attr_compare_err=1;
798 } else if (count_file != xattr_count_all) {
799 fprintf(stderr, "%s: directory does not have the same number of attributes (%d) as the tape (%d).\n",
800 cp, count_file, xattr_count_all );
801 attr_compare_err=1;
802 }
803 if (attr_compare_err)
804 do_compare_error;
805 ep->e_flags &= ~NEW;
806 }
807 }
808 if (ferror(mf))
809 panic("error setting directory modes\n");
810 (void) fclose(mf);
811 }
812
813 /*
814 * Generate a literal copy of a directory.
815 */
816 int
817 genliteraldir(const char *name, dump_ino_t ino)
818 {
819 struct inotab *itp;
820 int ofile, dp, i, size;
821 char buf[BUFSIZ];
822
823 itp = inotablookup(ino);
824 if (itp == NULL)
825 panic("Cannot find directory inode %d named %s\n", ino, name);
826 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
827 warn("%s: cannot create file\n", name);
828 return (FAIL);
829 }
830 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
831 dp = dup(dirp->dd_fd);
832 for (i = itp->t_size; i > 0; i -= BUFSIZ) {
833 size = i < BUFSIZ ? i : BUFSIZ;
834 if (read(dp, buf, (int) size) == -1) {
835 warnx("write error extracting inode %u, name %s\n",
836 curfile.ino, curfile.name);
837 err(1, "read");
838 }
839 if (!Nflag && write(ofile, buf, (int) size) == -1) {
840 warnx("write error extracting inode %u, name %s\n",
841 curfile.ino, curfile.name);
842 err(1, "write");
843 }
844 }
845 (void) close(dp);
846 (void) close(ofile);
847 return (GOOD);
848 }
849
850 /*
851 * Determine the type of an inode
852 */
853 int
854 inodetype(dump_ino_t ino)
855 {
856 struct inotab *itp;
857
858 itp = inotablookup(ino);
859 if (itp == NULL)
860 return (LEAF);
861 return (NODE);
862 }
863
864 /*
865 * Allocate and initialize a directory inode entry.
866 * If requested, save its pertinent mode, owner, and time info.
867 */
868 static struct inotab *
869 allocinotab(dump_ino_t ino, off_t seekpt)
870 {
871 struct inotab *itp;
872
873 itp = calloc(1, sizeof(struct inotab));
874 if (itp == NULL)
875 panic("no memory directory table\n");
876 itp->t_next = inotab[INOHASH(ino)];
877 inotab[INOHASH(ino)] = itp;
878 itp->t_ino = ino;
879 itp->t_seekpt = seekpt;
880 return itp;
881 }
882
883 static void
884 savemodeinfo(dump_ino_t ino, struct new_bsd_inode *dip, char *xattr) {
885 struct modeinfo node = {};
886
887 if (mf == NULL)
888 return;
889 node.ino = ino;
890 node.timep[0].tv_sec = dip->di_atime.tv_sec;
891 node.timep[0].tv_usec = dip->di_atime.tv_usec;
892 node.timep[1].tv_sec = dip->di_mtime.tv_sec;
893 node.timep[1].tv_usec = dip->di_mtime.tv_usec;
894 node.mode = dip->di_mode;
895 node.flags = dip->di_flags;
896 node.uid = dip->di_uid;
897 node.gid = dip->di_gid;
898 node.xattr = xattr ? 1 : 0;
899 if ( fwrite((char *)&node, sizeof(struct modeinfo), 1, mf) != 1 )
900 err(1,"cannot write to file %s", modefile);
901 if (xattr)
902 if ( fwrite(xattr, XATTR_MAXSIZE*2, 1, mf) != 1 )
903 err(1,"cannot write to file %s", modefile);
904 }
905
906 /*
907 * Look up an inode in the table of directories
908 */
909 static struct inotab *
910 inotablookup(dump_ino_t ino)
911 {
912 struct inotab *itp;
913
914 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
915 if (itp->t_ino == ino)
916 return (itp);
917 return (NULL);
918 }
919
920 /*
921 * Clean up and exit
922 */
923 void
924 cleanup(void)
925 {
926 close_tape();
927 if (modefile[0] != '#')
928 (void) unlink(modefile);
929 if (dirfile[0] != '#')
930 (void) unlink(dirfile);
931 }