1 /* @(#)create.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: create.c,v 1.75 2017/12/31 03:04:44 christos Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
37 #include <sys/param.h>
40 #if ! HAVE_NBTOOL_CONFIG_H
74 #define INDENTNAMELEN 15
82 #if defined(__FreeBSD__) || defined(__DragonFly__)
83 #define FTS_CONST const
88 static int dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
89 static void output(FILE *, int, int *, const char *, ...)
91 static int statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
93 static void statf(FILE *, int, FTSENT *);
101 char host[MAXHOSTNAMELEN + 1];
111 gethostname(host, sizeof(host));
112 host[sizeof(host) - 1] = '\0';
113 if ((user = getlogin()) == NULL) {
115 user = (pw = getpwuid(getuid())) == NULL ? pw->pw_name :
121 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n"
123 user, host, fullpath, ctime(&clocktime));
125 if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
126 mtree_err("fts_open: %s", strerror(errno));
127 while ((p = fts_read(t)) != NULL) {
129 indent = p->fts_level * 4;
130 if (check_excludes(p->fts_name, p->fts_path)) {
131 fts_set(t, p, FTS_SKIP);
134 if (!find_only(p->fts_path)) {
135 fts_set(t, p, FTS_SKIP);
138 switch(p->fts_info) {
143 fprintf(fp, "# %s\n", p->fts_path);
144 statd(fp, t, p, &uid, &gid, &mode, &flags);
145 statf(fp, indent, p);
148 if (p->fts_level > 0)
150 fprintf(fp, "%*s# %s\n", indent, "",
152 if (p->fts_level > 0 || flavor == F_FREEBSD9) {
153 fprintf(fp, "%*s..\n", indent, "");
162 p->fts_path, strerror(p->fts_errno));
166 statf(fp, indent, p);
172 if (sflag && keys & F_CKSUM)
173 mtree_err("%s checksum: %u", fullpath, crc_total);
177 dosum(FILE *fp, int indent, FTSENT *p, int *offset, int flag,
178 char * (*func)(const char *, char *), const char *key)
182 if ((keys & flag) == 0)
185 digestbuf = (*func)(p->fts_accpath, NULL);
186 if (digestbuf != NULL) {
187 output(fp, indent, offset, "%s=%s", key, digestbuf);
193 warn("%s: %s failed", p->fts_path, key);
197 mtree_err("%s: %s failed: %s", p->fts_path, key, strerror(errno));
201 crcFile(const char *fname, char *dummy __unused)
207 if ((fd = open(fname, O_RDONLY)) == -1)
210 e = crc(fd, &val, &len);
215 if (asprintf(&ptr, "%u", val) < 0)
220 mtree_err("%s: %s", fname, strerror(errno));
225 statf(FILE *fp, int indent, FTSENT *p)
228 const char *name = NULL;
230 offset = fprintf(fp, "%*s%s%s", indent, "",
231 S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
233 if (offset > (INDENTNAMELEN + indent))
236 offset += fprintf(fp, "%*s",
237 (INDENTNAMELEN + indent) - offset, "");
239 if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
240 output(fp, indent, &offset, "type=%s",
241 inotype(p->fts_statp->st_mode));
242 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
243 if (keys & F_UNAME &&
244 (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
245 output(fp, indent, &offset, "uname=%s", name);
246 if (keys & F_UID || (keys & F_UNAME && name == NULL))
247 output(fp, indent, &offset, "uid=%u",
248 p->fts_statp->st_uid);
250 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
251 if (keys & F_GNAME &&
252 (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
253 output(fp, indent, &offset, "gname=%s", name);
254 if (keys & F_GID || (keys & F_GNAME && name == NULL))
255 output(fp, indent, &offset, "gid=%u",
256 p->fts_statp->st_gid);
258 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
259 output(fp, indent, &offset, "mode=%#o",
260 p->fts_statp->st_mode & MBITS);
262 (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
263 output(fp, indent, &offset, "device=%#jx",
264 (uintmax_t)p->fts_statp->st_rdev);
265 if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
266 output(fp, indent, &offset, "nlink=%ju",
267 (uintmax_t)p->fts_statp->st_nlink);
269 (flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
270 output(fp, indent, &offset, "size=%ju",
271 (uintmax_t)p->fts_statp->st_size);
273 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
274 output(fp, indent, &offset, "time=%jd.%09ld",
275 (intmax_t)p->fts_statp->st_mtimespec.tv_sec,
276 p->fts_statp->st_mtimespec.tv_nsec);
278 output(fp, indent, &offset, "time=%jd.%09ld",
279 (intmax_t)p->fts_statp->st_mtime, (long)0);
281 if (S_ISREG(p->fts_statp->st_mode)) {
282 dosum(fp, indent, p, &offset, F_CKSUM, crcFile, "cksum");
284 dosum(fp, indent, p, &offset, F_MD5, MD5File, MD5KEY);
285 #endif /* ! NO_MD5 */
287 dosum(fp, indent, p, &offset, F_RMD160, RIPEMD160_File, RMD160KEY);
288 #endif /* ! NO_RMD160 */
290 dosum(fp, indent, p, &offset, F_SHA1, SHA1_File, SHA1KEY);
291 #endif /* ! NO_SHA1 */
293 dosum(fp, indent, p, &offset, F_SHA256, SHA256_File, SHA256KEY);
294 #ifdef SHA384_BLOCK_LENGTH
295 #if !defined(__DragonFly__)
296 dosum(fp, indent, p, &offset, F_SHA384, SHA384_File, SHA384KEY);
299 dosum(fp, indent, p, &offset, F_SHA512, SHA512_File, SHA512KEY);
300 #endif /* ! NO_SHA2 */
302 if (keys & F_SLINK &&
303 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
304 output(fp, indent, &offset, "link=%s",
305 vispath(rlink(p->fts_accpath)));
306 #if HAVE_STRUCT_STAT_ST_FLAGS
307 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
308 char *str = flags_to_string(p->fts_statp->st_flags, "none");
309 output(fp, indent, &offset, "flags=%s", str);
317 * FLAGS2INDEX will fail once the user and system settable bits need more
318 * than one byte, respectively.
320 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
322 #define MTREE_MAXGID 5000
323 #define MTREE_MAXUID 5000
324 #define MTREE_MAXMODE (MBITS + 1)
325 #if HAVE_STRUCT_STAT_ST_FLAGS
326 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
328 #define MTREE_MAXFLAGS 1
330 #define MTREE_MAXS 16
333 statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
341 const char *name = NULL;
346 u_short maxgid, maxuid, maxmode, maxflags;
347 u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
348 m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
349 static int first = 1;
355 if ((p = fts_children(t, 0)) == NULL) {
357 mtree_err("%s: %s", RP(parent), strerror(errno));
361 memset(g, 0, sizeof(g));
362 memset(u, 0, sizeof(u));
363 memset(m, 0, sizeof(m));
364 memset(f, 0, sizeof(f));
366 maxuid = maxgid = maxmode = maxflags = 0;
367 for (; p; p = p->fts_link) {
368 if (flavor == F_NETBSD6 || !dflag ||
369 (dflag && S_ISDIR(p->fts_statp->st_mode))) {
370 smode = p->fts_statp->st_mode & MBITS;
371 if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
375 sgid = p->fts_statp->st_gid;
376 if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
380 suid = p->fts_statp->st_uid;
381 if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
386 #if HAVE_STRUCT_STAT_ST_FLAGS
387 sflags = FLAGS2INDEX(p->fts_statp->st_flags);
388 if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
389 saveflags = p->fts_statp->st_flags;
390 maxflags = f[sflags];
396 * If the /set record is the same as the last one we do not need to
397 * output a new one. So first we check to see if anything changed.
398 * Note that we always output a /set record for the first directory.
400 if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
401 ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
402 ((keys & F_MODE) && (*pmode != savemode)) ||
403 ((keys & F_FLAGS) && (*pflags != saveflags)) ||
406 if (flavor != F_NETBSD6 && dflag)
407 fprintf(fp, "/set type=dir");
409 fprintf(fp, "/set type=file");
410 if (keys & (F_UID | F_UNAME)) {
411 if (keys & F_UNAME &&
412 (name = user_from_uid(saveuid, 1)) != NULL)
413 fprintf(fp, " uname=%s", name);
414 if (keys & F_UID || (keys & F_UNAME && name == NULL))
415 fprintf(fp, " uid=%lu", (u_long)saveuid);
417 if (keys & (F_GID | F_GNAME)) {
418 if (keys & F_GNAME &&
419 (name = group_from_gid(savegid, 1)) != NULL)
420 fprintf(fp, " gname=%s", name);
421 if (keys & F_GID || (keys & F_GNAME && name == NULL))
422 fprintf(fp, " gid=%lu", (u_long)savegid);
425 fprintf(fp, " mode=%#lo", (u_long)savemode);
427 fprintf(fp, " nlink=1");
428 if (keys & F_FLAGS) {
429 char *str = flags_to_string(saveflags, "none");
430 fprintf(fp, " flags=%s", str);
444 * used as a comparison function passed to fts_open() to control
445 * the order in which fts_read() returns results. We make
446 * directories sort after non-directories, but otherwise sort in
449 * Keep this in sync with nodecmp() in spec.c.
452 dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
455 if (S_ISDIR((*a)->fts_statp->st_mode)) {
456 if (!S_ISDIR((*b)->fts_statp->st_mode))
458 } else if (S_ISDIR((*b)->fts_statp->st_mode))
460 return (strcmp((*a)->fts_name, (*b)->fts_name));
464 output(FILE *fp, int indent, int *offset, const char *fmt, ...)
470 vsnprintf(buf, sizeof(buf), fmt, ap);
473 if (*offset + strlen(buf) > MAXLINELEN - 3) {
474 fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
475 *offset = INDENTNAMELEN + indent;
477 *offset += fprintf(fp, " %s", buf) + 1;