1 /* $NetBSD: dirs.c,v 1.53 2021/06/23 14:22:08 riastradh 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.53 2021/06/23 14:22:08 riastradh 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 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 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 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 * 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 * 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 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 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 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 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 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 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 * 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 * 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 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 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 * 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 595 setdirmodes(int flags) 596 { 597 struct modeinfo node; 598 struct entry *ep; 599 char *cp, *buf; 600 int bufsize; 601 602 vprintf(stdout, "Set directory mode, owner, and times.\n"); 603 if (command == 'r' || command == 'R') 604 (void) snprintf(modefile, sizeof(modefile), "%s/rstmode%jd", 605 tmpdir, (intmax_t)dumpdate); 606 if (modefile[0] == '#') { 607 panic("modefile not defined\n"); 608 fprintf(stderr, "directory mode, owner, and times not set\n"); 609 return; 610 } 611 mf = fopen(modefile, "r"); 612 if (mf == NULL) { 613 fprintf(stderr, "fopen: %s\n", strerror(errno)); 614 fprintf(stderr, "cannot open mode file %s\n", modefile); 615 fprintf(stderr, "directory mode, owner, and times not set\n"); 616 return; 617 } 618 clearerr(mf); 619 bufsize = 0; 620 buf = NULL; 621 for (;;) { 622 (void) fread((char *)&node, 1, sizeof(struct modeinfo), mf); 623 if (ferror(mf)) { 624 warn("%s: cannot read modefile.", modefile); 625 fprintf(stderr, "Mode, owner, and times not set.\n"); 626 break; 627 } 628 if (feof(mf)) 629 break; 630 if (node.extsize > 0) { 631 if (bufsize < node.extsize) { 632 if (bufsize > 0) 633 free(buf); 634 if ((buf = malloc(node.extsize)) != NULL) { 635 bufsize = node.extsize; 636 } else { 637 bufsize = 0; 638 } 639 } 640 if (bufsize >= node.extsize) { 641 (void) fread(buf, 1, node.extsize, mf); 642 if (ferror(mf)) { 643 warn("%s: cannot read modefile.", 644 modefile); 645 fprintf(stderr, "Not all external "); 646 fprintf(stderr, "attributes set.\n"); 647 break; 648 } 649 } else { 650 (void) fseek(mf, node.extsize, SEEK_CUR); 651 if (ferror(mf)) { 652 warn("%s: cannot seek in modefile.", 653 modefile); 654 fprintf(stderr, "Not all directory "); 655 fprintf(stderr, "attributes set.\n"); 656 break; 657 } 658 } 659 } 660 ep = lookupino(node.ino); 661 if (command == 'i' || command == 'x') { 662 if (ep == NULL) 663 continue; 664 if ((flags & FORCE) == 0 && ep->e_flags & EXISTED) { 665 ep->e_flags &= ~NEW; 666 continue; 667 } 668 if (node.ino == UFS_ROOTINO && dotflag == 0) 669 continue; 670 } 671 if (ep == NULL) { 672 panic("cannot find directory inode %ju\n", 673 (uintmax_t)node.ino); 674 continue; 675 } 676 if (!Nflag) { 677 cp = myname(ep); 678 if (node.extsize > 0) { 679 if (bufsize >= node.extsize) { 680 set_extattr(-1, cp, buf, node.extsize, SXA_FILE); 681 } else { 682 fprintf(stderr, "Cannot restore %s%s\n", 683 "extended attributes for ", cp); 684 } 685 } 686 (void) utimens(cp, node.ctimep); 687 (void) utimens(cp, node.mtimep); 688 (void) chown(cp, node.uid, node.gid); 689 (void) chmod(cp, node.mode); 690 if (Mtreefile) { 691 writemtree(cp, "dir", 692 node.uid, node.gid, node.mode, 693 node.flags); 694 } else 695 (void) chflags(cp, node.flags); 696 } 697 ep->e_flags &= ~NEW; 698 } 699 if (bufsize > 0) 700 free(buf); 701 (void) fclose(mf); 702 } 703 704 /* 705 * Generate a literal copy of a directory. 706 */ 707 int 708 genliteraldir(const char *name, ino_t ino) 709 { 710 struct inotab *itp; 711 int ofile, dp, i, size; 712 char buf[BUFSIZ]; 713 714 itp = inotablookup(ino); 715 if (itp == NULL) 716 panic("Cannot find directory inode %ju named %s\n", 717 (uintmax_t)ino, name); 718 if ((ofile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { 719 fprintf(stderr, "%s: ", name); 720 (void) fflush(stderr); 721 fprintf(stderr, "cannot create file: %s\n", strerror(errno)); 722 return (FAIL); 723 } 724 rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt); 725 dp = dup(dirp->dd_fd); 726 for (i = itp->t_size; i > 0; i -= BUFSIZ) { 727 size = MIN(i, BUFSIZ); 728 if (read(dp, buf, (int) size) == -1) { 729 fprintf(stderr, 730 "write error extracting inode %ju, name %s\n", 731 (uintmax_t)curfile.ino, curfile.name); 732 fprintf(stderr, "read: %s\n", strerror(errno)); 733 exit(1); 734 } 735 if (!Nflag && write(ofile, buf, (int) size) == -1) { 736 fprintf(stderr, 737 "write error extracting inode %ju, name %s\n", 738 (uintmax_t)curfile.ino, curfile.name); 739 fprintf(stderr, "write: %s\n", strerror(errno)); 740 exit(1); 741 } 742 } 743 (void) close(dp); 744 (void) close(ofile); 745 return (GOOD); 746 } 747 748 /* 749 * Determine the type of an inode 750 */ 751 int 752 inodetype(ino_t ino) 753 { 754 struct inotab *itp; 755 756 itp = inotablookup(ino); 757 if (itp == NULL) 758 return (LEAF); 759 return (NODE); 760 } 761 762 /* 763 * Allocate and initialize a directory inode entry. 764 * If requested, save its pertinent mode, owner, and time info. 765 */ 766 static struct inotab * 767 allocinotab(struct context *ctxp, long aseekpt) 768 { 769 struct inotab *itp; 770 struct modeinfo node; 771 772 itp = calloc(1, sizeof(struct inotab)); 773 if (itp == NULL) 774 panic("no memory for directory table\n"); 775 itp->t_next = inotab[INOHASH(ctxp->ino)]; 776 inotab[INOHASH(ctxp->ino)] = itp; 777 itp->t_ino = ctxp->ino; 778 itp->t_seekpt = aseekpt; 779 if (mf == NULL) 780 return (itp); 781 node.ino = ctxp->ino; 782 node.mtimep[0].tv_sec = ctxp->atime_sec; 783 node.mtimep[0].tv_nsec = ctxp->atime_nsec; 784 node.mtimep[1].tv_sec = ctxp->mtime_sec; 785 node.mtimep[1].tv_nsec = ctxp->mtime_nsec; 786 node.ctimep[0].tv_sec = ctxp->atime_sec; 787 node.ctimep[0].tv_nsec = ctxp->atime_nsec; 788 node.ctimep[1].tv_sec = ctxp->birthtime_sec; 789 node.ctimep[1].tv_nsec = ctxp->birthtime_nsec; 790 node.extsize = ctxp->extsize; 791 node.mode = ctxp->mode; 792 node.flags = ctxp->file_flags; 793 node.uid = ctxp->uid; 794 node.gid = ctxp->gid; 795 if (fwrite((char *)&node, sizeof(struct modeinfo), 1, mf) != 1) 796 fail_dirtmp(modefile); 797 return (itp); 798 } 799 800 /* 801 * Look up an inode in the table of directories 802 */ 803 static struct inotab * 804 inotablookup(ino_t ino) 805 { 806 struct inotab *itp; 807 808 for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next) 809 if (itp->t_ino == ino) 810 return (itp); 811 return (NULL); 812 } 813 814 /* 815 * Clean up and exit 816 */ 817 void 818 cleanup(void) 819 { 820 821 closemt(); 822 if (modefile[0] != '#') { 823 (void) truncate(modefile, 0); 824 (void) unlink(modefile); 825 } 826 if (dirfile[0] != '#') { 827 (void) truncate(dirfile, 0); 828 (void) unlink(dirfile); 829 } 830 } 831 832 /* 833 * Print out information about the failure to save directory, 834 * extended attribute, and mode information. 835 */ 836 static void __dead 837 fail_dirtmp(char *filename) 838 { 839 warn("%s: cannot write directory database", filename); 840 if (errno == ENOSPC) { 841 fprintf(stderr, "Try making space in %s, %s\n%s\n", tmpdir, 842 "or set environment variable TMPDIR", 843 "to an alternate location with more disk space."); 844 } 845 exit(1); 846 } 847