xref: /netbsd-src/usr.sbin/mtree/create.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: create.c,v 1.9 1995/03/07 21:12:06 cgd 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
39 #else
40 static char rcsid[] = "$NetBSD: create.c,v 1.9 1995/03/07 21:12:06 cgd Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 #include <time.h>
47 #include <fcntl.h>
48 #include <fts.h>
49 #include <dirent.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <unistd.h>
54 #include <stdio.h>
55 #include "mtree.h"
56 #include "extern.h"
57 
58 #define	INDENTNAMELEN	15
59 #define	MAXLINELEN	80
60 
61 extern int crc_total, ftsoptions;
62 extern int dflag, sflag;
63 extern u_short keys;
64 extern char fullpath[MAXPATHLEN];
65 
66 static gid_t gid;
67 static uid_t uid;
68 static mode_t mode;
69 
70 static int	dsort __P((const FTSENT **, const FTSENT **));
71 static void	output __P((int *, const char *, ...));
72 static int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *));
73 static void	statf __P((FTSENT *));
74 
75 void
76 cwalk()
77 {
78 	register FTS *t;
79 	register FTSENT *p;
80 	time_t clock;
81 	char *argv[2], host[MAXHOSTNAMELEN];
82 
83 	(void)time(&clock);
84 	(void)gethostname(host, sizeof(host));
85 	(void)printf(
86 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
87 	    getlogin(), host, fullpath, ctime(&clock));
88 
89 	argv[0] = ".";
90 	argv[1] = NULL;
91 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
92 		err("fts_open: %s", strerror(errno));
93 	while (p = fts_read(t))
94 		switch(p->fts_info) {
95 		case FTS_D:
96 			(void)printf("\n# %s\n", p->fts_path);
97 			statd(t, p, &uid, &gid, &mode);
98 			statf(p);
99 			break;
100 		case FTS_DP:
101 			if (p->fts_level > 0)
102 				(void)printf("# %s\n..\n\n", p->fts_path);
103 			break;
104 		case FTS_DNR:
105 		case FTS_ERR:
106 		case FTS_NS:
107 			(void)fprintf(stderr,
108 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
109 			break;
110 		default:
111 			if (!dflag)
112 				statf(p);
113 			break;
114 
115 		}
116 	(void)fts_close(t);
117 	if (sflag && keys & F_CKSUM)
118 		(void)fprintf(stderr,
119 		    "mtree: %s checksum: %lu\n", fullpath, crc_total);
120 }
121 
122 static void
123 statf(p)
124 	FTSENT *p;
125 {
126 	struct group *gr;
127 	struct passwd *pw;
128 	u_long len, val;
129 	int fd, indent;
130 
131 	if (S_ISDIR(p->fts_statp->st_mode))
132 		indent = printf("%s", p->fts_name);
133 	else
134 		indent = printf("    %s", p->fts_name);
135 
136 	if (indent > INDENTNAMELEN)
137 		indent = MAXLINELEN;
138 	else
139 		indent += printf("%*s", INDENTNAMELEN - indent, "");
140 
141 	if (!S_ISREG(p->fts_statp->st_mode))
142 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
143 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid)
144 		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
145 			output(&indent, "uname=%s", pw->pw_name);
146 		else /* if (keys & F_UID) */
147 			output(&indent, "uid=%u", p->fts_statp->st_uid);
148 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid)
149 		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
150 			output(&indent, "gname=%s", gr->gr_name);
151 		else /* if (keys & F_GID) */
152 			output(&indent, "gid=%u", p->fts_statp->st_gid);
153 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
154 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
155 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
156 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
157 	if (keys & F_SIZE)
158 		output(&indent, "size=%qd", p->fts_statp->st_size);
159 	if (keys & F_TIME)
160 		output(&indent, "time=%ld.%ld",
161 		    p->fts_statp->st_mtimespec.ts_sec,
162 		    p->fts_statp->st_mtimespec.ts_nsec);
163 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
164 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
165 		    crc(fd, &val, &len))
166 			err("%s: %s", p->fts_accpath, strerror(errno));
167 		(void)close(fd);
168 		output(&indent, "cksum=%lu", val);
169 	}
170 	if (keys & F_SLINK &&
171 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
172 		output(&indent, "link=%s", rlink(p->fts_accpath));
173 	(void)putchar('\n');
174 }
175 
176 #define	MAXGID	5000
177 #define	MAXUID	5000
178 #define	MAXMODE	MBITS + 1
179 
180 static int
181 statd(t, parent, puid, pgid, pmode)
182 	FTS *t;
183 	FTSENT *parent;
184 	uid_t *puid;
185 	gid_t *pgid;
186 	mode_t *pmode;
187 {
188 	register FTSENT *p;
189 	register gid_t sgid;
190 	register uid_t suid;
191 	register mode_t smode;
192 	struct group *gr;
193 	struct passwd *pw;
194 	gid_t savegid;
195 	uid_t saveuid;
196 	mode_t savemode;
197 	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
198 
199 	if ((p = fts_children(t, 0)) == NULL) {
200 		if (errno)
201 			err("%s: %s", RP(parent), strerror(errno));
202 		return (1);
203 	}
204 
205 	bzero(g, sizeof(g));
206 	bzero(u, sizeof(u));
207 	bzero(m, sizeof(m));
208 
209 	maxuid = maxgid = maxmode = 0;
210 	for (; p; p = p->fts_link) {
211 		smode = p->fts_statp->st_mode & MBITS;
212 		if (smode < MAXMODE && ++m[smode] > maxmode) {
213 			savemode = smode;
214 			maxmode = m[smode];
215 		}
216 		sgid = p->fts_statp->st_gid;
217 		if (sgid < MAXGID && ++g[sgid] > maxgid) {
218 			savegid = sgid;
219 			maxgid = g[sgid];
220 		}
221 		suid = p->fts_statp->st_uid;
222 		if (suid < MAXUID && ++u[suid] > maxuid) {
223 			saveuid = suid;
224 			maxuid = u[suid];
225 		}
226 	}
227 	(void)printf("/set type=file");
228 	if (keys & F_GID)
229 		(void)printf(" gid=%u", savegid);
230 	if (keys & F_GNAME)
231 		if ((gr = getgrgid(savegid)) != NULL)
232 			(void)printf(" gname=%s", gr->gr_name);
233 		else
234 			(void)printf(" gid=%u", savegid);
235 	if (keys & F_UNAME)
236 		if ((pw = getpwuid(saveuid)) != NULL)
237 			(void)printf(" uname=%s", pw->pw_name);
238 		else
239 			(void)printf(" uid=%u", saveuid);
240 	if (keys & F_UID)
241 		(void)printf(" uid=%u", saveuid);
242 	if (keys & F_MODE)
243 		(void)printf(" mode=%#o", savemode);
244 	if (keys & F_NLINK)
245 		(void)printf(" nlink=1");
246 	(void)printf("\n");
247 	*puid = saveuid;
248 	*pgid = savegid;
249 	*pmode = savemode;
250 	return (0);
251 }
252 
253 static int
254 dsort(a, b)
255 	const FTSENT **a, **b;
256 {
257 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
258 		if (!S_ISDIR((*b)->fts_statp->st_mode))
259 			return (1);
260 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
261 		return (-1);
262 	return (strcmp((*a)->fts_name, (*b)->fts_name));
263 }
264 
265 #if __STDC__
266 #include <stdarg.h>
267 #else
268 #include <varargs.h>
269 #endif
270 
271 void
272 #if __STDC__
273 output(int *offset, const char *fmt, ...)
274 #else
275 output(offset, fmt, va_alist)
276 	int *offset;
277 	char *fmt;
278         va_dcl
279 #endif
280 {
281 	va_list ap;
282 	char buf[1024];
283 #if __STDC__
284 	va_start(ap, fmt);
285 #else
286 	va_start(ap);
287 #endif
288 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
289 	va_end(ap);
290 
291 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
292 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
293 		*offset = INDENTNAMELEN;
294 	}
295 	*offset += printf(" %s", buf) + 1;
296 }
297