xref: /netbsd-src/usr.sbin/mtree/create.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: create.c,v 1.25 1999/12/01 22:12:52 wennmach Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: create.c,v 1.25 1999/12/01 22:12:52 wennmach Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <dirent.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <fts.h>
51 #include <grp.h>
52 #include <md5.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <vis.h>
59 #include "mtree.h"
60 #include "extern.h"
61 
62 #define	INDENTNAMELEN	15
63 #define	MAXLINELEN	80
64 #define VISFLAGS        VIS_CSTYLE
65 
66 extern int crc_total, ftsoptions;
67 extern int dflag, sflag;
68 extern int keys;
69 extern char fullpath[MAXPATHLEN];
70 
71 static gid_t gid;
72 static uid_t uid;
73 static mode_t mode;
74 static u_long flags;
75 static char codebuf[4*MAXPATHLEN + 1];
76 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
77 
78 static int	dsort __P((const FTSENT **, const FTSENT **));
79 static void	output __P((int *, const char *, ...));
80 static int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
81 			   u_long *));
82 static void	statf __P((FTSENT *));
83 
84 void
85 cwalk()
86 {
87 	FTS *t;
88 	FTSENT *p;
89 	time_t clock;
90 	char *argv[2], host[MAXHOSTNAMELEN + 1];
91 
92 	(void)time(&clock);
93 	(void)gethostname(host, sizeof(host));
94 	host[sizeof(host) - 1] = '\0';
95 	(void)printf(
96 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
97 	    getlogin(), host, fullpath, ctime(&clock));
98 
99 	argv[0] = ".";
100 	argv[1] = NULL;
101 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
102 		mtree_err("fts_open: %s", strerror(errno));
103 	while ((p = fts_read(t)) != NULL)
104 		switch(p->fts_info) {
105 		case FTS_D:
106 			(void)printf("\n# %s\n", p->fts_path);
107 			statd(t, p, &uid, &gid, &mode, &flags);
108 			statf(p);
109 			break;
110 		case FTS_DP:
111 			if (p->fts_level > 0)
112 				(void)printf("# %s\n..\n\n", p->fts_path);
113 			break;
114 		case FTS_DNR:
115 		case FTS_ERR:
116 		case FTS_NS:
117 			(void)fprintf(stderr,
118 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
119 			break;
120 		default:
121 			if (!dflag)
122 				statf(p);
123 			break;
124 
125 		}
126 	(void)fts_close(t);
127 	if (sflag && keys & F_CKSUM)
128 		(void)fprintf(stderr,
129 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
130 }
131 
132 static void
133 statf(p)
134 	FTSENT *p;
135 {
136 	struct group *gr;
137 	struct passwd *pw;
138 	u_int32_t len, val;
139 	int fd, indent;
140 	char md5buf[33], *md5cp;
141 
142 	strsvis(codebuf, p->fts_name, VISFLAGS, extra);
143 	if (S_ISDIR(p->fts_statp->st_mode))
144 		indent = printf("%s", codebuf);
145 	else
146 		indent = printf("    %s", codebuf);
147 
148 	if (indent > INDENTNAMELEN)
149 		indent = MAXLINELEN;
150 	else
151 		indent += printf("%*s", INDENTNAMELEN - indent, "");
152 
153 	if (!S_ISREG(p->fts_statp->st_mode))
154 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
155 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
156 		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
157 			output(&indent, "uname=%s", pw->pw_name);
158 		else /* if (keys & F_UID) */
159 			output(&indent, "uid=%u", p->fts_statp->st_uid);
160 	}
161 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
162 		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
163 			output(&indent, "gname=%s", gr->gr_name);
164 		else /* if (keys & F_GID) */
165 			output(&indent, "gid=%u", p->fts_statp->st_gid);
166 	}
167 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
168 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
169 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
170 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
171 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
172 		output(&indent, "size=%qd", p->fts_statp->st_size);
173 #ifndef __APPLE__
174 # ifdef BSD4_4
175 	if (keys & F_TIME)
176 		output(&indent, "time=%ld.%ld",
177 		    p->fts_statp->st_mtimespec.tv_sec,
178 		    p->fts_statp->st_mtimespec.tv_nsec);
179 # else
180 		output(&indent, "time=%ld.%ld",
181 		    p->fts_statp->st_mtime, 0);
182 # endif
183 #else
184 	if (keys & F_TIME)
185 		output(&indent, "time=%ld.%ld",
186 		    p->fts_statp->st_mtimespec.ts_sec,
187 		    p->fts_statp->st_mtimespec.ts_nsec);
188 #endif
189 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
190 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
191 		    crc(fd, &val, &len))
192 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
193 		(void)close(fd);
194 		output(&indent, "cksum=%lu", val);
195 	}
196 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
197 		if ((md5cp = MD5File(p->fts_accpath, md5buf)) == NULL)
198 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
199 				  output(&indent, "md5=%s", md5cp);
200 	}
201 	if (keys & F_SLINK &&
202 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
203 		output(&indent, "link=%s", rlink(p->fts_accpath));
204 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
205 		output(&indent, "flags=%s",
206 		    flags_to_string(p->fts_statp->st_flags, "none"));
207 	(void)putchar('\n');
208 }
209 
210 /* XXX
211  * FLAGS2INDEX will fail once the user and system settable bits need more
212  * than one byte, respectively.
213  */
214 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
215 
216 #define	MTREE_MAXGID	5000
217 #define	MTREE_MAXUID	5000
218 #define	MTREE_MAXMODE	(MBITS + 1)
219 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
220 #define	MTREE_MAXS 16
221 
222 static int
223 statd(t, parent, puid, pgid, pmode, pflags)
224 	FTS *t;
225 	FTSENT *parent;
226 	uid_t *puid;
227 	gid_t *pgid;
228 	mode_t *pmode;
229 	u_long *pflags;
230 {
231 	FTSENT *p;
232 	gid_t sgid;
233 	uid_t suid;
234 	mode_t smode;
235 	u_long sflags;
236 	struct group *gr;
237 	struct passwd *pw;
238 	gid_t savegid;
239 	uid_t saveuid;
240 	mode_t savemode;
241 	u_long saveflags;
242 	u_short maxgid, maxuid, maxmode, maxflags;
243 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
244 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
245 
246 	savegid = 0;
247 	saveuid = 0;
248 	savemode = 0;
249 	saveflags = 0;
250 	if ((p = fts_children(t, 0)) == NULL) {
251 		if (errno)
252 			mtree_err("%s: %s", RP(parent), strerror(errno));
253 		return (1);
254 	}
255 
256 	memset(g, 0, sizeof(g));
257 	memset(u, 0, sizeof(u));
258 	memset(m, 0, sizeof(m));
259 	memset(f, 0, sizeof(f));
260 
261 	maxuid = maxgid = maxmode = maxflags = 0;
262 	for (; p; p = p->fts_link) {
263 		smode = p->fts_statp->st_mode & MBITS;
264 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
265 			savemode = smode;
266 			maxmode = m[smode];
267 		}
268 		sgid = p->fts_statp->st_gid;
269 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
270 			savegid = sgid;
271 			maxgid = g[sgid];
272 		}
273 		suid = p->fts_statp->st_uid;
274 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
275 			saveuid = suid;
276 			maxuid = u[suid];
277 		}
278 
279 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
280 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
281 			saveflags = p->fts_statp->st_flags;
282 			maxflags = f[sflags];
283 		}
284 	}
285 	(void)printf("/set type=file");
286 	if (keys & F_GID)
287 		(void)printf(" gid=%lu", (u_long)savegid);
288 	if (keys & F_GNAME) {
289 		if ((gr = getgrgid(savegid)) != NULL)
290 			(void)printf(" gname=%s", gr->gr_name);
291 		else
292 			(void)printf(" gid=%lu", (u_long)savegid);
293 	}
294 	if (keys & F_UNAME) {
295 		if ((pw = getpwuid(saveuid)) != NULL)
296 			(void)printf(" uname=%s", pw->pw_name);
297 		else
298 			(void)printf(" uid=%lu", (u_long)saveuid);
299 	}
300 	if (keys & F_UID)
301 		(void)printf(" uid=%lu", (u_long)saveuid);
302 	if (keys & F_MODE)
303 		(void)printf(" mode=%#lo", (u_long)savemode);
304 	if (keys & F_NLINK)
305 		(void)printf(" nlink=1");
306 	if (keys & F_FLAGS)
307 		(void)printf(" flags=%s",
308 		    flags_to_string(saveflags, "none"));
309 	(void)printf("\n");
310 	*puid = saveuid;
311 	*pgid = savegid;
312 	*pmode = savemode;
313 	*pflags = saveflags;
314 	return (0);
315 }
316 
317 static int
318 dsort(a, b)
319 	const FTSENT **a, **b;
320 {
321 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
322 		if (!S_ISDIR((*b)->fts_statp->st_mode))
323 			return (1);
324 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
325 		return (-1);
326 	return (strcmp((*a)->fts_name, (*b)->fts_name));
327 }
328 
329 #if __STDC__
330 #include <stdarg.h>
331 #else
332 #include <varargs.h>
333 #endif
334 
335 void
336 #if __STDC__
337 output(int *offset, const char *fmt, ...)
338 #else
339 output(offset, fmt, va_alist)
340 	int *offset;
341 	char *fmt;
342         va_dcl
343 #endif
344 {
345 	va_list ap;
346 	char buf[1024];
347 #if __STDC__
348 	va_start(ap, fmt);
349 #else
350 	va_start(ap);
351 #endif
352 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
353 	va_end(ap);
354 
355 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
356 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
357 		*offset = INDENTNAMELEN;
358 	}
359 	*offset += printf(" %s", buf) + 1;
360 }
361