1 /* $NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $");
45 #include <sys/param.h>
48 #if ! HAVE_NBTOOL_CONFIG_H
79 #define INDENTNAMELEN 15
88 #define FTS_CONST const
93 static int dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
94 static void output(FILE *, int, int *, const char *, ...)
96 static int statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
98 static void statf(FILE *, int, FTSENT *);
106 char host[MAXHOSTNAMELEN + 1];
116 gethostname(host, sizeof(host));
117 host[sizeof(host) - 1] = '\0';
118 if ((user = getlogin()) == NULL) {
120 user = (pw = getpwuid(getuid())) != NULL ? pw->pw_name :
126 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n"
128 user, host, fullpath, ctime(&clocktime));
130 if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
131 mtree_err("fts_open: %s", strerror(errno));
132 while ((p = fts_read(t)) != NULL) {
134 indent = p->fts_level * 4;
135 if (check_excludes(p->fts_name, p->fts_path)) {
136 fts_set(t, p, FTS_SKIP);
139 if (!find_only(p->fts_path)) {
140 fts_set(t, p, FTS_SKIP);
143 switch(p->fts_info) {
148 fprintf(fp, "# %s\n", p->fts_path);
149 statd(fp, t, p, &uid, &gid, &mode, &flags);
150 statf(fp, indent, p);
153 if (p->fts_level > 0)
155 fprintf(fp, "%*s# %s\n", indent, "",
157 if (p->fts_level > 0 || flavor == F_FREEBSD9) {
158 fprintf(fp, "%*s..\n", indent, "");
167 p->fts_path, strerror(p->fts_errno));
171 statf(fp, indent, p);
177 if (sflag && keys & F_CKSUM)
178 mtree_err("%s checksum: %u", fullpath, crc_total);
182 statf(FILE *fp, int indent, FTSENT *p)
186 const char *name = NULL;
187 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
191 offset = fprintf(fp, "%*s%s%s", indent, "",
192 S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
194 if (offset > (INDENTNAMELEN + indent))
197 offset += fprintf(fp, "%*s",
198 (INDENTNAMELEN + indent) - offset, "");
200 if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
201 output(fp, indent, &offset, "type=%s",
202 inotype(p->fts_statp->st_mode));
203 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
204 if (keys & F_UNAME &&
205 (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
206 output(fp, indent, &offset, "uname=%s", name);
207 if (keys & F_UID || (keys & F_UNAME && name == NULL))
208 output(fp, indent, &offset, "uid=%u",
209 p->fts_statp->st_uid);
211 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
212 if (keys & F_GNAME &&
213 (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
214 output(fp, indent, &offset, "gname=%s", name);
215 if (keys & F_GID || (keys & F_GNAME && name == NULL))
216 output(fp, indent, &offset, "gid=%u",
217 p->fts_statp->st_gid);
219 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
220 output(fp, indent, &offset, "mode=%#o",
221 p->fts_statp->st_mode & MBITS);
223 (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
224 output(fp, indent, &offset, "device=%#jx",
225 (uintmax_t)p->fts_statp->st_rdev);
226 if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
227 output(fp, indent, &offset, "nlink=%ju",
228 (uintmax_t)p->fts_statp->st_nlink);
230 (flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
231 output(fp, indent, &offset, "size=%ju",
232 (uintmax_t)p->fts_statp->st_size);
234 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
235 output(fp, indent, &offset, "time=%jd.%09ld",
236 (intmax_t)p->fts_statp->st_mtimespec.tv_sec,
237 p->fts_statp->st_mtimespec.tv_nsec);
239 output(fp, indent, &offset, "time=%jd.%09ld",
240 (intmax_t)p->fts_statp->st_mtime, (long)0);
242 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
243 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
245 mtree_err("%s: %s", p->fts_accpath, strerror(errno));
247 output(fp, indent, &offset, "cksum=%lu", (long)val);
250 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
251 if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
252 mtree_err("%s: MD5File failed: %s", p->fts_accpath,
254 output(fp, indent, &offset, "%s=%s", MD5KEY, digestbuf);
257 #endif /* ! NO_MD5 */
259 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
260 if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
261 mtree_err("%s: RMD160File failed: %s", p->fts_accpath,
263 output(fp, indent, &offset, "%s=%s", RMD160KEY, digestbuf);
266 #endif /* ! NO_RMD160 */
268 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
269 if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
270 mtree_err("%s: SHA1File failed: %s", p->fts_accpath,
272 output(fp, indent, &offset, "%s=%s", SHA1KEY, digestbuf);
275 #endif /* ! NO_SHA1 */
277 if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
278 if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
279 mtree_err("%s: SHA256_File failed: %s", p->fts_accpath,
281 output(fp, indent, &offset, "%s=%s", SHA256KEY, digestbuf);
284 #ifdef SHA384_BLOCK_LENGTH
285 if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
286 if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
287 mtree_err("%s: SHA384_File failed: %s", p->fts_accpath,
289 output(fp, indent, &offset, "%s=%s", SHA384KEY, digestbuf);
293 if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
294 if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
295 mtree_err("%s: SHA512_File failed: %s", p->fts_accpath,
297 output(fp, indent, &offset, "%s=%s", SHA512KEY, digestbuf);
300 #endif /* ! NO_SHA2 */
301 if (keys & F_SLINK &&
302 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
303 output(fp, indent, &offset, "link=%s",
304 vispath(rlink(p->fts_accpath)));
305 #if HAVE_STRUCT_STAT_ST_FLAGS
306 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
307 char *str = flags_to_string(p->fts_statp->st_flags, "none");
308 output(fp, indent, &offset, "flags=%s", str);
316 * FLAGS2INDEX will fail once the user and system settable bits need more
317 * than one byte, respectively.
319 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
321 #define MTREE_MAXGID 5000
322 #define MTREE_MAXUID 5000
323 #define MTREE_MAXMODE (MBITS + 1)
324 #if HAVE_STRUCT_STAT_ST_FLAGS
325 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
327 #define MTREE_MAXFLAGS 1
329 #define MTREE_MAXS 16
332 statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
340 const char *name = NULL;
345 u_short maxgid, maxuid, maxmode, maxflags;
346 u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
347 m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
348 static int first = 1;
354 if ((p = fts_children(t, 0)) == NULL) {
356 mtree_err("%s: %s", RP(parent), strerror(errno));
360 memset(g, 0, sizeof(g));
361 memset(u, 0, sizeof(u));
362 memset(m, 0, sizeof(m));
363 memset(f, 0, sizeof(f));
365 maxuid = maxgid = maxmode = maxflags = 0;
366 for (; p; p = p->fts_link) {
367 if (flavor == F_NETBSD6 || !dflag ||
368 (dflag && S_ISDIR(p->fts_statp->st_mode))) {
369 smode = p->fts_statp->st_mode & MBITS;
370 if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
374 sgid = p->fts_statp->st_gid;
375 if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
379 suid = p->fts_statp->st_uid;
380 if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
385 #if HAVE_STRUCT_STAT_ST_FLAGS
386 sflags = FLAGS2INDEX(p->fts_statp->st_flags);
387 if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
388 saveflags = p->fts_statp->st_flags;
389 maxflags = f[sflags];
395 * If the /set record is the same as the last one we do not need to
396 * output a new one. So first we check to see if anything changed.
397 * Note that we always output a /set record for the first directory.
399 if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
400 ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
401 ((keys & F_MODE) && (*pmode != savemode)) ||
402 ((keys & F_FLAGS) && (*pflags != saveflags)) ||
405 if (flavor != F_NETBSD6 && dflag)
406 fprintf(fp, "/set type=dir");
408 fprintf(fp, "/set type=file");
409 if (keys & (F_UID | F_UNAME)) {
410 if (keys & F_UNAME &&
411 (name = user_from_uid(saveuid, 1)) != NULL)
412 fprintf(fp, " uname=%s", name);
413 if (keys & F_UID || (keys & F_UNAME && name == NULL))
414 fprintf(fp, " uid=%lu", (u_long)saveuid);
416 if (keys & (F_GID | F_GNAME)) {
417 if (keys & F_GNAME &&
418 (name = group_from_gid(savegid, 1)) != NULL)
419 fprintf(fp, " gname=%s", name);
420 if (keys & F_GID || (keys & F_GNAME && name == NULL))
421 fprintf(fp, " gid=%lu", (u_long)savegid);
424 fprintf(fp, " mode=%#lo", (u_long)savemode);
426 fprintf(fp, " nlink=1");
427 if (keys & F_FLAGS) {
428 char *str = flags_to_string(saveflags, "none");
429 fprintf(fp, " flags=%s", str);
443 * used as a comparison function passed to fts_open() to control
444 * the order in which fts_read() returns results. We make
445 * directories sort after non-directories, but otherwise sort in
448 * Keep this in sync with nodecmp() in spec.c.
451 dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
454 if (S_ISDIR((*a)->fts_statp->st_mode)) {
455 if (!S_ISDIR((*b)->fts_statp->st_mode))
457 } else if (S_ISDIR((*b)->fts_statp->st_mode))
459 return (strcmp((*a)->fts_name, (*b)->fts_name));
463 output(FILE *fp, int indent, int *offset, const char *fmt, ...)
469 vsnprintf(buf, sizeof(buf), fmt, ap);
472 if (*offset + strlen(buf) > MAXLINELEN - 3) {
473 fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
474 *offset = INDENTNAMELEN + indent;
476 *offset += fprintf(fp, " %s", buf) + 1;