xref: /netbsd-src/usr.sbin/mtree/create.c (revision 21a3d2f02241c56556f4b2305ef1b8036f268f70)
1 /*	$NetBSD: create.c,v 1.34 2001/10/18 05:06:02 lukem 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.34 2001/10/18 05:06:02 lukem Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 
48 #include <dirent.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <fts.h>
52 #include <grp.h>
53 #include <md5.h>
54 #include <pwd.h>
55 #include <stdarg.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <time.h>
59 #include <unistd.h>
60 #include <vis.h>
61 
62 #include "mtree.h"
63 #include "extern.h"
64 
65 #define	INDENTNAMELEN	15
66 #define	MAXLINELEN	80
67 #define VISFLAGS        VIS_CSTYLE
68 
69 static gid_t gid;
70 static uid_t uid;
71 static mode_t mode;
72 static u_long flags;
73 static char codebuf[4*MAXPATHLEN + 1];
74 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
75 
76 static int	dsort(const FTSENT **, const FTSENT **);
77 static void	output(int *, const char *, ...)
78 	__attribute__((__format__(__printf__, 2, 3)));
79 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
80 static void	statf(FTSENT *);
81 
82 void
83 cwalk(void)
84 {
85 	FTS *t;
86 	FTSENT *p;
87 	time_t clocktime;
88 	char host[MAXHOSTNAMELEN + 1];
89 	char  dot[] = ".";		/* XXX: work around gcc warning */
90 	char *argv[] = { dot, NULL };
91 
92 	(void)time(&clocktime);
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(&clocktime));
98 
99 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
100 		mtree_err("fts_open: %s", strerror(errno));
101 	while ((p = fts_read(t)) != NULL)
102 		switch(p->fts_info) {
103 		case FTS_D:
104 			(void)printf("\n# %s\n", p->fts_path);
105 			statd(t, p, &uid, &gid, &mode, &flags);
106 			statf(p);
107 			break;
108 		case FTS_DP:
109 			if (p->fts_level > 0)
110 				(void)printf("# %s\n..\n\n", p->fts_path);
111 			break;
112 		case FTS_DNR:
113 		case FTS_ERR:
114 		case FTS_NS:
115 			(void)fprintf(stderr,
116 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
117 			break;
118 		default:
119 			if (!dflag)
120 				statf(p);
121 			break;
122 
123 		}
124 	(void)fts_close(t);
125 	if (sflag && keys & F_CKSUM)
126 		(void)fprintf(stderr,
127 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
128 }
129 
130 static void
131 statf(FTSENT *p)
132 {
133 	u_int32_t len, val;
134 	int fd, indent;
135 	char md5buf[33], *md5cp;
136 	const char *name;
137 
138 	strsvis(codebuf, p->fts_name, VISFLAGS, extra);
139 	if (S_ISDIR(p->fts_statp->st_mode))
140 		indent = printf("%s", codebuf);
141 	else
142 		indent = printf("    %s", codebuf);
143 
144 	if (indent > INDENTNAMELEN)
145 		indent = MAXLINELEN;
146 	else
147 		indent += printf("%*s", INDENTNAMELEN - indent, "");
148 
149 	if (!S_ISREG(p->fts_statp->st_mode))
150 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
151 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
152 		if (keys & F_UNAME &&
153 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
154 			output(&indent, "uname=%s", name);
155 		else /* if (keys & F_UID) */
156 			output(&indent, "uid=%u", p->fts_statp->st_uid);
157 	}
158 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
159 		if (keys & F_GNAME &&
160 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
161 			output(&indent, "gname=%s", name);
162 		else /* if (keys & F_GID) */
163 			output(&indent, "gid=%u", p->fts_statp->st_gid);
164 	}
165 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
166 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
167 	if (keys & F_DEV &&
168 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
169 		output(&indent, "device=%#x", p->fts_statp->st_rdev);
170 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
171 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
172 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
173 		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
174 #ifdef BSD4_4
175 	if (keys & F_TIME)
176 		output(&indent, "time=%ld.%ld",
177 		    (long)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 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
184 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
185 		    crc(fd, &val, &len))
186 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
187 		(void)close(fd);
188 		output(&indent, "cksum=%lu", (long)val);
189 	}
190 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
191 		if ((md5cp = MD5File(p->fts_accpath, md5buf)) == NULL)
192 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
193 				  output(&indent, "md5=%s", md5cp);
194 	}
195 	if (keys & F_SLINK &&
196 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
197 		output(&indent, "link=%s", rlink(p->fts_accpath));
198 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
199 		output(&indent, "flags=%s",
200 		    flags_to_string(p->fts_statp->st_flags, "none"));
201 	(void)putchar('\n');
202 }
203 
204 /* XXX
205  * FLAGS2INDEX will fail once the user and system settable bits need more
206  * than one byte, respectively.
207  */
208 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
209 
210 #define	MTREE_MAXGID	5000
211 #define	MTREE_MAXUID	5000
212 #define	MTREE_MAXMODE	(MBITS + 1)
213 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
214 #define	MTREE_MAXS 16
215 
216 static int
217 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
218       u_long *pflags)
219 {
220 	FTSENT *p;
221 	gid_t sgid;
222 	uid_t suid;
223 	mode_t smode;
224 	u_long sflags;
225 	const char *name;
226 	gid_t savegid;
227 	uid_t saveuid;
228 	mode_t savemode;
229 	u_long saveflags;
230 	u_short maxgid, maxuid, maxmode, maxflags;
231 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
232 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
233 
234 	savegid = 0;
235 	saveuid = 0;
236 	savemode = 0;
237 	saveflags = 0;
238 	if ((p = fts_children(t, 0)) == NULL) {
239 		if (errno)
240 			mtree_err("%s: %s", RP(parent), strerror(errno));
241 		return (1);
242 	}
243 
244 	memset(g, 0, sizeof(g));
245 	memset(u, 0, sizeof(u));
246 	memset(m, 0, sizeof(m));
247 	memset(f, 0, sizeof(f));
248 
249 	maxuid = maxgid = maxmode = maxflags = 0;
250 	for (; p; p = p->fts_link) {
251 		smode = p->fts_statp->st_mode & MBITS;
252 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
253 			savemode = smode;
254 			maxmode = m[smode];
255 		}
256 		sgid = p->fts_statp->st_gid;
257 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
258 			savegid = sgid;
259 			maxgid = g[sgid];
260 		}
261 		suid = p->fts_statp->st_uid;
262 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
263 			saveuid = suid;
264 			maxuid = u[suid];
265 		}
266 
267 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
268 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
269 			saveflags = p->fts_statp->st_flags;
270 			maxflags = f[sflags];
271 		}
272 	}
273 	(void)printf("/set type=file");
274 	if (keys & F_GID)
275 		(void)printf(" gid=%lu", (u_long)savegid);
276 	if (keys & F_GNAME) {
277 		if ((name = group_from_gid(savegid, 1)) != NULL)
278 			(void)printf(" gname=%s", name);
279 		else
280 			(void)printf(" gid=%lu", (u_long)savegid);
281 	}
282 	if (keys & F_UNAME) {
283 		if ((name = user_from_uid(saveuid, 1)) != NULL)
284 			(void)printf(" uname=%s", name);
285 		else
286 			(void)printf(" uid=%lu", (u_long)saveuid);
287 	}
288 	if (keys & F_UID)
289 		(void)printf(" uid=%lu", (u_long)saveuid);
290 	if (keys & F_MODE)
291 		(void)printf(" mode=%#lo", (u_long)savemode);
292 	if (keys & F_NLINK)
293 		(void)printf(" nlink=1");
294 	if (keys & F_FLAGS)
295 		(void)printf(" flags=%s",
296 		    flags_to_string(saveflags, "none"));
297 	(void)printf("\n");
298 	*puid = saveuid;
299 	*pgid = savegid;
300 	*pmode = savemode;
301 	*pflags = saveflags;
302 	return (0);
303 }
304 
305 static int
306 dsort(const FTSENT **a, const FTSENT **b)
307 {
308 
309 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
310 		if (!S_ISDIR((*b)->fts_statp->st_mode))
311 			return (1);
312 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
313 		return (-1);
314 	return (strcmp((*a)->fts_name, (*b)->fts_name));
315 }
316 
317 void
318 output(int *offset, const char *fmt, ...)
319 {
320 	va_list ap;
321 	char buf[1024];
322 
323 	va_start(ap, fmt);
324 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
325 	va_end(ap);
326 
327 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
328 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
329 		*offset = INDENTNAMELEN;
330 	}
331 	*offset += printf(" %s", buf) + 1;
332 }
333