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