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