create.c revision 99800
1/*-
2 * Copyright (c) 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35#if 0
36static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/usr.sbin/mtree/create.c 99800 2002-07-11 18:31:16Z alfred $";
40#endif /* not lint */
41
42#include <sys/param.h>
43#include <sys/stat.h>
44#include <dirent.h>
45#include <err.h>
46#include <errno.h>
47#include <fcntl.h>
48#include <fts.h>
49#include <grp.h>
50#ifdef MD5
51#include <md5.h>
52#endif
53#ifdef SHA1
54#include <sha.h>
55#endif
56#ifdef RMD160
57#include <ripemd.h>
58#endif
59#include <pwd.h>
60#include <stdio.h>
61#include <time.h>
62#include <unistd.h>
63#include <vis.h>
64#include "mtree.h"
65#include "extern.h"
66
67#define	INDENTNAMELEN	15
68#define	MAXLINELEN	80
69
70extern long int crc_total;
71extern int ftsoptions;
72extern int dflag, iflag, nflag, sflag;
73extern u_int keys;
74extern char fullpath[MAXPATHLEN];
75extern int lineno;
76
77static gid_t gid;
78static uid_t uid;
79static mode_t mode;
80static u_long flags = 0xffffffff;
81
82static int	dsort(const FTSENT **, const FTSENT **);
83static void	output(int, int *, const char *, ...) __printflike(3, 4);
84static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
85static void	statf(int, FTSENT *);
86
87void
88cwalk()
89{
90	register FTS *t;
91	register FTSENT *p;
92	time_t clock;
93	char *argv[2], host[MAXHOSTNAMELEN];
94	int indent = 0;
95
96	(void)time(&clock);
97	(void)gethostname(host, sizeof(host));
98	(void)printf(
99	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
100	    getlogin(), host, fullpath, ctime(&clock));
101
102	argv[0] = ".";
103	argv[1] = NULL;
104	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
105		err(1, "line %d: fts_open", lineno);
106	while ((p = fts_read(t))) {
107		if (iflag)
108			indent = p->fts_level * 4;
109		if (check_excludes(p->fts_name, p->fts_path)) {
110			fts_set(t, p, FTS_SKIP);
111			continue;
112		}
113		switch(p->fts_info) {
114		case FTS_D:
115			if (!dflag)
116				(void)printf("\n");
117			if (!nflag)
118				(void)printf("# %s\n", p->fts_path);
119			statd(t, p, &uid, &gid, &mode, &flags);
120			statf(indent, p);
121			break;
122		case FTS_DP:
123			if (!nflag && (p->fts_level > 0))
124				(void)printf("%*s# %s\n", indent, "", p->fts_path);
125			(void)printf("%*s..\n", indent, "");
126			if (!dflag)
127				(void)printf("\n");
128			break;
129		case FTS_DNR:
130		case FTS_ERR:
131		case FTS_NS:
132			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
133			break;
134		default:
135			if (!dflag)
136				statf(indent, p);
137			break;
138
139		}
140	}
141	(void)fts_close(t);
142	if (sflag && keys & F_CKSUM)
143		warnx("%s checksum: %lu", fullpath, crc_total);
144}
145
146static void
147statf(indent, p)
148	int indent;
149	FTSENT *p;
150{
151	struct group *gr;
152	struct passwd *pw;
153	u_long len, val;
154	int fd, offset;
155	char *fflags;
156	char *escaped_name;
157
158	escaped_name = calloc(1, p->fts_namelen * 4  +  1);
159	if (escaped_name == NULL)
160		errx(1, "statf(): calloc() failed");
161	strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL);
162
163	if (iflag || S_ISDIR(p->fts_statp->st_mode))
164		offset = printf("%*s%s", indent, "", escaped_name);
165	else
166		offset = printf("%*s    %s", indent, "", escaped_name);
167
168	free(escaped_name);
169
170	if (offset > (INDENTNAMELEN + indent))
171		offset = MAXLINELEN;
172	else
173		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
174
175	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
176		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
177	if (p->fts_statp->st_uid != uid) {
178		if (keys & F_UNAME) {
179			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
180				output(indent, &offset, "uname=%s", pw->pw_name);
181			} else {
182				errx(1,
183				"line %d: could not get uname for uid=%u",
184				lineno, p->fts_statp->st_uid);
185			}
186		}
187		if (keys & F_UID)
188			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
189	}
190	if (p->fts_statp->st_gid != gid) {
191		if (keys & F_GNAME) {
192			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
193				output(indent, &offset, "gname=%s", gr->gr_name);
194			} else {
195				errx(1,
196				"line %d: could not get gname for gid=%u",
197				lineno, p->fts_statp->st_gid);
198			}
199		}
200		if (keys & F_GID)
201			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
202	}
203	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
204		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
205	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
206		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
207	if (keys & F_SIZE)
208		output(indent, &offset, "size=%qd", p->fts_statp->st_size);
209	if (keys & F_TIME)
210		output(indent, &offset, "time=%ld.%ld",
211		    p->fts_statp->st_mtimespec.tv_sec,
212		    p->fts_statp->st_mtimespec.tv_nsec);
213	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
214		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
215		    crc(fd, &val, &len))
216			err(1, "line %d: %s", lineno, p->fts_accpath);
217		(void)close(fd);
218		output(indent, &offset, "cksum=%lu", val);
219	}
220#ifdef MD5
221	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
222		char *digest, buf[33];
223
224		digest = MD5File(p->fts_accpath, buf);
225		if (!digest) {
226			err(1, "line %d: %s", lineno, p->fts_accpath);
227		} else {
228			output(indent, &offset, "md5digest=%s", digest);
229		}
230	}
231#endif /* MD5 */
232#ifdef SHA1
233	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
234		char *digest, buf[41];
235
236		digest = SHA1_File(p->fts_accpath, buf);
237		if (!digest) {
238			err(1, "line %d: %s", lineno, p->fts_accpath);
239		} else {
240			output(indent, &offset, "sha1digest=%s", digest);
241		}
242	}
243#endif /* SHA1 */
244#ifdef RMD160
245	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
246		char *digest, buf[41];
247
248		digest = RIPEMD160_File(p->fts_accpath, buf);
249		if (!digest) {
250			err(1, "line %d: %s", lineno, p->fts_accpath);
251		} else {
252			output(indent, &offset, "ripemd160digest=%s", digest);
253		}
254	}
255#endif /* RMD160 */
256	if (keys & F_SLINK &&
257	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
258		output(indent, &offset, "link=%s", rlink(p->fts_accpath));
259	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
260		fflags = flags_to_string(p->fts_statp->st_flags);
261		output(indent, &offset, "flags=%s", fflags);
262		free(fflags);
263	}
264	(void)putchar('\n');
265}
266
267#define	MAXGID	5000
268#define	MAXUID	5000
269#define	MAXMODE	MBITS + 1
270#define	MAXFLAGS 256
271#define	MAXS 16
272
273static int
274statd(t, parent, puid, pgid, pmode, pflags)
275	FTS *t;
276	FTSENT *parent;
277	uid_t *puid;
278	gid_t *pgid;
279	mode_t *pmode;
280	u_long *pflags;
281{
282	register FTSENT *p;
283	register gid_t sgid;
284	register uid_t suid;
285	register mode_t smode;
286	register u_long sflags;
287	struct group *gr;
288	struct passwd *pw;
289	gid_t savegid = *pgid;
290	uid_t saveuid = *puid;
291	mode_t savemode = *pmode;
292	u_long saveflags = *pflags;
293	u_short maxgid, maxuid, maxmode, maxflags;
294	u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
295	char *fflags;
296	static int first = 1;
297
298	if ((p = fts_children(t, 0)) == NULL) {
299		if (errno)
300			err(1, "line %d: %s", lineno, RP(parent));
301		return (1);
302	}
303
304	bzero(g, sizeof(g));
305	bzero(u, sizeof(u));
306	bzero(m, sizeof(m));
307	bzero(f, sizeof(f));
308
309	maxuid = maxgid = maxmode = maxflags = 0;
310	for (; p; p = p->fts_link) {
311		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
312			smode = p->fts_statp->st_mode & MBITS;
313			if (smode < MAXMODE && ++m[smode] > maxmode) {
314				savemode = smode;
315				maxmode = m[smode];
316			}
317			sgid = p->fts_statp->st_gid;
318			if (sgid < MAXGID && ++g[sgid] > maxgid) {
319				savegid = sgid;
320				maxgid = g[sgid];
321			}
322			suid = p->fts_statp->st_uid;
323			if (suid < MAXUID && ++u[suid] > maxuid) {
324				saveuid = suid;
325				maxuid = u[suid];
326			}
327
328			/*
329			 * XXX
330			 * note that the below will break when file flags
331			 * are extended beyond the first 4 bytes of each
332			 * half word of the flags
333			 */
334#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
335			sflags = p->fts_statp->st_flags;
336			if (FLAGS2IDX(sflags) < MAXFLAGS &&
337			    ++f[FLAGS2IDX(sflags)] > maxflags) {
338				saveflags = sflags;
339				maxflags = f[FLAGS2IDX(sflags)];
340			}
341		}
342	}
343	/*
344	 * If the /set record is the same as the last one we do not need to output
345	 * a new one.  So first we check to see if anything changed.  Note that we
346	 * always output a /set record for the first directory.
347	 */
348	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
349	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
350	    ((keys & F_MODE) && (*pmode != savemode)) ||
351	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
352	    (first)) {
353		first = 0;
354		if (dflag)
355			(void)printf("/set type=dir");
356		else
357			(void)printf("/set type=file");
358		if (keys & F_UNAME) {
359			if ((pw = getpwuid(saveuid)) != NULL)
360				(void)printf(" uname=%s", pw->pw_name);
361			else
362				errx(1,
363				"line %d: could not get uname for uid=%u",
364				lineno, saveuid);
365		}
366		if (keys & F_UID)
367			(void)printf(" uid=%lu", (u_long)saveuid);
368		if (keys & F_GNAME) {
369			if ((gr = getgrgid(savegid)) != NULL)
370				(void)printf(" gname=%s", gr->gr_name);
371			else
372				errx(1,
373				"line %d: could not get gname for gid=%u",
374				lineno, savegid);
375		}
376		if (keys & F_GID)
377			(void)printf(" gid=%lu", (u_long)savegid);
378		if (keys & F_MODE)
379			(void)printf(" mode=%#o", savemode);
380		if (keys & F_NLINK)
381			(void)printf(" nlink=1");
382		if (keys & F_FLAGS) {
383			fflags = flags_to_string(saveflags);
384			(void)printf(" flags=%s", fflags);
385			free(fflags);
386		}
387		(void)printf("\n");
388		*puid = saveuid;
389		*pgid = savegid;
390		*pmode = savemode;
391		*pflags = saveflags;
392	}
393	return (0);
394}
395
396static int
397dsort(a, b)
398	const FTSENT **a, **b;
399{
400	if (S_ISDIR((*a)->fts_statp->st_mode)) {
401		if (!S_ISDIR((*b)->fts_statp->st_mode))
402			return (1);
403	} else if (S_ISDIR((*b)->fts_statp->st_mode))
404		return (-1);
405	return (strcmp((*a)->fts_name, (*b)->fts_name));
406}
407
408#include <stdarg.h>
409
410void
411output(int indent, int *offset, const char *fmt, ...)
412{
413	va_list ap;
414	char buf[1024];
415	va_start(ap, fmt);
416	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
417	va_end(ap);
418
419	if (*offset + strlen(buf) > MAXLINELEN - 3) {
420		(void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
421		*offset = INDENTNAMELEN + indent;
422	}
423	*offset += printf(" %s", buf) + 1;
424}
425