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