create.c revision 30027
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[] =
3930027Scharnier	"$Id$";
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>
5030027Scharnier#include <md5.h>
511553Srgrimes#include <pwd.h>
5230027Scharnier#include <stdio.h>
5330027Scharnier#include <time.h>
541553Srgrimes#include <unistd.h>
551553Srgrimes#include "mtree.h"
561553Srgrimes#include "extern.h"
571553Srgrimes
581553Srgrimes#define	INDENTNAMELEN	15
591553Srgrimes#define	MAXLINELEN	80
601553Srgrimes
612860Srgrimesextern long int crc_total;
622860Srgrimesextern int ftsoptions;
632860Srgrimesextern int dflag, iflag, nflag, sflag;
641553Srgrimesextern u_short keys;
651553Srgrimesextern char fullpath[MAXPATHLEN];
6630027Scharnierextern int lineno;
671553Srgrimes
681553Srgrimesstatic gid_t gid;
691553Srgrimesstatic uid_t uid;
701553Srgrimesstatic mode_t mode;
711553Srgrimes
721553Srgrimesstatic int	dsort __P((const FTSENT **, const FTSENT **));
732860Srgrimesstatic void	output __P((int, int *, const char *, ...));
741553Srgrimesstatic int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *));
752860Srgrimesstatic void	statf __P((int, FTSENT *));
761553Srgrimes
771553Srgrimesvoid
781553Srgrimescwalk()
791553Srgrimes{
801553Srgrimes	register FTS *t;
811553Srgrimes	register FTSENT *p;
821553Srgrimes	time_t clock;
831553Srgrimes	char *argv[2], host[MAXHOSTNAMELEN];
842860Srgrimes	int indent = 0;
851553Srgrimes
861553Srgrimes	(void)time(&clock);
871553Srgrimes	(void)gethostname(host, sizeof(host));
881553Srgrimes	(void)printf(
891553Srgrimes	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
901553Srgrimes	    getlogin(), host, fullpath, ctime(&clock));
911553Srgrimes
921553Srgrimes	argv[0] = ".";
931553Srgrimes	argv[1] = NULL;
941553Srgrimes	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
9530027Scharnier		err(1, "line %d: fts_open", lineno);
962860Srgrimes	while ((p = fts_read(t))) {
972860Srgrimes		if (iflag)
982860Srgrimes			indent = p->fts_level * 4;
991553Srgrimes		switch(p->fts_info) {
1001553Srgrimes		case FTS_D:
1012860Srgrimes			if (!dflag)
1022860Srgrimes				(void)printf("\n");
1032860Srgrimes			if (!nflag)
1042860Srgrimes				(void)printf("# %s\n", p->fts_path);
1051553Srgrimes			statd(t, p, &uid, &gid, &mode);
1062860Srgrimes			statf(indent, p);
1071553Srgrimes			break;
1081553Srgrimes		case FTS_DP:
1092860Srgrimes			if (!nflag && (p->fts_level > 0))
1102860Srgrimes				(void)printf("%*s# %s\n", indent, "", p->fts_path);
1112860Srgrimes			(void)printf("%*s..\n", indent, "");
1122860Srgrimes			if (!dflag)
1132860Srgrimes				(void)printf("\n");
1141553Srgrimes			break;
1151553Srgrimes		case FTS_DNR:
1161553Srgrimes		case FTS_ERR:
1171553Srgrimes		case FTS_NS:
11830027Scharnier			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1191553Srgrimes			break;
1201553Srgrimes		default:
1211553Srgrimes			if (!dflag)
1222860Srgrimes				statf(indent, p);
1231553Srgrimes			break;
1248857Srgrimes
1251553Srgrimes		}
1262860Srgrimes	}
1271553Srgrimes	(void)fts_close(t);
1281553Srgrimes	if (sflag && keys & F_CKSUM)
12930027Scharnier		warnx("%s checksum: %lu", fullpath, crc_total);
1301553Srgrimes}
1311553Srgrimes
1321553Srgrimesstatic void
1332860Srgrimesstatf(indent, p)
1342860Srgrimes	int indent;
1351553Srgrimes	FTSENT *p;
1361553Srgrimes{
1371553Srgrimes	struct group *gr;
1381553Srgrimes	struct passwd *pw;
1391553Srgrimes	u_long len, val;
1402860Srgrimes	int fd, offset;
1411553Srgrimes
1422860Srgrimes	if (iflag || S_ISDIR(p->fts_statp->st_mode))
1432860Srgrimes		offset = printf("%*s%s", indent, "", p->fts_name);
1441553Srgrimes	else
1452860Srgrimes		offset = printf("%*s    %s", indent, "", p->fts_name);
1461553Srgrimes
1472860Srgrimes	if (offset > (INDENTNAMELEN + indent))
1482860Srgrimes		offset = MAXLINELEN;
1491553Srgrimes	else
1502860Srgrimes		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
1511553Srgrimes
1522860Srgrimes	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
1532860Srgrimes		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
1542860Srgrimes	if (p->fts_statp->st_uid != uid) {
1552860Srgrimes		if (keys & F_UNAME) {
1562860Srgrimes			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
1572860Srgrimes				output(indent, &offset, "uname=%s", pw->pw_name);
1582860Srgrimes			} else {
15930027Scharnier				errx(1,
16030027Scharnier				"line %d: could not get uname for uid=%u",
16130027Scharnier				lineno, p->fts_statp->st_uid);
1622860Srgrimes			}
1632860Srgrimes		}
1642860Srgrimes		if (keys & F_UID)
1652860Srgrimes			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
1662860Srgrimes	}
1672860Srgrimes	if (p->fts_statp->st_gid != gid) {
1682860Srgrimes		if (keys & F_GNAME) {
1692860Srgrimes			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
1702860Srgrimes				output(indent, &offset, "gname=%s", gr->gr_name);
1712860Srgrimes			} else {
17230027Scharnier				errx(1,
17330027Scharnier				"line %d: could not get gname for gid=%u",
17430027Scharnier				lineno, p->fts_statp->st_gid);
1752860Srgrimes			}
1762860Srgrimes		}
1772860Srgrimes		if (keys & F_GID)
1782860Srgrimes			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
1792860Srgrimes	}
1801553Srgrimes	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
1812860Srgrimes		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
1821553Srgrimes	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
1832860Srgrimes		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
1841553Srgrimes	if (keys & F_SIZE)
1852860Srgrimes		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
1861553Srgrimes	if (keys & F_TIME)
1872860Srgrimes		output(indent, &offset, "time=%ld.%ld",
18818404Snate		    p->fts_statp->st_mtimespec.tv_sec,
18918404Snate		    p->fts_statp->st_mtimespec.tv_nsec);
1901553Srgrimes	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
1911553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
1921553Srgrimes		    crc(fd, &val, &len))
19330027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
1941553Srgrimes		(void)close(fd);
1952860Srgrimes		output(indent, &offset, "cksum=%lu", val);
1961553Srgrimes	}
1976286Swollman	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
1989490Sphk		char *md5digest, buf[33];
1996286Swollman
2009490Sphk		md5digest = MD5File(p->fts_accpath,buf);
2016286Swollman		if (!md5digest) {
20230027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
2036286Swollman		} else {
2046286Swollman			output(indent, &offset, "md5digest=%s", md5digest);
2056286Swollman		}
2066286Swollman	}
2071553Srgrimes	if (keys & F_SLINK &&
2081553Srgrimes	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
2092860Srgrimes		output(indent, &offset, "link=%s", rlink(p->fts_accpath));
2101553Srgrimes	(void)putchar('\n');
2111553Srgrimes}
2121553Srgrimes
2131553Srgrimes#define	MAXGID	5000
2141553Srgrimes#define	MAXUID	5000
2151553Srgrimes#define	MAXMODE	MBITS + 1
2161553Srgrimes
2171553Srgrimesstatic int
2181553Srgrimesstatd(t, parent, puid, pgid, pmode)
2191553Srgrimes	FTS *t;
2201553Srgrimes	FTSENT *parent;
2211553Srgrimes	uid_t *puid;
2221553Srgrimes	gid_t *pgid;
2231553Srgrimes	mode_t *pmode;
2241553Srgrimes{
2251553Srgrimes	register FTSENT *p;
2261553Srgrimes	register gid_t sgid;
2271553Srgrimes	register uid_t suid;
2281553Srgrimes	register mode_t smode;
2291553Srgrimes	struct group *gr;
2301553Srgrimes	struct passwd *pw;
2312860Srgrimes	gid_t savegid = *pgid;
2322860Srgrimes	uid_t saveuid = *puid;
2332860Srgrimes	mode_t savemode = *pmode;
2341553Srgrimes	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
2352877Srgrimes	static int first = 1;
2361553Srgrimes
2371553Srgrimes	if ((p = fts_children(t, 0)) == NULL) {
2381553Srgrimes		if (errno)
23930027Scharnier			err(1, "line %d: %s", lineno, RP(parent));
2401553Srgrimes		return (1);
2411553Srgrimes	}
2421553Srgrimes
2431553Srgrimes	bzero(g, sizeof(g));
2441553Srgrimes	bzero(u, sizeof(u));
2451553Srgrimes	bzero(m, sizeof(m));
2461553Srgrimes
2471553Srgrimes	maxuid = maxgid = maxmode = 0;
2481553Srgrimes	for (; p; p = p->fts_link) {
2492860Srgrimes		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
2502860Srgrimes			smode = p->fts_statp->st_mode & MBITS;
2512860Srgrimes			if (smode < MAXMODE && ++m[smode] > maxmode) {
2522860Srgrimes				savemode = smode;
2532860Srgrimes				maxmode = m[smode];
2542860Srgrimes			}
2552860Srgrimes			sgid = p->fts_statp->st_gid;
2562860Srgrimes			if (sgid < MAXGID && ++g[sgid] > maxgid) {
2572860Srgrimes				savegid = sgid;
2582860Srgrimes				maxgid = g[sgid];
2592860Srgrimes			}
2602860Srgrimes			suid = p->fts_statp->st_uid;
2612860Srgrimes			if (suid < MAXUID && ++u[suid] > maxuid) {
2622860Srgrimes				saveuid = suid;
2632860Srgrimes				maxuid = u[suid];
2642860Srgrimes			}
2651553Srgrimes		}
2661553Srgrimes	}
2672860Srgrimes	/*
2682860Srgrimes	 * If the /set record is the same as the last one we do not need to output
2692877Srgrimes	 * a new one.  So first we check to see if anything changed.  Note that we
2702877Srgrimes	 * always output a /set record for the first directory.
2712860Srgrimes	 */
2722860Srgrimes	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
2732860Srgrimes	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
2742877Srgrimes	    ((keys & F_MODE) && (*pmode != savemode)) || (first)) {
2752877Srgrimes		first = 0;
2762860Srgrimes		if (dflag)
2772860Srgrimes			(void)printf("/set type=dir");
2781553Srgrimes		else
2792860Srgrimes			(void)printf("/set type=file");
2802860Srgrimes		if (keys & F_UNAME)
2812860Srgrimes			if ((pw = getpwuid(saveuid)) != NULL)
2822860Srgrimes				(void)printf(" uname=%s", pw->pw_name);
2832860Srgrimes			else
28430027Scharnier				errx(1,
28530027Scharnier				"line %d: could not get uname for uid=%u",
28630027Scharnier				lineno, saveuid);
2872860Srgrimes		if (keys & F_UID)
2882860Srgrimes			(void)printf(" uid=%lu", saveuid);
2892860Srgrimes		if (keys & F_GNAME)
2902860Srgrimes			if ((gr = getgrgid(savegid)) != NULL)
2912860Srgrimes				(void)printf(" gname=%s", gr->gr_name);
2922860Srgrimes			else
29330027Scharnier				errx(1,
29430027Scharnier				"line %d: could not get gname for gid=%u",
29530027Scharnier				lineno, savegid);
2962860Srgrimes		if (keys & F_GID)
2972860Srgrimes			(void)printf(" gid=%lu", savegid);
2982860Srgrimes		if (keys & F_MODE)
2992860Srgrimes			(void)printf(" mode=%#o", savemode);
3002860Srgrimes		if (keys & F_NLINK)
3012860Srgrimes			(void)printf(" nlink=1");
3022860Srgrimes		(void)printf("\n");
3032860Srgrimes		*puid = saveuid;
3042860Srgrimes		*pgid = savegid;
3052860Srgrimes		*pmode = savemode;
3062860Srgrimes	}
3071553Srgrimes	return (0);
3081553Srgrimes}
3091553Srgrimes
3101553Srgrimesstatic int
3111553Srgrimesdsort(a, b)
3121553Srgrimes	const FTSENT **a, **b;
3131553Srgrimes{
3141553Srgrimes	if (S_ISDIR((*a)->fts_statp->st_mode)) {
3151553Srgrimes		if (!S_ISDIR((*b)->fts_statp->st_mode))
3161553Srgrimes			return (1);
3171553Srgrimes	} else if (S_ISDIR((*b)->fts_statp->st_mode))
3181553Srgrimes		return (-1);
3191553Srgrimes	return (strcmp((*a)->fts_name, (*b)->fts_name));
3201553Srgrimes}
3211553Srgrimes
3221553Srgrimes#if __STDC__
3231553Srgrimes#include <stdarg.h>
3241553Srgrimes#else
3251553Srgrimes#include <varargs.h>
3261553Srgrimes#endif
3271553Srgrimes
3281553Srgrimesvoid
3291553Srgrimes#if __STDC__
3302860Srgrimesoutput(int indent, int *offset, const char *fmt, ...)
3311553Srgrimes#else
3322860Srgrimesoutput(indent, offset, fmt, va_alist)
3332860Srgrimes	int indent;
3341553Srgrimes	int *offset;
3351553Srgrimes	char *fmt;
3361553Srgrimes        va_dcl
3371553Srgrimes#endif
3381553Srgrimes{
3391553Srgrimes	va_list ap;
3401553Srgrimes	char buf[1024];
3411553Srgrimes#if __STDC__
3421553Srgrimes	va_start(ap, fmt);
3431553Srgrimes#else
3441553Srgrimes	va_start(ap);
3451553Srgrimes#endif
3461553Srgrimes	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
3471553Srgrimes	va_end(ap);
3481553Srgrimes
3491553Srgrimes	if (*offset + strlen(buf) > MAXLINELEN - 3) {
3502860Srgrimes		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
3512860Srgrimes		*offset = INDENTNAMELEN + indent;
3521553Srgrimes	}
3531553Srgrimes	*offset += printf(" %s", buf) + 1;
3541553Srgrimes}
355