xref: /dragonfly/usr.sbin/mtree/create.c (revision 92fc8b5c)
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  * @(#)create.c	8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/mtree/create.c,v 1.18.2.3 2001/01/12 19:17:18 phk Exp $
35  * $DragonFly: src/usr.sbin/mtree/create.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <fts.h>
45 #include <grp.h>
46 #ifdef MD5
47 #include <md5.h>
48 #endif
49 #ifdef SHA1
50 #include <sha.h>
51 #endif
52 #ifdef RMD160
53 #include <ripemd.h>
54 #endif
55 #include <pwd.h>
56 #include <stdio.h>
57 #include <time.h>
58 #include <unistd.h>
59 #include <stdarg.h>
60 #include <vis.h>
61 #include "mtree.h"
62 #include "extern.h"
63 
64 #define	INDENTNAMELEN	15
65 #define	MAXLINELEN	80
66 
67 static gid_t gid;
68 static uid_t uid;
69 static mode_t mode;
70 static u_long flags = 0xffffffff;
71 
72 static int	dsort(const FTSENT * const *, const FTSENT * const *);
73 static void	output(int, int *, const char *, ...) __printflike(3, 4);
74 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
75 			   u_long *);
76 static void	statf(int, FTSENT *);
77 
78 void
79 cwalk(void)
80 {
81 	FTS *t;
82 	FTSENT *p;
83 	time_t clk;
84 	char *argv[2], host[MAXHOSTNAMELEN], dot[] = ".";
85 	int indent = 0;
86 
87 	time(&clk);
88 	gethostname(host, sizeof(host));
89 	printf(
90 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
91 	    getlogin(), host, fullpath, ctime(&clk));
92 
93 	argv[0] = dot;
94 	argv[1] = NULL;
95 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
96 		err(1, "line %d: fts_open", lineno);
97 	while ((p = fts_read(t))) {
98 		if (iflag)
99 			indent = p->fts_level * 4;
100 		if (check_excludes(p->fts_name, p->fts_path)) {
101 			fts_set(t, p, FTS_SKIP);
102 			continue;
103 		}
104 		switch(p->fts_info) {
105 		case FTS_D:
106 			if (!dflag)
107 				printf("\n");
108 			if (!nflag)
109 				printf("# %s\n", p->fts_path);
110 			statd(t, p, &uid, &gid, &mode, &flags);
111 			statf(indent, p);
112 			break;
113 		case FTS_DP:
114 			if (!nflag && (p->fts_level > 0))
115 				printf("%*s# %s\n", indent, "", p->fts_path);
116 			printf("%*s..\n", indent, "");
117 			if (!dflag)
118 				printf("\n");
119 			break;
120 		case FTS_DNR:
121 		case FTS_ERR:
122 		case FTS_NS:
123 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
124 			break;
125 		default:
126 			if (!dflag)
127 				statf(indent, p);
128 			break;
129 
130 		}
131 	}
132 	fts_close(t);
133 	if (sflag && keys & F_CKSUM)
134 		warnx("%s checksum: %lu", fullpath, crc_total);
135 }
136 
137 static void
138 statf(int indent, FTSENT *p)
139 {
140 	struct group *gr;
141 	struct passwd *pw;
142 	u_long len, val;
143 	int fd, offset;
144 	char *fflags;
145 	char *escaped_name;
146 
147 	escaped_name = calloc(1, p->fts_namelen * 4  +  1);
148 	if (escaped_name == NULL)
149 		errx(1, "statf(): calloc() failed");
150 	strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL);
151 
152 	if (iflag || S_ISDIR(p->fts_statp->st_mode))
153 		offset = printf("%*s%s", indent, "", escaped_name);
154 	else
155 		offset = printf("%*s    %s", indent, "", escaped_name);
156 
157 	free(escaped_name);
158 
159 	if (offset > (INDENTNAMELEN + indent))
160 		offset = MAXLINELEN;
161 	else
162 		offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
163 
164 	if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
165 		output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
166 	if (p->fts_statp->st_uid != uid) {
167 		if (keys & F_UNAME) {
168 			if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
169 				output(indent, &offset, "uname=%s", pw->pw_name);
170 			} else {
171 				errx(1,
172 				"line %d: could not get uname for uid=%u",
173 				lineno, p->fts_statp->st_uid);
174 			}
175 		}
176 		if (keys & F_UID)
177 			output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
178 	}
179 	if (p->fts_statp->st_gid != gid) {
180 		if (keys & F_GNAME) {
181 			if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
182 				output(indent, &offset, "gname=%s", gr->gr_name);
183 			} else {
184 				errx(1,
185 				"line %d: could not get gname for gid=%u",
186 				lineno, p->fts_statp->st_gid);
187 			}
188 		}
189 		if (keys & F_GID)
190 			output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
191 	}
192 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
193 		output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
194 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
195 		output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
196 	if (keys & F_SIZE)
197 		output(indent, &offset, "size=%jd",
198 		    (uintmax_t)p->fts_statp->st_size);
199 	if (keys & F_TIME)
200 		output(indent, &offset, "time=%ld.%ld",
201 		    p->fts_statp->st_mtimespec.tv_sec,
202 		    p->fts_statp->st_mtimespec.tv_nsec);
203 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
204 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
205 		    crc(fd, &val, &len))
206 			err(1, "line %d: %s", lineno, p->fts_accpath);
207 		close(fd);
208 		output(indent, &offset, "cksum=%lu", val);
209 	}
210 #ifdef MD5
211 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
212 		char *digest, buf[33];
213 
214 		digest = MD5File(p->fts_accpath, buf);
215 		if (!digest) {
216 			err(1, "line %d: %s", lineno, p->fts_accpath);
217 		} else {
218 			output(indent, &offset, "md5digest=%s", digest);
219 		}
220 	}
221 #endif /* MD5 */
222 #ifdef SHA1
223 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
224 		char *digest, buf[41];
225 
226 		digest = SHA1_File(p->fts_accpath, buf);
227 		if (!digest) {
228 			err(1, "line %d: %s", lineno, p->fts_accpath);
229 		} else {
230 			output(indent, &offset, "sha1digest=%s", digest);
231 		}
232 	}
233 #endif /* SHA1 */
234 #ifdef RMD160
235 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
236 		char *digest, buf[41];
237 
238 		digest = RIPEMD160_File(p->fts_accpath, buf);
239 		if (!digest) {
240 			err(1, "line %d: %s", lineno, p->fts_accpath);
241 		} else {
242 			output(indent, &offset, "ripemd160digest=%s", digest);
243 		}
244 	}
245 #endif /* RMD160 */
246 	if (keys & F_SLINK &&
247 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
248 		output(indent, &offset, "link=%s", rlink(p->fts_accpath));
249 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
250 		fflags = flags_to_string(p->fts_statp->st_flags);
251 		output(indent, &offset, "flags=%s", fflags);
252 		free(fflags);
253 	}
254 	putchar('\n');
255 }
256 
257 #define	MAXGID	5000
258 #define	MAXUID	5000
259 #define	MAXMODE	MBITS + 1
260 #define	MAXFLAGS 256
261 #define	MAXS 16
262 
263 static int
264 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
265       u_long *pflags)
266 {
267 	FTSENT *p;
268 	gid_t sgid;
269 	uid_t suid;
270 	mode_t smode;
271 	u_long sflags;
272 	struct group *gr;
273 	struct passwd *pw;
274 	gid_t savegid = *pgid;
275 	uid_t saveuid = *puid;
276 	mode_t savemode = *pmode;
277 	u_long saveflags = *pflags;
278 	u_short maxgid, maxuid, maxmode, maxflags;
279 	u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
280 	char *fflags;
281 	static int first = 1;
282 
283 	if ((p = fts_children(t, 0)) == NULL) {
284 		if (errno)
285 			err(1, "line %d: %s", lineno, RP(parent));
286 		return (1);
287 	}
288 
289 	bzero(g, sizeof(g));
290 	bzero(u, sizeof(u));
291 	bzero(m, sizeof(m));
292 	bzero(f, sizeof(f));
293 
294 	maxuid = maxgid = maxmode = maxflags = 0;
295 	for (; p; p = p->fts_link) {
296 		if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
297 			smode = p->fts_statp->st_mode & MBITS;
298 			if (smode < MAXMODE && ++m[smode] > maxmode) {
299 				savemode = smode;
300 				maxmode = m[smode];
301 			}
302 			sgid = p->fts_statp->st_gid;
303 			if (sgid < MAXGID && ++g[sgid] > maxgid) {
304 				savegid = sgid;
305 				maxgid = g[sgid];
306 			}
307 			suid = p->fts_statp->st_uid;
308 			if (suid < MAXUID && ++u[suid] > maxuid) {
309 				saveuid = suid;
310 				maxuid = u[suid];
311 			}
312 
313 			/*
314 			 * XXX
315 			 * note that the below will break when file flags
316 			 * are extended beyond the first 4 bytes of each
317 			 * half word of the flags
318 			 */
319 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
320 			sflags = p->fts_statp->st_flags;
321 			if (FLAGS2IDX(sflags) < MAXFLAGS &&
322 			    ++f[FLAGS2IDX(sflags)] > maxflags) {
323 				saveflags = sflags;
324 				maxflags = f[FLAGS2IDX(sflags)];
325 			}
326 		}
327 	}
328 	/*
329 	 * If the /set record is the same as the last one we do not need to output
330 	 * a new one.  So first we check to see if anything changed.  Note that we
331 	 * always output a /set record for the first directory.
332 	 */
333 	if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
334 	    (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
335 	    ((keys & F_MODE) && (*pmode != savemode)) ||
336 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
337 	    (first)) {
338 		first = 0;
339 		if (dflag)
340 			printf("/set type=dir");
341 		else
342 			printf("/set type=file");
343 		if (keys & F_UNAME) {
344 			if ((pw = getpwuid(saveuid)) != NULL)
345 				printf(" uname=%s", pw->pw_name);
346 			else
347 				errx(1,
348 				"line %d: could not get uname for uid=%u",
349 				lineno, saveuid);
350 		}
351 		if (keys & F_UID)
352 			printf(" uid=%lu", (u_long)saveuid);
353 		if (keys & F_GNAME) {
354 			if ((gr = getgrgid(savegid)) != NULL)
355 				printf(" gname=%s", gr->gr_name);
356 			else
357 				errx(1,
358 				"line %d: could not get gname for gid=%u",
359 				lineno, savegid);
360 		}
361 		if (keys & F_GID)
362 			printf(" gid=%lu", (u_long)savegid);
363 		if (keys & F_MODE)
364 			printf(" mode=%#o", savemode);
365 		if (keys & F_NLINK)
366 			printf(" nlink=1");
367 		if (keys & F_FLAGS) {
368 			fflags = flags_to_string(saveflags);
369 			printf(" flags=%s", fflags);
370 			free(fflags);
371 		}
372 		printf("\n");
373 		*puid = saveuid;
374 		*pgid = savegid;
375 		*pmode = savemode;
376 		*pflags = saveflags;
377 	}
378 	return (0);
379 }
380 
381 static int
382 dsort(const FTSENT * const *a, const FTSENT * const *b)
383 {
384 
385 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
386 		if (!S_ISDIR((*b)->fts_statp->st_mode))
387 			return (1);
388 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
389 		return (-1);
390 	return (strcmp((*a)->fts_name, (*b)->fts_name));
391 }
392 
393 void
394 output(int indent, int *offset, const char *fmt, ...)
395 {
396 	va_list ap;
397 	char buf[1024];
398 
399 	va_start(ap, fmt);
400 	vsnprintf(buf, sizeof(buf), fmt, ap);
401 	va_end(ap);
402 
403 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
404 		printf(" \\\n%*s", INDENTNAMELEN + indent, "");
405 		*offset = INDENTNAMELEN + indent;
406 	}
407 	*offset += printf(" %s", buf) + 1;
408 }
409