create.c revision 99802
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[] =
3950479Speter  "$FreeBSD: head/usr.sbin/mtree/create.c 99802 2002-07-11 18:42:53Z alfred $";
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;
8066584Sphkstatic u_long flags = 0xffffffff;
811553Srgrimes
8299800Salfredstatic int	dsort(const FTSENT **, const FTSENT **);
8399800Salfredstatic void	output(int, int *, const char *, ...) __printflike(3, 4);
8499800Salfredstatic int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
8599800Salfredstatic void	statf(int, FTSENT *);
861553Srgrimes
871553Srgrimesvoid
881553Srgrimescwalk()
891553Srgrimes{
901553Srgrimes	register FTS *t;
911553Srgrimes	register FTSENT *p;
9299802Salfred	time_t cl;
931553Srgrimes	char *argv[2], host[MAXHOSTNAMELEN];
9499802Salfred	char dot[] = ".";
952860Srgrimes	int indent = 0;
961553Srgrimes
9799802Salfred	(void)time(&cl);
981553Srgrimes	(void)gethostname(host, sizeof(host));
991553Srgrimes	(void)printf(
1001553Srgrimes	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
10199802Salfred	    getlogin(), host, fullpath, ctime(&cl));
1021553Srgrimes
10399802Salfred	argv[0] = dot;
1041553Srgrimes	argv[1] = NULL;
1051553Srgrimes	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
10630027Scharnier		err(1, "line %d: fts_open", lineno);
1072860Srgrimes	while ((p = fts_read(t))) {
1082860Srgrimes		if (iflag)
1092860Srgrimes			indent = p->fts_level * 4;
11060418Swollman		if (check_excludes(p->fts_name, p->fts_path)) {
11160418Swollman			fts_set(t, p, FTS_SKIP);
11260418Swollman			continue;
11360418Swollman		}
1141553Srgrimes		switch(p->fts_info) {
1151553Srgrimes		case FTS_D:
1162860Srgrimes			if (!dflag)
1172860Srgrimes				(void)printf("\n");
1182860Srgrimes			if (!nflag)
1192860Srgrimes				(void)printf("# %s\n", p->fts_path);
12054375Sjoe			statd(t, p, &uid, &gid, &mode, &flags);
1212860Srgrimes			statf(indent, p);
1221553Srgrimes			break;
1231553Srgrimes		case FTS_DP:
1242860Srgrimes			if (!nflag && (p->fts_level > 0))
1252860Srgrimes				(void)printf("%*s# %s\n", indent, "", p->fts_path);
1262860Srgrimes			(void)printf("%*s..\n", indent, "");
1272860Srgrimes			if (!dflag)
1282860Srgrimes				(void)printf("\n");
1291553Srgrimes			break;
1301553Srgrimes		case FTS_DNR:
1311553Srgrimes		case FTS_ERR:
1321553Srgrimes		case FTS_NS:
13330027Scharnier			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1341553Srgrimes			break;
1351553Srgrimes		default:
1361553Srgrimes			if (!dflag)
1372860Srgrimes				statf(indent, p);
1381553Srgrimes			break;
1398857Srgrimes
1401553Srgrimes		}
1412860Srgrimes	}
1421553Srgrimes	(void)fts_close(t);
1431553Srgrimes	if (sflag && keys & F_CKSUM)
14430027Scharnier		warnx("%s checksum: %lu", fullpath, crc_total);
1451553Srgrimes}
1461553Srgrimes
1471553Srgrimesstatic void
1482860Srgrimesstatf(indent, p)
1492860Srgrimes	int indent;
1501553Srgrimes	FTSENT *p;
1511553Srgrimes{
1521553Srgrimes	struct group *gr;
1531553Srgrimes	struct passwd *pw;
1541553Srgrimes	u_long len, val;
1552860Srgrimes	int fd, offset;
15661749Sjoe	char *fflags;
15742561Sjkoshy	char *escaped_name;
1581553Srgrimes
15942561Sjkoshy	escaped_name = calloc(1, p->fts_namelen * 4  +  1);
16042561Sjkoshy	if (escaped_name == NULL)
16142561Sjkoshy		errx(1, "statf(): calloc() failed");
16242787Sjkoshy	strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL);
16342561Sjkoshy
1642860Srgrimes	if (iflag || S_ISDIR(p->fts_statp->st_mode))
16542561Sjkoshy		offset = printf("%*s%s", indent, "", escaped_name);
1661553Srgrimes	else
16742561Sjkoshy		offset = printf("%*s    %s", indent, "", escaped_name);
16842561Sjkoshy
16942561Sjkoshy	free(escaped_name);
1701553Srgrimes
1712860Srgrimes	if (offset > (INDENTNAMELEN + indent))
1722860Srgrimes		offset = MAXLINELEN;
1731553Srgrimes	else
1742860Srgrimes		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
1751553Srgrimes
1762860Srgrimes	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
1772860Srgrimes		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
1782860Srgrimes	if (p->fts_statp->st_uid != uid) {
1792860Srgrimes		if (keys & F_UNAME) {
1802860Srgrimes			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
1812860Srgrimes				output(indent, &offset, "uname=%s", pw->pw_name);
1822860Srgrimes			} else {
18330027Scharnier				errx(1,
18430027Scharnier				"line %d: could not get uname for uid=%u",
18530027Scharnier				lineno, p->fts_statp->st_uid);
1862860Srgrimes			}
1872860Srgrimes		}
1882860Srgrimes		if (keys & F_UID)
1892860Srgrimes			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
1902860Srgrimes	}
1912860Srgrimes	if (p->fts_statp->st_gid != gid) {
1922860Srgrimes		if (keys & F_GNAME) {
1932860Srgrimes			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
1942860Srgrimes				output(indent, &offset, "gname=%s", gr->gr_name);
1952860Srgrimes			} else {
19630027Scharnier				errx(1,
19730027Scharnier				"line %d: could not get gname for gid=%u",
19830027Scharnier				lineno, p->fts_statp->st_gid);
1992860Srgrimes			}
2002860Srgrimes		}
2012860Srgrimes		if (keys & F_GID)
2022860Srgrimes			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
2032860Srgrimes	}
2041553Srgrimes	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
2052860Srgrimes		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
2061553Srgrimes	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
2072860Srgrimes		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
2081553Srgrimes	if (keys & F_SIZE)
2092860Srgrimes		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
2101553Srgrimes	if (keys & F_TIME)
2112860Srgrimes		output(indent, &offset, "time=%ld.%ld",
21299802Salfred		    (long)p->fts_statp->st_mtimespec.tv_sec,
21318404Snate		    p->fts_statp->st_mtimespec.tv_nsec);
2141553Srgrimes	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
2151553Srgrimes		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
2161553Srgrimes		    crc(fd, &val, &len))
21730027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
2181553Srgrimes		(void)close(fd);
2192860Srgrimes		output(indent, &offset, "cksum=%lu", val);
2201553Srgrimes	}
22144303Swollman#ifdef MD5
2226286Swollman	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
22344303Swollman		char *digest, buf[33];
2246286Swollman
22544303Swollman		digest = MD5File(p->fts_accpath, buf);
22644303Swollman		if (!digest) {
22730027Scharnier			err(1, "line %d: %s", lineno, p->fts_accpath);
2286286Swollman		} else {
22944303Swollman			output(indent, &offset, "md5digest=%s", digest);
2306286Swollman		}
2316286Swollman	}
23244303Swollman#endif /* MD5 */
23344303Swollman#ifdef SHA1
23444303Swollman	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
23544303Swollman		char *digest, buf[41];
23644303Swollman
23744303Swollman		digest = SHA1_File(p->fts_accpath, buf);
23844303Swollman		if (!digest) {
23944303Swollman			err(1, "line %d: %s", lineno, p->fts_accpath);
24044303Swollman		} else {
24144303Swollman			output(indent, &offset, "sha1digest=%s", digest);
24244303Swollman		}
24344303Swollman	}
24444303Swollman#endif /* SHA1 */
24544303Swollman#ifdef RMD160
24644303Swollman	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
24744303Swollman		char *digest, buf[41];
24844303Swollman
24944303Swollman		digest = RIPEMD160_File(p->fts_accpath, buf);
25044303Swollman		if (!digest) {
25144303Swollman			err(1, "line %d: %s", lineno, p->fts_accpath);
25244303Swollman		} else {
25344303Swollman			output(indent, &offset, "ripemd160digest=%s", digest);
25444303Swollman		}
25544303Swollman	}
25644303Swollman#endif /* RMD160 */
2571553Srgrimes	if (keys & F_SLINK &&
2581553Srgrimes	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
2592860Srgrimes		output(indent, &offset, "link=%s", rlink(p->fts_accpath));
26061749Sjoe	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
26161749Sjoe		fflags = flags_to_string(p->fts_statp->st_flags);
26261749Sjoe		output(indent, &offset, "flags=%s", fflags);
26361749Sjoe		free(fflags);
26461749Sjoe	}
2651553Srgrimes	(void)putchar('\n');
2661553Srgrimes}
2671553Srgrimes
2681553Srgrimes#define	MAXGID	5000
2691553Srgrimes#define	MAXUID	5000
2701553Srgrimes#define	MAXMODE	MBITS + 1
27154375Sjoe#define	MAXFLAGS 256
27254375Sjoe#define	MAXS 16
2731553Srgrimes
2741553Srgrimesstatic int
27554375Sjoestatd(t, parent, puid, pgid, pmode, pflags)
2761553Srgrimes	FTS *t;
2771553Srgrimes	FTSENT *parent;
2781553Srgrimes	uid_t *puid;
2791553Srgrimes	gid_t *pgid;
2801553Srgrimes	mode_t *pmode;
28154375Sjoe	u_long *pflags;
2821553Srgrimes{
2831553Srgrimes	register FTSENT *p;
2841553Srgrimes	register gid_t sgid;
2851553Srgrimes	register uid_t suid;
2861553Srgrimes	register mode_t smode;
28754375Sjoe	register u_long sflags;
2881553Srgrimes	struct group *gr;
2891553Srgrimes	struct passwd *pw;
2902860Srgrimes	gid_t savegid = *pgid;
2912860Srgrimes	uid_t saveuid = *puid;
2922860Srgrimes	mode_t savemode = *pmode;
29366584Sphk	u_long saveflags = *pflags;
29454375Sjoe	u_short maxgid, maxuid, maxmode, maxflags;
29554375Sjoe	u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
29661749Sjoe	char *fflags;
2972877Srgrimes	static int first = 1;
2981553Srgrimes
2991553Srgrimes	if ((p = fts_children(t, 0)) == NULL) {
3001553Srgrimes		if (errno)
30130027Scharnier			err(1, "line %d: %s", lineno, RP(parent));
3021553Srgrimes		return (1);
3031553Srgrimes	}
3041553Srgrimes
3051553Srgrimes	bzero(g, sizeof(g));
3061553Srgrimes	bzero(u, sizeof(u));
3071553Srgrimes	bzero(m, sizeof(m));
30854375Sjoe	bzero(f, sizeof(f));
3091553Srgrimes
31054375Sjoe	maxuid = maxgid = maxmode = maxflags = 0;
3111553Srgrimes	for (; p; p = p->fts_link) {
3122860Srgrimes		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
3132860Srgrimes			smode = p->fts_statp->st_mode & MBITS;
3142860Srgrimes			if (smode < MAXMODE && ++m[smode] > maxmode) {
3152860Srgrimes				savemode = smode;
3162860Srgrimes				maxmode = m[smode];
3172860Srgrimes			}
3182860Srgrimes			sgid = p->fts_statp->st_gid;
3192860Srgrimes			if (sgid < MAXGID && ++g[sgid] > maxgid) {
3202860Srgrimes				savegid = sgid;
3212860Srgrimes				maxgid = g[sgid];
3222860Srgrimes			}
3232860Srgrimes			suid = p->fts_statp->st_uid;
3242860Srgrimes			if (suid < MAXUID && ++u[suid] > maxuid) {
3252860Srgrimes				saveuid = suid;
3262860Srgrimes				maxuid = u[suid];
3272860Srgrimes			}
32854375Sjoe
32954375Sjoe			/*
33054375Sjoe			 * XXX
33154375Sjoe			 * note that the below will break when file flags
33254375Sjoe			 * are extended beyond the first 4 bytes of each
33354375Sjoe			 * half word of the flags
33454375Sjoe			 */
33554375Sjoe#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
33654375Sjoe			sflags = p->fts_statp->st_flags;
33754375Sjoe			if (FLAGS2IDX(sflags) < MAXFLAGS &&
33854375Sjoe			    ++f[FLAGS2IDX(sflags)] > maxflags) {
33954375Sjoe				saveflags = sflags;
34066584Sphk				maxflags = f[FLAGS2IDX(sflags)];
34154375Sjoe			}
3421553Srgrimes		}
3431553Srgrimes	}
3442860Srgrimes	/*
3452860Srgrimes	 * If the /set record is the same as the last one we do not need to output
3462877Srgrimes	 * a new one.  So first we check to see if anything changed.  Note that we
3472877Srgrimes	 * always output a /set record for the first directory.
3482860Srgrimes	 */
3492860Srgrimes	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
3502860Srgrimes	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
35166584Sphk	    ((keys & F_MODE) && (*pmode != savemode)) ||
35266584Sphk	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
35366584Sphk	    (first)) {
3542877Srgrimes		first = 0;
3552860Srgrimes		if (dflag)
3562860Srgrimes			(void)printf("/set type=dir");
3571553Srgrimes		else
3582860Srgrimes			(void)printf("/set type=file");
35951705Sbillf		if (keys & F_UNAME) {
3602860Srgrimes			if ((pw = getpwuid(saveuid)) != NULL)
3612860Srgrimes				(void)printf(" uname=%s", pw->pw_name);
3622860Srgrimes			else
36330027Scharnier				errx(1,
36430027Scharnier				"line %d: could not get uname for uid=%u",
36530027Scharnier				lineno, saveuid);
36651705Sbillf		}
3672860Srgrimes		if (keys & F_UID)
36838020Sbde			(void)printf(" uid=%lu", (u_long)saveuid);
36951705Sbillf		if (keys & F_GNAME) {
3702860Srgrimes			if ((gr = getgrgid(savegid)) != NULL)
3712860Srgrimes				(void)printf(" gname=%s", gr->gr_name);
3722860Srgrimes			else
37330027Scharnier				errx(1,
37430027Scharnier				"line %d: could not get gname for gid=%u",
37530027Scharnier				lineno, savegid);
37651705Sbillf		}
3772860Srgrimes		if (keys & F_GID)
37838020Sbde			(void)printf(" gid=%lu", (u_long)savegid);
3792860Srgrimes		if (keys & F_MODE)
3802860Srgrimes			(void)printf(" mode=%#o", savemode);
3812860Srgrimes		if (keys & F_NLINK)
3822860Srgrimes			(void)printf(" nlink=1");
38366584Sphk		if (keys & F_FLAGS) {
38461749Sjoe			fflags = flags_to_string(saveflags);
38561749Sjoe			(void)printf(" flags=%s", fflags);
38661749Sjoe			free(fflags);
38761749Sjoe		}
3882860Srgrimes		(void)printf("\n");
3892860Srgrimes		*puid = saveuid;
3902860Srgrimes		*pgid = savegid;
3912860Srgrimes		*pmode = savemode;
39254375Sjoe		*pflags = saveflags;
3932860Srgrimes	}
3941553Srgrimes	return (0);
3951553Srgrimes}
3961553Srgrimes
3971553Srgrimesstatic int
3981553Srgrimesdsort(a, b)
3991553Srgrimes	const FTSENT **a, **b;
4001553Srgrimes{
4011553Srgrimes	if (S_ISDIR((*a)->fts_statp->st_mode)) {
4021553Srgrimes		if (!S_ISDIR((*b)->fts_statp->st_mode))
4031553Srgrimes			return (1);
4041553Srgrimes	} else if (S_ISDIR((*b)->fts_statp->st_mode))
4051553Srgrimes		return (-1);
4061553Srgrimes	return (strcmp((*a)->fts_name, (*b)->fts_name));
4071553Srgrimes}
4081553Srgrimes
4091553Srgrimes#include <stdarg.h>
4101553Srgrimes
4111553Srgrimesvoid
4122860Srgrimesoutput(int indent, int *offset, const char *fmt, ...)
4131553Srgrimes{
4141553Srgrimes	va_list ap;
4151553Srgrimes	char buf[1024];
4161553Srgrimes	va_start(ap, fmt);
4171553Srgrimes	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
4181553Srgrimes	va_end(ap);
4191553Srgrimes
4201553Srgrimes	if (*offset + strlen(buf) > MAXLINELEN - 3) {
4212860Srgrimes		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
4222860Srgrimes		*offset = INDENTNAMELEN + indent;
4231553Srgrimes	}
4241553Srgrimes	*offset += printf(" %s", buf) + 1;
4251553Srgrimes}
426