create.c revision 1.57 1 1.57 hubertf /* $NetBSD: create.c,v 1.57 2009/02/01 22:36:24 hubertf 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.43 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.45 jmc #if HAVE_NBTOOL_CONFIG_H
33 1.45 jmc #include "nbtool_config.h"
34 1.45 jmc #endif
35 1.45 jmc
36 1.13 lukem #include <sys/cdefs.h>
37 1.39 tv #if defined(__RCSID) && !defined(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.57 hubertf __RCSID("$NetBSD: create.c,v 1.57 2009/02/01 22:36:24 hubertf 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.44 lukem #if ! HAVE_NBTOOL_CONFIG_H
49 1.13 lukem #include <dirent.h>
50 1.38 tv #endif
51 1.38 tv
52 1.13 lukem #include <errno.h>
53 1.5 cgd #include <fcntl.h>
54 1.5 cgd #include <grp.h>
55 1.5 cgd #include <pwd.h>
56 1.37 lukem #include <stdio.h>
57 1.29 simonb #include <stdarg.h>
58 1.35 lukem #include <stdlib.h>
59 1.13 lukem #include <string.h>
60 1.13 lukem #include <time.h>
61 1.5 cgd #include <unistd.h>
62 1.29 simonb
63 1.37 lukem #ifndef NO_MD5
64 1.37 lukem #include <md5.h>
65 1.37 lukem #endif
66 1.37 lukem #ifndef NO_RMD160
67 1.50 christos #include <rmd160.h>
68 1.37 lukem #endif
69 1.37 lukem #ifndef NO_SHA1
70 1.37 lukem #include <sha1.h>
71 1.37 lukem #endif
72 1.47 elad #ifndef NO_SHA2
73 1.50 christos #include <sha2.h>
74 1.47 elad #endif
75 1.37 lukem
76 1.5 cgd #include "extern.h"
77 1.1 cgd
78 1.5 cgd #define INDENTNAMELEN 15
79 1.5 cgd #define MAXLINELEN 80
80 1.5 cgd
81 1.5 cgd static gid_t gid;
82 1.5 cgd static uid_t uid;
83 1.5 cgd static mode_t mode;
84 1.18 mrg static u_long flags;
85 1.5 cgd
86 1.54 rillig static int dcmp(const FTSENT **, const FTSENT **);
87 1.29 simonb static void output(int *, const char *, ...)
88 1.26 is __attribute__((__format__(__printf__, 2, 3)));
89 1.29 simonb static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
90 1.29 simonb static void statf(FTSENT *);
91 1.1 cgd
92 1.5 cgd void
93 1.29 simonb cwalk(void)
94 1.1 cgd {
95 1.13 lukem FTS *t;
96 1.13 lukem FTSENT *p;
97 1.33 lukem time_t clocktime;
98 1.34 lukem char host[MAXHOSTNAMELEN + 1];
99 1.55 christos const char *user;
100 1.41 grant char *argv[2];
101 1.41 grant char dot[] = ".";
102 1.55 christos
103 1.41 grant argv[0] = dot;
104 1.41 grant argv[1] = NULL;
105 1.5 cgd
106 1.36 lukem time(&clocktime);
107 1.36 lukem gethostname(host, sizeof(host));
108 1.14 mrg host[sizeof(host) - 1] = '\0';
109 1.55 christos if ((user = getlogin()) == NULL) {
110 1.55 christos struct passwd *pw;
111 1.55 christos user = (pw = getpwuid(getuid())) == NULL ? pw->pw_name :
112 1.55 christos "<unknown>";
113 1.55 christos }
114 1.55 christos
115 1.36 lukem printf(
116 1.5 cgd "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s",
117 1.55 christos user, host, fullpath, ctime(&clocktime));
118 1.1 cgd
119 1.54 rillig if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
120 1.17 wsanchez mtree_err("fts_open: %s", strerror(errno));
121 1.36 lukem while ((p = fts_read(t)) != NULL) {
122 1.36 lukem if (check_excludes(p->fts_name, p->fts_path)) {
123 1.36 lukem fts_set(t, p, FTS_SKIP);
124 1.36 lukem continue;
125 1.36 lukem }
126 1.1 cgd switch(p->fts_info) {
127 1.1 cgd case FTS_D:
128 1.36 lukem printf("\n# %s\n", p->fts_path);
129 1.18 mrg statd(t, p, &uid, &gid, &mode, &flags);
130 1.5 cgd statf(p);
131 1.1 cgd break;
132 1.1 cgd case FTS_DP:
133 1.5 cgd if (p->fts_level > 0)
134 1.36 lukem printf("# %s\n..\n\n", p->fts_path);
135 1.5 cgd break;
136 1.1 cgd case FTS_DNR:
137 1.1 cgd case FTS_ERR:
138 1.1 cgd case FTS_NS:
139 1.36 lukem mtree_err("%s: %s",
140 1.36 lukem p->fts_path, strerror(p->fts_errno));
141 1.5 cgd break;
142 1.1 cgd default:
143 1.5 cgd if (!dflag)
144 1.5 cgd statf(p);
145 1.5 cgd break;
146 1.36 lukem
147 1.1 cgd }
148 1.36 lukem }
149 1.36 lukem fts_close(t);
150 1.5 cgd if (sflag && keys & F_CKSUM)
151 1.40 soren mtree_err("%s checksum: %u", fullpath, crc_total);
152 1.5 cgd }
153 1.1 cgd
154 1.5 cgd static void
155 1.29 simonb statf(FTSENT *p)
156 1.5 cgd {
157 1.17 wsanchez u_int32_t len, val;
158 1.5 cgd int fd, indent;
159 1.46 lukem const char *name;
160 1.47 elad #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
161 1.51 elad char *digestbuf;
162 1.37 lukem #endif
163 1.5 cgd
164 1.42 lukem indent = printf("%s%s",
165 1.46 lukem S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
166 1.5 cgd
167 1.5 cgd if (indent > INDENTNAMELEN)
168 1.5 cgd indent = MAXLINELEN;
169 1.5 cgd else
170 1.5 cgd indent += printf("%*s", INDENTNAMELEN - indent, "");
171 1.5 cgd
172 1.5 cgd if (!S_ISREG(p->fts_statp->st_mode))
173 1.5 cgd output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
174 1.15 ross if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
175 1.30 lukem if (keys & F_UNAME &&
176 1.30 lukem (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
177 1.30 lukem output(&indent, "uname=%s", name);
178 1.5 cgd else /* if (keys & F_UID) */
179 1.5 cgd output(&indent, "uid=%u", p->fts_statp->st_uid);
180 1.15 ross }
181 1.15 ross if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
182 1.30 lukem if (keys & F_GNAME &&
183 1.30 lukem (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
184 1.30 lukem output(&indent, "gname=%s", name);
185 1.5 cgd else /* if (keys & F_GID) */
186 1.5 cgd output(&indent, "gid=%u", p->fts_statp->st_gid);
187 1.15 ross }
188 1.5 cgd if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
189 1.5 cgd output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
190 1.32 lukem if (keys & F_DEV &&
191 1.32 lukem (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
192 1.56 christos output(&indent, "device=%#llx",
193 1.56 christos (long long)p->fts_statp->st_rdev);
194 1.5 cgd if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
195 1.5 cgd output(&indent, "nlink=%u", p->fts_statp->st_nlink);
196 1.11 mycroft if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
197 1.27 is output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
198 1.53 rillig if (keys & F_TIME)
199 1.45 jmc #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
200 1.8 cgd output(&indent, "time=%ld.%ld",
201 1.27 is (long)p->fts_statp->st_mtimespec.tv_sec,
202 1.10 jtc p->fts_statp->st_mtimespec.tv_nsec);
203 1.28 hubertf #else
204 1.23 christos output(&indent, "time=%ld.%ld",
205 1.52 christos (long)p->fts_statp->st_mtime, (long)0);
206 1.17 wsanchez #endif
207 1.5 cgd if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
208 1.5 cgd if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
209 1.5 cgd crc(fd, &val, &len))
210 1.17 wsanchez mtree_err("%s: %s", p->fts_accpath, strerror(errno));
211 1.36 lukem close(fd);
212 1.26 is output(&indent, "cksum=%lu", (long)val);
213 1.22 hubertf }
214 1.37 lukem #ifndef NO_MD5
215 1.22 hubertf if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
216 1.51 elad if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
217 1.57 hubertf mtree_err("%s: MD5File failed: %s", p->fts_accpath, strerror(errno));
218 1.36 lukem output(&indent, "md5=%s", digestbuf);
219 1.51 elad free(digestbuf);
220 1.36 lukem }
221 1.37 lukem #endif /* ! NO_MD5 */
222 1.37 lukem #ifndef NO_RMD160
223 1.36 lukem if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
224 1.51 elad if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
225 1.57 hubertf mtree_err("%s: RMD160File failed: %s", p->fts_accpath, strerror(errno));
226 1.36 lukem output(&indent, "rmd160=%s", digestbuf);
227 1.51 elad free(digestbuf);
228 1.36 lukem }
229 1.37 lukem #endif /* ! NO_RMD160 */
230 1.37 lukem #ifndef NO_SHA1
231 1.36 lukem if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
232 1.51 elad if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
233 1.57 hubertf mtree_err("%s: SHA1File failed: %s", p->fts_accpath, strerror(errno));
234 1.36 lukem output(&indent, "sha1=%s", digestbuf);
235 1.51 elad free(digestbuf);
236 1.1 cgd }
237 1.37 lukem #endif /* ! NO_SHA1 */
238 1.47 elad #ifndef NO_SHA2
239 1.47 elad if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
240 1.51 elad if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
241 1.57 hubertf mtree_err("%s: SHA256_File failed: %s", p->fts_accpath, strerror(errno));
242 1.47 elad output(&indent, "sha256=%s", digestbuf);
243 1.51 elad free(digestbuf);
244 1.47 elad }
245 1.47 elad if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
246 1.51 elad if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
247 1.57 hubertf mtree_err("%s: SHA384_File failed: %s", p->fts_accpath, strerror(errno));
248 1.47 elad output(&indent, "sha384=%s", digestbuf);
249 1.51 elad free(digestbuf);
250 1.47 elad }
251 1.47 elad if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
252 1.51 elad if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
253 1.57 hubertf mtree_err("%s: SHA512_File failed: %s", p->fts_accpath, strerror(errno));
254 1.47 elad output(&indent, "sha512=%s", digestbuf);
255 1.51 elad free(digestbuf);
256 1.47 elad }
257 1.47 elad #endif /* ! NO_SHA2 */
258 1.5 cgd if (keys & F_SLINK &&
259 1.5 cgd (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
260 1.46 lukem output(&indent, "link=%s", vispath(rlink(p->fts_accpath)));
261 1.38 tv #if HAVE_STRUCT_STAT_ST_FLAGS
262 1.18 mrg if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
263 1.18 mrg output(&indent, "flags=%s",
264 1.18 mrg flags_to_string(p->fts_statp->st_flags, "none"));
265 1.38 tv #endif
266 1.36 lukem putchar('\n');
267 1.1 cgd }
268 1.1 cgd
269 1.21 mrg /* XXX
270 1.21 mrg * FLAGS2INDEX will fail once the user and system settable bits need more
271 1.21 mrg * than one byte, respectively.
272 1.21 mrg */
273 1.21 mrg #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
274 1.21 mrg
275 1.19 christos #define MTREE_MAXGID 5000
276 1.19 christos #define MTREE_MAXUID 5000
277 1.21 mrg #define MTREE_MAXMODE (MBITS + 1)
278 1.38 tv #if HAVE_STRUCT_STAT_ST_FLAGS
279 1.21 mrg #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
280 1.38 tv #else
281 1.38 tv #define MTREE_MAXFLAGS 1
282 1.38 tv #endif
283 1.19 christos #define MTREE_MAXS 16
284 1.1 cgd
285 1.5 cgd static int
286 1.29 simonb statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
287 1.29 simonb u_long *pflags)
288 1.1 cgd {
289 1.13 lukem FTSENT *p;
290 1.13 lukem gid_t sgid;
291 1.13 lukem uid_t suid;
292 1.13 lukem mode_t smode;
293 1.38 tv u_long sflags = 0;
294 1.30 lukem const char *name;
295 1.1 cgd gid_t savegid;
296 1.1 cgd uid_t saveuid;
297 1.1 cgd mode_t savemode;
298 1.18 mrg u_long saveflags;
299 1.18 mrg u_short maxgid, maxuid, maxmode, maxflags;
300 1.19 christos u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
301 1.19 christos m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
302 1.36 lukem static int first = 1;
303 1.1 cgd
304 1.36 lukem savegid = *pgid;
305 1.36 lukem saveuid = *puid;
306 1.36 lukem savemode = *pmode;
307 1.36 lukem saveflags = *pflags;
308 1.5 cgd if ((p = fts_children(t, 0)) == NULL) {
309 1.5 cgd if (errno)
310 1.17 wsanchez mtree_err("%s: %s", RP(parent), strerror(errno));
311 1.5 cgd return (1);
312 1.1 cgd }
313 1.1 cgd
314 1.13 lukem memset(g, 0, sizeof(g));
315 1.13 lukem memset(u, 0, sizeof(u));
316 1.13 lukem memset(m, 0, sizeof(m));
317 1.18 mrg memset(f, 0, sizeof(f));
318 1.1 cgd
319 1.18 mrg maxuid = maxgid = maxmode = maxflags = 0;
320 1.1 cgd for (; p; p = p->fts_link) {
321 1.5 cgd smode = p->fts_statp->st_mode & MBITS;
322 1.19 christos if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
323 1.5 cgd savemode = smode;
324 1.5 cgd maxmode = m[smode];
325 1.5 cgd }
326 1.5 cgd sgid = p->fts_statp->st_gid;
327 1.19 christos if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
328 1.5 cgd savegid = sgid;
329 1.5 cgd maxgid = g[sgid];
330 1.5 cgd }
331 1.5 cgd suid = p->fts_statp->st_uid;
332 1.19 christos if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
333 1.5 cgd saveuid = suid;
334 1.5 cgd maxuid = u[suid];
335 1.1 cgd }
336 1.18 mrg
337 1.38 tv #if HAVE_STRUCT_STAT_ST_FLAGS
338 1.21 mrg sflags = FLAGS2INDEX(p->fts_statp->st_flags);
339 1.21 mrg if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
340 1.21 mrg saveflags = p->fts_statp->st_flags;
341 1.21 mrg maxflags = f[sflags];
342 1.18 mrg }
343 1.38 tv #endif
344 1.1 cgd }
345 1.36 lukem /*
346 1.36 lukem * If the /set record is the same as the last one we do not need to
347 1.36 lukem * output a new one. So first we check to see if anything changed.
348 1.36 lukem * Note that we always output a /set record for the first directory.
349 1.36 lukem */
350 1.36 lukem if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
351 1.36 lukem ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
352 1.36 lukem ((keys & F_MODE) && (*pmode != savemode)) ||
353 1.36 lukem ((keys & F_FLAGS) && (*pflags != saveflags)) ||
354 1.36 lukem first) {
355 1.36 lukem first = 0;
356 1.36 lukem printf("/set type=file");
357 1.36 lukem if (keys & (F_UID | F_UNAME)) {
358 1.36 lukem if (keys & F_UNAME &&
359 1.36 lukem (name = user_from_uid(saveuid, 1)) != NULL)
360 1.36 lukem printf(" uname=%s", name);
361 1.36 lukem else /* if (keys & F_UID) */
362 1.36 lukem printf(" uid=%lu", (u_long)saveuid);
363 1.36 lukem }
364 1.36 lukem if (keys & (F_GID | F_GNAME)) {
365 1.36 lukem if (keys & F_GNAME &&
366 1.36 lukem (name = group_from_gid(savegid, 1)) != NULL)
367 1.36 lukem printf(" gname=%s", name);
368 1.36 lukem else /* if (keys & F_UID) */
369 1.36 lukem printf(" gid=%lu", (u_long)savegid);
370 1.36 lukem }
371 1.36 lukem if (keys & F_MODE)
372 1.36 lukem printf(" mode=%#lo", (u_long)savemode);
373 1.36 lukem if (keys & F_NLINK)
374 1.36 lukem printf(" nlink=1");
375 1.36 lukem if (keys & F_FLAGS)
376 1.36 lukem printf(" flags=%s",
377 1.36 lukem flags_to_string(saveflags, "none"));
378 1.36 lukem printf("\n");
379 1.36 lukem *puid = saveuid;
380 1.36 lukem *pgid = savegid;
381 1.36 lukem *pmode = savemode;
382 1.36 lukem *pflags = saveflags;
383 1.15 ross }
384 1.5 cgd return (0);
385 1.1 cgd }
386 1.1 cgd
387 1.5 cgd static int
388 1.54 rillig dcmp(const FTSENT **a, const FTSENT **b)
389 1.1 cgd {
390 1.29 simonb
391 1.5 cgd if (S_ISDIR((*a)->fts_statp->st_mode)) {
392 1.5 cgd if (!S_ISDIR((*b)->fts_statp->st_mode))
393 1.5 cgd return (1);
394 1.5 cgd } else if (S_ISDIR((*b)->fts_statp->st_mode))
395 1.5 cgd return (-1);
396 1.5 cgd return (strcmp((*a)->fts_name, (*b)->fts_name));
397 1.5 cgd }
398 1.1 cgd
399 1.5 cgd void
400 1.5 cgd output(int *offset, const char *fmt, ...)
401 1.5 cgd {
402 1.5 cgd va_list ap;
403 1.5 cgd char buf[1024];
404 1.29 simonb
405 1.5 cgd va_start(ap, fmt);
406 1.36 lukem vsnprintf(buf, sizeof(buf), fmt, ap);
407 1.5 cgd va_end(ap);
408 1.5 cgd
409 1.8 cgd if (*offset + strlen(buf) > MAXLINELEN - 3) {
410 1.36 lukem printf(" \\\n%*s", INDENTNAMELEN, "");
411 1.5 cgd *offset = INDENTNAMELEN;
412 1.5 cgd }
413 1.5 cgd *offset += printf(" %s", buf) + 1;
414 1.1 cgd }
415