17c478bd9Sstevel@tonic-gate /*
233a5e6b2Srm * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
333a5e6b2Srm * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate */
57c478bd9Sstevel@tonic-gate
67c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
77c478bd9Sstevel@tonic-gate /* All Rights Reserved */
87c478bd9Sstevel@tonic-gate
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
117c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate */
147c478bd9Sstevel@tonic-gate
157c478bd9Sstevel@tonic-gate #include "restore.h"
167c478bd9Sstevel@tonic-gate #include <byteorder.h>
177c478bd9Sstevel@tonic-gate #include <stdlib.h>
187c478bd9Sstevel@tonic-gate #include <unistd.h>
197c478bd9Sstevel@tonic-gate #include <utime.h>
207c478bd9Sstevel@tonic-gate
21*e0dfa398SToomas Soome struct context curfile;
22*e0dfa398SToomas Soome
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate * Symbol table of directories read from tape.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate #define HASHSIZE 1000
277c478bd9Sstevel@tonic-gate #define INOHASH(val) (val % HASHSIZE)
287c478bd9Sstevel@tonic-gate struct inotab {
297c478bd9Sstevel@tonic-gate struct inotab *t_next;
307c478bd9Sstevel@tonic-gate ino_t t_ino;
317c478bd9Sstevel@tonic-gate offset_t t_seekpt;
327c478bd9Sstevel@tonic-gate offset_t t_size;
337c478bd9Sstevel@tonic-gate struct inotab *t_xattr;
347c478bd9Sstevel@tonic-gate };
357c478bd9Sstevel@tonic-gate static struct inotab *inotab[HASHSIZE];
367c478bd9Sstevel@tonic-gate static struct inotab *xattrlist = NULL;
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Information retained about directories.
407c478bd9Sstevel@tonic-gate */
417c478bd9Sstevel@tonic-gate static struct modeinfo {
427c478bd9Sstevel@tonic-gate ino_t ino;
437c478bd9Sstevel@tonic-gate time_t timep[2];
447c478bd9Sstevel@tonic-gate mode_t mode;
457c478bd9Sstevel@tonic-gate uid_t uid;
467c478bd9Sstevel@tonic-gate gid_t gid;
477c478bd9Sstevel@tonic-gate size_t metasize;
487c478bd9Sstevel@tonic-gate } node;
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Global variables for this file.
527c478bd9Sstevel@tonic-gate */
537c478bd9Sstevel@tonic-gate static off64_t g_seekpt; /* some people have a local seekpt */
547c478bd9Sstevel@tonic-gate static FILE *df, *mf;
557c478bd9Sstevel@tonic-gate static char dirfile[MAXPATHLEN] = "#"; /* No file */
567c478bd9Sstevel@tonic-gate static char modefile[MAXPATHLEN] = "#"; /* No file */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static RST_DIR *dirp;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #define INIT_TEMPFILE(name, type) \
617c478bd9Sstevel@tonic-gate if (name[0] == '#') { \
627c478bd9Sstevel@tonic-gate if (tmpdir == (char *)NULL) /* can't happen; be paranoid */ \
637c478bd9Sstevel@tonic-gate tmpdir = "/tmp"; \
647c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), \
657c478bd9Sstevel@tonic-gate "%s/rst" type "%ld.XXXXXX", tmpdir, dumpdate); \
667c478bd9Sstevel@tonic-gate (void) mktemp(name); \
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate #define INIT_DIRFILE() INIT_TEMPFILE(dirfile, "dir")
707c478bd9Sstevel@tonic-gate #define INIT_MODEFILE() INIT_TEMPFILE(modefile, "mode")
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * Format of old style directories.
747c478bd9Sstevel@tonic-gate */
757c478bd9Sstevel@tonic-gate #define ODIRSIZ 14
767c478bd9Sstevel@tonic-gate struct odirect {
777c478bd9Sstevel@tonic-gate ushort_t d_ino;
787c478bd9Sstevel@tonic-gate char d_name[ODIRSIZ];
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate static ino_t search(ino_t, char *);
827c478bd9Sstevel@tonic-gate static void putdir(char *, size_t);
837c478bd9Sstevel@tonic-gate static void putent(struct direct *);
847c478bd9Sstevel@tonic-gate static void skipmetadata(FILE *, size_t);
857c478bd9Sstevel@tonic-gate static void flushent(void);
867c478bd9Sstevel@tonic-gate static void dcvt(struct odirect *, struct direct *);
877c478bd9Sstevel@tonic-gate static RST_DIR *rst_initdirfile(char *);
887c478bd9Sstevel@tonic-gate static offset_t rst_telldir(RST_DIR *);
897c478bd9Sstevel@tonic-gate static void rst_seekdir(RST_DIR *, offset_t, offset_t);
907c478bd9Sstevel@tonic-gate static struct inotab *allocinotab(ino_t, struct dinode *, off64_t);
917c478bd9Sstevel@tonic-gate static void nodeflush(void);
927c478bd9Sstevel@tonic-gate static struct inotab *inotablookup(ino_t);
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate * Extract directory contents, building up a directory structure
967c478bd9Sstevel@tonic-gate * on disk for extraction by name.
977c478bd9Sstevel@tonic-gate * If genmode is requested, save mode, owner, and times for all
987c478bd9Sstevel@tonic-gate * directories on the tape.
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate void
extractdirs(int genmode)101fe0e7ec4Smaheshvs extractdirs(int genmode)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate int ts;
1047c478bd9Sstevel@tonic-gate struct dinode *ip;
1057c478bd9Sstevel@tonic-gate int saverr;
1067c478bd9Sstevel@tonic-gate struct inotab *itp;
1077c478bd9Sstevel@tonic-gate struct direct nulldir;
1087c478bd9Sstevel@tonic-gate static char dotname[] = "."; /* dirlookup/psearch writes to its arg */
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Extract directories from tape\n"));
1117c478bd9Sstevel@tonic-gate INIT_DIRFILE();
1127c478bd9Sstevel@tonic-gate if ((df = safe_fopen(dirfile, "w", 0600)) == (FILE *)NULL) {
1137c478bd9Sstevel@tonic-gate saverr = errno;
1147c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1157c478bd9Sstevel@tonic-gate gettext("%s: %s - cannot create directory temporary\n"),
116d9529689SToomas Soome progname, dirfile);
1177c478bd9Sstevel@tonic-gate errno = saverr;
1187c478bd9Sstevel@tonic-gate perror("fopen");
1197c478bd9Sstevel@tonic-gate done(1);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate if (genmode != 0) {
1227c478bd9Sstevel@tonic-gate INIT_MODEFILE();
1237c478bd9Sstevel@tonic-gate if ((mf = safe_fopen(modefile, "w", 0600)) == (FILE *)NULL) {
1247c478bd9Sstevel@tonic-gate saverr = errno;
1257c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1267c478bd9Sstevel@tonic-gate gettext("%s: %s - cannot create modefile \n"),
127d9529689SToomas Soome progname, modefile);
1287c478bd9Sstevel@tonic-gate errno = saverr;
1297c478bd9Sstevel@tonic-gate perror("fopen");
1307c478bd9Sstevel@tonic-gate done(1);
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate nulldir.d_ino = 0;
1347c478bd9Sstevel@tonic-gate nulldir.d_namlen = 1;
1357c478bd9Sstevel@tonic-gate (void) strcpy(nulldir.d_name, "/");
1367c478bd9Sstevel@tonic-gate /* LINTED DIRSIZ will always fit into a ushort_t */
1377c478bd9Sstevel@tonic-gate nulldir.d_reclen = (ushort_t)DIRSIZ(&nulldir);
1387c478bd9Sstevel@tonic-gate /* LINTED sign extension ok in assert */
1397c478bd9Sstevel@tonic-gate assert(DIRSIZ(&nulldir) == (ulong_t)nulldir.d_reclen);
1407c478bd9Sstevel@tonic-gate for (;;) {
1417c478bd9Sstevel@tonic-gate curfile.name = gettext("<directory file - name unknown>");
1427c478bd9Sstevel@tonic-gate curfile.action = USING;
1437c478bd9Sstevel@tonic-gate ip = curfile.dip;
1447c478bd9Sstevel@tonic-gate ts = curfile.ts;
1457c478bd9Sstevel@tonic-gate if (ts != TS_END && ts != TS_INODE) {
1467c478bd9Sstevel@tonic-gate getfile(null, null);
1477c478bd9Sstevel@tonic-gate continue;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate if (ts == TS_INODE && ip == NULL) {
1507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1517c478bd9Sstevel@tonic-gate "%s: extractdirs: Failed internal consistency check, curfile.dip is NULL\n"),
1527c478bd9Sstevel@tonic-gate progname);
1537c478bd9Sstevel@tonic-gate done(1);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate if ((ts == TS_INODE && (ip->di_mode & IFMT) != IFDIR &&
1567c478bd9Sstevel@tonic-gate (ip->di_mode & IFMT) != IFATTRDIR) ||
1577c478bd9Sstevel@tonic-gate (ts == TS_END)) {
1587c478bd9Sstevel@tonic-gate (void) fflush(df);
1597c478bd9Sstevel@tonic-gate /* XXX Legitimate error, bad complaint string */
1607c478bd9Sstevel@tonic-gate if (ferror(df))
1617c478bd9Sstevel@tonic-gate panic("%s: %s\n", dirfile, strerror(errno));
1627c478bd9Sstevel@tonic-gate (void) fclose(df);
1637c478bd9Sstevel@tonic-gate rst_closedir(dirp);
1647c478bd9Sstevel@tonic-gate dirp = rst_initdirfile(dirfile);
1657c478bd9Sstevel@tonic-gate if (dirp == NULL)
1667c478bd9Sstevel@tonic-gate perror("initdirfile");
1677c478bd9Sstevel@tonic-gate if (mf != NULL) {
1687c478bd9Sstevel@tonic-gate (void) fflush(mf);
1697c478bd9Sstevel@tonic-gate /* XXX Legitimate error, bad complaint string */
1707c478bd9Sstevel@tonic-gate if (ferror(mf))
1717c478bd9Sstevel@tonic-gate panic("%s: %s\n",
1727c478bd9Sstevel@tonic-gate modefile, strerror(errno));
1737c478bd9Sstevel@tonic-gate (void) fclose(mf);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate if (dirlookup(dotname) == 0) {
1767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
1777c478bd9Sstevel@tonic-gate "Root directory is not on tape\n"));
1787c478bd9Sstevel@tonic-gate done(1);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate return;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate itp = allocinotab(curfile.ino, ip, g_seekpt);
1837c478bd9Sstevel@tonic-gate getfile(putdir, null);
1847c478bd9Sstevel@tonic-gate if (mf != NULL)
1857c478bd9Sstevel@tonic-gate nodeflush();
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate putent(&nulldir);
1887c478bd9Sstevel@tonic-gate flushent();
1897c478bd9Sstevel@tonic-gate itp->t_size = g_seekpt - itp->t_seekpt;
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate /*
1947c478bd9Sstevel@tonic-gate * skip over all the directories on the tape
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate void
skipdirs()1977c478bd9Sstevel@tonic-gate skipdirs()
1987c478bd9Sstevel@tonic-gate {
1997c478bd9Sstevel@tonic-gate while (curfile.dip != NULL &&
200d9529689SToomas Soome ((curfile.dip->di_mode & IFMT) == IFDIR ||
201d9529689SToomas Soome (curfile.dip->di_mode & IFMT) == IFATTRDIR)) {
2027c478bd9Sstevel@tonic-gate skipfile();
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate * Recursively find names and inumbers of all files in subtree
2087c478bd9Sstevel@tonic-gate * pname and pass them off to be processed.
2097c478bd9Sstevel@tonic-gate */
2107c478bd9Sstevel@tonic-gate void
treescan(char * pname,ino_t ino,long (* todo)())211fe0e7ec4Smaheshvs treescan(char *pname, ino_t ino, long (*todo)())
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate struct inotab *itp;
2147c478bd9Sstevel@tonic-gate struct direct *dp;
2157c478bd9Sstevel@tonic-gate uint_t loclen;
2167c478bd9Sstevel@tonic-gate offset_t bpt;
2177c478bd9Sstevel@tonic-gate char locname[MAXCOMPLEXLEN];
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate itp = inotablookup(ino);
2207c478bd9Sstevel@tonic-gate if (itp == NULL) {
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate * Pname is name of a simple file or an unchanged directory.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate (void) (*todo)(pname, ino, LEAF);
2257c478bd9Sstevel@tonic-gate return;
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate * Pname is a dumped directory name.
2297c478bd9Sstevel@tonic-gate */
2307c478bd9Sstevel@tonic-gate if ((*todo)(pname, ino, NODE) == FAIL)
2317c478bd9Sstevel@tonic-gate return;
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * begin search through the directory
2347c478bd9Sstevel@tonic-gate * skipping over "." and ".."
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate loclen = complexcpy(locname, pname, MAXCOMPLEXLEN);
2377c478bd9Sstevel@tonic-gate locname[loclen-1] = '/';
2387c478bd9Sstevel@tonic-gate rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
2397c478bd9Sstevel@tonic-gate dp = rst_readdir(dirp); /* "." */
2407c478bd9Sstevel@tonic-gate
2417c478bd9Sstevel@tonic-gate if (dp != NULL && strcmp(dp->d_name, ".") == 0)
2427c478bd9Sstevel@tonic-gate dp = rst_readdir(dirp); /* ".." */
2437c478bd9Sstevel@tonic-gate else
2447c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2457c478bd9Sstevel@tonic-gate gettext("Warning: `.' missing from directory %s\n"),
246d9529689SToomas Soome pname);
2477c478bd9Sstevel@tonic-gate if (dp != NULL && strcmp(dp->d_name, "..") == 0)
2487c478bd9Sstevel@tonic-gate dp = rst_readdir(dirp); /* first real entry */
2497c478bd9Sstevel@tonic-gate else
2507c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2517c478bd9Sstevel@tonic-gate gettext("Warning: `..' missing from directory %s\n"),
252d9529689SToomas Soome pname);
2537c478bd9Sstevel@tonic-gate bpt = rst_telldir(dirp);
2547c478bd9Sstevel@tonic-gate /*
2557c478bd9Sstevel@tonic-gate * a zero inode signals end of directory
2567c478bd9Sstevel@tonic-gate */
2577c478bd9Sstevel@tonic-gate while (dp != NULL && dp->d_ino != 0) {
2587c478bd9Sstevel@tonic-gate locname[loclen] = '\0';
2597c478bd9Sstevel@tonic-gate if ((loclen + dp->d_namlen) >= (sizeof (locname) - 2)) {
2607c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
2617c478bd9Sstevel@tonic-gate gettext(
262d9529689SToomas Soome "%s%s: ignoring name that exceeds %d char\n"),
2637c478bd9Sstevel@tonic-gate locname, dp->d_name, MAXCOMPLEXLEN);
2647c478bd9Sstevel@tonic-gate } else {
2657c478bd9Sstevel@tonic-gate /* Always fits by if() condition */
2667c478bd9Sstevel@tonic-gate (void) strcpy(locname + loclen, dp->d_name);
2677c478bd9Sstevel@tonic-gate /* put a double null on string for lookupname() */
2687c478bd9Sstevel@tonic-gate locname[loclen+dp->d_namlen+1] = '\0';
2697c478bd9Sstevel@tonic-gate treescan(locname, dp->d_ino, todo);
2707c478bd9Sstevel@tonic-gate rst_seekdir(dirp, bpt, itp->t_seekpt);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate dp = rst_readdir(dirp);
2737c478bd9Sstevel@tonic-gate bpt = rst_telldir(dirp);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate if (dp == NULL)
2767c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
277d9529689SToomas Soome gettext("corrupted directory: %s.\n"), locname);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate * Scan the directory table looking for extended attribute trees.
2827c478bd9Sstevel@tonic-gate * Recursively find names and inumbers in each tree and pass them
2837c478bd9Sstevel@tonic-gate * off to be processed. If the always parameter is not set, only
2847c478bd9Sstevel@tonic-gate * process the attribute tree if the attribute tree parent is to
2857c478bd9Sstevel@tonic-gate * be extracted.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate void
attrscan(int always,long (* todo)())288fe0e7ec4Smaheshvs attrscan(int always, long (*todo)())
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate struct inotab *itp;
2917c478bd9Sstevel@tonic-gate struct entry *ep, *parent;
2927c478bd9Sstevel@tonic-gate struct direct *dp;
2937c478bd9Sstevel@tonic-gate char name[MAXCOMPLEXLEN];
2947c478bd9Sstevel@tonic-gate int len;
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate for (itp = xattrlist; itp != NULL; itp = itp->t_xattr) {
2977c478bd9Sstevel@tonic-gate rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
2987c478bd9Sstevel@tonic-gate if ((dp = rst_readdir(dirp)) != NULL && /* "." */
2997c478bd9Sstevel@tonic-gate (dp = rst_readdir(dirp)) != NULL && /* ".." */
3007c478bd9Sstevel@tonic-gate strcmp(dp->d_name, "..") == 0) {
3017c478bd9Sstevel@tonic-gate if ((parent = lookupino(dp->d_ino)) != NULL) {
3027c478bd9Sstevel@tonic-gate if (!always &&
3037c478bd9Sstevel@tonic-gate (parent->e_flags & (NEW|EXTRACT)) == 0)
3047c478bd9Sstevel@tonic-gate continue;
3057c478bd9Sstevel@tonic-gate len = complexcpy(name, myname(parent),
306d9529689SToomas Soome MAXCOMPLEXLEN - 3);
3077c478bd9Sstevel@tonic-gate name[len] = '.';
3087c478bd9Sstevel@tonic-gate name[len+1] = '\0';
3097c478bd9Sstevel@tonic-gate name[len+2] = '\0';
3107c478bd9Sstevel@tonic-gate inattrspace = 1;
3117c478bd9Sstevel@tonic-gate if ((ep = lookupino(itp->t_ino)) == NULL) {
3127c478bd9Sstevel@tonic-gate ep = addentry(name, itp->t_ino,
313d9529689SToomas Soome NODE|ROOT);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate ep->e_flags |= XATTRROOT;
3167c478bd9Sstevel@tonic-gate treescan(name, itp->t_ino, todo);
3177c478bd9Sstevel@tonic-gate inattrspace = 0;
3187c478bd9Sstevel@tonic-gate } else {
3197c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
320d9529689SToomas Soome gettext("Warning: orphaned attribute "
321d9529689SToomas Soome "directory\n"));
3227c478bd9Sstevel@tonic-gate }
3237c478bd9Sstevel@tonic-gate } else {
324d9529689SToomas Soome (void) fprintf(stderr, gettext("Warning: `..' missing "
325d9529689SToomas Soome "from attribute directory\n"));
3267c478bd9Sstevel@tonic-gate }
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate
3307c478bd9Sstevel@tonic-gate /*
3317c478bd9Sstevel@tonic-gate * Search the directory tree rooted at inode ROOTINO
3327c478bd9Sstevel@tonic-gate * for the path pointed at by n. Note that n must be
3337c478bd9Sstevel@tonic-gate * modifiable, although it is returned in the same
3347c478bd9Sstevel@tonic-gate * condition it was given to us in.
3357c478bd9Sstevel@tonic-gate */
3367c478bd9Sstevel@tonic-gate ino_t
psearch(char * n)337fe0e7ec4Smaheshvs psearch(char *n)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate char *cp, *cp1;
3407c478bd9Sstevel@tonic-gate ino_t ino;
3417c478bd9Sstevel@tonic-gate char c;
3427c478bd9Sstevel@tonic-gate
3437c478bd9Sstevel@tonic-gate ino = ROOTINO;
3447c478bd9Sstevel@tonic-gate if (*(cp = n) == '/')
3457c478bd9Sstevel@tonic-gate cp++;
3467c478bd9Sstevel@tonic-gate next:
3477c478bd9Sstevel@tonic-gate cp1 = cp + 1;
3487c478bd9Sstevel@tonic-gate while (*cp1 != '/' && *cp1)
3497c478bd9Sstevel@tonic-gate cp1++;
3507c478bd9Sstevel@tonic-gate c = *cp1;
3517c478bd9Sstevel@tonic-gate *cp1 = 0;
3527c478bd9Sstevel@tonic-gate ino = search(ino, cp);
3537c478bd9Sstevel@tonic-gate if (ino == 0) {
3547c478bd9Sstevel@tonic-gate *cp1 = c;
3557c478bd9Sstevel@tonic-gate return (0);
3567c478bd9Sstevel@tonic-gate }
3577c478bd9Sstevel@tonic-gate *cp1 = c;
3587c478bd9Sstevel@tonic-gate if (c == '/') {
3597c478bd9Sstevel@tonic-gate cp = cp1+1;
3607c478bd9Sstevel@tonic-gate goto next;
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate return (ino);
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate
3657c478bd9Sstevel@tonic-gate /*
3667c478bd9Sstevel@tonic-gate * search the directory inode ino
3677c478bd9Sstevel@tonic-gate * looking for entry cp
3687c478bd9Sstevel@tonic-gate */
3697c478bd9Sstevel@tonic-gate static ino_t
search(ino_t inum,char * cp)370fe0e7ec4Smaheshvs search(ino_t inum, char *cp)
3717c478bd9Sstevel@tonic-gate {
3727c478bd9Sstevel@tonic-gate struct direct *dp;
3737c478bd9Sstevel@tonic-gate struct inotab *itp;
3747c478bd9Sstevel@tonic-gate uint_t len;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate itp = inotablookup(inum);
3777c478bd9Sstevel@tonic-gate if (itp == NULL)
3787c478bd9Sstevel@tonic-gate return (0);
3797c478bd9Sstevel@tonic-gate rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
3807c478bd9Sstevel@tonic-gate len = strlen(cp);
3817c478bd9Sstevel@tonic-gate do {
3827c478bd9Sstevel@tonic-gate dp = rst_readdir(dirp);
3837c478bd9Sstevel@tonic-gate if (dp == NULL || dp->d_ino == 0)
3847c478bd9Sstevel@tonic-gate return (0);
3857c478bd9Sstevel@tonic-gate } while (dp->d_namlen != len || strncmp(dp->d_name, cp, len) != 0);
3867c478bd9Sstevel@tonic-gate return (dp->d_ino);
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate * Put the directory entries in the directory file
3917c478bd9Sstevel@tonic-gate */
3927c478bd9Sstevel@tonic-gate static void
putdir(char * buf,size_t size)393fe0e7ec4Smaheshvs putdir(char *buf, size_t size)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate struct direct cvtbuf;
3967c478bd9Sstevel@tonic-gate struct odirect *odp;
3977c478bd9Sstevel@tonic-gate struct odirect *eodp;
3987c478bd9Sstevel@tonic-gate struct direct *dp;
3997c478bd9Sstevel@tonic-gate size_t loc, i;
4007c478bd9Sstevel@tonic-gate
4017c478bd9Sstevel@tonic-gate if (cvtflag) {
4027c478bd9Sstevel@tonic-gate /*LINTED [buf is char[] in getfile, size % fs_fsize == 0]*/
4037c478bd9Sstevel@tonic-gate eodp = (struct odirect *)&buf[size];
4047c478bd9Sstevel@tonic-gate /*LINTED [buf is char[] in getfile]*/
4057c478bd9Sstevel@tonic-gate for (odp = (struct odirect *)buf; odp < eodp; odp++)
4067c478bd9Sstevel@tonic-gate if (odp->d_ino != 0) {
4077c478bd9Sstevel@tonic-gate dcvt(odp, &cvtbuf);
4087c478bd9Sstevel@tonic-gate putent(&cvtbuf);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate } else {
4117c478bd9Sstevel@tonic-gate loc = 0;
4127c478bd9Sstevel@tonic-gate while (loc < size) {
4137c478bd9Sstevel@tonic-gate /*LINTED [buf is char[] in getfile, loc % 4 == 0]*/
4147c478bd9Sstevel@tonic-gate dp = (struct direct *)(buf + loc);
4157c478bd9Sstevel@tonic-gate normdirect(byteorder, dp);
4167c478bd9Sstevel@tonic-gate i = DIRBLKSIZ - (loc & (DIRBLKSIZ - 1));
4177c478bd9Sstevel@tonic-gate if (dp->d_reclen == 0 || (long)dp->d_reclen > i) {
4187c478bd9Sstevel@tonic-gate loc += i;
4197c478bd9Sstevel@tonic-gate continue;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate loc += dp->d_reclen;
4227c478bd9Sstevel@tonic-gate if (dp->d_ino != 0) {
4237c478bd9Sstevel@tonic-gate putent(dp);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate }
4287c478bd9Sstevel@tonic-gate
4297c478bd9Sstevel@tonic-gate /*
4307c478bd9Sstevel@tonic-gate * These variables are "local" to the following two functions.
4317c478bd9Sstevel@tonic-gate */
4327c478bd9Sstevel@tonic-gate static char dirbuf[DIRBLKSIZ];
4337c478bd9Sstevel@tonic-gate static int32_t dirloc = 0;
4347c478bd9Sstevel@tonic-gate static int32_t prev = 0;
4357c478bd9Sstevel@tonic-gate
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate * add a new directory entry to a file.
4387c478bd9Sstevel@tonic-gate */
4397c478bd9Sstevel@tonic-gate static void
putent(struct direct * dp)440fe0e7ec4Smaheshvs putent(struct direct *dp)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate /* LINTED DIRSIZ will always fit in a ushort_t */
4437c478bd9Sstevel@tonic-gate dp->d_reclen = (ushort_t)DIRSIZ(dp);
4447c478bd9Sstevel@tonic-gate /* LINTED sign extension ok in assert */
4457c478bd9Sstevel@tonic-gate assert(DIRSIZ(dp) == (ulong_t)dp->d_reclen);
4467c478bd9Sstevel@tonic-gate if (dirloc + (long)dp->d_reclen > DIRBLKSIZ) {
4477c478bd9Sstevel@tonic-gate /*LINTED [prev += dp->d_reclen, prev % 4 == 0]*/
4487c478bd9Sstevel@tonic-gate ((struct direct *)(dirbuf + prev))->d_reclen =
4497c478bd9Sstevel@tonic-gate DIRBLKSIZ - prev;
4507c478bd9Sstevel@tonic-gate (void) fwrite(dirbuf, 1, DIRBLKSIZ, df);
4517c478bd9Sstevel@tonic-gate if (ferror(df))
4527c478bd9Sstevel@tonic-gate panic("%s: %s\n", dirfile, strerror(errno));
4537c478bd9Sstevel@tonic-gate dirloc = 0;
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate bcopy((char *)dp, dirbuf + dirloc, (size_t)dp->d_reclen);
4567c478bd9Sstevel@tonic-gate prev = dirloc;
4577c478bd9Sstevel@tonic-gate dirloc += dp->d_reclen;
4587c478bd9Sstevel@tonic-gate }
4597c478bd9Sstevel@tonic-gate
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate * flush out a directory that is finished.
4627c478bd9Sstevel@tonic-gate */
4637c478bd9Sstevel@tonic-gate static void
flushent(void)4647c478bd9Sstevel@tonic-gate flushent(void)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate
4677c478bd9Sstevel@tonic-gate /* LINTED prev += dp->d_reclen, prev % 4 == 0 */
4687c478bd9Sstevel@tonic-gate ((struct direct *)(dirbuf + prev))->d_reclen = DIRBLKSIZ - prev;
4697c478bd9Sstevel@tonic-gate (void) fwrite(dirbuf, (size_t)dirloc, 1, df);
4707c478bd9Sstevel@tonic-gate if (ferror(df))
4717c478bd9Sstevel@tonic-gate panic("%s: %s\n", dirfile, strerror(errno));
4727c478bd9Sstevel@tonic-gate g_seekpt = ftello64(df);
4737c478bd9Sstevel@tonic-gate dirloc = 0;
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate
4767c478bd9Sstevel@tonic-gate static void
dcvt(struct odirect * odp,struct direct * ndp)477fe0e7ec4Smaheshvs dcvt(struct odirect *odp, struct direct *ndp)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate
4807c478bd9Sstevel@tonic-gate (void) bzero((char *)ndp, sizeof (*ndp));
4817c478bd9Sstevel@tonic-gate ndp->d_ino = odp->d_ino;
4827c478bd9Sstevel@tonic-gate /* Note that odp->d_name may not be null-terminated */
4837c478bd9Sstevel@tonic-gate /* LINTED assertion always true */
4847c478bd9Sstevel@tonic-gate assert(sizeof (ndp->d_name) > sizeof (odp->d_name));
4857c478bd9Sstevel@tonic-gate (void) strncpy(ndp->d_name, odp->d_name, sizeof (odp->d_name));
4867c478bd9Sstevel@tonic-gate ndp->d_name[sizeof (odp->d_name)] = '\0';
4877c478bd9Sstevel@tonic-gate /* LINTED: strlen will fit into d_namlen */
4887c478bd9Sstevel@tonic-gate ndp->d_namlen = strlen(ndp->d_name);
4897c478bd9Sstevel@tonic-gate
4907c478bd9Sstevel@tonic-gate /* LINTED sign extension ok in assert */
4917c478bd9Sstevel@tonic-gate assert(DIRSIZ(ndp) == (ulong_t)ndp->d_reclen);
4927c478bd9Sstevel@tonic-gate /* LINTED DIRSIZ always fits in ushort_t */
4937c478bd9Sstevel@tonic-gate ndp->d_reclen = (ushort_t)DIRSIZ(ndp);
4947c478bd9Sstevel@tonic-gate }
4957c478bd9Sstevel@tonic-gate
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate * Initialize the directory file
4987c478bd9Sstevel@tonic-gate */
4997c478bd9Sstevel@tonic-gate static RST_DIR *
rst_initdirfile(char * name)500fe0e7ec4Smaheshvs rst_initdirfile(char *name)
5017c478bd9Sstevel@tonic-gate {
5027c478bd9Sstevel@tonic-gate RST_DIR *dp;
5037c478bd9Sstevel@tonic-gate int fd;
5047c478bd9Sstevel@tonic-gate
5057c478bd9Sstevel@tonic-gate if ((fd = open(name, O_RDONLY | O_LARGEFILE)) == -1)
5067c478bd9Sstevel@tonic-gate return ((RST_DIR *)0);
5077c478bd9Sstevel@tonic-gate if ((dp = (RST_DIR *)malloc(sizeof (*dp))) == NULL) {
5087c478bd9Sstevel@tonic-gate (void) close(fd);
5097c478bd9Sstevel@tonic-gate return ((RST_DIR *)0);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate dp->dd_fd = fd;
5127c478bd9Sstevel@tonic-gate dp->dd_loc = 0;
5137c478bd9Sstevel@tonic-gate dp->dd_refcnt = 1;
5147c478bd9Sstevel@tonic-gate return (dp);
5157c478bd9Sstevel@tonic-gate }
5167c478bd9Sstevel@tonic-gate
5177c478bd9Sstevel@tonic-gate /*
5187c478bd9Sstevel@tonic-gate * Simulate the opening of a directory
5197c478bd9Sstevel@tonic-gate */
5207c478bd9Sstevel@tonic-gate RST_DIR *
rst_opendir(char * name)521fe0e7ec4Smaheshvs rst_opendir(char *name)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate struct inotab *itp;
5247c478bd9Sstevel@tonic-gate ino_t ino;
5257c478bd9Sstevel@tonic-gate
5267c478bd9Sstevel@tonic-gate if ((ino = dirlookup(name)) > 0 &&
5277c478bd9Sstevel@tonic-gate (itp = inotablookup(ino)) != NULL) {
5287c478bd9Sstevel@tonic-gate rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
5297c478bd9Sstevel@tonic-gate dirp->dd_refcnt++;
5307c478bd9Sstevel@tonic-gate return (dirp);
5317c478bd9Sstevel@tonic-gate }
5327c478bd9Sstevel@tonic-gate return ((RST_DIR *)0);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate
5357c478bd9Sstevel@tonic-gate /*
5367c478bd9Sstevel@tonic-gate * Releases the hidden state created by rst_opendir().
5377c478bd9Sstevel@tonic-gate * Specifically, the dirp it provided to the caller is malloc'd.
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate void
rst_closedir(RST_DIR * cdirp)540fe0e7ec4Smaheshvs rst_closedir(RST_DIR *cdirp)
5417c478bd9Sstevel@tonic-gate {
5427c478bd9Sstevel@tonic-gate if ((cdirp != NULL) && (--(cdirp->dd_refcnt) < 1))
5437c478bd9Sstevel@tonic-gate free(cdirp);
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate * return a pointer into a directory
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate static offset_t
rst_telldir(RST_DIR * tdirp)550fe0e7ec4Smaheshvs rst_telldir(RST_DIR *tdirp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate offset_t pos = llseek(tdirp->dd_fd, (offset_t)0, SEEK_CUR);
5537c478bd9Sstevel@tonic-gate
5547c478bd9Sstevel@tonic-gate if (pos == (offset_t)-1) {
5557c478bd9Sstevel@tonic-gate perror("Could not determine position in directory file");
5567c478bd9Sstevel@tonic-gate done(1);
5577c478bd9Sstevel@tonic-gate }
5587c478bd9Sstevel@tonic-gate
5597c478bd9Sstevel@tonic-gate return ((pos - tdirp->dd_size) + tdirp->dd_loc);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate
5627c478bd9Sstevel@tonic-gate /*
5637c478bd9Sstevel@tonic-gate * Seek to an entry in a directory.
5647c478bd9Sstevel@tonic-gate * Only values returned by ``rst_telldir'' should be passed to rst_seekdir.
5657c478bd9Sstevel@tonic-gate * This routine handles many directories in a single file.
5667c478bd9Sstevel@tonic-gate * It takes the base of the directory in the file, plus
5677c478bd9Sstevel@tonic-gate * the desired seek offset into it.
5687c478bd9Sstevel@tonic-gate */
5697c478bd9Sstevel@tonic-gate static void
rst_seekdir(RST_DIR * sdirp,offset_t loc,offset_t base)570fe0e7ec4Smaheshvs rst_seekdir(RST_DIR *sdirp, offset_t loc, offset_t base)
5717c478bd9Sstevel@tonic-gate {
5727c478bd9Sstevel@tonic-gate
5737c478bd9Sstevel@tonic-gate if (loc == rst_telldir(sdirp))
5747c478bd9Sstevel@tonic-gate return;
5757c478bd9Sstevel@tonic-gate loc -= base;
5767c478bd9Sstevel@tonic-gate if (loc < 0)
5777c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
578d9529689SToomas Soome gettext("bad seek pointer to rst_seekdir %d\n"), loc);
5797c478bd9Sstevel@tonic-gate (void) llseek(sdirp->dd_fd, base + (loc & ~(DIRBLKSIZ - 1)), 0);
5807c478bd9Sstevel@tonic-gate sdirp->dd_loc = loc & (DIRBLKSIZ - 1);
5817c478bd9Sstevel@tonic-gate if (sdirp->dd_loc != 0)
5827c478bd9Sstevel@tonic-gate sdirp->dd_size = read(sdirp->dd_fd, sdirp->dd_buf, DIRBLKSIZ);
5837c478bd9Sstevel@tonic-gate }
5847c478bd9Sstevel@tonic-gate
5857c478bd9Sstevel@tonic-gate /*
5867c478bd9Sstevel@tonic-gate * get next entry in a directory.
5877c478bd9Sstevel@tonic-gate */
5887c478bd9Sstevel@tonic-gate struct direct *
rst_readdir(RST_DIR * rdirp)589fe0e7ec4Smaheshvs rst_readdir(RST_DIR *rdirp)
5907c478bd9Sstevel@tonic-gate {
5917c478bd9Sstevel@tonic-gate struct direct *dp;
5927c478bd9Sstevel@tonic-gate
5937c478bd9Sstevel@tonic-gate for (;;) {
5947c478bd9Sstevel@tonic-gate if (rdirp->dd_loc == 0) {
5957c478bd9Sstevel@tonic-gate rdirp->dd_size = read(rdirp->dd_fd, rdirp->dd_buf,
5967c478bd9Sstevel@tonic-gate DIRBLKSIZ);
5977c478bd9Sstevel@tonic-gate if (rdirp->dd_size <= 0) {
5987c478bd9Sstevel@tonic-gate dprintf(stderr,
599d9529689SToomas Soome gettext("error reading directory\n"));
6007c478bd9Sstevel@tonic-gate return ((struct direct *)0);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate }
6037c478bd9Sstevel@tonic-gate if (rdirp->dd_loc >= rdirp->dd_size) {
6047c478bd9Sstevel@tonic-gate rdirp->dd_loc = 0;
6057c478bd9Sstevel@tonic-gate continue;
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate /*LINTED [rvalue will be aligned on int boundary]*/
6087c478bd9Sstevel@tonic-gate dp = (struct direct *)(rdirp->dd_buf + rdirp->dd_loc);
6097c478bd9Sstevel@tonic-gate if (dp->d_reclen == 0 ||
6107c478bd9Sstevel@tonic-gate (long)dp->d_reclen > (DIRBLKSIZ + 1 - rdirp->dd_loc)) {
6117c478bd9Sstevel@tonic-gate dprintf(stderr,
6127c478bd9Sstevel@tonic-gate gettext("corrupted directory: bad reclen %d\n"),
613d9529689SToomas Soome dp->d_reclen);
6147c478bd9Sstevel@tonic-gate return ((struct direct *)0);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate rdirp->dd_loc += dp->d_reclen;
6177c478bd9Sstevel@tonic-gate if (dp->d_ino == 0 && strcmp(dp->d_name, "/") != 0)
6187c478bd9Sstevel@tonic-gate continue;
6197c478bd9Sstevel@tonic-gate if ((ino_t)(dp->d_ino) >= maxino) {
6207c478bd9Sstevel@tonic-gate dprintf(stderr,
621d9529689SToomas Soome gettext("corrupted directory: bad inum %lu\n"),
622d9529689SToomas Soome dp->d_ino);
6237c478bd9Sstevel@tonic-gate continue;
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate return (dp);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate * Set the mode, owner, and times for all new or changed directories
6317c478bd9Sstevel@tonic-gate */
6327c478bd9Sstevel@tonic-gate void
setdirmodes(void)6337c478bd9Sstevel@tonic-gate setdirmodes(void)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate FILE *smf;
6367c478bd9Sstevel@tonic-gate struct entry *ep;
6377c478bd9Sstevel@tonic-gate char *cp, *metadata = NULL;
6387c478bd9Sstevel@tonic-gate size_t metasize = 0;
6397c478bd9Sstevel@tonic-gate int override = -1;
6407c478bd9Sstevel@tonic-gate int saverr;
6417c478bd9Sstevel@tonic-gate static int complained_chown = 0;
6427c478bd9Sstevel@tonic-gate static int complained_chmod = 0;
6437c478bd9Sstevel@tonic-gate int dfd;
6447c478bd9Sstevel@tonic-gate
6457c478bd9Sstevel@tonic-gate vprintf(stdout, gettext("Set directory mode, owner, and times.\n"));
6467c478bd9Sstevel@tonic-gate /* XXX if modefile[0] == '#', shouldn't we just bail here? */
6477c478bd9Sstevel@tonic-gate /* XXX why isn't it set already? */
6487c478bd9Sstevel@tonic-gate INIT_MODEFILE();
6497c478bd9Sstevel@tonic-gate smf = fopen64(modefile, "r");
6507c478bd9Sstevel@tonic-gate if (smf == NULL) {
6517c478bd9Sstevel@tonic-gate perror("fopen");
6527c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
653d9529689SToomas Soome gettext("cannot open mode file %s\n"), modefile);
6547c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
655d9529689SToomas Soome gettext("directory mode, owner, and times not set\n"));
6567c478bd9Sstevel@tonic-gate return;
6577c478bd9Sstevel@tonic-gate }
6587c478bd9Sstevel@tonic-gate clearerr(smf);
6597c478bd9Sstevel@tonic-gate for (;;) {
6607c478bd9Sstevel@tonic-gate (void) fread((char *)&node, 1, sizeof (node), smf);
6617c478bd9Sstevel@tonic-gate if (feof(smf))
6627c478bd9Sstevel@tonic-gate break;
6637c478bd9Sstevel@tonic-gate ep = lookupino(node.ino);
6647c478bd9Sstevel@tonic-gate if (command == 'i' || command == 'x') {
6657c478bd9Sstevel@tonic-gate if (ep == NIL) {
6667c478bd9Sstevel@tonic-gate skipmetadata(smf, node.metasize);
6677c478bd9Sstevel@tonic-gate continue;
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate if (ep->e_flags & EXISTED) {
6707c478bd9Sstevel@tonic-gate if (override < 0) {
6717c478bd9Sstevel@tonic-gate if (reply(gettext(
6727c478bd9Sstevel@tonic-gate "Directories already exist, set modes anyway"))
6737c478bd9Sstevel@tonic-gate == FAIL)
6747c478bd9Sstevel@tonic-gate override = 0;
6757c478bd9Sstevel@tonic-gate else
6767c478bd9Sstevel@tonic-gate override = 1;
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate if (override == 0) {
6797c478bd9Sstevel@tonic-gate /* LINTED: result fits into short */
6807c478bd9Sstevel@tonic-gate ep->e_flags &= ~NEW;
6817c478bd9Sstevel@tonic-gate skipmetadata(smf, node.metasize);
6827c478bd9Sstevel@tonic-gate continue;
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate }
6857c478bd9Sstevel@tonic-gate if (node.ino == ROOTINO &&
6867c478bd9Sstevel@tonic-gate reply(gettext("set owner/mode for '.'")) == FAIL) {
6877c478bd9Sstevel@tonic-gate skipmetadata(smf, node.metasize);
6887c478bd9Sstevel@tonic-gate continue;
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate if (ep == NIL) {
6927c478bd9Sstevel@tonic-gate panic(gettext("cannot find directory inode %d\n"),
693d9529689SToomas Soome node.ino);
6947c478bd9Sstevel@tonic-gate skipmetadata(smf, node.metasize);
6957c478bd9Sstevel@tonic-gate continue;
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate cp = myname(ep);
6987c478bd9Sstevel@tonic-gate resolve(myname(ep), &dfd, &cp);
6997c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
7007c478bd9Sstevel@tonic-gate if (fchdir(dfd) < 0) {
7017c478bd9Sstevel@tonic-gate saverr = errno;
7027c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
703d9529689SToomas Soome gettext("Can not set attribute context: %s\n"),
704d9529689SToomas Soome strerror(saverr));
7057c478bd9Sstevel@tonic-gate (void) close(dfd);
7067c478bd9Sstevel@tonic-gate continue;
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate if (chmod(cp, node.mode) < 0 && !complained_chmod) {
7107c478bd9Sstevel@tonic-gate saverr = errno;
7117c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7127c478bd9Sstevel@tonic-gate gettext("Can not set directory permissions: %s\n"),
713d9529689SToomas Soome strerror(saverr));
7147c478bd9Sstevel@tonic-gate complained_chmod = 1;
7157c478bd9Sstevel@tonic-gate }
7167c478bd9Sstevel@tonic-gate if (node.metasize != 0) {
7177c478bd9Sstevel@tonic-gate if (node.metasize > metasize)
7187c478bd9Sstevel@tonic-gate metadata = realloc(metadata,
7197c478bd9Sstevel@tonic-gate metasize = node.metasize);
7207c478bd9Sstevel@tonic-gate if (metadata == NULL) {
7217c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
722d9529689SToomas Soome gettext("Cannot malloc metadata\n"));
7237c478bd9Sstevel@tonic-gate done(1);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate (void) fread(metadata, 1, node.metasize, smf);
7267c478bd9Sstevel@tonic-gate metaproc(cp, metadata, node.metasize);
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate * BUG 4302943
7317c478bd9Sstevel@tonic-gate * Since the ACLs must be set before fixing the ownership,
7327c478bd9Sstevel@tonic-gate * chown should be called only after metaproc
7337c478bd9Sstevel@tonic-gate */
7347c478bd9Sstevel@tonic-gate if (chown(cp, node.uid, node.gid) < 0 && !complained_chown) {
7357c478bd9Sstevel@tonic-gate saverr = errno;
7367c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7377c478bd9Sstevel@tonic-gate gettext("Can not set directory ownership: %s\n"),
7387c478bd9Sstevel@tonic-gate strerror(saverr));
7397c478bd9Sstevel@tonic-gate complained_chown = 1;
7407c478bd9Sstevel@tonic-gate }
7417c478bd9Sstevel@tonic-gate utime(cp, (struct utimbuf *)node.timep);
7427c478bd9Sstevel@tonic-gate /* LINTED: result fits into short */
7437c478bd9Sstevel@tonic-gate ep->e_flags &= ~NEW;
7447c478bd9Sstevel@tonic-gate if (dfd != AT_FDCWD) {
7457c478bd9Sstevel@tonic-gate fchdir(savepwd);
7467c478bd9Sstevel@tonic-gate (void) close(dfd);
7477c478bd9Sstevel@tonic-gate }
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate if (ferror(smf))
7507c478bd9Sstevel@tonic-gate panic(gettext("error setting directory modes\n"));
7517c478bd9Sstevel@tonic-gate if (metadata != NULL)
7527c478bd9Sstevel@tonic-gate (void) free(metadata);
7537c478bd9Sstevel@tonic-gate (void) fclose(smf);
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate void
skipmetadata(FILE * f,size_t size)757fe0e7ec4Smaheshvs skipmetadata(FILE *f, size_t size)
7587c478bd9Sstevel@tonic-gate {
7597c478bd9Sstevel@tonic-gate /* XXX should we bail if this doesn't work? */
7607c478bd9Sstevel@tonic-gate /* LINTED unsigned -> signed conversion ok here */
7617c478bd9Sstevel@tonic-gate (void) fseeko(f, (off_t)size, SEEK_CUR);
7627c478bd9Sstevel@tonic-gate }
7637c478bd9Sstevel@tonic-gate
7647c478bd9Sstevel@tonic-gate /*
7657c478bd9Sstevel@tonic-gate * Generate a literal copy of a directory.
7667c478bd9Sstevel@tonic-gate */
767fe0e7ec4Smaheshvs int
genliteraldir(char * name,ino_t ino)768fe0e7ec4Smaheshvs genliteraldir(char *name, ino_t ino)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate struct inotab *itp;
7717c478bd9Sstevel@tonic-gate int ofile, dp;
7727c478bd9Sstevel@tonic-gate off64_t i;
7737c478bd9Sstevel@tonic-gate size_t size;
7747c478bd9Sstevel@tonic-gate char buf[BUFSIZ];
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate itp = inotablookup(ino);
7777c478bd9Sstevel@tonic-gate if (itp == NULL) {
7787c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
7797c478bd9Sstevel@tonic-gate gettext("Cannot find directory inode %d named %s\n"),
7807c478bd9Sstevel@tonic-gate ino, name);
7817c478bd9Sstevel@tonic-gate return (FAIL);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate if ((ofile = creat(name, 0666)) < 0) {
7847c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", name);
7857c478bd9Sstevel@tonic-gate (void) fflush(stderr);
7867c478bd9Sstevel@tonic-gate perror(gettext("cannot create file"));
7877c478bd9Sstevel@tonic-gate return (FAIL);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate rst_seekdir(dirp, itp->t_seekpt, itp->t_seekpt);
7907c478bd9Sstevel@tonic-gate dp = dup(dirp->dd_fd);
7917c478bd9Sstevel@tonic-gate if (dp < 0) {
7927c478bd9Sstevel@tonic-gate perror(gettext("dup(2) failed"));
7937c478bd9Sstevel@tonic-gate (void) close(ofile);
7947c478bd9Sstevel@tonic-gate (void) unlink(name);
7957c478bd9Sstevel@tonic-gate return (FAIL);
7967c478bd9Sstevel@tonic-gate }
7977c478bd9Sstevel@tonic-gate for (i = itp->t_size; i != 0; i -= size) {
7987c478bd9Sstevel@tonic-gate /* LINTED cast is safe due to comparison */
7997c478bd9Sstevel@tonic-gate size = i < BUFSIZ ? (size_t)i : BUFSIZ;
8007c478bd9Sstevel@tonic-gate /* XXX instead of done(), clean up and return FAIL? */
8017c478bd9Sstevel@tonic-gate if (read(dp, buf, size) == -1) {
8027c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
803d9529689SToomas Soome "read error extracting inode %d, name %s\n"),
804d9529689SToomas Soome curfile.ino, curfile.name);
8057c478bd9Sstevel@tonic-gate perror("read");
8067c478bd9Sstevel@tonic-gate done(1);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate if (write(ofile, buf, size) == -1) {
8097c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
810d9529689SToomas Soome "write error extracting inode %d, name %s\n"),
811d9529689SToomas Soome curfile.ino, curfile.name);
8127c478bd9Sstevel@tonic-gate perror("write");
8137c478bd9Sstevel@tonic-gate done(1);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate (void) close(dp);
8177c478bd9Sstevel@tonic-gate (void) close(ofile);
8187c478bd9Sstevel@tonic-gate return (GOOD);
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate
8217c478bd9Sstevel@tonic-gate /*
8227c478bd9Sstevel@tonic-gate * Determine the type of an inode
8237c478bd9Sstevel@tonic-gate */
824fe0e7ec4Smaheshvs int
inodetype(ino_t ino)825fe0e7ec4Smaheshvs inodetype(ino_t ino)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate struct inotab *itp;
8287c478bd9Sstevel@tonic-gate
8297c478bd9Sstevel@tonic-gate itp = inotablookup(ino);
8307c478bd9Sstevel@tonic-gate if (itp == NULL)
8317c478bd9Sstevel@tonic-gate return (LEAF);
8327c478bd9Sstevel@tonic-gate return (NODE);
8337c478bd9Sstevel@tonic-gate }
8347c478bd9Sstevel@tonic-gate
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate * Allocate and initialize a directory inode entry.
8377c478bd9Sstevel@tonic-gate * If requested, save its pertinent mode, owner, and time info.
8387c478bd9Sstevel@tonic-gate */
8397c478bd9Sstevel@tonic-gate static struct inotab *
allocinotab(ino_t ino,struct dinode * dip,off64_t seekpt)840fe0e7ec4Smaheshvs allocinotab(ino_t ino, struct dinode *dip, off64_t seekpt)
8417c478bd9Sstevel@tonic-gate {
8427c478bd9Sstevel@tonic-gate struct inotab *itp;
8437c478bd9Sstevel@tonic-gate
8447c478bd9Sstevel@tonic-gate itp = (struct inotab *)calloc(1, sizeof (*itp));
8457c478bd9Sstevel@tonic-gate if (itp == 0) {
8467c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
8477c478bd9Sstevel@tonic-gate gettext("no memory for directory table\n"));
8487c478bd9Sstevel@tonic-gate done(1);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate itp->t_next = inotab[INOHASH(ino)];
8517c478bd9Sstevel@tonic-gate inotab[INOHASH(ino)] = itp;
8527c478bd9Sstevel@tonic-gate itp->t_ino = ino;
8537c478bd9Sstevel@tonic-gate itp->t_seekpt = seekpt;
8547c478bd9Sstevel@tonic-gate if ((dip->di_mode & IFMT) == IFATTRDIR) {
8557c478bd9Sstevel@tonic-gate itp->t_xattr = xattrlist;
8567c478bd9Sstevel@tonic-gate xattrlist = itp;
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate if (mf == NULL)
8597c478bd9Sstevel@tonic-gate return (itp);
8607c478bd9Sstevel@tonic-gate node.ino = ino;
8617c478bd9Sstevel@tonic-gate node.timep[0] = dip->di_atime;
8627c478bd9Sstevel@tonic-gate node.timep[1] = dip->di_mtime;
8637c478bd9Sstevel@tonic-gate node.mode = dip->di_mode;
8647c478bd9Sstevel@tonic-gate node.uid =
865d9529689SToomas Soome dip->di_suid == UID_LONG ? dip->di_uid : (uid_t)dip->di_suid;
8667c478bd9Sstevel@tonic-gate node.gid =
867d9529689SToomas Soome dip->di_sgid == GID_LONG ? dip->di_gid : (gid_t)dip->di_sgid;
8687c478bd9Sstevel@tonic-gate return (itp);
8697c478bd9Sstevel@tonic-gate }
8707c478bd9Sstevel@tonic-gate
8717c478bd9Sstevel@tonic-gate void
nodeflush()8727c478bd9Sstevel@tonic-gate nodeflush()
8737c478bd9Sstevel@tonic-gate {
8747c478bd9Sstevel@tonic-gate char *metadata;
8757c478bd9Sstevel@tonic-gate
8767c478bd9Sstevel@tonic-gate if (mf == NULL) {
8777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(
8787c478bd9Sstevel@tonic-gate "Inconsistency detected: modefile pointer is NULL\n"));
8797c478bd9Sstevel@tonic-gate done(1);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate metaget(&metadata, &(node.metasize));
8827c478bd9Sstevel@tonic-gate (void) fwrite((char *)&node, 1, sizeof (node), mf);
8837c478bd9Sstevel@tonic-gate if (node.metasize != 0)
8847c478bd9Sstevel@tonic-gate (void) fwrite(metadata, 1, node.metasize, mf);
8857c478bd9Sstevel@tonic-gate if (ferror(mf))
8867c478bd9Sstevel@tonic-gate panic("%s: %s\n", modefile, strerror(errno));
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate * Look up an inode in the table of directories
8917c478bd9Sstevel@tonic-gate */
8927c478bd9Sstevel@tonic-gate static struct inotab *
inotablookup(ino_t ino)893fe0e7ec4Smaheshvs inotablookup(ino_t ino)
8947c478bd9Sstevel@tonic-gate {
8957c478bd9Sstevel@tonic-gate struct inotab *itp;
8967c478bd9Sstevel@tonic-gate
8977c478bd9Sstevel@tonic-gate for (itp = inotab[INOHASH(ino)]; itp != NULL; itp = itp->t_next)
8987c478bd9Sstevel@tonic-gate if (itp->t_ino == ino)
8997c478bd9Sstevel@tonic-gate return (itp);
9007c478bd9Sstevel@tonic-gate return ((struct inotab *)0);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate
9037c478bd9Sstevel@tonic-gate /*
9047c478bd9Sstevel@tonic-gate * Clean up and exit
9057c478bd9Sstevel@tonic-gate */
9067c478bd9Sstevel@tonic-gate void
done(int exitcode)907fe0e7ec4Smaheshvs done(int exitcode)
9087c478bd9Sstevel@tonic-gate {
90933a5e6b2Srm closemt(ALLOW_OFFLINE); /* don't force offline on exit */
9107c478bd9Sstevel@tonic-gate if (modefile[0] != '#')
9117c478bd9Sstevel@tonic-gate (void) unlink(modefile);
9127c478bd9Sstevel@tonic-gate if (dirfile[0] != '#')
9137c478bd9Sstevel@tonic-gate (void) unlink(dirfile);
9147c478bd9Sstevel@tonic-gate exit(exitcode);
9157c478bd9Sstevel@tonic-gate }
916