create.c revision 1553
11553Srgrimes/*-
21553Srgrimes * Copyright (c) 1989, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
351553Srgrimesstatic char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
361553Srgrimes#endif /* not lint */
371553Srgrimes
381553Srgrimes#include <sys/param.h>
391553Srgrimes#include <sys/stat.h>
401553Srgrimes#include <time.h>
411553Srgrimes#include <fcntl.h>
421553Srgrimes#include <fts.h>
431553Srgrimes#include <dirent.h>
441553Srgrimes#include <grp.h>
451553Srgrimes#include <pwd.h>
461553Srgrimes#include <errno.h>
471553Srgrimes#include <unistd.h>
481553Srgrimes#include <stdio.h>
491553Srgrimes#include "mtree.h"
501553Srgrimes#include "extern.h"
511553Srgrimes
521553Srgrimes#define	INDENTNAMELEN	15
531553Srgrimes#define	MAXLINELEN	80
541553Srgrimes
551553Srgrimesextern int crc_total, ftsoptions;
561553Srgrimesextern int dflag, sflag;
571553Srgrimesextern u_short keys;
581553Srgrimesextern char fullpath[MAXPATHLEN];
591553Srgrimes
601553Srgrimesstatic gid_t gid;
611553Srgrimesstatic uid_t uid;
621553Srgrimesstatic mode_t mode;
631553Srgrimes
641553Srgrimesstatic int	dsort __P((const FTSENT **, const FTSENT **));
651553Srgrimesstatic void	output __P((int *, const char *, ...));
661553Srgrimesstatic int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *));
671553Srgrimesstatic void	statf __P((FTSENT *));
681553Srgrimes
691553Srgrimesvoid
701553Srgrimescwalk()
711553Srgrimes{
721553Srgrimes	register FTS *t;
731553Srgrimes	register FTSENT *p;
741553Srgrimes	time_t clock;
751553Srgrimes	char *argv[2], host[MAXHOSTNAMELEN];
761553Srgrimes
771553Srgrimes	(void)time(&clock);
781553Srgrimes	(void)gethostname(host, sizeof(host));
791553Srgrimes	(void)printf(
801553Srgrimes	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
811553Srgrimes	    getlogin(), host, fullpath, ctime(&clock));
821553Srgrimes
831553Srgrimes	argv[0] = ".";
841553Srgrimes	argv[1] = NULL;
851553Srgrimes	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
861553Srgrimes		err("fts_open: %s", strerror(errno));
871553Srgrimes	while (p = fts_read(t))
881553Srgrimes		switch(p->fts_info) {
891553Srgrimes		case FTS_D:
901553Srgrimes			(void)printf("\n# %s\n", p->fts_path);
911553Srgrimes			statd(t, p, &uid, &gid, &mode);
921553Srgrimes			statf(p);
931553Srgrimes			break;
941553Srgrimes		case FTS_DP:
951553Srgrimes			if (p->fts_level > 0)
961553Srgrimes				(void)printf("# %s\n..\n\n", p->fts_path);
971553Srgrimes			break;
981553Srgrimes		case FTS_DNR:
991553Srgrimes		case FTS_ERR:
1001553Srgrimes		case FTS_NS:
1011553Srgrimes			(void)fprintf(stderr,
1021553Srgrimes			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
1031553Srgrimes			break;
1041553Srgrimes		default:
1051553Srgrimes			if (!dflag)
1061553Srgrimes				statf(p);
1071553Srgrimes			break;
1081553Srgrimes
1091553Srgrimes		}
1101553Srgrimes	(void)fts_close(t);
1111553Srgrimes	if (sflag && keys & F_CKSUM)
1121553Srgrimes		(void)fprintf(stderr,
1131553Srgrimes		    "mtree: %s checksum: %lu\n", fullpath, crc_total);
1141553Srgrimes}
1151553Srgrimes
1161553Srgrimesstatic void
1171553Srgrimesstatf(p)
1181553Srgrimes	FTSENT *p;
1191553Srgrimes{
1201553Srgrimes	struct group *gr;
1211553Srgrimes	struct passwd *pw;
1221553Srgrimes	u_long len, val;
1231553Srgrimes	int fd, indent;
1241553Srgrimes
1251553Srgrimes	if (S_ISDIR(p->fts_statp->st_mode))
1261553Srgrimes		indent = printf("%s", p->fts_name);
1271553Srgrimes	else
1281553Srgrimes		indent = printf("    %s", p->fts_name);
1291553Srgrimes
1301553Srgrimes	if (indent > INDENTNAMELEN)
1311553Srgrimes		indent = MAXLINELEN;
1321553Srgrimes	else
1331553Srgrimes		indent += printf("%*s", INDENTNAMELEN - indent, "");
1341553Srgrimes
1351553Srgrimes	if (!S_ISREG(p->fts_statp->st_mode))
1361553Srgrimes		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
1371553Srgrimes	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid)
1381553Srgrimes		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
1391553Srgrimes			output(&indent, "uname=%s", pw->pw_name);
1401553Srgrimes		else /* if (keys & F_UID) */
1411553Srgrimes			output(&indent, "uid=%u", p->fts_statp->st_uid);
1421553Srgrimes	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid)
1431553Srgrimes		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
1441553Srgrimes			output(&indent, "gname=%s", gr->gr_name);
1451553Srgrimes		else /* if (keys & F_GID) */
1461553Srgrimes			output(&indent, "gid=%u", p->fts_statp->st_gid);
1471553Srgrimes	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
1481553Srgrimes		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
1491553Srgrimes	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
1501553Srgrimes		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
1511553Srgrimes	if (keys & F_SIZE)
1521553Srgrimes		output(&indent, "size=%qd", p->fts_statp->st_size);
1531553Srgrimes	if (keys & F_TIME)
1541553Srgrimes		output(&indent, "time=%ld.%ld",
1551553Srgrimes		    p->fts_statp->st_mtimespec.ts_sec,
1561553Srgrimes		    p->fts_statp->st_mtimespec.ts_nsec);
1571553Srgrimes	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
1581553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
1591553Srgrimes		    crc(fd, &val, &len))
1601553Srgrimes			err("%s: %s", p->fts_accpath, strerror(errno));
1611553Srgrimes		(void)close(fd);
1621553Srgrimes		output(&indent, "cksum=%lu", val);
1631553Srgrimes	}
1641553Srgrimes	if (keys & F_SLINK &&
1651553Srgrimes	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
1661553Srgrimes		output(&indent, "link=%s", rlink(p->fts_accpath));
1671553Srgrimes	(void)putchar('\n');
1681553Srgrimes}
1691553Srgrimes
1701553Srgrimes#define	MAXGID	5000
1711553Srgrimes#define	MAXUID	5000
1721553Srgrimes#define	MAXMODE	MBITS + 1
1731553Srgrimes
1741553Srgrimesstatic int
1751553Srgrimesstatd(t, parent, puid, pgid, pmode)
1761553Srgrimes	FTS *t;
1771553Srgrimes	FTSENT *parent;
1781553Srgrimes	uid_t *puid;
1791553Srgrimes	gid_t *pgid;
1801553Srgrimes	mode_t *pmode;
1811553Srgrimes{
1821553Srgrimes	register FTSENT *p;
1831553Srgrimes	register gid_t sgid;
1841553Srgrimes	register uid_t suid;
1851553Srgrimes	register mode_t smode;
1861553Srgrimes	struct group *gr;
1871553Srgrimes	struct passwd *pw;
1881553Srgrimes	gid_t savegid;
1891553Srgrimes	uid_t saveuid;
1901553Srgrimes	mode_t savemode;
1911553Srgrimes	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
1921553Srgrimes
1931553Srgrimes	if ((p = fts_children(t, 0)) == NULL) {
1941553Srgrimes		if (errno)
1951553Srgrimes			err("%s: %s", RP(parent), strerror(errno));
1961553Srgrimes		return (1);
1971553Srgrimes	}
1981553Srgrimes
1991553Srgrimes	bzero(g, sizeof(g));
2001553Srgrimes	bzero(u, sizeof(u));
2011553Srgrimes	bzero(m, sizeof(m));
2021553Srgrimes
2031553Srgrimes	maxuid = maxgid = maxmode = 0;
2041553Srgrimes	for (; p; p = p->fts_link) {
2051553Srgrimes		smode = p->fts_statp->st_mode & MBITS;
2061553Srgrimes		if (smode < MAXMODE && ++m[smode] > maxmode) {
2071553Srgrimes			savemode = smode;
2081553Srgrimes			maxmode = m[smode];
2091553Srgrimes		}
2101553Srgrimes		sgid = p->fts_statp->st_gid;
2111553Srgrimes		if (sgid < MAXGID && ++g[sgid] > maxgid) {
2121553Srgrimes			savegid = sgid;
2131553Srgrimes			maxgid = g[sgid];
2141553Srgrimes		}
2151553Srgrimes		suid = p->fts_statp->st_uid;
2161553Srgrimes		if (suid < MAXUID && ++u[suid] > maxuid) {
2171553Srgrimes			saveuid = suid;
2181553Srgrimes			maxuid = u[suid];
2191553Srgrimes		}
2201553Srgrimes	}
2211553Srgrimes	(void)printf("/set type=file");
2221553Srgrimes	if (keys & F_GID)
2231553Srgrimes		(void)printf(" gid=%u", savegid);
2241553Srgrimes	if (keys & F_GNAME)
2251553Srgrimes		if ((gr = getgrgid(savegid)) != NULL)
2261553Srgrimes			(void)printf(" gname=%s", gr->gr_name);
2271553Srgrimes		else
2281553Srgrimes			(void)printf(" gid=%u", savegid);
2291553Srgrimes	if (keys & F_UNAME)
2301553Srgrimes		if ((pw = getpwuid(saveuid)) != NULL)
2311553Srgrimes			(void)printf(" uname=%s", pw->pw_name);
2321553Srgrimes		else
2331553Srgrimes			(void)printf(" uid=%u", saveuid);
2341553Srgrimes	if (keys & F_UID)
2351553Srgrimes		(void)printf(" uid=%u", saveuid);
2361553Srgrimes	if (keys & F_MODE)
2371553Srgrimes		(void)printf(" mode=%#o", savemode);
2381553Srgrimes	if (keys & F_NLINK)
2391553Srgrimes		(void)printf(" nlink=1");
2401553Srgrimes	(void)printf("\n");
2411553Srgrimes	*puid = saveuid;
2421553Srgrimes	*pgid = savegid;
2431553Srgrimes	*pmode = savemode;
2441553Srgrimes	return (0);
2451553Srgrimes}
2461553Srgrimes
2471553Srgrimesstatic int
2481553Srgrimesdsort(a, b)
2491553Srgrimes	const FTSENT **a, **b;
2501553Srgrimes{
2511553Srgrimes	if (S_ISDIR((*a)->fts_statp->st_mode)) {
2521553Srgrimes		if (!S_ISDIR((*b)->fts_statp->st_mode))
2531553Srgrimes			return (1);
2541553Srgrimes	} else if (S_ISDIR((*b)->fts_statp->st_mode))
2551553Srgrimes		return (-1);
2561553Srgrimes	return (strcmp((*a)->fts_name, (*b)->fts_name));
2571553Srgrimes}
2581553Srgrimes
2591553Srgrimes#if __STDC__
2601553Srgrimes#include <stdarg.h>
2611553Srgrimes#else
2621553Srgrimes#include <varargs.h>
2631553Srgrimes#endif
2641553Srgrimes
2651553Srgrimesvoid
2661553Srgrimes#if __STDC__
2671553Srgrimesoutput(int *offset, const char *fmt, ...)
2681553Srgrimes#else
2691553Srgrimesoutput(offset, fmt, va_alist)
2701553Srgrimes	int *offset;
2711553Srgrimes	char *fmt;
2721553Srgrimes        va_dcl
2731553Srgrimes#endif
2741553Srgrimes{
2751553Srgrimes	va_list ap;
2761553Srgrimes	char buf[1024];
2771553Srgrimes#if __STDC__
2781553Srgrimes	va_start(ap, fmt);
2791553Srgrimes#else
2801553Srgrimes	va_start(ap);
2811553Srgrimes#endif
2821553Srgrimes	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
2831553Srgrimes	va_end(ap);
2841553Srgrimes
2851553Srgrimes	if (*offset + strlen(buf) > MAXLINELEN - 3) {
2861553Srgrimes		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
2871553Srgrimes		*offset = INDENTNAMELEN;
2881553Srgrimes	}
2891553Srgrimes	*offset += printf(" %s", buf) + 1;
2901553Srgrimes}
291