create.c revision 44303
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
3530027Scharnier#if 0
361553Srgrimesstatic char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
3730027Scharnier#endif
3830027Scharnierstatic const char rcsid[] =
3944303Swollman	"$Id: create.c,v 1.13 1999/01/18 06:58:25 jkoshy Exp $";
401553Srgrimes#endif /* not lint */
411553Srgrimes
421553Srgrimes#include <sys/param.h>
431553Srgrimes#include <sys/stat.h>
4430027Scharnier#include <dirent.h>
4530027Scharnier#include <err.h>
4630027Scharnier#include <errno.h>
471553Srgrimes#include <fcntl.h>
481553Srgrimes#include <fts.h>
491553Srgrimes#include <grp.h>
5044303Swollman#ifdef MD5
5130027Scharnier#include <md5.h>
5244303Swollman#endif
5344303Swollman#ifdef SHA1
5444303Swollman#include <sha.h>
5544303Swollman#endif
5644303Swollman#ifdef RMD160
5744303Swollman#include <ripemd.h>
5844303Swollman#endif
591553Srgrimes#include <pwd.h>
6030027Scharnier#include <stdio.h>
6130027Scharnier#include <time.h>
621553Srgrimes#include <unistd.h>
6342561Sjkoshy#include <vis.h>
641553Srgrimes#include "mtree.h"
651553Srgrimes#include "extern.h"
661553Srgrimes
671553Srgrimes#define	INDENTNAMELEN	15
681553Srgrimes#define	MAXLINELEN	80
691553Srgrimes
702860Srgrimesextern long int crc_total;
712860Srgrimesextern int ftsoptions;
722860Srgrimesextern int dflag, iflag, nflag, sflag;
7336670Speterextern u_int keys;
741553Srgrimesextern char fullpath[MAXPATHLEN];
7530027Scharnierextern int lineno;
761553Srgrimes
771553Srgrimesstatic gid_t gid;
781553Srgrimesstatic uid_t uid;
791553Srgrimesstatic mode_t mode;
801553Srgrimes
811553Srgrimesstatic int	dsort __P((const FTSENT **, const FTSENT **));
822860Srgrimesstatic void	output __P((int, int *, const char *, ...));
831553Srgrimesstatic int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *));
842860Srgrimesstatic void	statf __P((int, FTSENT *));
851553Srgrimes
861553Srgrimesvoid
871553Srgrimescwalk()
881553Srgrimes{
891553Srgrimes	register FTS *t;
901553Srgrimes	register FTSENT *p;
911553Srgrimes	time_t clock;
921553Srgrimes	char *argv[2], host[MAXHOSTNAMELEN];
932860Srgrimes	int indent = 0;
941553Srgrimes
951553Srgrimes	(void)time(&clock);
961553Srgrimes	(void)gethostname(host, sizeof(host));
971553Srgrimes	(void)printf(
981553Srgrimes	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
991553Srgrimes	    getlogin(), host, fullpath, ctime(&clock));
1001553Srgrimes
1011553Srgrimes	argv[0] = ".";
1021553Srgrimes	argv[1] = NULL;
1031553Srgrimes	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
10430027Scharnier		err(1, "line %d: fts_open", lineno);
1052860Srgrimes	while ((p = fts_read(t))) {
1062860Srgrimes		if (iflag)
1072860Srgrimes			indent = p->fts_level * 4;
1081553Srgrimes		switch(p->fts_info) {
1091553Srgrimes		case FTS_D:
1102860Srgrimes			if (!dflag)
1112860Srgrimes				(void)printf("\n");
1122860Srgrimes			if (!nflag)
1132860Srgrimes				(void)printf("# %s\n", p->fts_path);
1141553Srgrimes			statd(t, p, &uid, &gid, &mode);
1152860Srgrimes			statf(indent, p);
1161553Srgrimes			break;
1171553Srgrimes		case FTS_DP:
1182860Srgrimes			if (!nflag && (p->fts_level > 0))
1192860Srgrimes				(void)printf("%*s# %s\n", indent, "", p->fts_path);
1202860Srgrimes			(void)printf("%*s..\n", indent, "");
1212860Srgrimes			if (!dflag)
1222860Srgrimes				(void)printf("\n");
1231553Srgrimes			break;
1241553Srgrimes		case FTS_DNR:
1251553Srgrimes		case FTS_ERR:
1261553Srgrimes		case FTS_NS:
12730027Scharnier			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1281553Srgrimes			break;
1291553Srgrimes		default:
1301553Srgrimes			if (!dflag)
1312860Srgrimes				statf(indent, p);
1321553Srgrimes			break;
1338857Srgrimes
1341553Srgrimes		}
1352860Srgrimes	}
1361553Srgrimes	(void)fts_close(t);
1371553Srgrimes	if (sflag && keys & F_CKSUM)
13830027Scharnier		warnx("%s checksum: %lu", fullpath, crc_total);
1391553Srgrimes}
1401553Srgrimes
1411553Srgrimesstatic void
1422860Srgrimesstatf(indent, p)
1432860Srgrimes	int indent;
1441553Srgrimes	FTSENT *p;
1451553Srgrimes{
1461553Srgrimes	struct group *gr;
1471553Srgrimes	struct passwd *pw;
1481553Srgrimes	u_long len, val;
1492860Srgrimes	int fd, offset;
15042561Sjkoshy	char *escaped_name;
1511553Srgrimes
15242561Sjkoshy	escaped_name = calloc(1, p->fts_namelen * 4  +  1);
15342561Sjkoshy	if (escaped_name == NULL)
15442561Sjkoshy		errx(1, "statf(): calloc() failed");
15542787Sjkoshy	strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL);
15642561Sjkoshy
1572860Srgrimes	if (iflag || S_ISDIR(p->fts_statp->st_mode))
15842561Sjkoshy		offset = printf("%*s%s", indent, "", escaped_name);
1591553Srgrimes	else
16042561Sjkoshy		offset = printf("%*s    %s", indent, "", escaped_name);
16142561Sjkoshy
16242561Sjkoshy	free(escaped_name);
1631553Srgrimes
1642860Srgrimes	if (offset > (INDENTNAMELEN + indent))
1652860Srgrimes		offset = MAXLINELEN;
1661553Srgrimes	else
1672860Srgrimes		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
1681553Srgrimes
1692860Srgrimes	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
1702860Srgrimes		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
1712860Srgrimes	if (p->fts_statp->st_uid != uid) {
1722860Srgrimes		if (keys & F_UNAME) {
1732860Srgrimes			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
1742860Srgrimes				output(indent, &offset, "uname=%s", pw->pw_name);
1752860Srgrimes			} else {
17630027Scharnier				errx(1,
17730027Scharnier				"line %d: could not get uname for uid=%u",
17830027Scharnier				lineno, p->fts_statp->st_uid);
1792860Srgrimes			}
1802860Srgrimes		}
1812860Srgrimes		if (keys & F_UID)
1822860Srgrimes			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
1832860Srgrimes	}
1842860Srgrimes	if (p->fts_statp->st_gid != gid) {
1852860Srgrimes		if (keys & F_GNAME) {
1862860Srgrimes			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
1872860Srgrimes				output(indent, &offset, "gname=%s", gr->gr_name);
1882860Srgrimes			} else {
18930027Scharnier				errx(1,
19030027Scharnier				"line %d: could not get gname for gid=%u",
19130027Scharnier				lineno, p->fts_statp->st_gid);
1922860Srgrimes			}
1932860Srgrimes		}
1942860Srgrimes		if (keys & F_GID)
1952860Srgrimes			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
1962860Srgrimes	}
1971553Srgrimes	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
1982860Srgrimes		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
1991553Srgrimes	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
2002860Srgrimes		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
2011553Srgrimes	if (keys & F_SIZE)
2022860Srgrimes		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
2031553Srgrimes	if (keys & F_TIME)
2042860Srgrimes		output(indent, &offset, "time=%ld.%ld",
20518404Snate		    p->fts_statp->st_mtimespec.tv_sec,
20618404Snate		    p->fts_statp->st_mtimespec.tv_nsec);
2071553Srgrimes	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
2081553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
2091553Srgrimes		    crc(fd, &val, &len))
21030027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
2111553Srgrimes		(void)close(fd);
2122860Srgrimes		output(indent, &offset, "cksum=%lu", val);
2131553Srgrimes	}
21444303Swollman#ifdef MD5
2156286Swollman	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
21644303Swollman		char *digest, buf[33];
2176286Swollman
21844303Swollman		digest = MD5File(p->fts_accpath, buf);
21944303Swollman		if (!digest) {
22030027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
2216286Swollman		} else {
22244303Swollman			output(indent, &offset, "md5digest=%s", digest);
2236286Swollman		}
2246286Swollman	}
22544303Swollman#endif /* MD5 */
22644303Swollman#ifdef SHA1
22744303Swollman	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
22844303Swollman		char *digest, buf[41];
22944303Swollman
23044303Swollman		digest = SHA1_File(p->fts_accpath, buf);
23144303Swollman		if (!digest) {
23244303Swollman			err(1, "line %d: %s", lineno, p->fts_accpath);
23344303Swollman		} else {
23444303Swollman			output(indent, &offset, "sha1digest=%s", digest);
23544303Swollman		}
23644303Swollman	}
23744303Swollman#endif /* SHA1 */
23844303Swollman#ifdef RMD160
23944303Swollman	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
24044303Swollman		char *digest, buf[41];
24144303Swollman
24244303Swollman		digest = RIPEMD160_File(p->fts_accpath, buf);
24344303Swollman		if (!digest) {
24444303Swollman			err(1, "line %d: %s", lineno, p->fts_accpath);
24544303Swollman		} else {
24644303Swollman			output(indent, &offset, "ripemd160digest=%s", digest);
24744303Swollman		}
24844303Swollman	}
24944303Swollman#endif /* RMD160 */
2501553Srgrimes	if (keys & F_SLINK &&
2511553Srgrimes	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
2522860Srgrimes		output(indent, &offset, "link=%s", rlink(p->fts_accpath));
2531553Srgrimes	(void)putchar('\n');
2541553Srgrimes}
2551553Srgrimes
2561553Srgrimes#define	MAXGID	5000
2571553Srgrimes#define	MAXUID	5000
2581553Srgrimes#define	MAXMODE	MBITS + 1
2591553Srgrimes
2601553Srgrimesstatic int
2611553Srgrimesstatd(t, parent, puid, pgid, pmode)
2621553Srgrimes	FTS *t;
2631553Srgrimes	FTSENT *parent;
2641553Srgrimes	uid_t *puid;
2651553Srgrimes	gid_t *pgid;
2661553Srgrimes	mode_t *pmode;
2671553Srgrimes{
2681553Srgrimes	register FTSENT *p;
2691553Srgrimes	register gid_t sgid;
2701553Srgrimes	register uid_t suid;
2711553Srgrimes	register mode_t smode;
2721553Srgrimes	struct group *gr;
2731553Srgrimes	struct passwd *pw;
2742860Srgrimes	gid_t savegid = *pgid;
2752860Srgrimes	uid_t saveuid = *puid;
2762860Srgrimes	mode_t savemode = *pmode;
2771553Srgrimes	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
2782877Srgrimes	static int first = 1;
2791553Srgrimes
2801553Srgrimes	if ((p = fts_children(t, 0)) == NULL) {
2811553Srgrimes		if (errno)
28230027Scharnier			err(1, "line %d: %s", lineno, RP(parent));
2831553Srgrimes		return (1);
2841553Srgrimes	}
2851553Srgrimes
2861553Srgrimes	bzero(g, sizeof(g));
2871553Srgrimes	bzero(u, sizeof(u));
2881553Srgrimes	bzero(m, sizeof(m));
2891553Srgrimes
2901553Srgrimes	maxuid = maxgid = maxmode = 0;
2911553Srgrimes	for (; p; p = p->fts_link) {
2922860Srgrimes		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
2932860Srgrimes			smode = p->fts_statp->st_mode & MBITS;
2942860Srgrimes			if (smode < MAXMODE && ++m[smode] > maxmode) {
2952860Srgrimes				savemode = smode;
2962860Srgrimes				maxmode = m[smode];
2972860Srgrimes			}
2982860Srgrimes			sgid = p->fts_statp->st_gid;
2992860Srgrimes			if (sgid < MAXGID && ++g[sgid] > maxgid) {
3002860Srgrimes				savegid = sgid;
3012860Srgrimes				maxgid = g[sgid];
3022860Srgrimes			}
3032860Srgrimes			suid = p->fts_statp->st_uid;
3042860Srgrimes			if (suid < MAXUID && ++u[suid] > maxuid) {
3052860Srgrimes				saveuid = suid;
3062860Srgrimes				maxuid = u[suid];
3072860Srgrimes			}
3081553Srgrimes		}
3091553Srgrimes	}
3102860Srgrimes	/*
3112860Srgrimes	 * If the /set record is the same as the last one we do not need to output
3122877Srgrimes	 * a new one.  So first we check to see if anything changed.  Note that we
3132877Srgrimes	 * always output a /set record for the first directory.
3142860Srgrimes	 */
3152860Srgrimes	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
3162860Srgrimes	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
3172877Srgrimes	    ((keys & F_MODE) && (*pmode != savemode)) || (first)) {
3182877Srgrimes		first = 0;
3192860Srgrimes		if (dflag)
3202860Srgrimes			(void)printf("/set type=dir");
3211553Srgrimes		else
3222860Srgrimes			(void)printf("/set type=file");
3232860Srgrimes		if (keys & F_UNAME)
3242860Srgrimes			if ((pw = getpwuid(saveuid)) != NULL)
3252860Srgrimes				(void)printf(" uname=%s", pw->pw_name);
3262860Srgrimes			else
32730027Scharnier				errx(1,
32830027Scharnier				"line %d: could not get uname for uid=%u",
32930027Scharnier				lineno, saveuid);
3302860Srgrimes		if (keys & F_UID)
33138020Sbde			(void)printf(" uid=%lu", (u_long)saveuid);
3322860Srgrimes		if (keys & F_GNAME)
3332860Srgrimes			if ((gr = getgrgid(savegid)) != NULL)
3342860Srgrimes				(void)printf(" gname=%s", gr->gr_name);
3352860Srgrimes			else
33630027Scharnier				errx(1,
33730027Scharnier				"line %d: could not get gname for gid=%u",
33830027Scharnier				lineno, savegid);
3392860Srgrimes		if (keys & F_GID)
34038020Sbde			(void)printf(" gid=%lu", (u_long)savegid);
3412860Srgrimes		if (keys & F_MODE)
3422860Srgrimes			(void)printf(" mode=%#o", savemode);
3432860Srgrimes		if (keys & F_NLINK)
3442860Srgrimes			(void)printf(" nlink=1");
3452860Srgrimes		(void)printf("\n");
3462860Srgrimes		*puid = saveuid;
3472860Srgrimes		*pgid = savegid;
3482860Srgrimes		*pmode = savemode;
3492860Srgrimes	}
3501553Srgrimes	return (0);
3511553Srgrimes}
3521553Srgrimes
3531553Srgrimesstatic int
3541553Srgrimesdsort(a, b)
3551553Srgrimes	const FTSENT **a, **b;
3561553Srgrimes{
3571553Srgrimes	if (S_ISDIR((*a)->fts_statp->st_mode)) {
3581553Srgrimes		if (!S_ISDIR((*b)->fts_statp->st_mode))
3591553Srgrimes			return (1);
3601553Srgrimes	} else if (S_ISDIR((*b)->fts_statp->st_mode))
3611553Srgrimes		return (-1);
3621553Srgrimes	return (strcmp((*a)->fts_name, (*b)->fts_name));
3631553Srgrimes}
3641553Srgrimes
3651553Srgrimes#if __STDC__
3661553Srgrimes#include <stdarg.h>
3671553Srgrimes#else
3681553Srgrimes#include <varargs.h>
3691553Srgrimes#endif
3701553Srgrimes
3711553Srgrimesvoid
3721553Srgrimes#if __STDC__
3732860Srgrimesoutput(int indent, int *offset, const char *fmt, ...)
3741553Srgrimes#else
3752860Srgrimesoutput(indent, offset, fmt, va_alist)
3762860Srgrimes	int indent;
3771553Srgrimes	int *offset;
3781553Srgrimes	char *fmt;
3791553Srgrimes        va_dcl
3801553Srgrimes#endif
3811553Srgrimes{
3821553Srgrimes	va_list ap;
3831553Srgrimes	char buf[1024];
3841553Srgrimes#if __STDC__
3851553Srgrimes	va_start(ap, fmt);
3861553Srgrimes#else
3871553Srgrimes	va_start(ap);
3881553Srgrimes#endif
3891553Srgrimes	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3901553Srgrimes	va_end(ap);
3911553Srgrimes
3921553Srgrimes	if (*offset + strlen(buf) > MAXLINELEN - 3) {
3932860Srgrimes		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
3942860Srgrimes		*offset = INDENTNAMELEN + indent;
3951553Srgrimes	}
3961553Srgrimes	*offset += printf(" %s", buf) + 1;
3971553Srgrimes}
398