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